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

test_that expect_silent fails with sink #1480

Closed
lcougnaud opened this issue Oct 27, 2021 · 2 comments
Closed

test_that expect_silent fails with sink #1480

lcougnaud opened this issue Oct 27, 2021 · 2 comments

Comments

@lcougnaud
Copy link

lcougnaud commented Oct 27, 2021

I use sink to export the messages (message/warning/error) generated by R function to a log file in a R package (via a specific parameter logFile).
I would expect expect_silent to return TRUE if the output is exported to a file with sink, as no message/warning/error are printed to the console.
However, expect_silent mentions that messages have been produced:

library(testthat)
myFct <- function(logFile = NULL){
  if(!is.null(logFile)){
      logFileCon <- file(logFile, "w");
      sink(file = logFileCon, type = "message");
      on.exit(sink(type="message"))
  }
  message("This is a test message")
}
testthat::expect_silent(myFct(logFile = "log.txt")))
Error: `myFct(logFile = "log.txt")` produced messages.

(This could be linked to #460)

@hadley
Copy link
Member

hadley commented Dec 20, 2021

Unfortunately I think it's going to be too difficult to tell if sink() is activated inside of expect_silent(), so you'll need to tackle this a different way.

@hadley hadley closed this as completed Dec 20, 2021
@pdbailey0
Copy link

I can reproduce this by sleeping my computer. The problem appears to be with the temporary file not being a valid path and not being opened in such a way that the containing folder is created. a tryCatch seems like it could fix this by simply creating the path. But the actual problem is that this file is not valid and that's not part of testthat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants