Skip to content

Commit e1b3833

Browse files
authored
Strip ANSI escapes in JunitReporter messages (#2157)
Fixes #2032. Fixes #1852.
1 parent 4a007ac commit e1b3833

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `JunitReporter()` strips ANSI escapes in more placese (#1852, #2032).
34
* `try_again()` is now publicised. The first argument is now the number of retries, not tries (#2050).
45
* `vignette("custom-expectations)` has been overhauled to make it much clearer how to create high-quality expectations (#2113, #2132, #2072).
56
* `expect_snapshot()` and friends will now fail when creating a new snapshot on CI. This is usually a signal that you've forgotten to run it locally before committing (#1461).

R/reporter-junit.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ JunitReporter <- R6::R6Class(
120120

121121
first_line <- function(x) {
122122
loc <- expectation_location(x, " (", ")")
123-
paste0(strsplit(x$message, split = "\n")[[1]][1], loc)
123+
paste0(strsplit(cli::ansi_strip(x$message), split = "\n")[[1]][1], loc)
124124
}
125125

126126
# add an extra XML child node if not a success

tests/testthat/test-reporter-junit.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,23 @@ test_that("permit Java-style class names", {
77
class <- "package_name_or_domain.ClassName"
88
expect_equal(classnameOK(class), class)
99
})
10+
11+
test_that("ANSI escapes are stripped from all user text in XML", {
12+
skip_if_not_installed("xml2")
13+
14+
tmp <- withr::local_tempfile(fileext = ".xml")
15+
reporter <- JunitReporterMock$new(file = tmp)
16+
reporter$start_reporter()
17+
18+
text_with_ansi <- "\033[33mFirst line\033[0m\nSecond line"
19+
reporter$start_context("c")
20+
reporter$start_test("c", "t")
21+
reporter$add_result("c", "t", new_expectation("error", text_with_ansi))
22+
reporter$add_result("c", "t", new_expectation("failure", text_with_ansi))
23+
reporter$add_result("c", "t", new_expectation("skip", text_with_ansi))
24+
reporter$end_test()
25+
reporter$end_context()
26+
reporter$end_reporter()
27+
28+
expect_no_error(xml2::read_xml(tmp))
29+
})

0 commit comments

Comments
 (0)