Skip to content

Use withr version that can defer events on global env #1107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Imports:
stats,
utils,
whisker,
withr,
withr (>= 2.2.0),
yaml
Suggests:
covr,
Expand Down
36 changes: 12 additions & 24 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ if (!is.null(session_temp_proj)) {
))
}

scoped_temporary_package <- function(dir = file_temp(pattern = "testpkg"),
create_local_package <- function(dir = file_temp(pattern = "testpkg"),
env = parent.frame(),
rstudio = FALSE) {
scoped_temporary_thing(dir, env, rstudio, "package")
create_local_thing(dir, env, rstudio, "package")
}

scoped_temporary_project <- function(dir = file_temp(pattern = "testproj"),
create_local_project <- function(dir = file_temp(pattern = "testproj"),
env = parent.frame(),
rstudio = FALSE) {
scoped_temporary_thing(dir, env, rstudio, "project")
create_local_thing(dir, env, rstudio, "project")
}

scoped_temporary_thing <- function(dir = file_temp(pattern = pattern),
create_local_thing <- function(dir = file_temp(pattern = pattern),
env = parent.frame(),
rstudio = FALSE,
thing = c("package", "project")) {
Expand All @@ -38,25 +38,13 @@ scoped_temporary_thing <- function(dir = file_temp(pattern = pattern),
}

old_project <- proj_get_()
## Can't schedule a deferred project reset if calling this from the R
## console, which is useful when developing tests
if (identical(env, globalenv())) {
ui_done("Switching to a temporary project!")
if (!is.null(old_project)) {
command <- paste0('proj_set(\"', old_project, '\")')
ui_todo(
"Restore current project with: {ui_code(command)}"
)
}
} else {
withr::defer({
ui_silence({
proj_set(old_project, force = TRUE)
})
setwd(old_project)
fs::dir_delete(dir)
}, envir = env)
}
withr::defer({
ui_silence({
proj_set(old_project, force = TRUE)
})
setwd(old_project)
fs::dir_delete(dir)
}, envir = env)

ui_silence({
switch(thing,
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-addin.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context("use_addin")

test_that("use_addin() creates the first addins.dcf as promised", {
scoped_temporary_package()
create_local_package()
use_addin("addin.test")

addin_dcf <- read_utf8(proj_path("inst", "rstudio", "addins.dcf"))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-browse.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("github_home() has fall back", {
})

test_that("cran_home() produces canonical URL", {
pkg <- scoped_temporary_package(file_temp("abc"))
pkg <- create_local_package(file_temp("abc"))
expect_match(cran_home(), "https://cran.r-project.org/package=abc")
expect_match(cran_home("bar"), "https://cran.r-project.org/package=bar")
})
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-create.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
test_that("create_package() creates a package", {
dir <- scoped_temporary_package()
dir <- create_local_package()
expect_true(possibly_in_proj(dir))
expect_true(is_package(dir))
})

test_that("create_project() creates a non-package project", {
dir <- scoped_temporary_project()
dir <- create_local_project()
expect_true(possibly_in_proj(dir))
expect_false(is_package(dir))
})
Expand All @@ -26,17 +26,17 @@ test_that("create functions return path to new proj, but restore active proj", {
})

test_that("nested package is disallowed, by default", {
dir <- scoped_temporary_package()
dir <- create_local_package()
expect_usethis_error(create_package(path(dir, "abcde")), "anyway")
})

test_that("nested project is disallowed, by default", {
dir <- scoped_temporary_project()
dir <- create_local_project()
expect_usethis_error(create_project(path(dir, "abcde")), "anyway")
})

test_that("nested package can be created if user really, really wants to", {
parent <- scoped_temporary_package()
parent <- create_local_package()
with_mock(
# since user can't approve interactively, use the backdoor
`usethis:::allow_nested_project` = function() TRUE,
Expand All @@ -47,7 +47,7 @@ test_that("nested package can be created if user really, really wants to", {
})

test_that("nested project can be created if user really, really wants to", {
parent <- scoped_temporary_project()
parent <- create_local_project()
with_mock(
# since user can't approve interactively, use the backdoor
`usethis:::allow_nested_project` = function() TRUE,
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-description.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ test_that("creation succeeds even if options are broken", {
withr::local_options(list(usethis.description = list(
`Authors@R` = 'person('
)))
scoped_temporary_project()
create_local_project()

expect_error(use_description(), NA)
})

test_that("default description is tidy", {
withr::local_options(list(usethis.description = NULL, devtools.desc = NULL))
scoped_temporary_package()
create_local_package()

before <- readLines(proj_path("DESCRIPTION"))
use_tidy_description()
Expand All @@ -75,7 +75,7 @@ test_that("default description is tidy", {

test_that("valid CRAN names checked", {
withr::local_options(list(usethis.description = NULL, devtools.desc = NULL))
scoped_temporary_package(dir = file_temp(pattern = "invalid_pkg_name"))
create_local_package(dir = file_temp(pattern = "invalid_pkg_name"))

expect_error(use_description(check_name = FALSE), NA)
expect_error(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-documentation.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
test_that("use_package_doc() requires a package", {
scoped_temporary_project()
create_local_project()
expect_false(has_package_doc())
expect_usethis_error(use_package_doc(), "not an R package")
})

test_that("use_package_doc() creates the promised file", {
scoped_temporary_package()
create_local_package()
use_package_doc()
expect_proj_file("R", paste0(project_name(), "-package.R"))
expect_true(has_package_doc())
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-edit.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,47 +114,47 @@ test_that("edit_git_XXX('user') ensures the file exists", {
})

test_that("edit_r_profile() ensures .Rprofile exists in project", {
scoped_temporary_package()
create_local_package()
edit_r_profile("project")
expect_proj_file(".Rprofile")

scoped_temporary_project()
create_local_project()
edit_r_profile("project")
expect_proj_file(".Rprofile")
})

test_that("edit_r_environ() ensures .Renviron exists in project", {
scoped_temporary_package()
create_local_package()
edit_r_environ("project")
expect_proj_file(".Renviron")

scoped_temporary_project()
create_local_project()
edit_r_environ("project")
expect_proj_file(".Renviron")
})

test_that("edit_r_makevars() ensures .R/Makevars exists in package", {
scoped_temporary_package()
create_local_package()
edit_r_makevars("project")
expect_proj_file(".R", "Makevars")
})

test_that("edit_git_config() ensures git ignore file exists in project", {
scoped_temporary_package()
create_local_package()
edit_git_config("project")
expect_proj_file(".git", "config")

scoped_temporary_project()
create_local_project()
edit_git_config("project")
expect_proj_file(".git", "config")
})

test_that("edit_git_ignore() ensures .gitignore exists in project", {
scoped_temporary_package()
create_local_package()
edit_git_ignore("project")
expect_proj_file(".gitignore")

scoped_temporary_project()
create_local_project()
edit_git_ignore("project")
expect_proj_file(".gitignore")
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-git-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ context("test-git-utils")
test_that("git_config returns old local values", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
repo <- git_init()

out <- git_config(x.y = "x", .repo = repo)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-git.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("git_has_commit() changes after first commit", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
git_init()

expect_true(uses_git())
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-github-action.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("use_github_action() allows for custom urls", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
use_git()
use_git_remote(name = "origin", url = "https://github.com/fake/fake")

Expand All @@ -16,7 +16,7 @@ test_that("use_github_action() allows for custom urls", {
test_that("use_github_action() appends yaml in name if missing", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
use_git()
use_git_remote(name = "origin", url = "https://github.com/fake/fake")

Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-github-actions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ context("GitHub Actions")
test_that("uses_github_actions() reports usage of GitHub Actions", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
expect_false(uses_github_actions())
use_git()
use_git_remote(name = "origin", url = "https://github.com/fake/fake")
Expand All @@ -13,7 +13,7 @@ test_that("uses_github_actions() reports usage of GitHub Actions", {
})

test_that("check_uses_github_actions() can throw error", {
scoped_temporary_package()
create_local_package()
expect_error(
check_uses_github_actions(),
"Do you need to run `use_github_actions()`?",
Expand All @@ -24,7 +24,7 @@ test_that("check_uses_github_actions() can throw error", {
test_that("use_github_actions() configures GitHub Actions", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
use_git()
use_git_remote(name = "origin", url = "https://github.com/fake/fake")
use_description_field("URL", "https://github.com/fake/fake")
Expand Down Expand Up @@ -54,7 +54,7 @@ test_that("use_github_actions() configures GitHub Actions", {
test_that("use_github_actions() configures .Rbuildignore", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
expect_false(uses_circleci())
use_git()
use_git_remote(name = "origin", url = "https://github.com/fake/fake")
Expand All @@ -66,7 +66,7 @@ test_that("use_github_actions() configures .Rbuildignore", {
test_that("use_github_action_check_full() configures full GitHub Actions", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
use_git()
use_git_remote(name = "origin", url = "https://github.com/fake/fake")
use_description_field("URL", "https://github.com/fake/fake")
Expand Down Expand Up @@ -99,7 +99,7 @@ test_that("use_github_action_check_full() configures full GitHub Actions", {
test_that("use_github_action_check_full() configures the pr commands", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
use_git()
use_git_remote(name = "origin", url = "https://github.com/fake/fake")
use_description_field("URL", "https://github.com/fake/fake")
Expand All @@ -113,7 +113,7 @@ test_that("use_github_action_check_full() configures the pr commands", {
test_that("use_tidy_github_actions() configures the full check and pr commands", {
skip_if_no_git_user()

scoped_temporary_package()
create_local_package()
use_git()
use_git_remote(name = "origin", url = "https://github.com/fake/fake")
use_description_field("URL", "https://github.com/fake/fake")
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

test_that("use_description_field() can address an existing field", {
pkg <- scoped_temporary_package()
pkg <- create_local_package()
orig <- tools::md5sum(proj_path("DESCRIPTION"))

## specify existing value of existing field --> should be no op
Expand Down Expand Up @@ -31,13 +31,13 @@ test_that("use_description_field() can address an existing field", {
})

test_that("use_description_field() can add new field", {
pkg <- scoped_temporary_package()
pkg <- create_local_package()
use_description_field(name = "foo", value = "bar", base_path = pkg)
expect_identical(c(foo = "bar"), desc::desc_get("foo", pkg))
})

test_that("use_description_field() ignores whitespace", {
pkg <- scoped_temporary_package()
pkg <- create_local_package()
use_description_field(name = "foo", value = "\n bar")
use_description_field(name = "foo", value = "bar")
expect_identical(c(foo = "\n bar"), desc::desc_get("foo", pkg))
Expand Down Expand Up @@ -74,7 +74,7 @@ test_that("valid_file_name() enforces valid file names", {
# use_dependency ----------------------------------------------------------

test_that("we message for new type and are silent for same type", {
scoped_temporary_package()
create_local_package()
withr::local_options(list(usethis.quiet = FALSE, crayon.enabled = FALSE))

expect_message(
Expand All @@ -85,7 +85,7 @@ test_that("we message for new type and are silent for same type", {
})

test_that("we message for version change and are silent for same version", {
scoped_temporary_package()
create_local_package()
withr::local_options(list(usethis.quiet = FALSE, crayon.enabled = FALSE))

expect_message(
Expand All @@ -106,7 +106,7 @@ test_that("we message for version change and are silent for same version", {

## https://github.com/r-lib/usethis/issues/99
test_that("use_dependency() upgrades a dependency", {
scoped_temporary_package()
create_local_package()
withr::local_options(list(usethis.quiet = FALSE, crayon.enabled = FALSE))

expect_message(use_dependency("usethis", "Suggests"))
Expand All @@ -119,7 +119,7 @@ test_that("use_dependency() upgrades a dependency", {

## https://github.com/r-lib/usethis/issues/99
test_that("use_dependency() declines to downgrade a dependency", {
scoped_temporary_package()
create_local_package()
withr::local_options(list(usethis.quiet = FALSE, crayon.enabled = FALSE))

expect_message(use_dependency("usethis", "Imports"))
Expand All @@ -131,7 +131,7 @@ test_that("use_dependency() declines to downgrade a dependency", {
})

test_that("can add LinkingTo dependency if other dependency already exists", {
scoped_temporary_package()
create_local_package()
withr::local_options(list(usethis.quiet = FALSE, crayon.enabled = FALSE))

expect_message(use_dependency("Rcpp", "Imports"), "Adding 'Rcpp'")
Expand Down
Loading