From 2ee85c7d75ea1ad503e97d55d3d168d3b50718cd Mon Sep 17 00:00:00 2001 From: Kuba Jalowiec Date: Fri, 15 Aug 2025 17:22:28 +0200 Subject: [PATCH 1/7] Enabling srcref argument in reporter$start_test --- R/parallel.R | 4 ++-- R/reporter-junit.R | 2 +- R/reporter-list.R | 2 +- R/reporter-location.R | 10 ++++++++-- R/reporter-multi.R | 4 ++-- R/reporter-slow.R | 2 +- R/reporter-stop.R | 2 +- R/reporter-teamcity.R | 2 +- R/reporter-timing.R | 2 +- R/reporter.R | 2 +- R/snapshot-reporter-parallel.R | 4 ++-- R/snapshot-reporter.R | 2 +- R/test-that.R | 3 ++- tests/testthat/_snaps/reporter-location.md | 16 ++++++++-------- tests/testthat/test-context.R | 2 +- 15 files changed, 33 insertions(+), 26 deletions(-) diff --git a/R/parallel.R b/R/parallel.R index f5fe10823..9267c2967 100644 --- a/R/parallel.R +++ b/R/parallel.R @@ -377,10 +377,10 @@ SubprocessReporter <- R6::R6Class( private$filename <- filename private$event("start_file", filename) }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { private$context <- context private$test <- test - private$event("start_test", context, test) + private$event("start_test", context, test, srcref) }, start_context = function(context) { private$context <- context diff --git a/R/reporter-junit.R b/R/reporter-junit.R index 12442f894..496a4d987 100644 --- a/R/reporter-junit.R +++ b/R/reporter-junit.R @@ -69,7 +69,7 @@ JunitReporter <- R6::R6Class( self$file_name <- file }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { if (is.null(context)) { context_start_file(self$file_name) } diff --git a/R/reporter-list.R b/R/reporter-list.R index f02214a0d..a1990b064 100644 --- a/R/reporter-list.R +++ b/R/reporter-list.R @@ -24,7 +24,7 @@ ListReporter <- R6::R6Class( self$results <- Stack$new() }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { if ( !identical(self$current_context, context) || !identical(self$current_test, test) diff --git a/R/reporter-location.R b/R/reporter-location.R index 5e5c384b5..7d6867158 100644 --- a/R/reporter-location.R +++ b/R/reporter-location.R @@ -10,8 +10,14 @@ LocationReporter <- R6::R6Class( "LocationReporter", inherit = Reporter, public = list( - start_test = function(context, test) { - self$cat_line("Start test: ", test) + start_test = function(context, test, srcref=NULL) { + if (!is.null(srcref)) { + filename <- attr(srcref, "srcfile")$filename + line_number <- as.integer(srcref[[1]]) + self$cat_line("Start test: '", test, "' at ", filename, ":", line_number) + } else { + self$cat_line("Start test: ", test) + } }, add_result = function(context, test, result) { diff --git a/R/reporter-multi.R b/R/reporter-multi.R index 0a49f3e39..21a0bc6f3 100644 --- a/R/reporter-multi.R +++ b/R/reporter-multi.R @@ -26,8 +26,8 @@ MultiReporter <- R6::R6Class( start_context = function(context) { o_apply(self$reporters, "start_context", context) }, - start_test = function(context, test) { - o_apply(self$reporters, "start_test", context, test) + start_test = function(context, test, srcref=NULL) { + o_apply(self$reporters, "start_test", context, test, srcref) }, add_result = function(context, test, result) { o_apply( diff --git a/R/reporter-slow.R b/R/reporter-slow.R index bd228d274..fa0ef0815 100644 --- a/R/reporter-slow.R +++ b/R/reporter-slow.R @@ -38,7 +38,7 @@ SlowReporter <- R6::R6Class( self$current_file <- file }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { self$current_test_start <- proc.time()[[3]] }, diff --git a/R/reporter-stop.R b/R/reporter-stop.R index 4c9f2a67b..a8de111cb 100644 --- a/R/reporter-stop.R +++ b/R/reporter-stop.R @@ -31,7 +31,7 @@ StopReporter <- R6::R6Class( self$stop_reporter <- stop_reporter }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { self$issues <- Stack$new() }, diff --git a/R/reporter-teamcity.R b/R/reporter-teamcity.R index d14bdae60..600ae5a67 100644 --- a/R/reporter-teamcity.R +++ b/R/reporter-teamcity.R @@ -27,7 +27,7 @@ TeamcityReporter <- R6::R6Class( self$cat_line() }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { private$report_event("testSuiteStarted", test) self$i <- 1L }, diff --git a/R/reporter-timing.R b/R/reporter-timing.R index 84a89633e..da9fe239d 100644 --- a/R/reporter-timing.R +++ b/R/reporter-timing.R @@ -31,7 +31,7 @@ SlowReporter <- R6::R6Class( self$current_file <- file }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { self$current_test_start <- proc.time()[[3]] }, diff --git a/R/reporter.R b/R/reporter.R index dc7998255..0e85a89da 100644 --- a/R/reporter.R +++ b/R/reporter.R @@ -29,7 +29,7 @@ Reporter <- R6::R6Class( capabilities = list(parallel_support = FALSE, parallel_updates = FALSE), start_reporter = function() {}, start_context = function(context) {}, - start_test = function(context, test) {}, + start_test = function(context, test, srcref=NULL) {}, start_file = function(filename) {}, add_result = function(context, test, result) {}, end_test = function(context, test) {}, diff --git a/R/snapshot-reporter-parallel.R b/R/snapshot-reporter-parallel.R index 4306e48c6..9042ddd55 100644 --- a/R/snapshot-reporter-parallel.R +++ b/R/snapshot-reporter-parallel.R @@ -34,10 +34,10 @@ SubprocessSnapshotReporter <- R6::R6Class( super$end_test(context, test) }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { private$context <- context private$test <- test - super$start_test(context, test) + super$start_test(context, test, srcref) }, announce_file_snapshot = function(name) { diff --git a/R/snapshot-reporter.R b/R/snapshot-reporter.R index 9b28029e0..db02e9d4e 100644 --- a/R/snapshot-reporter.R +++ b/R/snapshot-reporter.R @@ -36,7 +36,7 @@ SnapshotReporter <- R6::R6Class( } }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { if (is.null(test)) { return() } diff --git a/R/test-that.R b/R/test-that.R index 1308976ff..0beafdbf7 100644 --- a/R/test-that.R +++ b/R/test-that.R @@ -51,7 +51,8 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) { test <- test_description() if (!is.null(test)) { - reporter$start_test(context = reporter$.context, test = test) + srcref <- attr(code, "srcref")[[1]] + reporter$start_test(context = reporter$.context, test = test, srcref = srcref) withr::defer(reporter$end_test(context = reporter$.context, test = test)) } diff --git a/tests/testthat/_snaps/reporter-location.md b/tests/testthat/_snaps/reporter-location.md index 4da674543..e09980e71 100644 --- a/tests/testthat/_snaps/reporter-location.md +++ b/tests/testthat/_snaps/reporter-location.md @@ -1,34 +1,34 @@ # reporter as expected - Start test: Success + Start test: 'Success' at reporters/tests.R:5 'reporters/tests.R:6:3' [success] End test: Success - Start test: Failure:1 + Start test: 'Failure:1' at reporters/tests.R:11 'reporters/tests.R:12:3' [failure] End test: Failure:1 - Start test: Failure:2a + Start test: 'Failure:2a' at reporters/tests.R:15 'reporters/tests.R:16:8' [failure] End test: Failure:2a - Start test: Error:1 + Start test: 'Error:1' at reporters/tests.R:22 'reporters/tests.R:23:3' [error] End test: Error:1 - Start test: errors get tracebacks + Start test: 'errors get tracebacks' at reporters/tests.R:26 'reporters/tests.R:29:8' [error] End test: errors get tracebacks - Start test: explicit skips are reported + Start test: 'explicit skips are reported' at reporters/tests.R:36 'reporters/tests.R:37:3' [skip] End test: explicit skips are reported - Start test: empty tests are implicitly skipped + Start test: 'empty tests are implicitly skipped' at reporters/tests.R:40 'reporters/tests.R:40:1' [skip] End test: empty tests are implicitly skipped - Start test: warnings get backtraces + Start test: 'warnings get backtraces' at reporters/tests.R:44 'reporters/tests.R:46:5' [warning] 'reporters/tests.R:44:1' [skip] End test: warnings get backtraces diff --git a/tests/testthat/test-context.R b/tests/testthat/test-context.R index 9322a2adb..0a828c699 100644 --- a/tests/testthat/test-context.R +++ b/tests/testthat/test-context.R @@ -15,7 +15,7 @@ CountReporter <- R6::R6Class( self$context_i <- self$context_i - 1 stopifnot(self$context_i >= 0) }, - start_test = function(context, test) { + start_test = function(context, test, srcref=NULL) { self$test_count <- self$test_count + 1 self$test_i <- self$test_i + 1 }, From f6ee7582f9100896e1bf09bde1b236f953ce4c6d Mon Sep 17 00:00:00 2001 From: Kuba Jalowiec Date: Wed, 20 Aug 2025 00:42:35 +0200 Subject: [PATCH 2/7] Fixing tests --- tests/testthat/_snaps/reporter-location.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/testthat/_snaps/reporter-location.md b/tests/testthat/_snaps/reporter-location.md index bd3e104ec..6fb021058 100644 --- a/tests/testthat/_snaps/reporter-location.md +++ b/tests/testthat/_snaps/reporter-location.md @@ -5,30 +5,30 @@ End test: Success Start test: 'Failure:1' at reporters/tests.R:11 - 'reporters/tests.R:12:3' [failure] + 'reporters/tests.R:13:3' [failure] End test: Failure:1 - Start test: 'Failure:2a' at reporters/tests.R:15 + Start test: 'Failure:2a' at reporters/tests.R:16 'reporters/tests.R:17:8' [failure] End test: Failure:2a - Start test: 'Error:1' at reporters/tests.R:22 + Start test: 'Error:1' at reporters/tests.R:23 'reporters/tests.R:24:3' [error] End test: Error:1 - Start test: 'errors get tracebacks' at reporters/tests.R:26 + Start test: 'errors get tracebacks' at reporters/tests.R:27 'reporters/tests.R:30:8' [error] End test: errors get tracebacks - Start test: 'explicit skips are reported' at reporters/tests.R:36 + Start test: 'explicit skips are reported' at reporters/tests.R:37 'reporters/tests.R:38:3' [skip] End test: explicit skips are reported - Start test: 'empty tests are implicitly skipped' at reporters/tests.R:40 + Start test: 'empty tests are implicitly skipped' at reporters/tests.R:41 'reporters/tests.R:41:1' [skip] End test: empty tests are implicitly skipped - Start test: 'warnings get backtraces' at reporters/tests.R:44 + Start test: 'warnings get backtraces' at reporters/tests.R:45 'reporters/tests.R:47:5' [warning] 'reporters/tests.R:45:1' [skip] End test: warnings get backtraces From c75ee943b2549536c4c3f9696fc9d2c5d9093673 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 27 Aug 2025 13:51:53 -0500 Subject: [PATCH 3/7] Restyle with air --- R/expect-named.R | 1 - R/parallel.R | 2 +- R/reporter-junit.R | 2 +- R/reporter-list.R | 2 +- R/reporter-location.R | 15 +++++++++++---- R/reporter-multi.R | 2 +- R/reporter-slow.R | 2 +- R/reporter-stop.R | 2 +- R/reporter-teamcity.R | 2 +- R/reporter-timing.R | 2 +- R/reporter.R | 2 +- R/snapshot-reporter-parallel.R | 2 +- R/snapshot-reporter.R | 2 +- R/test-that.R | 6 +++++- tests/testthat/test-context.R | 2 +- 15 files changed, 28 insertions(+), 18 deletions(-) diff --git a/R/expect-named.R b/R/expect-named.R index 4f8cb10c6..4a71ca121 100644 --- a/R/expect-named.R +++ b/R/expect-named.R @@ -35,7 +35,6 @@ expect_named <- function( check_bool(ignore.order) check_bool(ignore.case) - act <- quasi_label(enquo(object), label) if (missing(expected)) { diff --git a/R/parallel.R b/R/parallel.R index 1612a2710..a7d5270f9 100644 --- a/R/parallel.R +++ b/R/parallel.R @@ -376,7 +376,7 @@ SubprocessReporter <- R6::R6Class( private$filename <- filename private$event("start_file", filename) }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { private$context <- context private$test <- test private$event("start_test", context, test, srcref) diff --git a/R/reporter-junit.R b/R/reporter-junit.R index 496a4d987..df9293ea8 100644 --- a/R/reporter-junit.R +++ b/R/reporter-junit.R @@ -69,7 +69,7 @@ JunitReporter <- R6::R6Class( self$file_name <- file }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { if (is.null(context)) { context_start_file(self$file_name) } diff --git a/R/reporter-list.R b/R/reporter-list.R index a1990b064..4c78f5c91 100644 --- a/R/reporter-list.R +++ b/R/reporter-list.R @@ -24,7 +24,7 @@ ListReporter <- R6::R6Class( self$results <- Stack$new() }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { if ( !identical(self$current_context, context) || !identical(self$current_test, test) diff --git a/R/reporter-location.R b/R/reporter-location.R index 7d6867158..69b2a3673 100644 --- a/R/reporter-location.R +++ b/R/reporter-location.R @@ -10,11 +10,18 @@ LocationReporter <- R6::R6Class( "LocationReporter", inherit = Reporter, public = list( - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { if (!is.null(srcref)) { - filename <- attr(srcref, "srcfile")$filename - line_number <- as.integer(srcref[[1]]) - self$cat_line("Start test: '", test, "' at ", filename, ":", line_number) + filename <- attr(srcref, "srcfile")$filename + line_number <- as.integer(srcref[[1]]) + self$cat_line( + "Start test: '", + test, + "' at ", + filename, + ":", + line_number + ) } else { self$cat_line("Start test: ", test) } diff --git a/R/reporter-multi.R b/R/reporter-multi.R index 21a0bc6f3..5aa641bba 100644 --- a/R/reporter-multi.R +++ b/R/reporter-multi.R @@ -26,7 +26,7 @@ MultiReporter <- R6::R6Class( start_context = function(context) { o_apply(self$reporters, "start_context", context) }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { o_apply(self$reporters, "start_test", context, test, srcref) }, add_result = function(context, test, result) { diff --git a/R/reporter-slow.R b/R/reporter-slow.R index fa0ef0815..27d6540c0 100644 --- a/R/reporter-slow.R +++ b/R/reporter-slow.R @@ -38,7 +38,7 @@ SlowReporter <- R6::R6Class( self$current_file <- file }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { self$current_test_start <- proc.time()[[3]] }, diff --git a/R/reporter-stop.R b/R/reporter-stop.R index a8de111cb..e169c22eb 100644 --- a/R/reporter-stop.R +++ b/R/reporter-stop.R @@ -31,7 +31,7 @@ StopReporter <- R6::R6Class( self$stop_reporter <- stop_reporter }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { self$issues <- Stack$new() }, diff --git a/R/reporter-teamcity.R b/R/reporter-teamcity.R index 600ae5a67..8125fcb21 100644 --- a/R/reporter-teamcity.R +++ b/R/reporter-teamcity.R @@ -27,7 +27,7 @@ TeamcityReporter <- R6::R6Class( self$cat_line() }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { private$report_event("testSuiteStarted", test) self$i <- 1L }, diff --git a/R/reporter-timing.R b/R/reporter-timing.R index da9fe239d..026966a12 100644 --- a/R/reporter-timing.R +++ b/R/reporter-timing.R @@ -31,7 +31,7 @@ SlowReporter <- R6::R6Class( self$current_file <- file }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { self$current_test_start <- proc.time()[[3]] }, diff --git a/R/reporter.R b/R/reporter.R index 0e85a89da..6f849af39 100644 --- a/R/reporter.R +++ b/R/reporter.R @@ -29,7 +29,7 @@ Reporter <- R6::R6Class( capabilities = list(parallel_support = FALSE, parallel_updates = FALSE), start_reporter = function() {}, start_context = function(context) {}, - start_test = function(context, test, srcref=NULL) {}, + start_test = function(context, test, srcref = NULL) {}, start_file = function(filename) {}, add_result = function(context, test, result) {}, end_test = function(context, test) {}, diff --git a/R/snapshot-reporter-parallel.R b/R/snapshot-reporter-parallel.R index 9042ddd55..665cbbb89 100644 --- a/R/snapshot-reporter-parallel.R +++ b/R/snapshot-reporter-parallel.R @@ -34,7 +34,7 @@ SubprocessSnapshotReporter <- R6::R6Class( super$end_test(context, test) }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { private$context <- context private$test <- test super$start_test(context, test, srcref) diff --git a/R/snapshot-reporter.R b/R/snapshot-reporter.R index 9186a690b..9afaf657b 100644 --- a/R/snapshot-reporter.R +++ b/R/snapshot-reporter.R @@ -58,7 +58,7 @@ SnapshotReporter <- R6::R6Class( } }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { if (is.null(test)) { return() } diff --git a/R/test-that.R b/R/test-that.R index 6757c7c20..94e423437 100644 --- a/R/test-that.R +++ b/R/test-that.R @@ -52,7 +52,11 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) { test <- test_description() if (!is.null(test)) { srcref <- attr(code, "srcref")[[1]] - reporter$start_test(context = reporter$.context, test = test, srcref = srcref) + reporter$start_test( + context = reporter$.context, + test = test, + srcref = srcref + ) withr::defer(reporter$end_test(context = reporter$.context, test = test)) } diff --git a/tests/testthat/test-context.R b/tests/testthat/test-context.R index 0a828c699..e0fc6aadd 100644 --- a/tests/testthat/test-context.R +++ b/tests/testthat/test-context.R @@ -15,7 +15,7 @@ CountReporter <- R6::R6Class( self$context_i <- self$context_i - 1 stopifnot(self$context_i >= 0) }, - start_test = function(context, test, srcref=NULL) { + start_test = function(context, test, srcref = NULL) { self$test_count <- self$test_count + 1 self$test_i <- self$test_i + 1 }, From ed3b9f1e98720a15ff3826866a69323095941333 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 27 Aug 2025 14:35:51 -0500 Subject: [PATCH 4/7] Reuse existing srcref code --- R/expectation.R | 9 +++++---- R/reporter-location.R | 19 +++++-------------- tests/testthat/_snaps/reporter-location.md | 16 ++++++++-------- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/R/expectation.R b/R/expectation.R index 55f00845a..98d89de6f 100644 --- a/R/expectation.R +++ b/R/expectation.R @@ -227,13 +227,14 @@ single_letter_summary <- function(x) { } expectation_location <- function(x, prefix = "", suffix = "") { - srcref <- x$srcref + srcref_location(x$srcref, prefix = prefix, suffix = suffix) +} +srcref_location <- function(srcref, prefix = "", suffix = "") { if (!inherits(srcref, "srcref")) { return("") } filename <- attr(srcref, "srcfile")$filename - cli::format_inline( - "{prefix}{.file {filename}:{srcref[1]}:{srcref[2]}}{suffix}" - ) + link <- cli::format_inline("{.file {filename}:{srcref[1]}:{srcref[2]}}") + paste0(prefix, link, suffix) } diff --git a/R/reporter-location.R b/R/reporter-location.R index 69b2a3673..d9b0b81bc 100644 --- a/R/reporter-location.R +++ b/R/reporter-location.R @@ -11,20 +11,11 @@ LocationReporter <- R6::R6Class( inherit = Reporter, public = list( start_test = function(context, test, srcref = NULL) { - if (!is.null(srcref)) { - filename <- attr(srcref, "srcfile")$filename - line_number <- as.integer(srcref[[1]]) - self$cat_line( - "Start test: '", - test, - "' at ", - filename, - ":", - line_number - ) - } else { - self$cat_line("Start test: ", test) - } + self$cat_line( + "Start test: ", + test, + srcref_location(srcref, prefix = " at ") + ) }, add_result = function(context, test, result) { diff --git a/tests/testthat/_snaps/reporter-location.md b/tests/testthat/_snaps/reporter-location.md index 6fb021058..bf131d384 100644 --- a/tests/testthat/_snaps/reporter-location.md +++ b/tests/testthat/_snaps/reporter-location.md @@ -1,34 +1,34 @@ # reporter as expected - Start test: 'Success' at reporters/tests.R:5 + Start test: Success at 'reporters/tests.R:5:22' 'reporters/tests.R:6:3' [success] End test: Success - Start test: 'Failure:1' at reporters/tests.R:11 + Start test: Failure:1 at 'reporters/tests.R:11:24' 'reporters/tests.R:13:3' [failure] End test: Failure:1 - Start test: 'Failure:2a' at reporters/tests.R:16 + Start test: Failure:2a at 'reporters/tests.R:16:25' 'reporters/tests.R:17:8' [failure] End test: Failure:2a - Start test: 'Error:1' at reporters/tests.R:23 + Start test: Error:1 at 'reporters/tests.R:23:22' 'reporters/tests.R:24:3' [error] End test: Error:1 - Start test: 'errors get tracebacks' at reporters/tests.R:27 + Start test: errors get tracebacks at 'reporters/tests.R:27:36' 'reporters/tests.R:30:8' [error] End test: errors get tracebacks - Start test: 'explicit skips are reported' at reporters/tests.R:37 + Start test: explicit skips are reported at 'reporters/tests.R:37:42' 'reporters/tests.R:38:3' [skip] End test: explicit skips are reported - Start test: 'empty tests are implicitly skipped' at reporters/tests.R:41 + Start test: empty tests are implicitly skipped at 'reporters/tests.R:41:49' 'reporters/tests.R:41:1' [skip] End test: empty tests are implicitly skipped - Start test: 'warnings get backtraces' at reporters/tests.R:45 + Start test: warnings get backtraces at 'reporters/tests.R:45:38' 'reporters/tests.R:47:5' [warning] 'reporters/tests.R:45:1' [skip] End test: warnings get backtraces From 5d3af727594eb2f065a86f3cb38edfdccb31c735 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 27 Aug 2025 14:37:20 -0500 Subject: [PATCH 5/7] More alignment --- R/reporter-location.R | 2 +- tests/testthat/_snaps/reporter-location.md | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/R/reporter-location.R b/R/reporter-location.R index d9b0b81bc..d838f2ba4 100644 --- a/R/reporter-location.R +++ b/R/reporter-location.R @@ -14,7 +14,7 @@ LocationReporter <- R6::R6Class( self$cat_line( "Start test: ", test, - srcref_location(srcref, prefix = " at ") + srcref_location(srcref, prefix = " (", suffix = ")") ) }, diff --git a/tests/testthat/_snaps/reporter-location.md b/tests/testthat/_snaps/reporter-location.md index bf131d384..7a369d06d 100644 --- a/tests/testthat/_snaps/reporter-location.md +++ b/tests/testthat/_snaps/reporter-location.md @@ -1,34 +1,34 @@ # reporter as expected - Start test: Success at 'reporters/tests.R:5:22' + Start test: Success ('reporters/tests.R:5:22') 'reporters/tests.R:6:3' [success] End test: Success - Start test: Failure:1 at 'reporters/tests.R:11:24' + Start test: Failure:1 ('reporters/tests.R:11:24') 'reporters/tests.R:13:3' [failure] End test: Failure:1 - Start test: Failure:2a at 'reporters/tests.R:16:25' + Start test: Failure:2a ('reporters/tests.R:16:25') 'reporters/tests.R:17:8' [failure] End test: Failure:2a - Start test: Error:1 at 'reporters/tests.R:23:22' + Start test: Error:1 ('reporters/tests.R:23:22') 'reporters/tests.R:24:3' [error] End test: Error:1 - Start test: errors get tracebacks at 'reporters/tests.R:27:36' + Start test: errors get tracebacks ('reporters/tests.R:27:36') 'reporters/tests.R:30:8' [error] End test: errors get tracebacks - Start test: explicit skips are reported at 'reporters/tests.R:37:42' + Start test: explicit skips are reported ('reporters/tests.R:37:42') 'reporters/tests.R:38:3' [skip] End test: explicit skips are reported - Start test: empty tests are implicitly skipped at 'reporters/tests.R:41:49' + Start test: empty tests are implicitly skipped ('reporters/tests.R:41:49') 'reporters/tests.R:41:1' [skip] End test: empty tests are implicitly skipped - Start test: warnings get backtraces at 'reporters/tests.R:45:38' + Start test: warnings get backtraces ('reporters/tests.R:45:38') 'reporters/tests.R:47:5' [warning] 'reporters/tests.R:45:1' [skip] End test: warnings get backtraces From 0d3fedf36cf7d0660149a82db05aab1c653601a4 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 27 Aug 2025 14:41:50 -0500 Subject: [PATCH 6/7] Explicitly test nesting --- tests/testthat/_snaps/reporter-location.md | 17 +++++++++++++++++ tests/testthat/reporters/nested.R | 11 +++++++++++ tests/testthat/test-reporter-location.R | 7 +++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/testthat/reporters/nested.R diff --git a/tests/testthat/_snaps/reporter-location.md b/tests/testthat/_snaps/reporter-location.md index 7a369d06d..30616952c 100644 --- a/tests/testthat/_snaps/reporter-location.md +++ b/tests/testthat/_snaps/reporter-location.md @@ -34,3 +34,20 @@ End test: warnings get backtraces +--- + + Start test: x ('reporters/nested.R:1:15') + Start test: x / y ('reporters/nested.R:2:17') + Start test: x / y / z1 ('reporters/nested.R:3:20') + 'reporters/nested.R:4:7' [success] + End test: x / y / z1 + + Start test: x / y / z2 ('reporters/nested.R:7:20') + 'reporters/nested.R:8:7' [failure] + End test: x / y / z2 + + End test: x / y + + End test: x + + diff --git a/tests/testthat/reporters/nested.R b/tests/testthat/reporters/nested.R new file mode 100644 index 000000000..da51001f3 --- /dev/null +++ b/tests/testthat/reporters/nested.R @@ -0,0 +1,11 @@ +describe("x", { + describe("y", { + describe("z1", { + expect_true(TRUE) + }) + + describe("z2", { + expect_true(FALSE) + }) + }) +}) diff --git a/tests/testthat/test-reporter-location.R b/tests/testthat/test-reporter-location.R index 2eb33022d..2a11a283d 100644 --- a/tests/testthat/test-reporter-location.R +++ b/tests/testthat/test-reporter-location.R @@ -1,3 +1,10 @@ test_that("reporter as expected", { expect_snapshot_reporter(LocationReporter$new()) }) + +test_that("reporter as expected", { + expect_snapshot_reporter( + LocationReporter$new(), + test_path("reporters/nested.R") + ) +}) From 162226a1a15090af9dbf6e149b7e526f9a76a129 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 27 Aug 2025 14:44:11 -0500 Subject: [PATCH 7/7] Only add empty line at end of top-level test --- R/reporter-location.R | 10 +++++++++- tests/testthat/_snaps/reporter-location.md | 21 +++++++++++---------- tests/testthat/reporters/nested.R | 8 ++++++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/R/reporter-location.R b/R/reporter-location.R index d838f2ba4..10cd3f587 100644 --- a/R/reporter-location.R +++ b/R/reporter-location.R @@ -10,7 +10,11 @@ LocationReporter <- R6::R6Class( "LocationReporter", inherit = Reporter, public = list( + depth = 0, + start_test = function(context, test, srcref = NULL) { + self$depth <- self$depth + 1 + self$cat_line( "Start test: ", test, @@ -24,8 +28,12 @@ LocationReporter <- R6::R6Class( }, end_test = function(context, test) { + self$depth <- self$depth - 1 self$cat_line("End test: ", test) - self$cat_line() + + if (self$depth == 0) { + self$cat_line() + } } ) ) diff --git a/tests/testthat/_snaps/reporter-location.md b/tests/testthat/_snaps/reporter-location.md index 30616952c..c332d85da 100644 --- a/tests/testthat/_snaps/reporter-location.md +++ b/tests/testthat/_snaps/reporter-location.md @@ -36,18 +36,19 @@ --- - Start test: x ('reporters/nested.R:1:15') - Start test: x / y ('reporters/nested.R:2:17') - Start test: x / y / z1 ('reporters/nested.R:3:20') + Start test: x1 ('reporters/nested.R:1:16') + Start test: x1 / y1 ('reporters/nested.R:2:18') + Start test: x1 / y1 / z1 ('reporters/nested.R:3:20') 'reporters/nested.R:4:7' [success] - End test: x / y / z1 - - Start test: x / y / z2 ('reporters/nested.R:7:20') + End test: x1 / y1 / z1 + Start test: x1 / y1 / z2 ('reporters/nested.R:7:20') 'reporters/nested.R:8:7' [failure] - End test: x / y / z2 - - End test: x / y + End test: x1 / y1 / z2 + End test: x1 / y1 + End test: x1 - End test: x + Start test: x2 ('reporters/nested.R:13:16') + 'reporters/nested.R:14:3' [success] + End test: x2 diff --git a/tests/testthat/reporters/nested.R b/tests/testthat/reporters/nested.R index da51001f3..f4de69f42 100644 --- a/tests/testthat/reporters/nested.R +++ b/tests/testthat/reporters/nested.R @@ -1,5 +1,5 @@ -describe("x", { - describe("y", { +describe("x1", { + describe("y1", { describe("z1", { expect_true(TRUE) }) @@ -9,3 +9,7 @@ describe("x", { }) }) }) + +describe("x2", { + expect_true(TRUE) +})