Skip to content

Commit b94a2f0

Browse files
authored
Tweak the new mocking helpers (#1835)
* Tweak the new mocking helpers * Incorporate feedback
1 parent fc100a0 commit b94a2f0

19 files changed

+80
-106
lines changed

tests/testthat/helper-mocks.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local_cran_version <- function(version, .env = caller_env()) {
2+
local_mocked_bindings(cran_version = function() version, .env = .env)
3+
}
4+
5+
local_check_installed <- function(.env = caller_env()) {
6+
local_mocked_bindings(check_installed = function(...) NULL, .env = .env)
7+
}
8+
9+
local_rstudio_available <- function(val, .env = caller_env()) {
10+
local_mocked_bindings(rstudio_available = function(...) val, .env = .env)
11+
}
12+
13+
local_target_repo_spec <- function(spec, .env = caller_env()) {
14+
local_mocked_bindings(target_repo_spec = function(...) spec, .env = .env)
15+
}
16+
17+
local_roxygen_update_ns <- function(.env = caller_env()) {
18+
local_mocked_bindings(roxygen_update_ns = function(...) NULL, .env = .env)
19+
}
20+
21+
local_check_fun_exists <- function(.env = caller_env()) {
22+
local_mocked_bindings(check_fun_exists = function(...) NULL, .env = .env)
23+
}
24+
25+
local_ui_yeah <- function(.env = caller_env()) {
26+
local_mocked_bindings(ui_yeah = function(...) TRUE, .env = .env)
27+
}
28+
29+
local_git_default_branch_remote <- function(.env = caller_env()) {
30+
local_mocked_bindings(
31+
git_default_branch_remote = function(remote) {
32+
list(
33+
name = remote,
34+
is_configured = TRUE,
35+
url = NA_character_,
36+
repo_spec = NA_character_,
37+
default_branch = as.character(glue("default-branch-of-{remote}"))
38+
)
39+
},
40+
.env = .env
41+
)
42+
}

tests/testthat/helper.R

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -128,75 +128,7 @@ test_file <- function(fname) testthat::test_path("ref", fname)
128128
expect_proj_file <- function(...) expect_true(file_exists(proj_path(...)))
129129
expect_proj_dir <- function(...) expect_true(dir_exists(proj_path(...)))
130130

131-
mock_cran_version <- function(version, .env = caller_env()) {
132-
local_mocked_bindings(cran_version = function() version, .env = .env)
133-
}
134-
135131
scrub_checklist_footer <- function(text) {
136132
gsub("(^<sup>.+on )[-/0-9]{10}(.+ v)[0-9.]{3,12}(.+</sup>$)",
137133
"\\1DATE\\2VERSION\\3", text)
138134
}
139-
140-
mock_check_installed <- function(.env = caller_env()) {
141-
local_mocked_bindings(
142-
check_installed = function(...) TRUE,
143-
.env = .env
144-
)
145-
}
146-
147-
mock_check_is_package <- function(.env = caller_env()) {
148-
local_mocked_bindings(
149-
check_is_package = function(...) invisible(),
150-
.env = .env
151-
)
152-
}
153-
154-
mock_rstudio_not_available <- function(.env = caller_env()) {
155-
local_mocked_bindings(
156-
rstudio_available = function(...) FALSE,
157-
.env = .env
158-
)
159-
}
160-
161-
mock_target_repo_spec <- function(spec = "OWNER/REPO", .env = caller_env()) {
162-
local_mocked_bindings(
163-
target_repo_spec = function(...) spec,
164-
.env = .env
165-
)
166-
}
167-
168-
mock_roxygen_update_ns <- function(.env = caller_env()) {
169-
local_mocked_bindings(
170-
roxygen_update_ns = function(...) NULL,
171-
.env = .env
172-
)
173-
}
174-
175-
mock_check_functions_exist <- function(.env = caller_env()) {
176-
local_mocked_bindings(
177-
check_functions_exist = function(...) TRUE,
178-
.env = .env
179-
)
180-
}
181-
182-
mock_git_default_branch_remote <- function(remote = "upstream", .env = caller_env()) {
183-
local_mocked_bindings(
184-
git_default_branch_remote = function(remote) {
185-
list(
186-
name = remote,
187-
is_configured = TRUE,
188-
url = NA_character_,
189-
repo_spec = NA_character_,
190-
default_branch = as.character(glue("default-branch-of-{remote}"))
191-
)
192-
},
193-
.env = .env
194-
)
195-
}
196-
197-
mock_ui_yeah <- function(.env = caller_env()) {
198-
local_mocked_bindings(
199-
ui_yeah = function(...) TRUE,
200-
.env = .env
201-
)
202-
}

tests/testthat/test-ci.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test_that("use_circleci() configures CircleCI", {
55
create_local_package()
66
use_git()
77

8-
mock_target_repo_spec()
8+
local_target_repo_spec("OWNER/REPO")
99

1010
use_circleci(browse = FALSE)
1111

tests/testthat/test-cpp11.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test_that("use_cpp11() requires a package", {
22
create_local_project()
3-
mock_check_installed()
3+
local_check_installed()
44
expect_usethis_error(use_cpp11(), "not an R package")
55
})
66

@@ -9,7 +9,7 @@ test_that("use_cpp11() creates files/dirs, edits DESCRIPTION and .gitignore", {
99
use_roxygen_md()
1010

1111
local_interactive(FALSE)
12-
mock_check_installed()
12+
local_check_installed()
1313
local_mocked_bindings(check_cpp_register_deps = function() invisible())
1414

1515
use_cpp11()

tests/testthat/test-create.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test_that("create_*() works w/ non-existing rel path, open = TRUE, not in RStudi
113113
withr::defer(dir_delete(sandbox))
114114
withr::defer(proj_set(orig_proj, force = TRUE))
115115
withr::local_dir(sandbox)
116-
mock_rstudio_not_available()
116+
local_rstudio_available(FALSE)
117117

118118
# package
119119
rel_path_pkg <- path_file(file_temp(pattern = "ghi"))

tests/testthat/test-data-table.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ test_that("use_data_table() requires a package", {
66
test_that("use_data_table() Imports data.table", {
77
create_local_package()
88
use_package_doc()
9-
mock_check_installed()
10-
mock_roxygen_update_ns()
11-
mock_check_functions_exist()
9+
local_check_installed()
10+
local_roxygen_update_ns()
11+
local_check_fun_exists()
1212

1313
use_data_table()
1414

@@ -22,9 +22,9 @@ test_that("use_data_table() blocks use of Depends", {
2222
create_local_package()
2323
use_package_doc()
2424
desc::desc_set("Depends", "data.table")
25-
mock_check_installed()
26-
mock_roxygen_update_ns()
27-
mock_check_functions_exist()
25+
local_check_installed()
26+
local_roxygen_update_ns()
27+
local_check_fun_exists()
2828

2929
expect_warning(
3030
use_data_table(),

tests/testthat/test-git-default-branch.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test_that("git_default_branch() consults the default branch candidates, in order
2929

3030
# finally, prefer something that matches what upstream says is default
3131
gert::git_branch_create("default-branch-of-upstream", checkout = TRUE, repo = repo)
32-
mock_git_default_branch_remote()
32+
local_git_default_branch_remote()
3333
expect_equal(git_default_branch(), "default-branch-of-upstream")
3434
})
3535

@@ -63,13 +63,13 @@ test_that("git_default_branch() errors for local vs remote mismatch", {
6363
gert::git_add(".gitignore", repo = repo)
6464
gert::git_commit("a commit, so we are not on an unborn branch", repo = repo)
6565
git_default_branch_rename(from = git_branch(), to = "foofy")
66-
mock_git_default_branch_remote()
66+
local_git_default_branch_remote()
6767

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

70-
gert::git_branch_create("blarg", checkout = TRUE, repo = repo)
71-
mock_git_default_branch_remote()
72-
expect_error(git_default_branch(), class = "error_default_branch")
70+
gert::git_branch_create("blarg", checkout = TRUE, repo = repo)
71+
local_git_default_branch_remote()
72+
expect_error(git_default_branch(), class = "error_default_branch")
7373
})
7474

7575
test_that("git_default_branch_rename() surfaces files that smell fishy", {

tests/testthat/test-news.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test_that("use_news_md() sets (development version)/'Initial submission' in new pkg", {
22
create_local_package()
3-
mock_cran_version(NULL)
3+
local_cran_version(NULL)
44

55
use_news_md()
66

@@ -13,7 +13,7 @@ test_that("use_news_md() sets bullet to 'Added a NEWS.md file...' when on CRAN",
1313

1414
# on CRAN, local dev version
1515
proj_desc_field_update(key = "Version", value = "0.1.0.9000")
16-
mock_cran_version("0.1.0")
16+
local_cran_version("0.1.0")
1717

1818
use_news_md()
1919

@@ -25,7 +25,7 @@ test_that("use_news_md() sets version number when 'production version'", {
2525
create_local_package()
2626

2727
proj_desc_field_update(key = "Version", value = "0.2.0")
28-
mock_cran_version(NULL)
28+
local_cran_version(NULL)
2929

3030
use_news_md()
3131

tests/testthat/test-package.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test_that("use_package(type = 'Suggests') guidance w/o and w/ rlang", {
4040

4141
test_that("use_dev_package() writes a remote", {
4242
create_local_package()
43-
mock_ui_yeah()
43+
local_ui_yeah()
4444

4545
use_dev_package("usethis")
4646
expect_equal(proj_desc()$get_remotes(), "r-lib/usethis")
@@ -59,7 +59,7 @@ test_that("package_remote() works for an installed package with github URL", {
5959
"Package: test",
6060
"URL: https://github.com/OWNER/test"
6161
))
62-
mock_ui_yeah()
62+
local_ui_yeah()
6363
expect_equal(package_remote(d), "OWNER/test")
6464
})
6565

tests/testthat/test-pkgdown.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test_that("use_pkgdown() requires a package", {
66
test_that("use_pkgdown() creates and ignores the promised file/dir", {
77
create_local_package()
88
local_interactive(FALSE)
9-
mock_check_installed()
9+
local_check_installed()
1010
local_mocked_bindings(pkgdown_version = function() "1.9000")
1111

1212
use_pkgdown()
@@ -28,7 +28,7 @@ test_that("pkgdown helpers behave in the absence of pkgdown", {
2828
test_that("pkgdown_config_meta() returns a list", {
2929
create_local_package()
3030
local_interactive(FALSE)
31-
mock_check_installed()
31+
local_check_installed()
3232
local_mocked_bindings(pkgdown_version = function() "1.9000")
3333

3434
use_pkgdown()
@@ -44,7 +44,7 @@ test_that("pkgdown_config_meta() returns a list", {
4444
test_that("pkgdown_url() returns correct data, warns if pedantic", {
4545
create_local_package()
4646
local_interactive(FALSE)
47-
mock_check_installed()
47+
local_check_installed()
4848
local_mocked_bindings(pkgdown_version = function() "1.9000")
4949

5050
use_pkgdown()

0 commit comments

Comments
 (0)