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

Is calling test_path() at a global level within a helper-*.R file not supported? #1562

Closed
DavisVaughan opened this issue Feb 1, 2022 · 6 comments · Fixed by #1760
Closed

Comments

@DavisVaughan
Copy link
Member

In the censored package we had a helper- file that loaded some helper data objects with test_path() and readRDS(). Like this:
https://github.com/tidymodels/censored/pull/145/files#diff-86973057dbc44244492fcd424b56710d5bccdab7c751ca7d469b4c744f85606aL1

It turns out that this doesn't work. You can easily reproduce with this repo:
https://github.com/DavisVaughan/testthathelperpath

Just do load_all(), which errors with:

> devtools::load_all(".")
ℹ Loading testthathelperpath
Error in `test_path()`:
! Can't find `tests/testthat/` in current directory.
Run `rlang::last_error()` to see where the error occurred.

> rlang::last_trace()
<error/rlang_error>
Error in `test_path()`:
! Can't find `tests/testthat/` in current directory.
Backtrace:1. └─devtools::load_all(".")
  2.   └─pkgload::load_all(...)
  3.     ├─withr withr_with_envvar(...)
  4.     │ └─base::force(code)
  5.     └─testthat testthat_source_test_helpers(find_test_dir(path), env = pkg_env(package))
  6.       └─testthat::source_dir(path, "^helper.*\\.[rR]$", env = env, wrap = FALSE)
  7.         └─base::lapply(files, source_file, env = env, chdir = chdir, wrap = wrap)
  8.           └─testthat FUN(X[[i]], ...)
  9.             └─base::eval(exprs, env)
 10.               └─base::eval(exprs, env)
 11.                 ├─base::readRDS(test_path("data", "data.rds")) at tests/testthat/helper-load.R:2:0
 12.                 └─testthat::test_path("data", "data.rds")
 13.                   └─rlang::abort("Can't find `tests/testthat/` in current directory.")

There are lots of ways to work around this, but I was a bit surprised that this didn't work so we figured it was best to open an issue

@gaborcsardi
Copy link
Member

gaborcsardi commented Feb 1, 2022

That might not work, depending which test function you use. E.g. devtools::test() sources the helper files before calling testthat, so testthat couldn't have had time to set up the package, paths, etc. yet.

@DavisVaughan
Copy link
Member Author

Do you think it is a bug? Maybe a devtools bug? If not, I am happy to close this since we have workarounds

@gaborcsardi
Copy link
Member

I don't think a bug, it is a consequence of how devtools + testthat work together. It would also be rather inconvenient to consider it a bug, because I am not sure if there is a way to fix it.

@DavisVaughan
Copy link
Member Author

That is what I figured, good enough for me!

@DanChaltiel
Copy link

DanChaltiel commented Mar 21, 2023

@gaborcsardi I encounter this too.
If you really want to consider it not a bug, could we say that this is at least an unexpected behavior?
I think it would be worth documenting in ?test_path where it works and where it does not.

@gaborcsardi
Copy link
Member

gaborcsardi commented Mar 21, 2023

Its manual says:

This function is designed to work both interactively and during
tests, locating files in the ‘tests/testthat’ directory

I guess we could say that it works in test files, but not in helper files. Do you want to submit a PR?

hadley added a commit that referenced this issue Mar 29, 2023
And generally improve code, tests, and docs. Closes #1756. Fixes #1562.
hadley added a commit that referenced this issue Apr 5, 2023
And generally improve code, tests, and docs. Closes #1756. Fixes #1562.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants