Skip to content

Commit 574e249

Browse files
committed
PoC of markdown summary on GHA
1 parent 2f48ca6 commit 574e249

13 files changed

+137
-10
lines changed

R/gha-summary.R

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
create_gha_summary <- function(results) {
3+
nope <- c("false", "no", "off", "n", "0", "nope", "nay")
4+
if (tolower(Sys.getenv("TESTTHAT_GHA_SUMMARY")) %in% nope) {
5+
return()
6+
}
7+
if ((out <- Sys.getenv("GITHUB_STEP_SUMMARY")) == "") {
8+
return()
9+
}
10+
11+
p <- function(...) cat(..., file = out, append = TRUE)
12+
13+
# header
14+
p("# Test results\n\n")
15+
p("| FAIL | WARN | SKIP | PASS | Context | Test | Time |\n")
16+
p("|-----:|-----:|-----:|-----:|:--------|:-----|:-----|\n")
17+
18+
# one line per test
19+
per_test <- lapply(results, gha_file_summary, p = p)
20+
21+
# totals
22+
totals <- lapply(do.call(rbind, per_test), sum)
23+
p(paste0(
24+
"|", totals$fail,
25+
"|", totals$warn,
26+
"|", totals$skip,
27+
"|", totals$ok,
28+
"|", "Total",
29+
"|", "",
30+
"|", sprintf("%.3f s", totals$time),
31+
"|\n"
32+
))
33+
34+
invisible(results)
35+
}
36+
37+
gha_file_summary <- function(result, p) {
38+
39+
n_fail <- n_skip <- n_warn <- n_ok <- 0L
40+
for (exp in result$results) {
41+
if (expectation_broken(exp)) {
42+
n_fail <- n_fail + 1L
43+
} else if (expectation_skip(exp)) {
44+
n_skip <- n_skip + 1L
45+
} else if (expectation_warning(exp)) {
46+
n_warn <- n_warn + 1L
47+
} else {
48+
n_ok <- n_ok + 1L
49+
}
50+
}
51+
52+
ctx <- context_name(result$file)
53+
time <- sprintf("%.3f s", result$real)
54+
55+
escape <- function(x) {
56+
x <- gsub("|", "\\|", x, fixed = TRUE)
57+
x <- gsub("\n", " ", x, fixed = TRUE)
58+
x
59+
}
60+
61+
p(paste0(
62+
"|", n_fail,
63+
"|", n_warn,
64+
"|", n_skip,
65+
"|", n_ok,
66+
"|", escape(ctx),
67+
"|", escape(result$test),
68+
"|", time,
69+
"|\n"
70+
))
71+
72+
data.frame(
73+
stringsAsFactors = FALSE,
74+
fail = n_fail,
75+
skip = n_skip,
76+
warn = n_warn,
77+
ok = n_ok,
78+
time = result$real
79+
)
80+
}

R/parallel.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ test_files_parallel <- function(
7979
}
8080
})
8181

82+
create_gha_summary(reporters$list$get_results())
83+
8284
test_files_check(reporters$list$get_results(),
8385
stop_on_failure = stop_on_failure,
8486
stop_on_warning = stop_on_warning

R/test-files.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ test_files_serial <- function(test_dir,
204204
lapply(test_paths, test_one_file, env = env, wrap = wrap)
205205
)
206206

207+
create_gha_summary(reporters$list$get_results())
208+
207209
test_files_check(reporters$list$get_results(),
208210
stop_on_failure = stop_on_failure,
209211
stop_on_warning = stop_on_warning

tests/testthat/test-parallel-crash.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ test_that("crash", {
44

55
skip_on_cran()
66
skip_on_covr()
7-
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
7+
withr::local_envvar(c(
8+
TESTTHAT_PARALLEL = "TRUE",
9+
TESTTHAT_GHA_SUMMARY = "FALSE"
10+
))
811

912
do <- function() {
1013
err <- NULL

tests/testthat/test-parallel-outside.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
test_that("error outside of test_that()", {
3-
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
3+
withr::local_envvar(c(
4+
TESTTHAT_PARALLEL = "TRUE",
5+
TESTTHAT_GHA_SUMMARY = "FALSE"
6+
))
47
err <- tryCatch(
58
suppressMessages(testthat::test_local(
69
test_path("test-parallel", "outside"),

tests/testthat/test-parallel-setup.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11

22
test_that("error in parallel setup code", {
33
skip_on_covr()
4-
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
4+
withr::local_envvar(c(
5+
TESTTHAT_PARALLEL = "TRUE",
6+
TESTTHAT_GHA_SUMMARY = "FALSE"
7+
))
58
err <- tryCatch(
69
suppressMessages(testthat::test_local(
710
test_path("test-parallel", "setup"),

tests/testthat/test-parallel-startup.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11

22
test_that("startup error", {
33
skip_on_covr()
4-
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
4+
withr::local_envvar(c(
5+
TESTTHAT_PARALLEL = "TRUE",
6+
TESTTHAT_GHA_SUMMARY = "FALSE"
7+
))
58
err <- tryCatch(
69
suppressMessages(testthat::test_local(
710
test_path("test-parallel", "startup"),

tests/testthat/test-parallel-teardown.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11

22
test_that("teardown error", {
33
skip("teardown errors are ignored")
4-
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
4+
withr::local_envvar(c(
5+
TESTTHAT_PARALLEL = "TRUE",
6+
TESTTHAT_GHA_SUMMARY = "FALSE"
7+
))
58
err <- tryCatch(
69
suppressMessages(testthat::test_local(
710
test_path("test-parallel", "teardown"),

tests/testthat/test-parallel.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ test_that("detect number of cpus to use", {
2323
})
2424

2525
test_that("ok", {
26-
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
26+
withr::local_envvar(c(
27+
TESTTHAT_PARALLEL = "TRUE",
28+
TESTTHAT_GHA_SUMMARY = "FALSE"
29+
))
2730
suppressMessages(ret <- test_local(
2831
test_path("test-parallel", "ok"),
2932
reporter = "silent",

tests/testthat/test-reporter-list.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# regression test: test_file() used to crash with a NULL reporter
33
test_that("ListReporter with test_file and NULL reporter", {
4+
withr::local_envvar(TESTTHAT_GHA_SUMMARY = "FALSE")
45
test_file_path <- 'test-list-reporter/test-exercise-list-reporter.R'
56
expect_error(test_file(test_path(test_file_path), reporter = NULL), NA)
67
})
@@ -9,6 +10,7 @@ test_that("ListReporter with test_file and NULL reporter", {
910
# of a test (test_that() call).
1011
# N.B: the exception here happens between two tests: "before" and "after"
1112
test_that("ListReporter - exception outside of test_that()", {
13+
withr::local_envvar(TESTTHAT_GHA_SUMMARY = "FALSE")
1214
test_file_path <- 'test-list-reporter/test-exception-outside-tests.R'
1315
res <- test_file(test_path(test_file_path), reporter = NULL)
1416

@@ -31,6 +33,7 @@ test_that("ListReporter - exception outside of test_that()", {
3133

3234

3335
test_that("captures error if only thing in file", {
36+
withr::local_envvar(TESTTHAT_GHA_SUMMARY = "FALSE")
3437
test_file_path <- 'test-list-reporter/test-only-error.R'
3538
res <- test_file(test_path(test_file_path), reporter = NULL)
3639

@@ -40,6 +43,7 @@ test_that("captures error if only thing in file", {
4043

4144
# ListReporter on a "standard" test file: 2 contexts, passing, failing and crashing tests
4245
test_that("exercise ListReporter", {
46+
withr::local_envvar(TESTTHAT_GHA_SUMMARY = "FALSE")
4347
test_file_path <- 'test-list-reporter/test-exercise-list-reporter.R'
4448
res <- test_file(test_path(test_file_path), reporter = NULL)
4549
expect_s3_class(res, "testthat_results")
@@ -60,6 +64,7 @@ test_that("exercise ListReporter", {
6064

6165
# bare expectations are ignored
6266
test_that("ListReporter and bare expectations", {
67+
withr::local_envvar(TESTTHAT_GHA_SUMMARY = "FALSE")
6368
test_file_path <- 'test-list-reporter/test-bare-expectations.R'
6469
res <- test_file(test_path(test_file_path), reporter = NULL)
6570

tests/testthat/test-snapshot-reporter.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ test_that("errors in test doesn't change snapshot", {
144144
})
145145

146146
test_that("skips and unexpected errors reset snapshots", {
147+
withr::local_envvar(TESTTHAT_GHA_SUMMARY = "FALSE")
147148
regenerate <- FALSE
148149

149150
if (regenerate) {
@@ -166,6 +167,7 @@ test_that("skips and unexpected errors reset snapshots", {
166167
})
167168

168169
test_that("`expect_error()` can fail inside `expect_snapshot()`", {
170+
withr::local_envvar(TESTTHAT_GHA_SUMMARY = "FALSE")
169171
out <- test_file(
170172
test_path("test-snapshot", "test-expect-condition.R"),
171173
reporter = NULL

tests/testthat/test-teardown.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ test_that("teardowns runs in order", {
2626
})
2727

2828
test_that("teardown run after tests complete", {
29+
withr::local_envvar(TESTTHAT_GHA_SUMMARY = "FALSE")
2930
test_file(test_path("test-teardown/test-teardown.R"), "silent")
3031
expect_false(file.exists(test_path("test-teardown/teardown.txt")))
3132
})

tests/testthat/test-test-files.R

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# test_dir() --------------------------------------------------------------
22

33
test_that("stops on failure", {
4-
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
4+
withr::local_envvar(c(
5+
TESTTHAT_PARALLEL = "TRUE",
6+
TESTTHAT_GHA_SUMMARY = "FALSE"
7+
))
58
expect_error(
69
test_dir(test_path("test_dir"), reporter = "silent")
710
)
811
})
912

1013
test_that("runs all tests and records output", {
11-
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
14+
withr::local_envvar(c(
15+
TESTTHAT_PARALLEL = "TRUE",
16+
TESTTHAT_GHA_SUMMARY = "FALSE"
17+
))
1218
res <- test_dir(test_path("test_dir"), reporter = "silent", stop_on_failure = FALSE)
1319
df <- as.data.frame(res)
1420
df$user <- df$system <- df$real <- df$result <- NULL
@@ -27,7 +33,10 @@ test_that("complains if no files", {
2733
})
2834

2935
test_that("can control if failures generate errors", {
30-
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
36+
withr::local_envvar(c(
37+
TESTTHAT_PARALLEL = "TRUE",
38+
TESTTHAT_GHA_SUMMARY = "FALSE"
39+
))
3140
test_error <- function(...) {
3241
test_dir(test_path("test-error"), reporter = "silent", ...)
3342
}
@@ -37,7 +46,11 @@ test_that("can control if failures generate errors", {
3746
})
3847

3948
test_that("can control if warnings errors", {
40-
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
49+
withr::local_envvar(c(
50+
TESTTHAT_PARALLEL = "TRUE",
51+
TESTTHAT_GHA_SUMMARY = "FALSE"
52+
))
53+
4154
test_warning <- function(...) {
4255
test_dir(test_path("test-warning"), reporter = "silent", ...)
4356
}
@@ -49,6 +62,10 @@ test_that("can control if warnings errors", {
4962
# test_file ---------------------------------------------------------------
5063

5164
test_that("can test single file", {
65+
withr::local_envvar(c(
66+
TESTTHAT_PARALLEL = "TRUE",
67+
TESTTHAT_GHA_SUMMARY = "FALSE"
68+
))
5269
out <- test_file(test_path("test_dir/test-basic.R"), reporter = "silent")
5370
expect_length(out, 5)
5471
})

0 commit comments

Comments
 (0)