Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better error messages for failed tests #1424

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/testthat/helper_localSystem2.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ localSystem2 <- function(command, args = character(),
if (is.null(attr(output, 'status', exact = TRUE))) {
attr(output, 'status') <- 0
}

# include command and arguments in output for pretty errors
attr(output, 'command') <- command
attr(output, 'args') <- args

return(output)
}
60 changes: 50 additions & 10 deletions tests/testthat/helper_skipFailed.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,59 @@
# | Contact: remind@pik-potsdam.de
helperSkipFailed <- FALSE

expectSuccessStatus <- function(output) {
status <- attr(output, "status", exact = TRUE)
if (0 != status) {
helperSkipFailed <<- TRUE
expect_exit_status_n <- function(object, n = 0, invert = FALSE) {
act <- quasi_label(rlang::enquo(object), arg = 'object')

status <- attr(act[['val']], 'status', exact = TRUE)

if (all(c('command', 'args') %in% names(attributes(act[['val']])))) {
label <- paste0('`', attr(act[['val']], 'command', exact = TRUE), ' ',
paste(attr(act[['val']], 'args', exact = TRUE),
collapse = ' '),
'`')
}
else {
label <- act[['lab']]
}

# empty trace to suppress testthat backtrace
empty_trace <- structure(
list(call = list(),
parent = integer(0),
visible = logical(0),
namespace = character(0),
scope = character(0)),
row.names = integer(0),
version = 2L,
class = c('rlang_trace', 'rlib_trace', 'tbl', 'data.frame'))

if (isFALSE(invert)) {
if (n != status)
helperSkipFailed <<- TRUE

expect(n == status,
sprintf('%s returned exit status %i, not %i', label, status, n),
trace = empty_trace)
}
expect_equal(status, 0)
else {
if (n == status)
helperSkipFailed <<- TRUE

expect(n != status,
sprintf('%s returned exit status %i, which it should not',
label, status),
trace = empty_trace)
}

invisible(act[['val']])
}

expectSuccessStatus <- function(output) {
expect_exit_status_n(output, 0, FALSE)
}

expectFailStatus <- function(output) {
status <- attr(output, "status", exact = TRUE)
if (1 != status) {
helperSkipFailed <<- TRUE
}
expect_equal(status, 1)
expect_exit_status_n(output, 0, TRUE)
}

skipIfPreviousFailed <- function() {
Expand Down
Loading