diff --git a/NEWS.md b/NEWS.md index c40e985bb..ee32d2b83 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ * `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). * `release()` is deprecated in favour of `usethis::use_release_issue()`. * `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). +* `test_active_file()` now works when the active file is a snapshot file. # devtools 2.4.6 diff --git a/R/active.R b/R/active.R index 51e1e4b20..73f2598e2 100644 --- a/R/active.R +++ b/R/active.R @@ -37,6 +37,7 @@ find_test_file <- function(path, call = parent.frame()) { test_file_type <- function(path) { dir <- path_file(path_dir(path)) + parent_dir <- path_file(path_dir(path_dir(path))) name <- path_file(path) ext <- tolower(path_ext(path)) @@ -46,6 +47,8 @@ test_file_type <- function(path) { type[dir == "R" & ext == "r"] <- "R" type[dir == "testthat" & ext == "r" & grepl("^test", name)] <- "test" type[dir == "src" & ext %in% src_ext] <- "src" + type[dir == "_snaps" & ext == "md"] <- "snap" + type[parent_dir == "_snaps" & ext == "md"] <- "snap" type } diff --git a/tests/testthat/test-active.R b/tests/testthat/test-active.R index 5e4ca6ce8..96094677a 100644 --- a/tests/testthat/test-active.R +++ b/tests/testthat/test-active.R @@ -12,6 +12,29 @@ test_that("fails if can't find tests", { }) }) +test_that("find_test_file() works with snapshot files", { + dir <- local_package_create() + withr::local_dir(dir) + dir_create("tests/testthat/_snaps") + file_create("tests/testthat/test-foo.R") + file_create("tests/testthat/_snaps/foo.md") + + path <- find_test_file("tests/testthat/_snaps/foo.md") + expect_equal(path_file(path), "test-foo.R") +}) + +test_that("find_test_file() works with snapshot variant files", { + dir <- local_package_create() + + withr::local_dir(dir) + dir_create("tests/testthat/_snaps/variant") + file_create("tests/testthat/test-foo.R") + file_create("tests/testthat/_snaps/variant/foo.md") + + path <- find_test_file("tests/testthat/_snaps/variant/foo.md") + expect_equal(path_file(path), "test-foo.R") +}) + test_that("can determine file type", { expect_equal(test_file_type("R/foo.R"), "R") expect_equal(test_file_type("R/foo.c"), NA_character_) @@ -23,5 +46,9 @@ test_that("can determine file type", { expect_equal(test_file_type("tests/testthat/test-foo.c"), NA_character_) expect_equal(test_file_type("tests/testthat/foo.R"), NA_character_) + expect_equal(test_file_type("tests/testthat/_snaps/foo.md"), "snap") + expect_equal(test_file_type("tests/testthat/_snaps/variant/foo.md"), "snap") + expect_equal(test_file_type("tests/testthat/_snaps/foo.R"), NA_character_) + expect_equal(test_file_type("DESCRIPTION"), NA_character_) })