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

Issue finding `muffleWarning` when changing a warning to a condition #874

Closed
jimhester opened this issue May 3, 2019 · 1 comment · Fixed by #923

Comments

@jimhester
Copy link
Member

commented May 3, 2019

If I want to convert a warning to an error typically I would do something like the following.

library(testthat)
foo <- function() {
  warning("foo")
}

f <- function() {
  tryCatch(warning = function(e) stop(e), foo())
}
f()
#> Error in foo() : foo

However if I then try to use this in testthat we get a strange errors due to missing restarts.

# this works interactively, but not in a reprex
expect_error(f())
#> Warning in foo(): foo
#> Error in invokeRestart("muffleWarning"): no 'restart' 'muffleWarning' found

# this does not
test_that("blah", {
  expect_error(f())
})
#> Error: Test failed: 'blah'
#> * no 'restart' 'muffleWarning' found
#> 1: expect_error(f()) at <text>:15
#> 2: quasi_capture(enquo(object), label, capture_error)
#> 3: .capture(act$val <- eval_bare(get_expr(.quo), get_env(.quo)), ...)
#> 4: tryCatch({
#>        code
#>        NULL
#>    }, error = function(e) e)
#> 5: tryCatchList(expr, classes, parentenv, handlers)
#> 6: tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 7: doTryCatch(return(expr), name, parentenv, handler)
#> 8: eval_bare(get_expr(.quo), get_env(.quo))
#> 9: f()
#> 10: tryCatch(warning = function(e) stop(e), foo()) at <text>:7
#> 11: tryCatchList(expr, classes, parentenv, handlers)
#> 12: tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 13: value[[3L]](cond)
#> 14: stop(e) at <text>:7
#> 15: (function (e) 
#>    {
#>        if (getOption("warn") >= 2) 
#>            return()
#>        handled <<- TRUE
#>        e$expectation_calls <- frame_calls(11, 5)
#>        register_expectation(e)
#>        invokeRestart("muffleWarning")
#>    })(structure(list(message = "foo", call = foo()), class = c("simpleWarning", 
#>    "warning", "condition")))
#> 16: invokeRestart("muffleWarning")
#> 17: stop(gettextf("no 'restart' '%s' found", as.character(r)), domain = NA)
@jimhester jimhester added the bug label May 3, 2019
@hadley hadley added the conditions label Jul 15, 2019
lionel- added a commit to lionel-/testthat that referenced this issue Aug 9, 2019
@hadley

This comment has been minimized.

Copy link
Member

commented Aug 9, 2019

This is a nice example of a big backtrace win!

library(testthat)
foo <- function() {
  warning("foo")
}

f <- function() {
  tryCatch(warning = function(e) stop(e), foo())
}
f()
#> Warning in foo(): foo
#> Error in invokeRestart("muffleWarning"): no 'restart' 'muffleWarning' found

test_that("blah", {
  expect_error(f())
})
#> Error: Test failed: 'blah'
#> * no 'restart' 'muffleWarning' found
#> Backtrace:
#>   1. testthat::expect_error(f())
#>  13. base::invokeRestart("muffleWarning")

Created on 2019-08-09 by the reprex package (v0.3.0)

@lionel- lionel- closed this in #923 Aug 9, 2019
lionel- added a commit that referenced this issue Aug 9, 2019
Closes #874
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.