Skip to content

Commit 141ad83

Browse files
committed
Refactor to simplify testing
1 parent 20a533f commit 141ad83

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

R/extract.R

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ extract_test <- function(location, path = stdout()) {
2424
test_path <- test_path(pieces[[1]])
2525
line <- as.integer(pieces[2])
2626
source <- paste0("# Extracted from ", test_path, ":", line)
27+
exprs <- parse_file(test_path)
2728

2829
lines <- tryCatch(
29-
extract_test_lines(test_path, line),
30+
extract_test_lines(exprs, line),
3031
error = function(cnd) {
3132
lines <- strsplit(conditionMessage(cnd), "\n")[[1]]
3233
lines <- c("", "Failed to extract test: ", lines)
@@ -38,17 +39,9 @@ extract_test <- function(location, path = stdout()) {
3839
base::writeLines(lines, con = path)
3940
}
4041

41-
extract_test_lines <- function(path, line, error_call = caller_env()) {
42-
check_string(path)
43-
if (!file.exists(path)) {
44-
cli::cli_abort(
45-
"{.arg path} ({.path path}) does not exist.",
46-
call = error_call
47-
)
48-
}
42+
extract_test_lines <- function(exprs, line, error_call = caller_env()) {
4943
check_number_whole(line, min = 1, call = error_call)
5044

51-
exprs <- parse(file = path, keep.source = TRUE)
5245
srcrefs <- attr(exprs, "srcref")
5346

5447
# Focus on srcrefs before the selected line
@@ -88,6 +81,17 @@ extract_test_lines <- function(path, line, error_call = caller_env()) {
8881

8982
# Helpers ---------------------------------------------------------------------
9083

84+
parse_file <- function(path, error_call = caller_env()) {
85+
check_string(path, call = error_call)
86+
if (!file.exists(path)) {
87+
cli::cli_abort(
88+
"{.arg path} ({.path path}) does not exist.",
89+
call = error_call
90+
)
91+
}
92+
parse(path, keep.source = TRUE)
93+
}
94+
9195
check_test_call <- function(expr, error_call = caller_env()) {
9296
if (!is_call(expr, n = 2)) {
9397
cli::cli_abort(

0 commit comments

Comments
 (0)