Skip to content

Commit

Permalink
tweak compute_name() to recognize test helper and snapshot files
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed May 14, 2024
1 parent da18e11 commit e373436
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions R/r.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-r.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit e373436

Please sign in to comment.