test_path()
works when wd is either top-level directory or tests/testhat
. And that's it.
I was trying to use it from a vignette because a token file I need is in tests/testthat
. I already have testthat
in Suggests (but not devtools
), so thought I'd build a path with testthat::test_path()
instead of devtools::package_file()
, but:
getwd()
#> [1] "/Users/jenny/rrr/googlesheets/vignettes"
testthat::test_path("googlesheets_token.rds")
#> Error: Can't find `tests/testthat/` in current directory.
Same error with devtools::document()
.
I know this is not what testthat::test_path()
was designed for. I'm just trying to figure how to specify a path that [1] doesn't require manually changing wd during interactive development, [2] works when the vignette gets rendered with other machinery, and [3] doesn't add another dependency, e.g. devtools
.
If it needs to be used with vignettes and tests, I'd wonder about putting the token somewhere else. I wonder we need some conventions around putting it in inst/
?
You could be right.I did note that tests
is a good location because even if you screw up and include in package bundle, it won't make it into the binary package? But this is neither here nor there w/r/t my attempt to get creative with test_path()
.
Why not have an implementation like e.g.
test_path <- function(filename) {
file.path(find_pkg_root(), "tests/testthat", filename)
}
Where find_pkg_root()
just crawls directories upwards until it finds a directory with an appropriate DESCRIPTION
file?
(I feel like @krlmlr was just working on something related to this...)
Yeah, just need to cran the rprojroot package...
Yes there's rprojroot
馃憤 and this is also exactly what devtools::package_file()
does. I was just hoping to freeload on the fact that testthat
is already in Suggests. This wd stuff has become my second cause, second only to promoting rendered markdown on GitHub.
Side note: If you want to be totally sure not to release the token, put it somewhere under .git
. Or use Hadley's secure package (alas, not on CRAN yet)?
@krlmlr You've got rprojroot
on CRAN now and it's very lightweight. Maybe you have a nice solution for this now? Twitter just reminded me of this.
You can use
rprojroot::is_r_package$find_file("tests/testthat", "a")
but you have to specify the "tests/testthat" prefix. It might be possible to enhance make_fix_file()
to support a subdir
argument, so that you could run
test_file <- rprojroot::is_r_package$make_fix_file(subdir = "tests/testthat")
and then test_file()
just works. PR welcome ;-)
Files won't be found when tests are run from R CMD check, it uses a different directory structure.
@krlmlr I now know exactly what you meant by this 馃様:
Files won't be found when tests are run from R CMD check, it uses a different directory structure.
In a helper file I have
here <- rprojroot::find_package_root_file
TEST_DIR <- here("tests", "testthat")
and I use TEST_DIR
and here()
elsewhere in the tests. All is well until I run R CMD check, at which point I get this error:
> test_check("testtest")
Error: No root directory found. Test criterion:
Contains a file 'DESCRIPTION' with contents matching '^Package: '
So is there a criterion that will work when you're working in your source package, when you run devtools::test()
, AND during R CMD check? I'm 2 for 3 right now.
Should be done in rprojroot.
I can't really find much about this, and this is one of the few places that mentions rprojroot
, so I'm giving this a shot..
When I try to use root <- rprojroot::find_package_root_file
from within a test file to jump to the root and then select my test data in /data/
, R CMD CHECK hands me a Error in library(rprojroot) : there is no package called 'rprojroot'
.
I have absolutely no clue why, since I can do this when I run the tests separately with devtools::test()
Any suggestions?
Have you installed the rprojroot package? You should also add it to the "Suggests" of your package.
Thanks for the quick reply on such an old issue. I did not know about adding it to the "Suggests" in DESCRIPTION. That did the trick, thanks!
On a side note, could you explain how R CMD CHECK interprets this? Since I can't explain how it magically appends .Rcheck/test/testthat
to the root of the package.
Error: No root directory found in /Users/someone/git/leqtar.Rcheck/tests/testthat or its parent directories. Root criterion: contains a file DESCRIPTION
with contents matching ^Package:
(I googled for this .Rcheck directory, which should stay if I turn off clean up after RMD check, but I don't have this. Can that be the case?)
Please review the section "testthat files" section in the vignette.
@ErikSchutte I use rprojroot to make a large set of spreadsheets available to the tests in readxl. Maybe that is a helpful example of real world use:
https://github.com/tidyverse/readxl/blob/master/tests/testthat/helper.R
Thanks @krlmlr & @jennybc for the help. I figured it out.
Most helpful comment
@ErikSchutte I use rprojroot to make a large set of spreadsheets available to the tests in readxl. Maybe that is a helpful example of real world use:
https://github.com/tidyverse/readxl/blob/master/tests/testthat/helper.R