diff --git a/DESCRIPTION b/DESCRIPTION index 9403b9d18..3e21a38c4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,6 +23,7 @@ Imports: withr, yaml Suggests: + callr, desc, docopt (>= 0.7.1), git2r, diff --git a/R/testing.R b/R/testing.R index ad1e98e11..d8b379cf1 100644 --- a/R/testing.R +++ b/R/testing.R @@ -37,13 +37,12 @@ run_test <- function(hook_name, file_name = hook_name, suffix = ".R", - std_err = NULL, - std_out = NULL, + std_out = "", cmd_args = NULL, artifacts = NULL, file_transformer = function(files) files, env = character(), - expect_success = is.null(std_err), + expect_success, read_only = FALSE) { withr::local_envvar(list(R_PRECOMMIT_HOOK_ENV = "1")) path_executable <- fs::dir_ls(system.file( @@ -57,7 +56,6 @@ run_test <- function(hook_name, ensure_named(names(file_name), fs::path_file) run_test_impl( path_executable, path_candidate, - std_err = std_err, std_out = std_out, cmd_args = cmd_args, artifacts = ensure_named(artifacts), @@ -92,7 +90,6 @@ run_test <- function(hook_name, #' @keywords internal run_test_impl <- function(path_executable, path_candidate, - std_err, std_out, cmd_args, artifacts, @@ -124,29 +121,16 @@ run_test_impl <- function(path_executable, env ) hook_state_assert( - path_candidate, - tempdir, - path_candidate_temp, - file_transformer, - path_stdout, - path_stderr, - expect_success, - std_err, - std_out, - exit_status + path_candidate = path_candidate, + tempdir = tempdir, + path_candidate_temp = path_candidate_temp, + file_transformer = file_transformer, + path_stdout = path_stdout, + expect_success = expect_success, + std_out = std_out, + exit_status = exit_status ) - if (isTRUE(read_only)) { - files_after_hook <- fs::dir_ls(tempdir, all = TRUE, recurse = TRUE) - testthat::expect_equal(files_before_hook, files_after_hook) - - if (!is.null(artifacts)) { - purrr::iwalk(artifacts, function(reference_path, temp_path) { - artifact_before_hook <- readLines(reference_path) - artifact_after_hook <- readLines(fs::path_join(c(tempdir, temp_path))) - testthat::expect_equal(artifact_before_hook, artifact_after_hook) - }) - } - } + hook_state_assert_ready_only(tempdir, files_before_hook = files_before_hook, artifacts = artifacts) } @@ -170,10 +154,16 @@ hook_state_create <- function(tempdir, # each as a single term files <- shQuote(files) - system2(paste0(Sys.getenv("R_HOME"), "/bin/Rscript"), - args = as.character(c(path_executable, cmd_args, files)), - stderr = path_stderr, stdout = path_stdout, env = env + output <- callr::rscript( + script = path_executable, + cmdargs = as.character(c(cmd_args, files)), + stderr = path_stdout, + stdout = path_stdout, + env = c(callr::rcmd_safe_env(), env), + fail_on_status = FALSE, + show = FALSE ) + output$status } #' Check if the hook produced what you want @@ -185,9 +175,7 @@ hook_state_assert <- function(path_candidate, path_candidate_temp, file_transformer, path_stdout, - path_stderr, expect_success, - std_err, std_out, exit_status) { purrr::map2(path_candidate, path_candidate_temp, @@ -195,9 +183,7 @@ hook_state_assert <- function(path_candidate, tempdir = tempdir, file_transformer = file_transformer, path_stdout = path_stdout, - path_stderr = path_stderr, expect_success = expect_success, - std_err = std_err, std_out = std_out, exit_status = exit_status ) @@ -208,9 +194,7 @@ hook_state_assert_one <- function(path_candidate, path_candidate_temp, file_transformer, path_stdout, - path_stderr, expect_success, - std_err, std_out, exit_status) { candidate <- readLines(path_candidate_temp) @@ -221,43 +205,30 @@ hook_state_assert_one <- function(path_candidate, file_transformer(path_temp) ) reference <- readLines(path_temp) - if (expect_success) { - # file not changed + no stderr - contents <- readLines(path_stderr) - if (exit_status != 0) { - testthat::fail("Expected: No error. Found:", contents) - } - testthat::expect_equal(candidate, reference, ignore_attr = TRUE) - if (!is.null(std_out)) { - contents <- readLines(path_stdout) - testthat::expect_match( - paste(contents, collapse = "\n"), std_out, - fixed = TRUE - ) - } - } else if (!expect_success) { - # either file changed or stderr - if (is.na(std_err)) { - if (identical(candidate, reference)) { - testthat::fail(paste0( - path_candidate, " and ", path_candidate_temp, - " are not supposed to be identical but they are" - )) - } - } else { - contents <- readLines(path_stderr) - testthat::expect_match( - paste(contents, collapse = "\n"), std_err, - fixed = TRUE - ) - if (!is.null(std_out)) { - contents <- readLines(path_stdout) - testthat::expect_match( - paste(contents, collapse = "\n"), std_out, - fixed = TRUE - ) - } - testthat::expect_false(exit_status == 0) + # assert exit status + is_success <- exit_status == 0 + testthat::expect_equal(is_success, expect_success) + + # assert stdout + contents <- readLines(path_stdout) + testthat::expect_match( + paste(contents, collapse = "\n"), std_out, + fixed = TRUE + ) +} + + +hook_state_assert_ready_only <- function(read_only, tempdir, files_before_hook, artifacts) { + if (isTRUE(read_only)) { + files_after_hook <- fs::dir_ls(tempdir, all = TRUE, recurse = TRUE) + testthat::expect_equal(files_before_hook, files_after_hook) + + if (!is.null(artifacts)) { + purrr::iwalk(artifacts, function(reference_path, temp_path) { + artifact_before_hook <- readLines(reference_path) + artifact_after_hook <- readLines(fs::path_join(c(tempdir, temp_path))) + testthat::expect_equal(artifact_before_hook, artifact_after_hook) + }) } } } diff --git a/inst/WORDLIST b/inst/WORDLIST index 2817f96f9..189501542 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -12,6 +12,7 @@ beaefa behaviour bioconductor bliblablupp +callr cfe Changelog ci diff --git a/man/hook_state_assert.Rd b/man/hook_state_assert.Rd index 5ec5baf61..cc994bf01 100644 --- a/man/hook_state_assert.Rd +++ b/man/hook_state_assert.Rd @@ -10,9 +10,7 @@ hook_state_assert( path_candidate_temp, file_transformer, path_stdout, - path_stderr, expect_success, - std_err, std_out, exit_status ) diff --git a/man/run_test.Rd b/man/run_test.Rd index 60749a8a9..30ee5fad5 100644 --- a/man/run_test.Rd +++ b/man/run_test.Rd @@ -8,13 +8,12 @@ run_test( hook_name, file_name = hook_name, suffix = ".R", - std_err = NULL, - std_out = NULL, + std_out = "", cmd_args = NULL, artifacts = NULL, file_transformer = function(files) files, env = character(), - expect_success = is.null(std_err), + expect_success, read_only = FALSE ) } @@ -28,9 +27,6 @@ to the temporary location and the value is the source of the file.} \item{suffix}{The suffix of \code{file_name}.} -\item{std_err}{An expected error message. If no error is expected, this -can be \code{NULL}. In that case, the \code{comparator} is applied.} - \item{std_out}{The expected stdout message. If \code{NULL}, this check is omitted.} \item{cmd_args}{More arguments passed to the file. Pre-commit handles it as diff --git a/man/run_test_impl.Rd b/man/run_test_impl.Rd index 61accf0b2..30c254bfc 100644 --- a/man/run_test_impl.Rd +++ b/man/run_test_impl.Rd @@ -7,7 +7,6 @@ run_test_impl( path_executable, path_candidate, - std_err, std_out, cmd_args, artifacts, @@ -23,9 +22,6 @@ run_test_impl( \item{path_candidate}{The path to a file that should be modified by the executable.} -\item{std_err}{An expected error message. If no error is expected, this -can be \code{NULL}. In that case, the \code{comparator} is applied.} - \item{std_out}{The expected stdout message. If \code{NULL}, this check is omitted.} \item{cmd_args}{More arguments passed to the file. Pre-commit handles it as @@ -45,6 +41,9 @@ error, but just a message.} \item{read_only}{If \code{TRUE}, then assert that no new files were created. Additionally, if \code{artifacts} are not \code{NULL}, then assert that hook did not modify the artifacts.} + +\item{std_err}{An expected error message. If no error is expected, this +can be \code{NULL}. In that case, the \code{comparator} is applied.} } \description{ Implement a test run diff --git a/tests/testthat/test-conda.R b/tests/testthat/test-conda.R deleted file mode 100644 index c4a7f1e42..000000000 --- a/tests/testthat/test-conda.R +++ /dev/null @@ -1,240 +0,0 @@ -if (!on_cran()) { - test_that("can install pre-commit", { - skip_if(not_conda()) - expect_error(install_precommit(), NA) - expect_error(install_precommit(force = TRUE), NA) - }) - - test_that("can use pre-commit", { - tempdir <- local_test_setup(quiet = FALSE, install_hooks = FALSE) - expect_message( - use_precommit(open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir), - "R specific hooks" - ) - expect_message( - use_precommit(open = FALSE, force = FALSE, install_hooks = FALSE, root = tempdir), - "There is already " - ) - }) - - test_that("can use pre-commit", { - tempdir <- local_test_setup(quiet = FALSE, install_hooks = FALSE) - expect_message( - use_precommit(open = FALSE, force = TRUE, install_hooks = TRUE, root = tempdir), - "R specific hooks" - ) - }) - - test_that("fails early if repo is not a git repo ", { - tempdir <- local_test_setup(git = FALSE, quiet = FALSE, install_hooks = FALSE) - - expect_error( - use_precommit(open = FALSE, install_hooks = FALSE, root = tempdir), - "is not a git repo" - ) - }) - - test_that("can use custom config file ", { - tempdir1 <- local_test_setup(install_hooks = FALSE) - tempdir2 <- local_test_setup(install_hooks = FALSE) - - path_custom <- fs::path(tempdir2, "some-precommit.yaml") - new_text <- "# 4js93" - readLines(system.file("pre-commit-config-proj.yaml", package = "precommit")) %>% - c(new_text) %>% - writeLines(path_custom) - git_init(tempdir1) - use_precommit(config_source = path_custom, open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir1) - config <- readLines(fs::path(tempdir1, ".pre-commit-config.yaml")) - expect_equal( - config[length(config)], - new_text - ) - }) - - test_that("existing hooks are recognized", { - tempdir <- local_test_setup(quiet = FALSE, install_hooks = FALSE) - withr::with_dir(tempdir, { - git_init() - usethis::proj_set(".") - usethis::use_readme_rmd(open = FALSE) - withr::local_options(precommit.block_install_hooks = TRUE) - - # usethis hook is removed without error - expect_message( - use_precommit(legacy_hooks = "forbid", open = FALSE, install_hooks = FALSE, root = "."), - "Removed the render-README hook," - ) - writeLines(letters, ".git/hooks/pre-commit") - expect_error( - use_precommit(legacy_hooks = "forbid", open = FALSE, install_hooks = FALSE, root = "."), - "existing hooks installed" - ) - - # tolerate other hook scripts in migration mode - expect_message( - use_precommit(legacy_hooks = "allow", force = TRUE, open = FALSE, install_hooks = FALSE, root = "."), - "Running in migration" - ) - - # can also remove other hooks - writeLines(letters, ".git/hooks/pre-commit") - expect_message( - use_precommit(legacy_hooks = "remove", force = TRUE, open = FALSE, install_hooks = FALSE, root = "."), - "Sucessfully installed" - ) - }) - }) - - - test_that("Can uninstall pre-commit (repo scope)", { - # with all files there - tempdir <- local_test_setup(use_precommit = TRUE, quiet = FALSE, install_hooks = FALSE) - expect_message( - uninstall_precommit(scope = "repo", root = tempdir), - "Uninstalled pre-commit from repo scope.*" - ) - expect_false(file_exists(fs::path(tempdir, ".pre-commit-config.yaml"))) - expect_false(any(grepl(".pre-commit", readline(fs::path(tempdir, ".Rbuildignore"))))) - # second time - expect_message( - uninstall_precommit(scope = "repo", root = tempdir), - "You can re-install" - ) - - # when there is no pre-commit.yaml anymore - suppressMessages( - use_precommit( - open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir - ) - ) - fs::file_delete(fs::path(tempdir, ".pre-commit-config.yaml")) - expect_message( - uninstall_precommit(scope = "repo", root = tempdir), - paste("Uninstalled pre-commit from repo scope.*") - ) - }) - - test_that("Can uninstall (userly)", { - if (not_conda()) { - tempdir <- local_test_setup(use_precommit = TRUE, quiet = FALSE, install_hooks = FALSE) - expect_error( - uninstall_precommit(scope = "user", ask = "none", root = tempdir), - "installed with conda" - ) - } else { - tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE, install_hooks = FALSE) - expect_message( - uninstall_precommit(scope = "user", ask = "none", root = tempdir), - "Removed pre-commit from" - ) - expect_error( - uninstall_precommit(scope = "user", ask = "none", root = tempdir), - "No installation found." - ) - } - }) - - test_that("use_precommit fails when no user installation is found", { - skip_if(not_conda()) - expect_error(use_precommit(open = FALSE, install_hooks = FALSE, root = tempdir), "installed on your system") - }) - - test_that("can install pre-commit with remote config", { - if (!not_conda()) { - expect_error(install_precommit(), NA) - } - tempdir <- local_test_setup(quiet = FALSE, install_hooks = FALSE) - expect_message( - use_precommit( - example_remote_config(), - open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir - ), - "R specific hooks" - ) - }) - - test_that("fails gracefully when there are", { - if (!not_conda()) { - expect_message(install_precommit(), "already installed") - } - tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE, install_hooks = FALSE) - withr::local_options(precommit.block_install_hooks = TRUE) - withr::with_dir( - tempdir, - { - withr::defer(call_and_capture("git", "config --unset-all core.hooksPath")) - call_and_capture("git", "config core.hooksPath .githooks") - expect_error( - use_precommit(open = FALSE, force = TRUE, install_hooks = FALSE, root = tempdir), - "stdout: [ERROR] Cowardly refusing to install hooks with `core.hooksPath` set.", - fixed = TRUE - ) - } - ) - }) - - test_that("fails gracefully when reticulate is not available", { - if (not_conda()) { - expect_error(install_precommit(), "Please install the R package reticulate") - } - }) - test_that("can update via conda", { - if (not_conda()) { - local_mocked_bindings(assert_reticulate_is_installed = function(...) NULL) - expect_error( - update_precommit(), - paste( - "You can only update your pre-commit executable via the R API if you", - "chose the installation method via conda" - ) - ) - - expect_match(version_precommit(), "[0-9]+\\.[0-9]+\\.[0-9]+") - } else { - uninstall_precommit(scope = "user", ask = "none", root = ".") - version <- "3.3.2" # TODO make this automaticly the last version - reticulate::conda_install("r-precommit", paste0("pre-commit==", version)) - expect_equal(version_precommit(), version) - skip_on_os("windows") - expect_invisible(update_precommit(), 0) - expect_false(version_precommit() == version) - } - }) -} - -test_that("Autoupdate is not conducted when renv present in incompatible setup", { - skip_on_cran() - - # mock old pre-commit and renv versions - local_mocked_bindings(version_precommit = function(...) "2.13.0") - - local_test_setup( - git = TRUE, use_precommit = TRUE, install_hooks = FALSE, open = FALSE - ) - initial <- rev_read() %>% - rev_as_pkg_version() - # simulate adding {renv} - writeLines("", "renv.lock") - - # should downgrade rev - expect_error( - ensure_renv_precommit_compat( - package_version_renv = package_version("0.13.0"), root = getwd() - ), - "Please update" - ) - downgraded <- rev_read() %>% - rev_as_pkg_version() - expect_true(downgraded == initial) - - # simulate removing {renv} should be updated - fs::file_delete("renv.lock") - expect_warning( - ensure_renv_precommit_compat( - package_version("0.13.0"), - root = getwd() - ), - NA - ) -}) diff --git a/tests/testthat/test-config.R b/tests/testthat/test-config.R deleted file mode 100644 index d5be56d64..000000000 --- a/tests/testthat/test-config.R +++ /dev/null @@ -1,71 +0,0 @@ -test_that("can set path to local config", { - tmp <- tempdir() - test.pkg <- fs::dir_create(tmp, "test.proj") - expect_equal( - set_config_source(NULL, root = test.pkg), - system.file("pre-commit-config-proj.yaml", package = "precommit") - ) - expect_error( - set_config_source(tempfile()), - "does not exist" - ) -}) - -test_that("can set path to remote config", { - skip_on_cran() - path <- set_config_source( - example_remote_config() - ) - expect_equal(fs::path_ext(path), "yaml") - expect_silent(yaml::read_yaml(path)) - - expect_error(set_config_source("https://apple.com"), "valid yaml") -}) - -test_that("defaults to right config depending on whether or not root is a pkg", { - tmp <- tempdir() - test.pkg <- fs::dir_create(tmp, "test.pkg") - withr::with_dir(test.pkg, { - desc <- desc::description$new("!new") - desc$set(Package = "test.pkg") - desc$write("DESCRIPTION") - }) - expect_message( - set_config_source(NULL, root = test.pkg), - "pkg\\.yaml" - ) - fs::file_delete(fs::path(test.pkg, "DESCRIPTION")) - expect_message( - set_config_source(NULL, root = test.pkg), - "proj\\.yaml" - ) -}) - -test_that(".Rbuildignore is written to the right directory when root is relative", { - root <- tempfile() - fs::dir_create(root) - - withr::with_dir(root, { - desc <- desc::description$new("!new") - desc$set(Package = "test.pkg") - desc$write("DESCRIPTION") - }) - withr::with_dir( - fs::path_dir(root), - use_precommit_config(root = fs::path_file(root)) - ) - expect_true(file_exists(fs::path(root, ".Rbuildignore"))) -}) - -test_that(".Rbuildignore is written to the right directory when root is absolute", { - root <- tempfile() - fs::dir_create(root) - - withr::with_dir(root, { - desc <- desc::description$new("!new") - desc$set(Package = "test.pkg") - desc$write("DESCRIPTION") - }) - use_precommit_config(root = root) - expect_true(file_exists(fs::path(root, ".Rbuildignore"))) -}) diff --git a/tests/testthat/test-docopt.R b/tests/testthat/test-docopt.R deleted file mode 100644 index f83f08195..000000000 --- a/tests/testthat/test-docopt.R +++ /dev/null @@ -1,39 +0,0 @@ -test_that("custom docopt interface parses as expected", { - args_variants <- list( - "file a.R", - c("file a.R", "file-B.R"), - c("File A.R", "File c.R"), - c("Another file with spaces.R", "--warn_only"), - c("Another file with spaces.R", "Yet another file with spaces (YAFWS).R", "--warn_only"), - c("--warn_only", "Another file with spaces.R"), - c("--warn_only", "Another file with spaces.R", "Yet another file with spaces (YAFWS).R"), - c("Another file with spaces.R", "--warn_only", "Yet another file with spaces (YAFWS).R") - ) - - "Run lintr on R files during a precommit. -Usage: - cmdtest [--warn_only] ... -Options: - --warn_only Placeholder for test. -" -> doc - - for (args in args_variants) { - new_args <- precommit_docopt(doc, args) - - # to show failures in vanilla docopt, use this: - # new_args <- docopt::docopt(doc, args) - - expect_equal(length(new_args), 4) - if ("--warn_only" %in% args) { - expect_equal(length(new_args$files), length(args) - 1) - expect_equal(length(new_args$``), length(args) - 1) - expect_true(new_args$warn_only) - expect_true(new_args$`--warn_only`) - } else { - expect_equal(length(new_args$files), length(args)) - expect_equal(length(new_args$``), length(args)) - expect_false(new_args$warn_only) - expect_false(new_args$`--warn_only`) - } - } -}) diff --git a/tests/testthat/test-exec.R b/tests/testthat/test-exec.R deleted file mode 100644 index 69cace352..000000000 --- a/tests/testthat/test-exec.R +++ /dev/null @@ -1,94 +0,0 @@ -test_that("Path can be derived for windows Python >= 3.0 (mocked)", { - local_mocked_bindings( - path_derive_precommit_exec_win_python3plus_candidates = function() { - c( - fs::path_home("AppData/Roaming/Python/Python35"), - fs::path_home("AppData/Roaming/Python/Python37") - ) - } - ) - - expect_equal( - path_derive_precommit_exec_win_python3plus_base(), - c( - fs::path(fs::path_home(), "AppData/Roaming/Python/Python37/Scripts"), - fs::path(fs::path_home(), "AppData/Roaming/Python/Python35/Scripts") - ), - ignore_attr = TRUE - ) -}) - - -test_that("Path can be derived for windows Python >= 3.0 (actual)", { - skip_if(!is_windows()) - skip_if(!not_conda()) - skip_if(on_cran()) - expect_match(path_derive_precommit_exec_win_python3plus_base(), "AppData/Roaming") - expect_equal( - fs::path_file(path_derive_precommit_exec_win()), - precommit_executable_file() - ) -}) - - -test_that("Warns when there are multiple installations found (2x os)", { - local_mocked_bindings( - path_derive_precommit_exec_path = function(candidate) { - fs::path_home("AppData/Roaming/Python/Python35") - }, - get_os = function(...) { - c(sysname = "windows") - }, - path_derive_precommit_exec_win = function() { - c( - fs::path_home("AppData/Roaming/Python/Python34"), - fs::path_home("AppData/Roaming/Python/Python37") - ) - } - ) - - expect_warning( - path_derive_precommit_exec(), - "We detected multiple pre-commit executables" - ) -}) - -test_that("Warns when there are multiple installations found (2x path)", { - local_mocked_bindings( - path_derive_precommit_exec_path = function(candidate) { - c( - fs::path_home("AppData/Roaming/Python/Python35"), - fs::path_home("AppData/Roaming/Python/Python37") - ) - }, - get_os = function(...) { - c(sysname = "windows") - }, - path_derive_precommit_exec_win = function() { - fs::path_home("AppData/Roaming/Python/Python34") - } - ) - expect_warning( - path_derive_precommit_exec(), - "We detected multiple pre-commit executables" - ) -}) - -test_that("Warns when there are multiple installations found (path and os)", { - local_mocked_bindings( - path_derive_precommit_exec_path = function(candidate) { - fs::path_home("AppData/Roaming/Python/Python35") - }, - path_derive_precommit_exec_win = function() { - fs::path_home("AppData/Roaming/Python/Python34") - }, - get_os = function(...) { - c(sysname = "windows") - }, - ) - - expect_warning( - path_derive_precommit_exec(), - "We detected multiple pre-commit executables" - ) -}) diff --git a/tests/testthat/test-hook-codemeta-description-updated.R b/tests/testthat/test-hook-codemeta-description-updated.R index 0ec6e7949..d3b86fb9a 100644 --- a/tests/testthat/test-hook-codemeta-description-updated.R +++ b/tests/testthat/test-hook-codemeta-description-updated.R @@ -1,24 +1,25 @@ run_test("codemeta-description-update", file_name = c("codemeta.json"), suffix = "", - std_err = "No `DESCRIPTION` found in repository.", - std_out = NULL, + std_out = "No `DESCRIPTION` found in repository.", + expect_success = FALSE, + read_only = TRUE, ) run_test("codemeta-description-update", file_name = c("DESCRIPTION"), suffix = "", - std_err = "No `codemeta.json` found in repository.", - std_out = NULL, + std_out = "No `codemeta.json` found in repository.", + expect_success = FALSE, + read_only = TRUE ) - # outdated run_test("codemeta-description-update", file_name = c("DESCRIPTION", "codemeta.json"), suffix = "", - std_err = "out of date", - std_out = NULL, + std_out = "out of date", + expect_success = FALSE, file_transformer = function(files) { if (length(files) > 1) { # transformer is called once on all files and once per file @@ -27,7 +28,7 @@ run_test("codemeta-description-update", writeLines(content_2, files[1]) } files - } + }, ) # succeed @@ -42,7 +43,9 @@ run_test("codemeta-description-update", writeLines(content_2, files[2]) } files - } + }, + expect_success = TRUE, + read_only = TRUE ) if (!on_cran()) { @@ -62,7 +65,9 @@ if (!on_cran()) { writeLines(content_2, files[2]) } files - } + }, + expect_success = TRUE, + read_only = TRUE ) # # fail in wrong root @@ -73,7 +78,7 @@ if (!on_cran()) { "rpkg2/codemeta.json" = "README.md" ), cmd_args = "--root=rpkg2", - std_err = "No `DESCRIPTION` found in repository.", + std_out = "No `DESCRIPTION` found in repository.", suffix = "", file_transformer = function(files) { if (length(files) > 1) { @@ -83,6 +88,8 @@ if (!on_cran()) { writeLines(content_2, files[2]) } files - } + }, + expect_success = FALSE, + read_only = TRUE ) } diff --git a/tests/testthat/test-hook-deps-in-desc-R.R b/tests/testthat/test-hook-deps-in-desc-R.R deleted file mode 100644 index ff7ab8ddf..000000000 --- a/tests/testthat/test-hook-deps-in-desc-R.R +++ /dev/null @@ -1,133 +0,0 @@ -# succeed (call to library that is in description) -run_test("deps-in-desc", - suffix = "-success.R", std_err = NULL, - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -# fail (call to library that is not in description) -run_test("deps-in-desc", - suffix = "-fail.R", std_err = "Dependency check failed", - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -# in sub directory with wrong root -run_test("deps-in-desc", - suffix = "-fail.R", std_err = "Could not find R package", - file_transformer = function(files) { - fs::path_abs(fs::file_move(files, "rpkg")) - }, - artifacts = c("rpkg/DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -# in sub directory with correct root -if (!on_cran()) { - run_test("deps-in-desc", - cmd_args = "--root=rpkg", - suffix = "-fail.R", std_err = "Dependency check failed", - file_transformer = function(files) { - fs::path_abs(fs::file_move(files, "rpkg")) - }, - artifacts = c("rpkg/DESCRIPTION" = test_path("in/DESCRIPTION")) - ) - # in sub directory with correct root - run_test("deps-in-desc", - cmd_args = "--root=rpkg", - suffix = "-success.R", std_err = NULL, - file_transformer = function(files) { - fs::path_abs(fs::file_move(files, "rpkg")) - }, - artifacts = c("rpkg/DESCRIPTION" = test_path("in/DESCRIPTION")) - ) -} - - -# with ::: -run_test("deps-in-desc", - "deps-in-desc-dot3", - suffix = "-fail.R", std_err = "Dependency check failed", - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -run_test("deps-in-desc", - "deps-in-desc-dot3", - suffix = "-success.R", std_err = NULL, - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -run_test("deps-in-desc", - "deps-in-desc-dot3", - suffix = "-fail.R", std_err = NULL, - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")), - cmd_args = "--allow_private_imports" -) - -# Rmd -run_test("deps-in-desc", - "deps-in-desc", - suffix = "-fail.Rmd", std_err = "Dependency check failed", - std_out = "deps-in-desc-fail.Rmd`: ttyzp", - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -run_test("deps-in-desc", - "deps-in-desc", - suffix = "-success.Rmd", std_err = NULL, - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -# README.Rmd is excluded -run_test("deps-in-desc", - "README.Rmd", - suffix = "", std_err = NULL, - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf")) -) - - - -# Rnw -run_test("deps-in-desc", - "deps-in-desc", - suffix = "-fail.Rnw", std_err = "Dependency check failed", - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -run_test("deps-in-desc", - "deps-in-desc", - suffix = "-success.Rnw", std_err = NULL, - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")) -) - -# Rprofile -# because .Rprofile is executed on startup, this must be an installed -# package (to not give an error staight away) not listed in -# test_path("in/DESCRIPTION") -if (Sys.getenv("GITHUB_WORKFLOW") != "Hook tests") { - # seems like .Rprofile with renv activation does not get executed when - # argument to Rscript contains Rprofile ?! Skip this - expect_true(rlang::is_installed("R.cache")) - run_test("deps-in-desc", - "Rprofile", - suffix = "", std_err = "Dependency check failed", - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")), - file_transformer = function(files) { - writeLines("R.cache::findCache", files) - fs::file_move( - files, - fs::path(fs::path_dir(files), paste0(".", fs::path_file(files))) - ) - } - ) - - run_test("deps-in-desc", - "Rprofile", - suffix = "", std_err = NULL, - artifacts = c("DESCRIPTION" = test_path("in/DESCRIPTION")), - file_transformer = function(files) { - writeLines("utils::head", files) - fs::file_move( - files, - fs::path(fs::path_dir(files), paste0(".", fs::path_file(files))) - ) - } - ) -} diff --git a/tests/testthat/test-hook-lintr.R b/tests/testthat/test-hook-lintr.R deleted file mode 100644 index a44fcb23c..000000000 --- a/tests/testthat/test-hook-lintr.R +++ /dev/null @@ -1,24 +0,0 @@ -# .R ---- - -# success -run_test("lintr", - suffix = "-success.R", - std_err = NULL -) - -# failure -run_test("lintr", suffix = "-fail.R", std_err = "not lint free") - -# warning -run_test( - "lintr", - suffix = "-fail.R", cmd_args = "--warn_only", std_err = NULL -) - -# .qmd ---- - -# success -run_test("lintr", suffix = "-success.qmd", std_err = NULL) - -# failure -run_test("lintr", suffix = "-fail.qmd", std_err = "not lint free") diff --git a/tests/testthat/test-hook-no-browser-statement.R b/tests/testthat/test-hook-no-browser-statement.R deleted file mode 100644 index 56ff5a209..000000000 --- a/tests/testthat/test-hook-no-browser-statement.R +++ /dev/null @@ -1,13 +0,0 @@ -# success -run_test( - "no-browser-statement", - suffix = "-success.R", - std_err = NULL -) - -# failure -run_test( - "no-browser-statement", - suffix = "-fail.R", - std_err = "contains a `browser()` statement." -) diff --git a/tests/testthat/test-hook-no-debug-statement.R b/tests/testthat/test-hook-no-debug-statement.R deleted file mode 100644 index 45ac2eb14..000000000 --- a/tests/testthat/test-hook-no-debug-statement.R +++ /dev/null @@ -1,13 +0,0 @@ -# success -run_test( - "no-debug-statement", - suffix = "-success.R", - std_err = NULL -) - -# failure -run_test( - "no-debug-statement", - suffix = "-fail.R", - std_err = "contains a `debug()` or `debugonce()` statement." -) diff --git a/tests/testthat/test-hook-no-print-statement.R b/tests/testthat/test-hook-no-print-statement.R deleted file mode 100644 index 1a6a2e558..000000000 --- a/tests/testthat/test-hook-no-print-statement.R +++ /dev/null @@ -1,13 +0,0 @@ -# success -run_test( - "no-print-statement", - suffix = "-success.R", - std_err = NULL -) - -# failure -run_test( - "no-print-statement", - suffix = "-fail.R", - std_err = "contains a `print()` statement." -) diff --git a/tests/testthat/test-hook-parsable-R.R b/tests/testthat/test-hook-parsable-R.R deleted file mode 100644 index a592c413d..000000000 --- a/tests/testthat/test-hook-parsable-R.R +++ /dev/null @@ -1,19 +0,0 @@ -run_test("parsable-R", - suffix = "-success.R", - std_err = NULL -) - -run_test("parsable-R", - suffix = "-success.Rmd", - std_err = NULL -) - -# failure -run_test("parsable-R", suffix = "-fail.R", std_out = "Full context", std_err = "1 1") - -run_test( - "parsable-R", - suffix = "-fail.Rmd", - std_out = "parsable-R-fail.Rmd", - std_err = "1 1" -) diff --git a/tests/testthat/test-hook-pkgdown.R b/tests/testthat/test-hook-pkgdown.R deleted file mode 100644 index 0a4a02c76..000000000 --- a/tests/testthat/test-hook-pkgdown.R +++ /dev/null @@ -1,43 +0,0 @@ -# success index -run_test("pkgdown", - file_name = c( - "man/autoudpate.Rd" = "autoupdate.Rd", - "_pkgdown.yml" = "_pkgdown-index.yml", - "DESCRIPTION" = "DESCRIPTION" - ), - suffix = "", std_err = NULL -) - -# failed index -run_test("pkgdown", - file_name = c( - "man/flie-true.Rd" = "flie-true.Rd", - "_pkgdown.yml" = "_pkgdown-index.yml", - "DESCRIPTION" = "DESCRIPTION" - ), - suffix = "", - std_err = empty_on_cran("must be a known") -) - -# failed articles -run_test("pkgdown", - file_name = c( - "vignettes/pkgdown.Rmd" = "pkgdown.Rmd", - "_pkgdown.yml" = "_pkgdown-articles.yml", - "DESCRIPTION" = "DESCRIPTION" - ), - suffix = "", - std_err = "why-use-hooks" -) - -# success index and article -run_test("pkgdown", - file_name = c( - "man/autoudpate.Rd" = "autoupdate.Rd", - "vignettes/pkgdown.Rmd" = "pkgdown.Rmd", - "_pkgdown.yml" = "_pkgdown-index-articles.yml", - "DESCRIPTION" = "DESCRIPTION" - ), - suffix = "", - std_err = NULL -) diff --git a/tests/testthat/test-hook-readme-rmd-rendered.R b/tests/testthat/test-hook-readme-rmd-rendered.R deleted file mode 100644 index 7404acd08..000000000 --- a/tests/testthat/test-hook-readme-rmd-rendered.R +++ /dev/null @@ -1,65 +0,0 @@ -if (has_git()) { - run_test("readme-rmd-rendered", - file_name = c("README.md", "README.Rmd"), - suffix = "", - std_err = "out of date", - std_out = NULL, - file_transformer = function(files) { - if (length(files) > 1) { - # transformer is called once on all files and once per file - content_2 <- readLines(files[2]) - Sys.sleep(2) - writeLines(content_2, files[2]) - git_init() - git2r::add(path = files) - } - files - } - ) - - - # only one file staged - run_test("readme-rmd-rendered", - file_name = c("README.Rmd", "README.md"), - suffix = "", - std_err = "should be both staged", - std_out = NULL, - file_transformer = function(files) { - if (length(files) > 1) { - # transformer is called once on all files and once per file - content_2 <- readLines(files[2]) - Sys.sleep(2) - writeLines(content_2, files[2]) - git_init() - git2r::add(path = files[1]) - } - files - } - ) - - # only has md - run_test("readme-rmd-rendered", - file_name = "README.md", - suffix = "", - std_err = NULL, - std_out = NULL, - file_transformer = function(files) { - git_init() - git2r::add(path = files[1]) - files - } - ) - - # only has Rmd - run_test("readme-rmd-rendered", - file_name = "README.Rmd", - suffix = "", - std_err = NULL, - std_out = NULL, - file_transformer = function(files) { - git_init() - git2r::add(path = files[1]) - files - } - ) -} diff --git a/tests/testthat/test-hook-roxygenize.R b/tests/testthat/test-hook-roxygenize.R deleted file mode 100644 index 3954f5c92..000000000 --- a/tests/testthat/test-hook-roxygenize.R +++ /dev/null @@ -1,95 +0,0 @@ -# with outdated Rd present -run_test("roxygenize", - file_name = c("man/flie.Rd" = "flie.Rd"), - suffix = "", - std_err = NA, - std_out = empty_on_cran("Writing NAMESPACE"), - artifacts = c( - "DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf"), - "R/roxygenize.R" = test_path("in/roxygenize.R") - ), - file_transformer = function(files) { - git_init() - git2r::add(path = files) - # hack to add artifact to trigger diff_requires_roxygenize() - git2r::add(path = fs::path(fs::path_dir(fs::path_dir(files[1])), "R")) - files - } -) - -if (!on_cran()) { - # with outdated Rd present in correct root - run_test("roxygenize", - file_name = c("rpkg/man/flie.Rd" = "flie.Rd"), - suffix = "", - std_err = NA, - cmd_args = "--root=rpkg", - std_out = empty_on_cran("Writing NAMESPACE"), - artifacts = c( - "rpkg/DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf"), - "rpkg/R/roxygenize.R" = test_path("in/roxygenize.R") - ), - file_transformer = function(files) { - withr::local_dir("rpkg") - git_init() - git2r::add(path = files) - # hack to add artifact to trigger diff_requires_roxygenize() - git2r::add(path = fs::path(fs::path_dir(fs::path_dir(files[1])), "R")) - files - } - ) - # without Rd present - run_test("roxygenize", - file_name = c("rpkg1/R/roxygenize.R" = "roxygenize.R"), - suffix = "", - cmd_args = "--root=rpkg1", - std_err = "Please commit the new `.Rd` files", - artifacts = c( - "rpkg1/DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf"), - "rpkg2/R/roxygenize.R" = test_path("in/roxygenize.R") - ), - file_transformer = function(files) { - withr::local_dir("rpkg1") - git_init() - git2r::add(path = files) - files - } - ) -} - - - -# with Rd present in wrong root -run_test("roxygenize", - file_name = c("R/roxygenize.R" = "roxygenize.R"), - suffix = "", - std_err = "Please commit the new `.Rd` files", - artifacts = c( - "DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf") - ), - file_transformer = function(files) { - git_init() - git2r::add(path = files) - files - } -) - - -# with up to date rd present -run_test("roxygenize", - file_name = c("man/flie.Rd" = "flie-true.Rd"), - suffix = "", - std_err = empty_on_cran("Writing NAMESPACE"), - artifacts = c( - "DESCRIPTION" = test_path("in/DESCRIPTION-no-deps.dcf"), - "R/roxygenize.R" = test_path("in/roxygenize.R") - ), - file_transformer = function(files) { - git_init() - git2r::add(path = files) - # hack to add artifact to trigger diff_requires_roxygenize() - git2r::add(path = fs::path(fs::path_dir(fs::path_dir(files[1])), "R")) - files - }, - expect_success = TRUE -) diff --git a/tests/testthat/test-hook-spell-check.R b/tests/testthat/test-hook-spell-check.R deleted file mode 100644 index f5e551bc3..000000000 --- a/tests/testthat/test-hook-spell-check.R +++ /dev/null @@ -1,32 +0,0 @@ -# success -run_test("spell-check", suffix = "-success.md", std_err = NULL) - -# failure with --read-only does not create WORDLIST -run_test( - "spell-check", - suffix = "-fail.md", - std_err = "Spell check failed", - cmd_args = "--read-only", - read_only = TRUE -) - -# failure with --read-only does not update WORDLIST -run_test( - "spell-check", - suffix = "-fail-2.md", - std_err = "Spell check failed", - cmd_args = "--read-only", - artifacts = c("inst/WORDLIST" = test_path("in/WORDLIST")), - read_only = TRUE -) - -# success with wordlist -run_test("spell-check", - suffix = "-wordlist-success.md", - std_err = NULL, - artifacts = c("inst/WORDLIST" = test_path("in/WORDLIST")) -) - -# success with ignored files -# uses lang argument -run_test("spell-check", suffix = "-language-success.md", cmd_args = "--lang=en_GB") diff --git a/tests/testthat/test-hook-style-files.R b/tests/testthat/test-hook-style-files.R deleted file mode 100644 index f01cfb707..000000000 --- a/tests/testthat/test-hook-style-files.R +++ /dev/null @@ -1,113 +0,0 @@ -# success -run_test("style-files", - suffix = "-success.R", cmd_args = c("--cache-root=styler") -) -# fail -run_test("style-files", - suffix = "-fail-changed.R", cmd_args = c("--cache-root=styler"), - std_err = NA -) - -run_test("style-files", - suffix = "-fail-parse.R", cmd_args = c("--cache-root=styler"), - std_err = "" -) - -# success with cmd args -run_test("style-files", - file_name = "style-files-cmd", - suffix = "-success.R", - cmd_args = c("--style_pkg=styler", "--style_fun=tidyverse_style", "--cache-root=styler") -) - -run_test("style-files", - file_name = "style-files-cmd", - suffix = "-success.R", - cmd_args = c("--scope=spaces", "--cache-root=styler") -) - -run_test("style-files", - file_name = "style-files-cmd", - suffix = "-success.R", - cmd_args = c('--scope="I(\'spaces\')"', "--cache-root=styler") -) - -run_test("style-files", - file_name = "style-files-cmd", - suffix = "-success.R", - cmd_args = c( - '--scope="I(\'spaces\')"', - "--base_indention=0", - "--include_roxygen_examples=TRUE", - "--cache-root=styler" - ) -) - -run_test("style-files", - file_name = "style-files-reindention", - suffix = "-success.R", - cmd_args = c( - '--scope="I(\'spaces\')"', - "--base_indention=0", - "--include_roxygen_examples=TRUE", - '--reindention="specify_reindention(\'#\')"', - "--cache-root=styler" - ) -) - -run_test("style-files", - file_name = "style-files", - suffix = "-base-indention-success.R", - cmd_args = c("--base_indention=4", "--cache-root=styler") -) - -run_test("style-files", - file_name = "style-files", - suffix = "-roxygen-success.R", - cmd_args = c("--include_roxygen_examples=FALSE", "--cache-root=styler") -) - -run_test("style-files", - file_name = "style-files", - suffix = "-ignore-success.R", - cmd_args = c( - "--cache-root=styler", - '--ignore-start="^# styler: off$"', - '--ignore-stop="^# styler: on$"' - ) -) - -run_test("style-files", - file_name = "style-files", - suffix = "-ignore-fail.R", - cmd_args = "--cache-root=styler", - std_err = "" -) - - -# fail with cmd args -run_test("style-files", - file_name = "style-files-cmd", - suffix = "-success.R", - cmd_args = c("--scope=space", "--cache-root=styler"), - std_err = "", - expect_success = FALSE -) - -run_test("style-files", - file_name = "style-files-cmd", - suffix = "-fail.R", - std_err = NA, - cmd_args = c( - "--style_pkg=styler", "--style_fun=tidyverse_style", "--cache-root=styler" - ) -) - -run_test("style-files", - file_name = "style-files-cmd", - suffix = "-fail.R", - std_err = "must be listed in `additional_dependencies:`", - cmd_args = c( - "--style_pkg=blubliblax", "--style_fun=tidyverse_style", "--cache-root=styler" - ) -) diff --git a/tests/testthat/test-hook-use-tidy-description.R b/tests/testthat/test-hook-use-tidy-description.R deleted file mode 100644 index 2315d2030..000000000 --- a/tests/testthat/test-hook-use-tidy-description.R +++ /dev/null @@ -1,23 +0,0 @@ -# success -run_test("use-tidy-description", "DESCRIPTION", suffix = "") - - - -if (!on_cran()) { - # in sub directory with correct root - run_test("use-tidy-description", - "DESCRIPTION", - suffix = "", - cmd_args = "--root=rpkg", - artifacts = c("rpkg/DESCRIPTION" = test_path("in/DESCRIPTION")) - ) - # in sub directory with incorrect root - # Need to generate the directoy `rpkg` but without DESCRIPTION file. - run_test("use-tidy-description", - "DESCRIPTION", - suffix = "", - cmd_args = "--root=rpkg", - std_err = "No `DESCRIPTION` found in repository.", - artifacts = c("rpkg/README.md" = test_path("in/README.md")) - ) -} diff --git a/tests/testthat/test-impl-hook-roxygenize.R b/tests/testthat/test-impl-hook-roxygenize.R deleted file mode 100644 index eb79361e3..000000000 --- a/tests/testthat/test-impl-hook-roxygenize.R +++ /dev/null @@ -1,169 +0,0 @@ -test_that("relevant diffs can be detected", { - skip_on_cran() - withr::with_tempdir({ - fs::dir_create("R") - # when new lines are added - text <- c("#' Roxygen comment", "#'", "#' more things", "NULL") - writeLines(text, "R/first.R") - writeLines(text, "R/second.R") - git_init(".") - expect_false(diff_requires_run_roxygenize(".")) - git2r::add(".", "R/first.R") - git2r::status() - - expect_true(diff_requires_run_roxygenize(".")) - git2r::commit(".", "add roxgen2 file") - - # when non roxygen lines are added - text2 <- c("if (x) {", " TRUE", "} else {", " not_TRue(sinux(-i))", "}") - writeLines(text2, "R/third.R") - - git2r::add(".", "R/third.R") - expect_false(diff_requires_run_roxygenize(".")) - git2r::commit(".", "add non-roxygen2 file") - - # when roxygen line is replaced - text[1] <- "# not roxygen, but replaced old " - writeLines(text, "R/first.R") - writeLines(text[1], "R/fourth.R") - - git2r::add(".", c("R/first.R", "R/fourth.R")) - git2r::status() - expect_true(diff_requires_run_roxygenize(".")) - git2r::commit(".", "replaced") - - - # when roxygen line is removed - writeLines("#", "R/first.R") - - git2r::add(".", "R/first.R") - expect_true(diff_requires_run_roxygenize(".")) - git2r::commit(".", "when reomved") - }) -}) - -test_that("change in formals alone triggers invalidation", { - skip_on_cran() - # when the function formals change but nothing else - withr::with_tempdir({ - fs::dir_create("R") - git_init(".") - # when new lines are added - text <- c("#' Roxygen comment", "#'", "#' more things", "x <- function(a = 2) {", " a", "}") - writeLines(text, "R/fifth.R") - expect_false(diff_requires_run_roxygenize(".")) - git2r::add(".", "R/fifth.R") - - expect_true(diff_requires_run_roxygenize(".")) - git2r::commit(".", "add file 5") - # change signature - text <- c("#' Roxygen comment", "#'", "#' more things", "x <- function(a = 3) {", " a", "}") - writeLines(text, "R/fifth.R") - git2r::add(".", "R/fifth.R") - git2r::commit(".", "clear case 5") - }) -}) - - -test_that("asserting installed dependencies", { - local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) - installed <- c("pkgload", "rlang", "testthat") - purrr::walk(installed, usethis::use_package) - writeLines(c("utils::adist", "rlang::is_installed"), "R/blur.R") - testthat::expect_silent(roxygen_assert_additional_dependencies()) - writeLines(generate_uninstalled_pkg_call(), "R/core.R") - testthat::expect_error( - roxygen_assert_additional_dependencies(), - "there is no package called" - ) -}) - -test_that("roxygenize works in general", { - skip_on_cran() - local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) - writeLines(c("#' This is a title", "#'", "#' More", "#' @name test", "NULL"), "R/blur.R") - # works - local_mocked_bindings(diff_requires_run_roxygenize = function(...) TRUE) - expect_message( - roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), - "test\\.Rd" - ) -}) - - -test_that("fails when package is called but not installed in roclets", { - skip_on_cran() - local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) - writeLines(c("NULL"), "R/blur.R") - # works - local_mocked_bindings(diff_requires_run_roxygenize = function(...) TRUE) - # when there is a missing package - roxygen_field <- paste0( - 'list(markdown = TRUE, roclets = c("rd", "namespace", "collate", "', - generate_uninstalled_pkg_call(), '"))' - ) - desc::desc_set(Roxygen = roxygen_field) - expect_error( - roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), - "Please add the package as a dependency" - ) -}) - - -test_that("fails gratefully when not installed package is called (packageNotFoundError)", { - skip_on_cran() - local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) - writeLines(generate_uninstalled_pkg_call(), "R/blur.R") - # works - local_mocked_bindings(diff_requires_run_roxygenize = function(...) TRUE) - # when there is a missing package - expect_error( - roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), - "there is no package called" - ) -}) - -test_that("fails gratefully when not installed package is required according to `DESCRIPTION`", { - skip_on_cran() - local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) - desc::desc_set_deps( - tibble::tibble(type = "Imports", package = generate_uninstalled_pkg_name(), version = "*") - ) - local_mocked_bindings(diff_requires_run_roxygenize = function(...) TRUE) - expect_error( - suppressWarnings( - roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")) - ), - "The package.*required" - ) -}) - - -test_that("fails when there is invalid code", { - skip_on_cran() - local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) - local_mocked_bindings(diff_requires_run_roxygenize = function(...) TRUE) - # when there is a missing package - writeLines(c("invalid code stuff /3kj"), "R/more.R") - expect_error( - roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), - "[Uu]nexpected symbol" - ) -}) - -test_that("warns if there is any other warning", { - skip_on_cran() - local_test_setup(git = FALSE, use_precommit = FALSE, package = TRUE) - local_mocked_bindings(diff_requires_run_roxygenize = function(...) TRUE) - writeLines( - c( - "#' This is a title", "#'", "#' More", "#", "NULL" - ), - "R/blur.R" - ) - - expect_message( - roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")), - "(with|a) @name" - ) -}) diff --git a/tests/testthat/test-impl-hooks-regex-exclude.R b/tests/testthat/test-impl-hooks-regex-exclude.R deleted file mode 100644 index 3d8e0bc2b..000000000 --- a/tests/testthat/test-impl-hooks-regex-exclude.R +++ /dev/null @@ -1,70 +0,0 @@ -test_that("exclude regex for spell check hook matches expected files", { - skip_if(not_conda()) - skip_if(on_cran()) - - # declare - re <- reticulate::import("re") - - pattern_read <- function(file) { - readLines(file) %>% - gsub("^ *exclude *: *>", " exclude: |", .) %>% - yaml::yaml.load() %>% - purrr::keep(~ .x$id == "spell-check") %>% - magrittr::extract2(1) %>% - magrittr::extract2("exclude") - } - - is_match <- function(pattern, x) { - matches <- purrr::map_lgl( - x, - ~ !is.null(re$match(pattern, .x, flags = re$VERBOSE)) - ) - if (any(!matches)) { - rlang::abort(paste( - "The following expressions don't match the pattern: ", - paste0(x[!matches], collapse = ", "), ". Did you forget a trailing |?" - )) - } - } - - files_to_match <- c( - "data/x", - ".Rprofile", - ".Renviron", - "renv.lock", - "renv/settings.dcf", - "vignettes/.gitignore", - "NAMESPACE", - "inst/WORDLIST", - ".travis.yml", - "appveyor.yml", - "file.RData", - ".Rbuildignore", - "analysis.R", - "linked/yids/gg.r", - "things.py", - "data/data.feather", - "more/data.rds", - "things/xx.Rds", - ".pre-commit-", - ".Rproj", - "some/X.pdf", - "figure.png", - "pciture.jpeg", - ".github/workflows/r-cmd-check.yaml" - ) - - # run - pattern <- pattern_read(system.file("pre-commit-hooks.yaml", package = "precommit")) - - individual_patterns <- setdiff( - unlist(strsplit(pattern, "\n", fixed = TRUE)), - c("(?x)^(", ")$") - ) - - expect_equal(length(individual_patterns), length(files_to_match)) - expect_silent(all(is_match( - pattern, - files_to_match - ))) -}) diff --git a/tests/testthat/test-setup.R b/tests/testthat/test-setup.R deleted file mode 100644 index a2519755e..000000000 --- a/tests/testthat/test-setup.R +++ /dev/null @@ -1,84 +0,0 @@ -test_that("snippet generation works", { - local_test_setup( - git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE - ) - usethis::use_package("R", "Depends", "3.6.0") - expect_error( - out <- capture_output(snippet_generate("additional-deps-roxygenize")), - NA, - ) - expect_equal(out, "") - usethis::use_package("styler") - expect_error( - out <- capture_output(snippet_generate("additional-deps-roxygenize")), - NA, - ) - - expect_match( - out, " - id: roxygenize\n.* - styler\n$", - ) - desc::desc_set("Remotes", "r-lib/styler") - expect_warning( - out <- capture_output(snippet_generate("additional-deps-roxygenize")), - "you have remote dependencies " - ) - expect_match( - out, " - id: roxygenize\n.* - styler\n$", - ) -}) - -test_that("snippet generation only includes hard dependencies", { - local_test_setup( - git = FALSE, use_precommit = FALSE, package = TRUE, - install_hooks = FALSE, open = FALSE - ) - usethis::use_package("styler") - usethis::use_package("lintr", type = "Suggest") - expect_warning( - out <- capture_output(snippet_generate("additional-deps-roxygenize")), - NA - ) - expect_match( - out, " - id: roxygenize\n.* - styler\n$", - ) -}) - - -test_that("GitHub Action CI setup works", { - local_test_setup( - git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE - ) - use_precommit_config( - root = getwd(), - open = FALSE, verbose = FALSE - ) - expect_error(use_ci("stuff", root = getwd()), "must be one of") - use_ci("gha", root = getwd()) - expect_true(file_exists(".github/workflows/pre-commit.yaml")) -}) - -test_that("Pre-commit CI GitHub Action template is parsable", { - expect_error( - yaml::read_yaml(system.file("pre-commit-gha.yaml", package = "precommit")), - NA - ) -}) - -test_that("Pre-commit CI setup works", { - local_test_setup( - git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE - ) - use_precommit_config( - root = getwd(), - open = FALSE, verbose = FALSE - ) - use_ci(root = getwd(), open = FALSE) - expect_false(file_exists(".github/workflows/pre-commit.yaml")) -}) - -test_that("Pre-commit CI setup works", { - local_test_setup( - git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE - ) - expect_error(use_ci(root = getwd(), open = FALSE), "o `.pre-commit-config.yaml`") -}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R deleted file mode 100644 index 5e45f576f..000000000 --- a/tests/testthat/test-utils.R +++ /dev/null @@ -1,70 +0,0 @@ -test_that("call capturing for pass", { - expect_silent(captured <- call_and_capture("echo", "1")) - expect_true( - captured$exit_status == 0 - ) - expect_true( - length(captured$stdout) == 1 - ) - - expect_true( - length(captured$stderr) == 0 - ) - expect_warning( - communicate_captured_call(captured), - "stdout" - ) -}) - -test_that("git repo status can be evaluated", { - tmpdir <- local_test_setup(git = FALSE) - expect_false(is_git_repo(root = tmpdir)) - git_init(tmpdir) - expect_true(is_git_repo(root = tmpdir)) -}) - -test_that("call capturing for error (command that does not exist)", { - expect_silent(captured <- call_and_capture("j23lkjsdi", "1")) - expect_true( - captured$exit_status != 0 - ) - expect_true( - length(captured$stdout) == 0 - ) - - expect_true( - length(captured$stderr) == 1 - ) - if (is_windows()) { - expected <- "Could not recover stderr." - } else { - expected <- "not found" - } - expect_error( - communicate_captured_call(captured), - expected - ) -}) - -test_that("call capturing for error fo command that exists (but arguments that don't)", { - expect_silent(captured <- call_and_capture("ls", "djdfjdoidj")) - expect_true( - captured$exit_status != 0 - ) - expect_true( - length(captured$stdout) == 0 - ) - - expect_true( - length(captured$stderr) > 0 - ) - expect_error( - communicate_captured_call(captured), - "No such file or " - ) -}) - -test_that("inputs meet requirements", { - expect_error(captured <- call_and_capture("echo", 1), "character vector.") - expect_error(captured <- call_and_capture("echo", list("x")), "character vector.") -}) diff --git a/tests/testthat/test-zzz.R b/tests/testthat/test-zzz.R deleted file mode 100644 index f1482718d..000000000 --- a/tests/testthat/test-zzz.R +++ /dev/null @@ -1,6 +0,0 @@ -test_that("tests don't write to styler-perm", { - skip_on_cran() - expect_false(file_exists( - fs::path(R.cache::getCacheRootPath(), "styler-perm") - )) -})