Skip to content

Commit

Permalink
Add test_file function
Browse files Browse the repository at this point in the history
This allows you to have tight iteration when editing a single R or test
file.

Fixes r-lib#1755
  • Loading branch information
jimhester authored and HughParsonage committed Jul 2, 2019
1 parent c97b38b commit d66711b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export(system_check)
export(system_output)
export(test)
export(test_coverage)
export(test_file)
export(uninstall)
export(unload)
export(update_packages)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# devtools 1.13.3.9000

* `test_file()` function added to test one or more files from a package
(#1755).

* `install_remote()` now uses the repo and type arguments from the remote
rather than using the argument passed to `...` (#1747).

Expand Down
50 changes: 48 additions & 2 deletions R/test.r
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#' Execute all \pkg{test_that} tests in a package.
#' Execute \pkg{test_that} tests in a package.
#'
#' `test()` is a shortcut for [testthat::test_dir()].
#' `test_coverage()` is a shortcut for [covr::package_coverage()].
#' `test_file` runs `test()` on one or more test files files.
#' `test_coverage()` is a shortcut for [covr::report()].
#'
#' @md
#' @param pkg package description, can be path or package name. See
#' [as.package()] for more information
#' @param ... additional arguments passed to [testthat::test_dir()] and
#' [covr::package_coverage()]
#' @param file One or more source or test files. If a source file the
#' corresponding test file will be run. The default is to use the active file
#' in RStudio (if available).
#' @inheritParams testthat::test_dir
#' @inheritParams run_examples
#' @export
Expand Down Expand Up @@ -128,3 +132,45 @@ uses_testthat <- function(pkg = ".") {

any(dir.exists(paths))
}

#' @export
#' @rdname test
test_file <- function(file = find_active_file(), ...) {
is_source_file <- basename(dirname(file)) == "R"

has_r_ext <- grepl("\\.[rR]$", file)
if (any(!has_r_ext)) {
stop("file(s): ",
paste0("'", file[!has_r_ext], "'", collapse = ", "),
" are not R files", call. = FALSE)
}

file <- basename(file)
file[!is_source_file] <- sub("^test-?", "", file[!is_source_file])

file <- sub("\\.[rR]$", "", file)

regex <- paste0("^", escape_special_regex(file), "$", collapse = "|")

test(filter = regex, ...)
}

find_active_file <- function(arg = "file") {
if (!rstudioapi::isAvailable()) {
stop("Argument `", arg, "` is missing, with no default", call. = FALSE)
}
rstudioapi::getSourceEditorContext()$path
}

find_test_file <- function(file) {
dir <- basename(dirname(file))
if (dir != "R") {
stop("Open file not in `R/` directory", call. = FALSE)
}

if (!grepl("\\.[Rr]$", file)) {
stop("Open file is does not end in `.R`", call. = FALSE)
}

file
}
5 changes: 5 additions & 0 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,8 @@ menu <- function(...) {
file.info <- function(...) {
base::file.info(...)
}

escape_special_regex <- function(x) {
chars <- c("*", ".", "?", "^", "+", "$", "|", "(", ")", "[", "]", "{", "}", "\\")
gsub(paste0("([\\", paste0(collapse = "\\", chars), "])"), "\\\\\\1", x, perl = TRUE)
}
12 changes: 10 additions & 2 deletions man/test.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-test.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ test_that("Filtering works with devtools::test", {
test("testTest", filter = "dummy", reporter = "stop")
expect_true(TRUE)
})

test_that("devtools::test_file works", {
expect_error(test_file("testTest/DESCRIPTION"), "are not R files")
test_file("testTest/tests/testthat/test-dummy.R", pkg = "testTest", reporter = "stop")
test_file("testTest/R/dummy.R", pkg = "testTest", reporter = "stop")
expect_true(TRUE)
})
Empty file.

0 comments on commit d66711b

Please sign in to comment.