77# ' @param location A string giving the location in the form
88# ' `FILE:LINE[:COLUMN]`.
99# ' @param path Path to write the reprex to. Defaults to `stdout()`.
10+ # ' @return This function is called for its side effect of rendering a
11+ # ' reprex to `path`. This function will never error: if extraction
12+ # ' fails, the error message will be written to `path`.
1013# ' @export
1114extract_test <- function (location , path = stdout()) {
1215 check_string(location )
@@ -20,8 +23,18 @@ extract_test <- function(location, path = stdout()) {
2023
2124 test_path <- test_path(pieces [[1 ]])
2225 line <- as.integer(pieces [2 ])
26+ source <- paste0(" # Extracted from tests/testthat/" , path , " :" , line )
27+
28+ lines <- tryCatch(
29+ extract_test_lines(test_path , line ),
30+ error = function (cnd ) {
31+ lines <- strsplit(conditionMessage(cnd ), " \n " )[[1 ]]
32+ lines <- c(" Failed to extract test" , lines )
33+ paste0(" # " , lines )
34+ }
35+ )
36+ lines <- c(source , lines )
2337
24- lines <- extract_test_lines(test_path , line )
2538 base :: writeLines(lines , con = path )
2639}
2740
@@ -62,14 +75,12 @@ extract_test_lines <- function(path, line, error_call = caller_env()) {
6275
6376 test_contents <- attr(call [[3 ]], " srcref" )[- 1 ] # drop `{`
6477 keep <- start_line(test_contents ) < = line
65- test <- map_chr(test_contents [keep ], as.character )
66-
67- c(
68- paste0(" # Extracted from tests/testthat/" , path , " :" , line ),
69- prequel ,
78+ test <- c(
7079 comment_header(" test" ),
71- test
80+ map_chr( test_contents [ keep ], as.character )
7281 )
82+
83+ c(prequel , test )
7384}
7485
7586# Helpers ---------------------------------------------------------------------
0 commit comments