From e373436a64ec597a56d50502c91c3d777691bd9e Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 14 May 2024 06:47:31 -0400 Subject: [PATCH] tweak `compute_name()` to recognize test helper and snapshot files --- NEWS.md | 2 ++ R/r.R | 4 ++-- tests/testthat/test-r.R | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index d7f3a70a0..d3969b47e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # usethis (development version) +* `use_test()` and `use_r()` now work when you are in `tests/testthat/helper-{foo}.R` or `tests/testthat/_snaps/{foo}.md` (@olivroy, #1988). + * The `ui_*()` functions have been marked as [superseded](https://lifecycle.r-lib.org/articles/stages.html#superseded). External users of these functions are encouraged to use the diff --git a/R/r.R b/R/r.R index 40b7ffa77..8964d39d2 100644 --- a/R/r.R +++ b/R/r.R @@ -116,13 +116,13 @@ compute_active_name <- function(path, ext, error_call = caller_env()) { path <- proj_path_prep(path_expand_r(path)) dir <- path_dir(proj_rel_path(path)) - if (!dir %in% c("R", "src", "tests/testthat")) { + if (!dir %in% c("R", "src", "tests/testthat", "tests/testthat/_snaps")) { cli::cli_abort("Open file must be a code or test file.", call = error_call) } file <- path_file(path) if (dir == "tests/testthat") { - file <- gsub("^test[-_]", "", file) + file <- gsub("^test[-_]|^helper[-_]", "", file) } as.character(path_ext_set(file, ext)) } diff --git a/tests/testthat/test-r.R b/tests/testthat/test-r.R index b24bbaca3..5c225654c 100644 --- a/tests/testthat/test-r.R +++ b/tests/testthat/test-r.R @@ -60,6 +60,14 @@ test_that("compute_active_name() standardises name", { "bar.R" ) + expect_equal( + compute_active_name(path(dir, "tests/testthat/helper-bar.R"), "R"), + "bar.R" + ) + expect_equal( + compute_active_name(path(dir, "tests/testthat/_snaps/bar.md"), "R"), + "bar.R" + ) # https://github.com/r-lib/usethis/issues/1690 expect_equal( compute_active_name(path(dir, "R/data.frame.R"), "R"),