Skip to content

Commit

Permalink
Make it possible to suppress praise in StopReporter (#1872)
Browse files Browse the repository at this point in the history
As needed for devtools::test_coverage_active_file()
  • Loading branch information
hadley authored Sep 27, 2023
1 parent 356900d commit 442c8f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# testthat (development version)

* `StopReporter()` gains the ability to suppress praise when a test passes.

* `expect_snapshot_file()` now generates clickable links to review changes
(#1821).

Expand Down
10 changes: 7 additions & 3 deletions R/reporter-stop.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ StopReporter <- R6::R6Class("StopReporter",
failures = NULL,
n_fail = 0L,
stop_reporter = TRUE,
praise = TRUE,

initialize = function(stop_reporter = TRUE) {
initialize = function(stop_reporter = TRUE, praise = TRUE) {
super$initialize()
self$failures <- Stack$new()
self$praise <- praise
self$stop_reporter <- stop_reporter
},

Expand All @@ -43,13 +45,15 @@ StopReporter <- R6::R6Class("StopReporter",
self$local_user_output()

failures <- self$failures$as_list()
if (length(failures) == 0) {
if (length(failures) == 0 && self$praise) {
self$cat_line(colourise("Test passed", "success"), " ", praise_emoji())
return()
}

messages <- vapply(failures, issue_summary, rule = TRUE, character(1))
self$cat_line(messages, "\n")
if (length(messages) > 0) {
self$cat_line(messages, "\n")
}
},
stop_if_needed = function() {
if (self$stop_reporter && self$n_fail > 0) {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/reporter-stop.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@
Reason: empty test


# can suppress praise



7 changes: 7 additions & 0 deletions tests/testthat/test-reporter-stop.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ test_that("produces useful output", {
expect_snapshot_reporter(StopReporter$new())
})

test_that("can suppress praise", {
expect_snapshot_reporter(
StopReporter$new(praise = FALSE),
test_path("reporters/successes.R")
)
})

test_that("stop if needed errors when needed",{
r <- StopReporter$new()
expect_error(r$stop_if_needed(), NA)
Expand Down

0 comments on commit 442c8f3

Please sign in to comment.