1# Branch ------------------------------------------------------------------
2test_that("git_branch() works", {
3  skip_if_no_git_user()
4  create_local_project()
5
6  expect_usethis_error(git_branch(), "Cannot detect")
7
8  git_init()
9  expect_usethis_error(git_branch(), "unborn branch")
10
11  writeLines("blah", proj_path("blah.txt"))
12  gert::git_add("blah.txt", repo = git_repo())
13  gert::git_commit("Make one commit", repo = git_repo())
14  # branch name can depend on user's config, e.g. could be 'master' or 'main'
15  expect_error_free(
16    b <- git_branch()
17  )
18  expect_true(nzchar(b))
19})
20
21# Protocol ------------------------------------------------------------------
22test_that("git_protocol() catches bad input from usethis.protocol option", {
23  withr::with_options(
24    list(usethis.protocol = "nope"),
25    {
26      expect_usethis_error(git_protocol(), "must be either")
27      expect_null(getOption("usethis.protocol"))
28    }
29  )
30  withr::with_options(
31    list(usethis.protocol = c("ssh", "https")),
32    {
33      expect_usethis_error(git_protocol(), "must be either")
34      expect_null(getOption("usethis.protocol"))
35    }
36  )
37})
38
39test_that("use_git_protocol() errors for bad input", {
40  expect_usethis_error(use_git_protocol("nope"), "must be either")
41})
42
43test_that("git_protocol() defaults to 'https'", {
44  withr::with_options(
45    list(usethis.protocol = NULL),
46    expect_identical(git_protocol(), "https")
47  )
48})
49
50test_that("git_protocol() honors, vets, and lowercases the option", {
51  withr::with_options(
52    list(usethis.protocol = "ssh"),
53    expect_identical(git_protocol(), "ssh")
54  )
55  withr::with_options(
56    list(usethis.protocol = "SSH"),
57    expect_identical(git_protocol(), "ssh")
58  )
59  withr::with_options(
60    list(usethis.protocol = "https"),
61    expect_identical(git_protocol(), "https")
62  )
63  withr::with_options(
64    list(usethis.protocol = "nope"),
65    expect_usethis_error(git_protocol(), "must be either")
66  )
67})
68
69test_that("use_git_protocol() prioritizes and lowercases direct input", {
70  withr::with_options(
71    list(usethis.protocol = "ssh"),
72    {
73      expect_identical(use_git_protocol("HTTPS"), "https")
74      expect_identical(git_protocol(), "https")
75    }
76  )
77})
78