Skip to content

Commit

Permalink
Speed up tests (#1908)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hadley authored Oct 31, 2023
1 parent eabae3c commit 3a5e2ba
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 45 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,4 @@ importFrom(purrr,map)
importFrom(purrr,map_chr)
importFrom(purrr,map_int)
importFrom(purrr,map_lgl)
importFrom(utils,available.packages)
2 changes: 1 addition & 1 deletion R/release.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions R/usethis-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 20 additions & 44 deletions tests/testthat/test-release.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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"))
})

0 comments on commit 3a5e2ba

Please sign in to comment.