Skip to content
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
7 changes: 2 additions & 5 deletions R/parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test_files_parallel <- function(

test_files_reporter_parallel <- function(reporter, .env = parent.frame()) {
lister <- ListReporter$new()
snapshotter <- MainprocessSnapshotReporter$new("_snaps", fail_on_new = FALSE)
snapshotter <- MainprocessSnapshotReporter$new("_snaps")
reporters <- list(
find_reporter(reporter),
lister, # track data
Expand Down Expand Up @@ -301,10 +301,7 @@ queue_process_setup <- function(

queue_task <- function(path) {
withr::local_envvar("TESTTHAT_IS_PARALLEL" = "true")
snapshotter <- SubprocessSnapshotReporter$new(
snap_dir = "_snaps",
fail_on_new = FALSE
)
snapshotter <- SubprocessSnapshotReporter$new(snap_dir = "_snaps")
withr::local_options(testthat.snapshotter = snapshotter)
reporters <- list(
SubprocessReporter$new(),
Expand Down
14 changes: 10 additions & 4 deletions R/snapshot-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ expect_snapshot_file <- function(
variant = variant,
trace_env = caller_env()
)
if (inherits(equal, "expectation_failure")) {
return(equal)
}

hint <- snapshot_review_hint(snapshotter$file, name)

if (!equal) {
Expand Down Expand Up @@ -196,7 +200,7 @@ snapshot_file_equal <- function(
snap_variant, # variant (optional)
path, # path to new file
file_equal = compare_file_binary,
fail_on_new = FALSE,
fail_on_new = NULL,
trace_env = caller_env()
) {
if (!file.exists(path)) {
Expand All @@ -208,6 +212,7 @@ snapshot_file_equal <- function(
} else {
snap_test_dir <- file.path(snap_dir, snap_variant, snap_test)
}
fail_on_new <- fail_on_new %||% on_ci()

cur_path <- file.path(snap_test_dir, snap_name)
new_path <- file.path(snap_test_dir, new_name(snap_name))
Expand All @@ -232,12 +237,13 @@ snapshot_file_equal <- function(
snap_name,
"'"
)

# We want to fail on CI since this suggests that the user has failed
# to record the value locally
if (fail_on_new) {
return(fail(message, trace_env = trace_env))
} else {
testthat_warn(message)
}

testthat_warn(message)
TRUE
}
}
Expand Down
13 changes: 6 additions & 7 deletions R/snapshot-reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ SnapshotReporter <- R6::R6Class(
test_file_seen = character(),
snap_file_seen = character(),
variants_changed = FALSE,
fail_on_new = FALSE,
fail_on_new = NULL,

old_snaps = NULL,
cur_snaps = NULL,
new_snaps = NULL,

initialize = function(snap_dir = "_snaps", fail_on_new = FALSE) {
initialize = function(snap_dir = "_snaps", fail_on_new = NULL) {
self$snap_dir <- normalizePath(snap_dir, mustWork = FALSE)
self$fail_on_new <- fail_on_new
},
Expand Down Expand Up @@ -82,19 +82,18 @@ SnapshotReporter <- R6::R6Class(
value_enc <- save(value)

self$cur_snaps$append(self$test, variant, value_enc)
fail_on_new <- self$fail_on_new %||% on_ci()

message <- paste0(
"Adding new snapshot",
if (variant != "_default") paste0(" for variant '", variant, "'"),
if (self$fail_on_new) " in CI",
":\n",
value_enc
)
if (self$fail_on_new) {
if (fail_on_new) {
return(fail(message, trace_env = trace_env))
} else {
testthat_warn(message)
}
testthat_warn(message)
character()
}
},
Expand Down Expand Up @@ -196,7 +195,7 @@ get_snapshotter <- function() {
local_snapshotter <- function(
snap_dir = NULL,
cleanup = FALSE,
fail_on_new = FALSE,
fail_on_new = NULL,
.env = parent.frame()
) {
snap_dir <- snap_dir %||% withr::local_tempdir(.local_envir = .env)
Expand Down
3 changes: 3 additions & 0 deletions R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ expect_snapshot_helper <- function(
variant = variant,
trace_env = trace_env
)
if (inherits(comp, "expectation_failure")) {
return(comp)
}

if (!identical(variant, "_default")) {
variant_lab <- paste0(" (variant '", variant, "')")
Expand Down
14 changes: 11 additions & 3 deletions R/test-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ test_files_reporter <- function(reporter, .env = parent.frame()) {
reporters <- list(
find_reporter(reporter),
lister, # track data
local_snapshotter("_snaps", fail_on_new = on_ci(), .env = .env)
local_snapshotter("_snaps", .env = .env)
)
list(
multi = MultiReporter$new(reporters = compact(reporters)),
Expand All @@ -325,10 +325,18 @@ test_files_check <- function(
stop_on_warning = FALSE
) {
if (stop_on_failure && !all_passed(results)) {
cli::cli_abort("Test failures.")
cli::cli_abort(
"Test failures.",
call = NULL,
trace = data.frame()
)
}
if (stop_on_warning && any_warnings(results)) {
cli::cli_abort("Tests generated warnings.")
cli::cli_abort(
"Tests generated warnings.",
call = NULL,
trace = data.frame()
)
}

invisible(results)
Expand Down
2 changes: 1 addition & 1 deletion man/local_snapshotter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/_snaps/R4.6/snapshot-file/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
R4.6
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/R4.6/snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# variants save different values

Code
r_version()
Output
[1] "R4.6"

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/test-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Code
test_dir(test_path("test_dir"), reporter = "silent")
Condition
Error in `test_files_check()`:
Error:
! Test failures.

# runs all tests and records output
Expand Down Expand Up @@ -40,15 +40,15 @@
Code
test_error(stop_on_failure = TRUE)
Condition
Error in `test_files_check()`:
Error:
! Test failures.

# can control if warnings errors

Code
test_warning(stop_on_warning = TRUE)
Condition
Error in `test_files_check()`:
Error:
! Tests generated warnings.

# complains if file doesn't exist
Expand Down
13 changes: 6 additions & 7 deletions tests/testthat/test-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ test_that("fail", {

test_that("snapshots", {
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
withr::defer(unlink(tmp, recursive = TRUE))
dir.create(tmp <- tempfile("testthat-snap-"))
tmp <- withr::local_tempdir("testthat-snap-")
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
# we cannot run these with the silent reporter, because it is not
# parallel compatible, and they'll not run in parallel
Expand All @@ -82,11 +81,11 @@ test_that("snapshots", {
})

test_that("new snapshots are added", {
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
withr::defer(unlink(tmp, recursive = TRUE))
dir.create(tmp <- tempfile("testthat-snap-"))
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE", CI = "false"))
tmp <- withr::local_tempdir("testthat-snap-")
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
unlink(file.path(tmp, "snap", "tests", "testthat", "_snaps", "snap-2.md"))

# we cannot run these with the silent reporter, because it is not
# parallel compatible, and they'll not run in parallel
capture.output(suppressMessages(
Expand All @@ -107,13 +106,13 @@ test_that("new snapshots are added", {

test_that("snapshots are removed if test file has no snapshots", {
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
withr::defer(unlink(tmp, recursive = TRUE))
dir.create(tmp <- tempfile("testthat-snap-"))
tmp <- withr::local_tempdir("testthat-snap-")
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
writeLines(
"test_that(\"2\", { expect_true(TRUE) })",
file.path(tmp, "snap", "tests", "testthat", "test-snap-2.R")
)

# we cannot run these with the silent reporter, because it is not
# parallel compatible, and they'll not run in parallel
capture.output(suppressMessages(
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-snapshot-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ test_that("warns on first creation", {
snap_test = "my-test",
snap_name = "test.txt",
snap_variant = NULL,
path = path
path = path,
fail_on_new = FALSE
)
}

Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-snapshot-reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ test_that("basic workflow", {
expect_true(file.exists(file.path(path, "snapshot-2.new.md")))
})

test_that("defaults to failing on CI", {
withr::local_envvar(CI = "true")

path <- withr::local_tempdir()
snapper <- local_snapshotter(path)
snapper$start_file("snapshot-2")
# warns on first creation
snapper$start_file("snapshot-2", "test")
expect_error(expect_snapshot_output("x"), "Adding new")
})

test_that("only create new files for changed variants", {
snapper <- local_snapshotter(fail_on_new = FALSE)
snapper$start_file("variants", "test")
Expand Down
Loading