Skip to content

Commit cd4442c

Browse files
committed
Teach test_active_file() about snapshot files
1 parent 1bb9570 commit cd4442c

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

R/active.R

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

3838
test_file_type <- function(path) {
3939
dir <- path_file(path_dir(path))
40+
parent_dir <- path_file(path_dir(path_dir(path)))
4041
name <- path_file(path)
4142
ext <- tolower(path_ext(path))
4243

@@ -46,6 +47,8 @@ test_file_type <- function(path) {
4647
type[dir == "R" & ext == "r"] <- "R"
4748
type[dir == "testthat" & ext == "r" & grepl("^test", name)] <- "test"
4849
type[dir == "src" & ext %in% src_ext] <- "src"
50+
type[dir == "_snaps" & ext == "md"] <- "snap"
51+
type[parent_dir == "_snaps" & ext == "md"] <- "snap"
4952
type
5053
}
5154

tests/testthat/test-active.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ 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+
dir_create("tests/testthat/_snaps")
19+
file_create("tests/testthat/test-foo.R")
20+
file_create("tests/testthat/_snaps/foo.md")
21+
22+
path <- find_test_file("tests/testthat/_snaps/foo.md")
23+
expect_equal(path_file(path), "test-foo.R")
24+
})
25+
26+
test_that("find_test_file() works with snapshot variant files", {
27+
dir <- local_package_create()
28+
29+
withr::local_dir(dir)
30+
dir_create("tests/testthat/_snaps/variant")
31+
file_create("tests/testthat/test-foo.R")
32+
file_create("tests/testthat/_snaps/variant/foo.md")
33+
34+
path <- find_test_file("tests/testthat/_snaps/variant/foo.md")
35+
expect_equal(path_file(path), "test-foo.R")
36+
})
37+
1538
test_that("can determine file type", {
1639
expect_equal(test_file_type("R/foo.R"), "R")
1740
expect_equal(test_file_type("R/foo.c"), NA_character_)
@@ -23,5 +46,9 @@ test_that("can determine file type", {
2346
expect_equal(test_file_type("tests/testthat/test-foo.c"), NA_character_)
2447
expect_equal(test_file_type("tests/testthat/foo.R"), NA_character_)
2548

49+
expect_equal(test_file_type("tests/testthat/_snaps/foo.md"), "snap")
50+
expect_equal(test_file_type("tests/testthat/_snaps/variant/foo.md"), "snap")
51+
expect_equal(test_file_type("tests/testthat/_snaps/foo.R"), NA_character_)
52+
2653
expect_equal(test_file_type("DESCRIPTION"), NA_character_)
2754
})

0 commit comments

Comments
 (0)