Skip to content

Commit

Permalink
Restrict the number of file names provided in use_r() and `use_test…
Browse files Browse the repository at this point in the history
…()` (#862)

For for number of files in check_file_name(). Fixes #861.
  • Loading branch information
Metin Yazici authored and hadley committed Oct 16, 2019
1 parent 4538dd3 commit 3996db9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 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_r()` and `use_test()` would throw an error if multiple names are provided in their 'name' argument (#862, @strboul).

* `edit_rstudio_snippets()` makes it more clear which snippet types are allowed and that user's snippets mask the built-in snippets (#885, @GegznaV).

* `use_test()` should work again on Windows, when called with no argument, i.e. when the active `.R` file is determined from RStudio's source editor context (#901).
Expand Down
3 changes: 3 additions & 0 deletions R/r.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use_r <- function(name = NULL) {
}

check_file_name <- function(name) {
if (!is_string(name)) {
ui_stop("Name must be a single string")
}
if (!valid_file_name(path_ext_remove(name))) {
ui_stop(c(
"{ui_value(name)} is not a valid file name. It should:",
Expand Down
9 changes: 3 additions & 6 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ use_test <- function(name = NULL, open = interactive()) {
use_testthat()
}

if (is.null(name)) {
name <- get_active_r_file(path = "R")
} else {
check_file_name(name)
}

name <- name %||% get_active_r_file(path = "R")
name <- paste0("test-", name)
name <- slug(name, "R")
check_file_name(name)

path <- path("tests", "testthat", name)

if (file_exists(proj_path(path))) {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-use-r.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ test_that("use_r() creates a .R file below R/", {
use_r("foo")
expect_proj_file("R/foo.R")
})

test_that("check_file_name() requires single vector", {
expect_usethis_error(check_file_name(c("a", "b")), "single string")
})

0 comments on commit 3996db9

Please sign in to comment.