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

expect_error and friends report unused arguments for fixed when used interactively but not when used non-interactively #1932

Closed
billdenney opened this issue Feb 15, 2024 · 3 comments · Fixed by #2014
Labels
3e 📚 feature a feature request or enhancement

Comments

@billdenney
Copy link
Contributor

When I ran the code below interactively, I got the following warning:

Error: 1 did not throw the expected warning.
Warning in expect_warning(1, regexp = "foo", fixed = TRUE) :
  Arguments in `...` must be used.
✖ Problematic argument:
• fixed = TRUE
ℹ Did you misspell an argument name?

But when run in reprex, I don't see the warning about fixed. Why is there different behavior for interactive vs non-interactive use for argument matching?

library(testthat)

expect_message(
  message("foo"),
  regexp = "foo",
  fixed = TRUE
)
  
expect_message(
  1,
  regexp = "foo",
  fixed = TRUE
)
#> Error: 1 did not produce any messages.

expect_warning(
  warning("foo"),
  regexp = "foo",
  fixed = TRUE
)

expect_warning(
  1,
  regexp = "foo",
  fixed = TRUE
)
#> Error: 1 did not produce any warnings.

expect_error(
  stop("foo"),
  regexp = "foo",
  fixed = TRUE
)

expect_error(
  1,
  regexp = "foo",
  fixed = TRUE
)
#> Error: 1 did not throw an error.

Created on 2024-02-15 with reprex v2.1.0

@hadley
Copy link
Member

hadley commented Apr 17, 2024

Somewhat more minimal reprex, but must be run interactively:

testthat::expect_message(1, "x", fixed = TRUE)

This is going to be a fun one to debug!

@billdenney
Copy link
Contributor Author

I have isolated the issue to the edition_get() function.

When run interactively, edition_get() looks for the edition in the current directory, and in my case, it gets edition 3 because I'm using that in the current package. When run via reprex(), it is running in a different directory, so it gets the default edition 2.

The specific way that I isolated the error was to put a stop() before and after this line:

if (edition_get() >= 3) {

The stop() before that line triggered both interactively and with reprex(). The stop() after that line (inside the if block), triggered interactively but not with reprex().

I don't see any good fix. The ones that I think of are:

  1. To make reprex::reprex() aware of testthat by using local_edition() before changing the working directory.
  2. To make reprex::reprex() temporarily set some environmental variable indicating what the original directory was before changing to its new working directory (wd). And then, make edition_get() look for that new environmental variable after its current ways of looking.
  3. Do nothing, but then any edition-specific differences will always have different behavior in reprex() compared to when run either in tests or interactively.

@hadley
Copy link
Member

hadley commented Oct 22, 2024

Ok, here's a reprex that succinctly illustrates the problem:

testthat::local_edition(2)
testthat::expect_message(1, "x", fixed = TRUE)
#> Error: 1 did not produce any messages.

testthat::local_edition(3)
testthat::expect_message(1, "x", fixed = TRUE)
#> Error: 1 did not throw the expected message.
#> Warning in testthat::expect_message(1, "x", fixed = TRUE): Arguments in `...` must be used.
#> ✖ Problematic argument:
#> • fixed = TRUE
#> ℹ Did you misspell an argument name?

Created on 2024-10-22 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3e 📚 feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants