Skip to content

Commit 279fa1b

Browse files
authored
Merge pull request #162 from r-lib/fix/test-fail-as-cran
2 parents f0a7e3c + 42d299e commit 279fa1b

File tree

6 files changed

+106
-32
lines changed

6 files changed

+106
-32
lines changed

R/callback.R

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
#' @importFrom utils head tail
88
#' @importFrom prettyunits pretty_dt
99

10-
block_callback <- function(top_line = TRUE, sys_time = NULL) {
10+
block_callback <- function(
11+
top_line = TRUE,
12+
sys_time = NULL,
13+
as_cran = NA) {
1114

1215
sys_time <- sys_time %||% Sys.time
1316
partial_line <- ""
@@ -99,26 +102,6 @@ block_callback <- function(top_line = TRUE, sys_time = NULL) {
99102
}
100103
}
101104

102-
## The output from the tests is a bit messed up, especially if we
103-
## want to process it line by line.
104-
## The tests start with '* checking test ...\n' and then when a test
105-
## file starts running we see sg like ' Running 'testthat.R''
106-
## This is without the newline character.
107-
##
108-
## The first test file that errors out will stop the tests entirely.
109-
##
110-
## When a test file is done, we get a '\n', and then either the next one
111-
## is started with another ' Running ...' line (without \n), or they are
112-
## done completely, and we get a '\n' and ' ERROR\n' / ' OK\n' depending
113-
## on the result.
114-
##
115-
## So the tricky thing is, we can only update a 'Running ' line after
116-
## we already know what is in the next line. If the next line is ' ERROR',
117-
## then the test file failed, otherwise succeeded. So we also do the actual
118-
## updating based on the ' Running' partial lines.
119-
##
120-
## As usually, prev_line contains the previous line.
121-
122105
do_test_mode <- function(x) {
123106
## Maybe we just learned the result of the current test file
124107
if (test_running) {
@@ -132,7 +115,14 @@ block_callback <- function(top_line = TRUE, sys_time = NULL) {
132115
## Tests are over, error
133116
state <<- "ERROR"
134117
test_running <<- FALSE
135-
xx <- style(err = "E", pale = no(prev_line))
118+
if (isTRUE(as_cran)) {
119+
xp <- style(pale = symbol$line, pale = no(prev_line))
120+
xp <- style(xp, timing = time_if_long())
121+
cat(xp, "\n", sep = "")
122+
xx <- style(err = "E", pale = " Some test files failed")
123+
} else {
124+
xx <- style(err = "E", pale = no(prev_line))
125+
}
136126
xx <- style(xx, timing = time_if_long())
137127
} else if (grepl("^\\s+Comparing", x)) {
138128
## Comparison
@@ -141,7 +131,11 @@ block_callback <- function(top_line = TRUE, sys_time = NULL) {
141131
xx <- style(xx, timing = time_if_long())
142132
} else if (grepl("^\\s+Running", x)) {
143133
## Next test is running now, state unchanged
144-
xx <- style(ok = symbol$tick, pale = no(prev_line))
134+
if (isTRUE(as_cran)) {
135+
xx <- style(pale = symbol$line, pale = no(prev_line))
136+
} else {
137+
xx <- style(ok = symbol$tick, pale = no(prev_line))
138+
}
145139
xx <- style(xx, timing = time_if_long())
146140
now <<- sys_time()
147141
} else {
@@ -190,7 +184,11 @@ block_callback <- function(top_line = TRUE, sys_time = NULL) {
190184
if (grepl("^\\s+Running ", x) || grepl("^\\s+Comparing", x)) {
191185
test_running <<- FALSE
192186
if (grepl("^\\s+Running ", x)) {
193-
xx <- style(ok = symbol$tick, pale = no(prev_line))
187+
if (isTRUE(as_cran)) {
188+
xx <- style(pale = symbol$line, pale = no(prev_line))
189+
} else {
190+
xx <- style(ok = symbol$tick, pale = no(prev_line))
191+
}
194192
xx <- style(xx, timing = time_if_long())
195193
} else {
196194
xx <- style(ok = symbol$tick, pale = prev_line)
@@ -224,14 +222,14 @@ is_new_check <- function(x) {
224222
grepl("^\\*\\*? ", x)
225223
}
226224

227-
simple_callback <- function(top_line = TRUE, sys_time = NULL) {
225+
simple_callback <- function(top_line = TRUE, sys_time = NULL, ...) {
228226
function(x) cat(gsub("[\r\n]+", "\n", x, useBytes = TRUE))
229227
}
230228

231229
#' @importFrom cli is_dynamic_tty
232230

233-
detect_callback <- function() {
234-
if (cli::is_dynamic_tty()) block_callback() else simple_callback()
231+
detect_callback <- function(...) {
232+
if (cli::is_dynamic_tty()) block_callback(...) else simple_callback(...)
235233
}
236234

237235
should_add_spinner <- function() {

R/package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ do_check <- function(targz, package, args, libpath, repos,
208208
user_profile = TRUE,
209209
repos = repos,
210210
stderr = "2>&1",
211-
block_callback = if (!quiet) detect_callback(),
211+
block_callback = if (!quiet) detect_callback(as_cran = "--as-cran" %in% args),
212212
spinner = !quiet && should_add_spinner(),
213213
timeout = timeout,
214214
fail_on_status = FALSE

tests/testthat/_snaps/callback.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,41 @@
532532
[71] ""
533533
[72] "Status: OK"
534534

535+
# multiple tests
536+
537+
Code
538+
out
539+
Output
540+
[1] " \r- checking examples ... NONE"
541+
[2] "\r \rv checking for unstated dependencies in ‘tests’"
542+
[3] "\r \r- checking tests"
543+
[4] "\r \r\r \r- Running ‘a.R’"
544+
[5] "\r \r- Running ‘b.R’"
545+
[6] "\r \r- Running ‘c.R’"
546+
[7] "E Some test files failed"
547+
[8] "\r \r Running the tests in ‘tests/b.R’ failed."
548+
[9] "\r \r Complete output:"
549+
[10] "\r \r > stop(\"error\")"
550+
[11] "\r \r Error: error"
551+
[12] "\r \r Execution halted"
552+
[13] "\r \rv checking PDF version of manual"
553+
[14] "\r"
554+
555+
---
556+
557+
Code
558+
out
559+
Output
560+
[1] " \r- checking examples ... NONE"
561+
[2] "\r \rv checking for unstated dependencies in ‘tests’"
562+
[3] "\r \r- checking tests"
563+
[4] "\r \r\r \rv Running ‘a.R’"
564+
[5] "\r \rE Running ‘b.R’"
565+
[6] "\r \r Running the tests in ‘tests/b.R’ failed."
566+
[7] "\r \r Complete output:"
567+
[8] "\r \r > stop(\"error\")"
568+
[9] "\r \r Error: error"
569+
[10] "\r \r Execution halted"
570+
[11] "\r \rv checking PDF version of manual"
571+
[12] "\r"
572+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
* checking examples ... NONE
2+
* checking for unstated dependencies in ‘tests’ ... OK
3+
* checking tests ...
4+
Running ‘a.R’
5+
Running ‘b.R’
6+
Running ‘c.R’
7+
ERROR
8+
Running the tests in ‘tests/b.R’ failed.
9+
Complete output:
10+
> stop("error")
11+
Error: error
12+
Execution halted
13+
* checking PDF version of manual ... OK
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
* checking examples ... NONE
2+
* checking for unstated dependencies in ‘tests’ ... OK
3+
* checking tests ...
4+
Running ‘a.R’
5+
Running ‘b.R’
6+
ERROR
7+
Running the tests in ‘tests/b.R’ failed.
8+
Complete output:
9+
> stop("error")
10+
Error: error
11+
Execution halted
12+
* checking PDF version of manual ... OK

tests/testthat/test-callback.R

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ test_that("multiple test file run times are measured properly", {
127127

128128
test_that("multi-arch test cases", {
129129
txt <- list(
130-
list("* using log directory 'C:/Users/csard/works/ps.Rcheck'\n", 0.01),
130+
list("* using log directory 'C:/Users/csard/works/ps.Rcheck'\n", 0.01),
131131
list("* using R version 4.1.1 (2021-08-10)\n", 0.01),
132132
list("* using platform: x86_64-w64-mingw32 (64-bit)\n", 0.01),
133133
list("* using session charset: ISO8859-1\n", 0.01),
@@ -173,7 +173,7 @@ test_that("partial comparing line", {
173173
list(" Comparing ‘test-2.Rout’ to ‘test-2.Rout.save’ ... OK\n", 0),
174174
list(" OK\n", 0.01),
175175
list("* checking PDF version of manual ... OK\n", 0.01),
176-
list("* DONE\n", 0.01)
176+
list("* DONE\n", 0.01)
177177
)
178178

179179
out <- capture.output(replay(lines))
@@ -184,7 +184,7 @@ test_that("multiple comparing blocks", {
184184
txt <- readLines(test_path("fixtures", "checks", "comparing2.txt"))[53:88]
185185
lines <- lapply(txt, function(x) list(paste0(x, "\n"), 0.001))
186186
out <- capture.output(replay(lines))
187-
expect_snapshot(out)
187+
expect_snapshot(out)
188188
})
189189

190190
test_that("simple_callback", {
@@ -200,7 +200,7 @@ test_that("detect_callback", {
200200

201201
withr::local_options(cli.dynamic = TRUE)
202202
expect_equal(detect_callback(), "block")
203-
203+
204204
withr::local_options(cli.dynamic = FALSE)
205205
expect_equal(detect_callback(), "simple")
206206
})
@@ -211,3 +211,16 @@ test_that("should_add_spinner", {
211211
withr::local_options(cli.dynamic = FALSE)
212212
expect_false(should_add_spinner())
213213
})
214+
215+
test_that("multiple tests", {
216+
txt <- readLines(test_path("fixtures", "tests-as-cran.txt"))
217+
lines <- lapply(txt, function(x) list(paste0(x, "\n"), 0.001))
218+
cb <- function(...) block_callback(as_cran = TRUE, ...)
219+
out <- capture.output(replay(lines, callback = cb))
220+
expect_snapshot(out)
221+
222+
txt <- readLines(test_path("fixtures", "tests-not-as-cran.txt"))
223+
lines <- lapply(txt, function(x) list(paste0(x, "\n"), 0.001))
224+
out <- capture.output(replay(lines))
225+
expect_snapshot(out)
226+
})

0 commit comments

Comments
 (0)