-
Notifications
You must be signed in to change notification settings - Fork 330
Run single test #1840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run single test #1840
Changes from all commits
6133fea
6da6fb6
518970d
d089352
644ae77
eadf3b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,18 @@ | |
#' @param path Path to files. | ||
#' @param pattern Regular expression used to filter files. | ||
#' @param env Environment in which to evaluate code. | ||
#' @param desc If not-`NULL`, will run only test with this `desc`ription. | ||
#' @param chdir Change working directory to `dirname(path)`? | ||
#' @param wrap Automatically wrap all code within [test_that()]? This ensures | ||
#' that all expectations are reported, even if outside a test block. | ||
#' @export | ||
#' @keywords internal | ||
source_file <- function(path, env = test_env(), chdir = TRUE, | ||
wrap = TRUE) { | ||
source_file <- function(path, | ||
env = test_env(), | ||
chdir = TRUE, | ||
desc = NULL, | ||
wrap = TRUE, | ||
error_call = caller_env()) { | ||
stopifnot(file.exists(path)) | ||
stopifnot(is.environment(env)) | ||
|
||
|
@@ -23,6 +28,7 @@ source_file <- function(path, env = test_env(), chdir = TRUE, | |
con <- textConnection(lines, encoding = "UTF-8") | ||
on.exit(try(close(con), silent = TRUE), add = TRUE) | ||
exprs <- parse(con, n = -1, srcfile = srcfile, encoding = "UTF-8") | ||
exprs <- filter_desc(exprs, desc, error_call = error_call) | ||
|
||
n <- length(exprs) | ||
if (n == 0L) return(invisible()) | ||
|
@@ -46,13 +52,52 @@ source_file <- function(path, env = test_env(), chdir = TRUE, | |
error = function(err) { | ||
abort( | ||
paste0("In path: ", encodeString(path, quote = '"')), | ||
parent = err | ||
parent = err, | ||
call = error_call | ||
) | ||
} | ||
) | ||
} | ||
} | ||
|
||
filter_desc <- function(exprs, desc = NULL, error_call = caller_env()) { | ||
if (is.null(desc)) { | ||
return(exprs) | ||
} | ||
|
||
found <- FALSE | ||
include <- rep(FALSE, length(exprs)) | ||
|
||
for (i in seq_along(exprs)) { | ||
expr <- exprs[[i]] | ||
|
||
if (!is_call(expr, "test_that", n = 2)) { | ||
if (!found) { | ||
include[[i]] <- TRUE | ||
} | ||
} else { | ||
if (!is_string(expr[[2]])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to worry about people running code like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do that you're on your own 😄 People do weird things here (i.e. I bet someone does something like |
||
next | ||
|
||
test_desc <- as.character(expr[[2]]) | ||
if (test_desc != desc) | ||
next | ||
|
||
if (found) { | ||
abort("Found multiple tests with specified description", call = error_call) | ||
} | ||
include[[i]] <- TRUE | ||
found <- TRUE | ||
} | ||
} | ||
|
||
if (!found) { | ||
abort("Failed to find test with specified description", call = error_call) | ||
} | ||
|
||
exprs[include] | ||
} | ||
|
||
#' @rdname source_file | ||
#' @export | ||
source_dir <- function(path, pattern = "\\.[rR]$", env = test_env(), | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we only execute top-level code before the matching
test_that()
call?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooh good thought.