diff --git a/NEWS.md b/NEWS.md index fafa931f7..a7e4987c2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # testthat (development version) +* `source_file()`, which is used by various parts of the helper and + setup/teardown machinery, now reports the file name in the case of + errors (#1704). + * New `vignette("special-files")` describes the various special files that testthat uses (#1638). diff --git a/R/source.R b/R/source.R index b6fc90286..68f1f08a0 100644 --- a/R/source.R +++ b/R/source.R @@ -36,7 +36,15 @@ source_file <- function(path, env = test_env(), chdir = TRUE, if (wrap) { invisible(test_code(NULL, exprs, env)) } else { - invisible(eval(exprs, env)) + withCallingHandlers( + invisible(eval(exprs, env)), + error = function(err) { + abort( + paste0("In path: ", encodeString(path, quote = '"')), + parent = err + ) + } + ) } } @@ -45,7 +53,9 @@ source_file <- function(path, env = test_env(), chdir = TRUE, source_dir <- function(path, pattern = "\\.[rR]$", env = test_env(), chdir = TRUE, wrap = TRUE) { files <- normalizePath(sort(dir(path, pattern, full.names = TRUE))) - lapply(files, source_file, env = env, chdir = chdir, wrap = wrap) + lapply(files, function(path) { + source_file(path, env = env, chdir = chdir, wrap = wrap) + }) } #' @rdname source_file diff --git a/tests/testthat/_snaps/source.md b/tests/testthat/_snaps/source.md new file mode 100644 index 000000000..99623a8d2 --- /dev/null +++ b/tests/testthat/_snaps/source.md @@ -0,0 +1,10 @@ +# source_file wraps error + + Code + source_file(test_path("reporters/error-setup.R"), wrap = FALSE) + Condition + Error in `source_file()`: + ! In path: "reporters/error-setup.R" + Caused by error in `h()`: + ! ! + diff --git a/tests/testthat/test-source.R b/tests/testthat/test-source.R index d5eb1f251..591e67a3b 100644 --- a/tests/testthat/test-source.R +++ b/tests/testthat/test-source.R @@ -39,3 +39,9 @@ test_that("source_file always uses UTF-8 encoding", { run_test("German_Germany.1252") run_test(Sys.getlocale("LC_CTYPE")) }) + +test_that("source_file wraps error", { + expect_snapshot(error = TRUE, { + source_file(test_path("reporters/error-setup.R"), wrap = FALSE) + }) +})