From 3a5e2ba9ce2d051ef2b8c4f1019be9a7f0269ba9 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 31 Oct 2023 12:52:04 -0500 Subject: [PATCH] Speed up tests (#1908) * Enable parallel testing. For me, this dropped runn time down from 30s to 11s, 10s of which is the release tests. * Refactor release tests to avoid expensive CRAN downloads. This has equivalent test coverage but reduces test time from 10s to 4s, bringing total test time down to around 8 seconds. * Start the slowest tests (github-actions and release) first. This brings total test time down to around 6s --- DESCRIPTION | 2 ++ NAMESPACE | 1 + R/release.R | 2 +- R/usethis-package.R | 1 + tests/testthat/test-release.R | 64 +++++++++++------------------------ 5 files changed, 25 insertions(+), 45 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 42ee5a122..7b5ac9a67 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -55,6 +55,8 @@ Suggests: testthat (>= 3.1.8) Config/Needs/website: tidyverse/tidytemplate, xml2 Config/testthat/edition: 3 +Config/testthat/parallel: TRUE +Config/testthat/start-first: github-actions, release Encoding: UTF-8 Language: en-US Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index 14daa906d..b6bcb9a93 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -221,3 +221,4 @@ importFrom(purrr,map) importFrom(purrr,map_chr) importFrom(purrr,map_int) importFrom(purrr,map_lgl) +importFrom(utils,available.packages) diff --git a/R/release.R b/R/release.R index 7bff3ed56..61302d6de 100644 --- a/R/release.R +++ b/R/release.R @@ -412,7 +412,7 @@ cran_version <- function(package = project_name(), available = NULL) { if (is.null(available)) { # Guard against CRAN mirror being unset available <- tryCatch( - utils::available.packages(repos = default_cran_mirror()), + available.packages(repos = default_cran_mirror()), error = function(e) NULL ) if (is.null(available)) { diff --git a/R/usethis-package.R b/R/usethis-package.R index 082017ade..fd1b8bb99 100644 --- a/R/usethis-package.R +++ b/R/usethis-package.R @@ -7,6 +7,7 @@ #' @importFrom glue glue glue_collapse glue_data #' @importFrom lifecycle deprecated #' @importFrom purrr map map_chr map_lgl map_int +#' @importFrom utils available.packages ## usethis namespace: end NULL diff --git a/tests/testthat/test-release.R b/tests/testthat/test-release.R index dabdddba3..d9644ada1 100644 --- a/tests/testthat/test-release.R +++ b/tests/testthat/test-release.R @@ -29,6 +29,8 @@ test_that("non-patch + lifecycle = advanced deprecation process", { create_local_package() use_package("lifecycle") + local_mocked_bindings(tidy_minimum_r_version = function() "3.6") + has_deprecation <- function(x) any(grepl("deprecation processes", x)) expect_true(has_deprecation(release_checklist("1.0.0", on_cran = TRUE))) expect_true(has_deprecation(release_checklist("1.1.0", on_cran = TRUE))) @@ -200,54 +202,28 @@ test_that("get_release_data() works for new-style CRAN-RELEASE", { expect_equal(path_file(res$file), "CRAN-SUBMISSION") }) -test_that("cran_version() is robust to unset CRAN mirror with unreleased pkg (#1857)", { - skip_if_offline() - # we don't want this to be the name of a package someone actually releases on CRAN - pkg <- "foofy.asdf123" - - withr::with_options( - list(repos = c(CRAN = "https://cloud.r-project.org")), - expect_null(cran_version(pkg)) - ) - - withr::with_options( - list(repos = c(CRAN = "@CRAN@")), - expect_null(cran_version(pkg)) - ) - - withr::with_options( - list(repos = c(CRAN = NA)), - expect_null(cran_version(pkg)) - ) +test_that("cran_version() returns package version if package found", { + local_mocked_bindings(available.packages = function(...) { + # simulate minimal available.packages entry + as.matrix(data.frame(Package = c(usethis = "usethis"), Version = "1.0.0")) + }) - withr::with_options( - list(repos = NULL), - expect_null(cran_version(pkg)) - ) + expect_null(cran_version("doesntexist")) + expect_equal(cran_version("usethis"), package_version("1.0.0")) }) -test_that("cran_version() is robust to unset CRAN mirror with released pkg (#1857)", { - skip_if_offline() - pkg <- "usethis" - - withr::with_options( - list(repos = c(CRAN = "https://cloud.r-project.org")), - expect_s3_class(cran_version(pkg), "package_version") - ) +test_that("cran_version() returns NULL if no available packages", { + local_mocked_bindings(available.packages = function(...) NULL) + expect_null(cran_version("doesntexist")) +}) - withr::with_options( - list(repos = c(CRAN = "@CRAN@")), - expect_s3_class(cran_version(pkg), "package_version") - ) +test_that("default_cran_mirror() is respects set value but falls back to cloud", { + withr::local_options(repos = c(CRAN = "https://example.com")) + expect_equal(default_cran_mirror(), c(CRAN = "https://example.com")) - withr::with_options( - list(repos = c(CRAN = NA)), - expect_s3_class(cran_version(pkg), "package_version") - ) + withr::local_options(repos = c(CRAN = "@CRAN@")) + expect_equal(default_cran_mirror(), c(CRAN = "https://cloud.r-project.org")) - withr::with_options( - list(repos = NULL), - expect_s3_class(cran_version(pkg), "package_version") - ) + withr::local_options(repos = c()) + expect_equal(default_cran_mirror(), c(CRAN = "https://cloud.r-project.org")) }) -