Skip to content

Commit

Permalink
Preventively error when calling use_r(), use_test() if active fil…
Browse files Browse the repository at this point in the history
…e is `tests/testthat/helper.R`
  • Loading branch information
olivroy committed Apr 11, 2024
1 parent 1b28745 commit 31d68a2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion R/r.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,17 @@ compute_active_name <- function(path, ext, error_call = caller_env()) {

file <- path_file(path)
if (dir == "tests/testthat") {
if (file == "helper.R") {
cli::cli_abort(c(
"Can't call {.fn use_r} / {.fn use_test} when active file is \\
{.path {pth('tests/testthat/helper.R')}}",
i = "Supply {.arg name} or navigate to your file."
),
call = error_call
)
}
file <- gsub("^test[-_]", "", file)
file <- gsub("^helper[-_]", "", file)

}
as.character(path_ext_set(file, ext))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-r.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ test_that("can use use_test() and use_test_helper() in a project", {
create_local_project()
expect_no_error(use_test("foofy"))
expect_no_error(use_test_helper("foofy"))

})

# helpers -----------------------------------------------------------------
Expand Down Expand Up @@ -69,6 +68,7 @@ test_that("compute_active_name() standardises name", {
compute_active_name(path(dir, "tests/testthat/helper-bar.R"), "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 31d68a2

Please sign in to comment.