Skip to content

Commit f71ed22

Browse files
hadleyjennybc
andauthored
Teach test_active_file() about snapshot files (#2666)
* Teach `test_active_file()` about snapshot files * Add news bullet * Small tweaks --------- Co-authored-by: Jenny Bryan <jenny.f.bryan@gmail.com>
1 parent c017247 commit f71ed22

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* `load_all()` now errors if called recursively, i.e. if you accidentally include a `load_all()` call in one of your R source files (#2617).
1313
* `release()` is deprecated in favour of `usethis::use_release_issue()`.
1414
* `show_news()` now looks for NEWS files in the same locations as `utils::news()`: `inst/NEWS.Rd`, `NEWS.md`, `NEWS`, and `inst/NEWS` (@arcresu, #2499).
15+
* `test_active_file()` now works when the active file is a snapshot file.
1516
* `test_coverage()` and `test_coverage_active_file()` gain a new `report` argument that can be set to `"html"` (the default, for an interactive browser report), `"zero"` (prints uncovered lines to the console, used for LLMs and non-interactive contexts), or `"silent"`. The `show_report` argument has been removed (#2632).
1617
* `test_file()` and `test_coverage_file()` are now defunct. These were deprecated in devtools 2.4.0 (2021-04-07) in favour of `test_active_file()` and `test_coverage_active_file()`. Removing `test_file()` eliminates the conflict with `testthat::test_file()`.
1718

R/active.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ find_test_file <- function(path, call = parent.frame()) {
3737

3838
test_file_type <- function(path) {
3939
dir <- path_file(path_dir(path))
40+
# this accounts for snapshot files in a variant subfolder
41+
parent_dir <- path_file(path_dir(path_dir(path)))
4042
name <- path_file(path)
4143
ext <- tolower(path_ext(path))
4244

@@ -46,6 +48,8 @@ test_file_type <- function(path) {
4648
type[dir == "R" & ext == "r"] <- "R"
4749
type[dir == "testthat" & ext == "r" & grepl("^test", name)] <- "test"
4850
type[dir == "src" & ext %in% src_ext] <- "src"
51+
type[dir == "_snaps" & ext == "md"] <- "snap"
52+
type[parent_dir == "_snaps" & ext == "md"] <- "snap"
4953
type
5054
}
5155

tests/testthat/test-active.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ test_that("fails if can't find tests", {
1212
})
1313
})
1414

15+
test_that("find_test_file() works with snapshot files", {
16+
dir <- local_package_create()
17+
withr::local_dir(dir)
18+
19+
dir_create("tests/testthat/_snaps")
20+
file_create("tests/testthat/test-foo.R")
21+
file_create("tests/testthat/_snaps/foo.md")
22+
23+
path <- find_test_file("tests/testthat/_snaps/foo.md")
24+
expect_equal(path_file(path), "test-foo.R")
25+
})
26+
27+
test_that("find_test_file() works with snapshot variant files", {
28+
dir <- local_package_create()
29+
withr::local_dir(dir)
30+
31+
dir_create("tests/testthat/_snaps/variant")
32+
file_create("tests/testthat/test-foo.R")
33+
file_create("tests/testthat/_snaps/variant/foo.md")
34+
35+
path <- find_test_file("tests/testthat/_snaps/variant/foo.md")
36+
expect_equal(path_file(path), "test-foo.R")
37+
})
38+
1539
test_that("can determine file type", {
1640
expect_equal(test_file_type("R/foo.R"), "R")
1741
expect_equal(test_file_type("R/foo.c"), NA_character_)
@@ -23,5 +47,9 @@ test_that("can determine file type", {
2347
expect_equal(test_file_type("tests/testthat/test-foo.c"), NA_character_)
2448
expect_equal(test_file_type("tests/testthat/foo.R"), NA_character_)
2549

50+
expect_equal(test_file_type("tests/testthat/_snaps/foo.md"), "snap")
51+
expect_equal(test_file_type("tests/testthat/_snaps/variant/foo.md"), "snap")
52+
expect_equal(test_file_type("tests/testthat/_snaps/foo.R"), NA_character_)
53+
2654
expect_equal(test_file_type("DESCRIPTION"), NA_character_)
2755
})

0 commit comments

Comments
 (0)