Skip to content

Commit d1d28c9

Browse files
authored
Clean up code and tests around nested projects (#1085)
* Reduce coincidental use of "aaa" in tests Trying to make it more clear when we use it for a reason related to nested projects * Start removing the "aaa" and is_testing() magic * Rework how we allow a nested project; work on tests
1 parent eb82346 commit d1d28c9

File tree

7 files changed

+46
-28
lines changed

7 files changed

+46
-28
lines changed

R/create.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,16 @@ create_from_github <- function(repo_spec,
260260
invisible(proj_get())
261261
}
262262

263+
# creates a backdoor we can exploit in tests
264+
allow_nested_project <- function() FALSE
265+
263266
check_not_nested <- function(path, name) {
264267
if (!possibly_in_proj(path)) {
265268
return(invisible())
266269
}
267270

268-
## special case: allow nested project if
269-
## 1) is_testing()
270-
## 2) proposed project name matches magic string we build into test projects
271-
## https://github.com/r-lib/usethis/pull/241
272-
if (is_testing() && grepl("aaa", name)) {
271+
# we mock this in a few tests, to allow a nested project
272+
if (allow_nested_project()) {
273273
return()
274274
}
275275

R/utils.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ is_installed <- function(pkg) {
5757
## mimimalist, type-specific purrr::pluck()'s
5858
pluck_chr <- function(l, what) vapply(l, `[[`, character(1), what)
5959

60-
is_testing <- function() {
61-
identical(Sys.getenv("TESTTHAT"), "true") || identical(Sys.getenv("R_COVR"), "true")
62-
}
63-
6460
interactive <- function() {
6561
ui_stop(
6662
"Internal error: use rlang's {ui_code('is_interactive()')} \\

tests/testthat/helper.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ if (!is.null(session_temp_proj)) {
1616
))
1717
}
1818

19-
## putting `pattern` in the package or project name is part of our strategy for
20-
## suspending the nested project check during testing
21-
pattern <- "aaa"
22-
23-
scoped_temporary_package <- function(dir = file_temp(pattern = pattern),
19+
scoped_temporary_package <- function(dir = file_temp(pattern = "testpkg"),
2420
env = parent.frame(),
2521
rstudio = FALSE) {
2622
scoped_temporary_thing(dir, env, rstudio, "package")
2723
}
2824

29-
scoped_temporary_project <- function(dir = file_temp(pattern = pattern),
25+
scoped_temporary_project <- function(dir = file_temp(pattern = "testproj"),
3026
env = parent.frame(),
3127
rstudio = FALSE) {
3228
scoped_temporary_thing(dir, env, rstudio, "project")

tests/testthat/test-browse.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ test_that("github_home() has fall back", {
1111
})
1212

1313
test_that("cran_home() produces canonical URL", {
14-
pkg <- scoped_temporary_package(file_temp("aaa"))
15-
expect_match(cran_home(), "https://cran.r-project.org/package=aaa")
14+
pkg <- scoped_temporary_package(file_temp("abc"))
15+
expect_match(cran_home(), "https://cran.r-project.org/package=abc")
1616
expect_match(cran_home("bar"), "https://cran.r-project.org/package=bar")
1717
})
1818

tests/testthat/test-create.R

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
context("create")
2-
31
test_that("create_package() creates a package", {
42
dir <- scoped_temporary_package()
53
expect_true(possibly_in_proj(dir))
@@ -29,31 +27,59 @@ test_that("create functions return path to new proj, but restore active proj", {
2927

3028
test_that("nested package is disallowed, by default", {
3129
dir <- scoped_temporary_package()
32-
expect_usethis_error(scoped_temporary_package(path(dir, "abcde")), "anyway")
30+
expect_usethis_error(create_package(path(dir, "abcde")), "anyway")
3331
})
3432

3533
test_that("nested project is disallowed, by default", {
3634
dir <- scoped_temporary_project()
37-
expect_usethis_error(scoped_temporary_project(path(dir, "abcde")), "anyway")
35+
expect_usethis_error(create_project(path(dir, "abcde")), "anyway")
36+
})
37+
38+
test_that("nested package can be created if user really, really wants to", {
39+
parent <- scoped_temporary_package()
40+
with_mock(
41+
# since user can't approve interactively, use the backdoor
42+
`usethis:::allow_nested_project` = function() TRUE,
43+
child <- create_package(path(parent, "fghijk"))
44+
)
45+
expect_true(possibly_in_proj(child))
46+
expect_true(is_package(child))
47+
})
48+
49+
test_that("nested project can be created if user really, really wants to", {
50+
parent <- scoped_temporary_project()
51+
with_mock(
52+
# since user can't approve interactively, use the backdoor
53+
`usethis:::allow_nested_project` = function() TRUE,
54+
child <- create_project(path(parent, "fghijk"))
55+
)
56+
expect_true(possibly_in_proj(child))
57+
expect_false(is_package(child))
3858
})
3959

4060
test_that("can create package in current directory", {
41-
dir <- dir_create(path(file_temp(), "mypackage"))
42-
withr::local_dir(dir)
61+
# doing this "by hand" vs. via withr because Windows appears to be unwilling
62+
# to delete current working directory
63+
newdir <- dir_create(path(file_temp(), "mypackage"))
64+
oldwd <- setwd(newdir)
65+
on.exit({
66+
setwd(oldwd)
67+
dir_delete(newdir)
68+
})
4369
expect_error_free(create_package("."))
4470
})
4571

4672
## https://github.com/r-lib/usethis/issues/227
4773
test_that("create_* works w/ non-existing rel path and absolutizes it", {
4874
## take care to provide a **non-absolute** path
49-
path_package <- path_file(file_temp(pattern = "aaa"))
75+
path_package <- path_file(file_temp(pattern = "abc"))
5076
withr::with_dir(
5177
path_temp(),
5278
create_package(path_package, rstudio = FALSE, open = FALSE)
5379
)
5480
expect_true(dir_exists(path_temp(path_package)))
5581

56-
path_project <- path_file(file_temp(pattern = "aaa"))
82+
path_project <- path_file(file_temp(pattern = "abc"))
5783
withr::with_dir(
5884
path_temp(),
5985
create_project(path_project, rstudio = FALSE, open = FALSE)

tests/testthat/test-proj.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ test_that("local_project() activates proj til scope ends", {
186186
old_project <- proj_get_()
187187
on.exit(proj_set_(old_project))
188188

189-
new_proj <- file_temp(pattern = "aaa")
189+
new_proj <- file_temp(pattern = "localprojtest")
190190
create_project(new_proj, rstudio = FALSE, open = FALSE)
191191
proj_set_(NULL)
192192

tests/testthat/test-use-course.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ test_that("create_download_url() works", {
163163

164164
test_that("normalize_url() prepends https:// (or not)", {
165165
expect_error(normalize_url(1), "is\\.character.*not TRUE")
166-
expect_identical(normalize_url("http://bit.ly/aaa"), "http://bit.ly/aaa")
167-
expect_identical(normalize_url("bit.ly/aaa"), "https://bit.ly/aaa")
166+
expect_identical(normalize_url("http://bit.ly/abc"), "http://bit.ly/abc")
167+
expect_identical(normalize_url("bit.ly/abc"), "https://bit.ly/abc")
168168
expect_identical(
169169
normalize_url("https://github.com/r-lib/rematch2/archive/master.zip"),
170170
"https://github.com/r-lib/rematch2/archive/master.zip"

0 commit comments

Comments
 (0)