Skip to content

Commit

Permalink
Merge 6169e5d into 88c5606
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored Jun 5, 2023
2 parents 88c5606 + 6169e5d commit 9920bcf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Imports:
testthat
Suggests:
callr,
here,
pkgload,
rstudioapi,
usethis,
Expand Down
13 changes: 5 additions & 8 deletions R/lazytest_local.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ lazytest_local <- function(path = ".",
filter = NULL,
load_package = "source") {

if (!identical(path, ".")) {
cli::cli_abort('{.code lazytest_local()} currently only works with {.code path = "."}.') # nolint
}

if (!is.null(filter)) {
cli::cli_abort("{.code lazytest_local()} requires {.code filter = NULL}, use {.code testthat::test_local()} to pass a {.code filter} argument.") # nolint
}

CONTEXT_FILE_NAME <- ".lazytest"
CONTEXT_FILE_PATH <- file.path(path, CONTEXT_FILE_NAME)

has_context <- fs::file_exists(CONTEXT_FILE_NAME)
has_context <- fs::file_exists(CONTEXT_FILE_PATH)
if (has_context) {
contexts <- brio::read_lines(CONTEXT_FILE_NAME)
fs::file_delete(CONTEXT_FILE_NAME)
contexts <- brio::read_lines(CONTEXT_FILE_PATH)
fs::file_delete(CONTEXT_FILE_PATH)
}

if (!has_context || lazytest_reset) {
Expand Down Expand Up @@ -80,7 +77,7 @@ lazytest_local <- function(path = ".",
failed_contexts <- context_name(failed_files)

if (length(failed_contexts) > 0) {
brio::write_lines(failed_contexts, CONTEXT_FILE_NAME)
brio::write_lines(failed_contexts, CONTEXT_FILE_PATH)
if (identical(failed_contexts, contexts)) {
cli::cli_inform(c(
">" = "Repeating the same tests next time."
Expand Down
30 changes: 15 additions & 15 deletions tests/testthat/helper-callr-tests.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# convoluted way to run the tests
# without their results being reported for the calling test file
run_lazytest <- function(pkg_dir, lazytest_dir) {
withr::with_dir(pkg_dir, {
process <- callr::r_bg(
function(lazytest_dir) {
process <- callr::r_bg(
function(pkg_dir, lazytest_dir) {
# use library for covr and R CMD check
if (isTRUE(as.logical(Sys.getenv("R_COVR", "false"))) || grepl("Rcheck", lazytest_dir)) {
library("lazytest")
} else {
pkgload::load_all(lazytest_dir)
testthat_results <- lazytest::lazytest_local(stop_on_failure = FALSE)
return(testthat_results)
},
args = list(lazytest_dir = lazytest_dir),
stderr = file.path(pkg_dir, "lazytest-msg") # catching lazytest messages
)
process$wait()
})
}

lazytest_dir <- function() {
here::here()
}
testthat_results <- lazytest_local(pkg_dir, stop_on_failure = FALSE)
return(testthat_results)
},
args = list(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir),
stderr = file.path(pkg_dir, "lazytest-msg") # catching lazytest messages
)
process$wait()
}

lazytest_dir <- find.package("lazytest")
executed_test_files <- function(callr_output) {
as.data.frame.testthat_results(callr_output$get_result())[,1]
}
10 changes: 5 additions & 5 deletions tests/testthat/test-lazytest_local.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that("lazytest_local() works", {
edit_test("blip", failing_test_lines(), pkg_dir = pkg_dir)

# run tests, including one failing test, twice ----
first_run <- run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir())
first_run <- run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir)

expect_true(file.exists(file.path(pkg_dir, ".lazytest")))
# expect_snapshot_file didn't record anything?!
Expand All @@ -27,29 +27,29 @@ test_that("lazytest_local() works", {
c("test-blip.R", "test-blop.R")
)

second_run <- run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir())
second_run <- run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir)
expect_equal(
executed_test_files(second_run),
c("test-blip.R")
)

# fix failing test, run tests twice ----
edit_test("blip", passing_test_lines(), pkg_dir = pkg_dir)
new_run <- run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir())
new_run <- run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir)
# When all failing tests have succeeded, the entire test suite is rerun.
expect_equal(
executed_test_files(new_run),
c("test-blip.R", "test-blop.R")
)
expect_false(file.exists(file.path(pkg_dir, ".lazytest")))

run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir())
run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir)
expect_match(
toString(brio::read_lines(file.path(pkg_dir, "lazytest-msg"))),
"Testing all tests next time"
)

last_run <- run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir())
last_run <- run_lazytest(pkg_dir = pkg_dir, lazytest_dir = lazytest_dir)
expect_false(file.exists(file.path(pkg_dir, ".lazytest")))
expect_equal(
executed_test_files(last_run),
Expand Down

0 comments on commit 9920bcf

Please sign in to comment.