File: test-git-default-branch.R

package info (click to toggle)
r-cran-usethis 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,228 kB
  • sloc: sh: 26; makefile: 17; cpp: 6; ansic: 3
file content (118 lines) | stat: -rw-r--r-- 3,858 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
test_that("git_default_branch() consults the default branch candidates, in order", {
  skip_on_cran()
  skip_if_no_git_user()
  local_interactive(FALSE)

  create_local_project()
  use_git()
  repo <- git_repo()

  gert::git_add(".gitignore", repo = repo)
  gert::git_commit("a commit, so we are not on an unborn branch", repo = repo)

  # singleton branch, with weird name
  git_default_branch_rename(from = git_branch(), to = "foofy")
  expect_equal(git_default_branch(), "foofy")

  # two weirdly named branches, but one matches init.defaultBranch (local) config
  gert::git_branch_create("blarg", checkout = TRUE, repo = repo)
  use_git_config("project", `init.defaultBranch` = "blarg")
  expect_equal(git_default_branch(), "blarg")

  # one of the Usual Suspects shows up
  gert::git_branch_create("master", checkout = TRUE, repo = repo)
  expect_equal(git_default_branch(), "master")

  # and another Usual Suspect shows up
  gert::git_branch_create("main", checkout = TRUE, repo = repo)
  expect_equal(git_default_branch(), "main")

  # finally, prefer something that matches what upstream says is default
  gert::git_branch_create("default-branch-of-upstream", checkout = TRUE, repo = repo)
  local_git_default_branch_remote()
  expect_equal(git_default_branch(), "default-branch-of-upstream")
})

test_that("git_default_branch() errors if can't find obvious local default branch", {
  skip_on_cran()
  skip_if_no_git_user()
  local_interactive(FALSE)

  create_local_project()
  use_git()
  repo <- git_repo()

  gert::git_add(".gitignore", repo = repo)
  gert::git_commit("a commit, so we are not on an unborn branch", repo = repo)
  git_default_branch_rename(from = git_branch(), to = "foofy")

  gert::git_branch_create("blarg", checkout = TRUE, repo = repo)

  expect_error(git_default_branch(), class = "error_default_branch")
})

test_that("git_default_branch() errors for local vs remote mismatch", {
  skip_on_cran()
  skip_if_no_git_user()
  local_interactive(FALSE)

  create_local_project()
  use_git()
  repo <- git_repo()

  gert::git_add(".gitignore", repo = repo)
  gert::git_commit("a commit, so we are not on an unborn branch", repo = repo)
  git_default_branch_rename(from = git_branch(), to = "foofy")
  local_git_default_branch_remote()

  expect_error(git_default_branch(), class = "error_default_branch")

  gert::git_branch_create("blarg", checkout = TRUE, repo = repo)
  local_git_default_branch_remote()
  expect_error(git_default_branch(), class = "error_default_branch")
})

test_that("git_default_branch_rename() surfaces files that smell fishy", {
  skip_on_cran()
  skip_if_no_git_user()
  local_interactive(FALSE)

  # for snapshot purposes, I don't want a random project name
  create_local_project(path(path_temp(), "abcde"))
  use_git()
  repo <- git_repo()

  gert::git_add(".gitignore", repo = repo)
  gert::git_commit("a commit, so we are not on an unborn branch", repo = repo)

  # make sure we start with default branch = 'master'
  git_default_branch_rename(from = git_branch(), to = "master")
  expect_equal(git_default_branch(), "master")

  badge_lines <- c(
    "<!-- badges: start -->",
    "[![Codecov test coverage](https://codecov.io/gh/OWNER/REPO/branch/master/graph/badge.svg)](https://codecov.io/gh/OWNER/REPO?branch=master)",
    "<!-- badges: end -->"
  )
  cli::cat_line(badge_lines, file = proj_path("README.md"))

  gha_lines <- c(
    "on:",
    "  push:",
    "    branches:",
    "      - master"
  )
  create_directory(".github/workflows")
  cli::cat_line(gha_lines, file = path(".github", "workflows", "blah.yml"))

  create_directory("whatever/foo")
  cli::cat_line(
    "edit: https://github.com/OWNER/REPO/edit/master/%s",
    file = path("whatever", "foo", "_bookdown.yaml")
  )

  withr::local_options(usethis.quiet = FALSE)
  expect_snapshot(
    git_default_branch_rename()
  )
})