Skip to content

Commit 2459dbd

Browse files
committed
Include test path in snaphot hints
If you're not testing the current directory/package. Fixes #1577
1 parent 00b3cc1 commit 2459dbd

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

R/local.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,15 @@ local_test_directory <- function(path, package = NULL, .env = parent.frame()) {
177177
# Set edition before changing working directory in case path is relative
178178
local_edition(find_edition(path, package), .env = .env)
179179

180-
withr::local_dir(
181-
path,
182-
.local_envir = .env
183-
)
180+
# Capture current working directory so we can use for relative paths
181+
wd <- getwd()
182+
183+
withr::local_dir(path, .local_envir = .env)
184184
withr::local_envvar(
185185
R_TESTS = "",
186186
TESTTHAT = "true",
187187
TESTTHAT_PKG = package,
188+
TESTTHAT_WD = wd,
188189
.local_envir = .env
189190
)
190191
}

R/snapshot.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,23 @@ snapshot_accept_hint <- function(variant, file, reset_output = TRUE) {
364364
name <- file.path(variant, file)
365365
}
366366

367+
args <- encodeString(name, quote = '"')
368+
# Include path argument if we're in a different working directory
369+
wd <- Sys.getenv("TESTTHAT_WD", unset = "")
370+
if (wd != "") {
371+
test_path <- file.path(wd, "tests/testthat")
372+
if (test_path != getwd()) {
373+
args <- paste0(args, ", ", encodeString(getwd(), quote = '"'))
374+
}
375+
}
376+
367377
paste0(
368378
cli::format_inline(
369-
"* Run {.run testthat::snapshot_accept('{name}')} to accept the change."
379+
"* Run {.run testthat::snapshot_accept({args})} to accept the change."
370380
),
371381
"\n",
372382
cli::format_inline(
373-
"* Run {.run testthat::snapshot_review('{name}')} to interactively review the change."
383+
"* Run {.run testthat::snapshot_review({args})} to interactively review the change."
374384
)
375385
)
376386
}

tests/testthat/_snaps/snapshot.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@
258258
Code
259259
cat(snapshot_accept_hint("_default", "bar.R", reset_output = FALSE))
260260
Output
261-
* Run ]8;;ide:run:testthat::snapshot_accept('bar.R')testthat::snapshot_accept('bar.R')]8;; to accept the change.
262-
* Run ]8;;ide:run:testthat::snapshot_review('bar.R')testthat::snapshot_review('bar.R')]8;; to interactively review the change.
261+
* Run ]8;;ide:run:testthat::snapshot_accept("bar.R")testthat::snapshot_accept("bar.R")]8;; to accept the change.
262+
* Run ]8;;ide:run:testthat::snapshot_review("bar.R")testthat::snapshot_review("bar.R")]8;; to interactively review the change.
263263
Code
264264
cat(snapshot_accept_hint("foo", "bar.R", reset_output = FALSE))
265265
Output
266-
* Run ]8;;ide:run:testthat::snapshot_accept('foo/bar.R')testthat::snapshot_accept('foo/bar.R')]8;; to accept the change.
267-
* Run ]8;;ide:run:testthat::snapshot_review('foo/bar.R')testthat::snapshot_review('foo/bar.R')]8;; to interactively review the change.
266+
* Run ]8;;ide:run:testthat::snapshot_accept("foo/bar.R")testthat::snapshot_accept("foo/bar.R")]8;; to accept the change.
267+
* Run ]8;;ide:run:testthat::snapshot_review("foo/bar.R")testthat::snapshot_review("foo/bar.R")]8;; to interactively review the change.
268268

269269
# expect_snapshot validates its inputs
270270

tests/testthat/test-snapshot.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ test_that("hint is informative", {
174174
})
175175
})
176176

177+
test_that("hint includes path when WD is different", {
178+
withr::local_envvar(TESTTHAT_WD = "..")
179+
180+
hint <- snapshot_accept_hint("_default", "bar.R", reset_output = FALSE)
181+
hint <- gsub(getwd(), "<WD>", hint)
182+
# Can't use snapshot here because its hint will get the wrong path
183+
expect_match(hint, 'snapshot_accept("bar.R", "<WD>")', fixed = TRUE)
184+
})
185+
177186
test_that("expect_snapshot requires a non-empty test label", {
178187
local_description_set()
179188

0 commit comments

Comments
 (0)