I'm writing a package that will operate on other projects (specifically, it will read, write and delete files within project folders). I need to write some tests for it which means creating temporary dummy project folders, testing my functions in them, and cleaning up afterwards. Basically I'm after some kind of withr::with_temporary_project()
function.
I've found a couple of approaches that work but the test output is full of messages about switching directories and I can't figure out how to get rid of them.
Attempt 1
I've come across a helper function in usethis
, create_local_thing()
, which seems to do exactly what I want. I've pinched the source code and dropped it into my tests (only modifying it to prefix functions with usethis::
where necessary).
Here's an example test:
# mypackage/tests/testthat/test-foo.R
test_that("foo is created", {
proj <- create_local_thing(thing = "project")
file.create("foo") # This should be in the root folder of my temp project
expect_true(file.exists("foo"))
})
And here's the output of running that test file:
==> Testing R file using 'testthat'
ℹ Loading mypackage
══ Testing test-foo.R ══════════════════════════════════════════════════════════
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 0 ]v Setting active project to '/home/lewin/git/mypackage'
v Setting active project to '/tmp/RtmpLjJnjV/file28d06f4f1bc5'
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ]v Restoring original working directory: '/home/lewin/git/mypackage/tests/testthat/'
v Setting active project to '/home/lewin/git/mypackage'
v Deleting temporary project: '/tmp/RtmpLjJnjV/file28d06f4f1bc5/'
Done!
Test complete
The output is full of messages about switching directories, which I believe come from the usethis::proj_*()
family.
I found an example use of this function in usethis/tests/testthat/test-helpers.R
, and running the tests for that file gives me this:
==> Testing R file using 'testthat'
ℹ Loading usethis
══ Testing test-helpers.R ══════════════════════════════════════════════════════
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 46 ] Done!
Test complete
I.e. nice clean output with no messages about changing project directories. How can I achieve the same result? Is there some kind of verbosity option?
I have tried wrapping the code in suppressMessages()
and usethis::ui_silence()
with no success. I assume I'm just using it wrong...
Attempt 2
I also experimented with making my own version:
with_temp_project <- function(code, quiet = TRUE) {
# Create a temporary directory
temp_proj <- tempdir()
on.exit(unlink(temp_proj, recursive = TRUE, force = TRUE))
# This makes it look like a project to `usethis`
file.create(file.path(temp_proj, ".here"))
# Run the code, quietly
usethis::ui_silence(usethis::with_project(temp_proj, code, quiet = quiet))
}
But ran into similar issues.
I guess I would like to know how other people writing packages-that-operate-on-packages have approached this. Any input gratefully received!
Thanks, Lewin
> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Manjaro Linux
Matrix products: default
BLAS: /usr/lib/libblas.so.3.10.0
LAPACK: /usr/lib/liblapack.so.3.10.0
locale:
[1] LC_CTYPE=en_NZ.UTF-8 LC_NUMERIC=C LC_TIME=en_NZ.UTF-8 LC_COLLATE=en_NZ.UTF-8 LC_MONETARY=en_NZ.UTF-8 LC_MESSAGES=en_NZ.UTF-8
[7] LC_PAPER=en_NZ.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_NZ.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] mypackage_0.1.0.9005 testthat_3.0.4
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 rstudioapi_0.13 xml2_1.3.2 knitr_1.33 roxygen2_7.1.1 magrittr_2.0.1 usethis_2.0.1 devtools_2.4.2 pkgload_1.2.1
[10] R6_2.5.0 rlang_0.4.11 fastmap_1.1.0 stringr_1.4.0 httr_1.4.2 tools_4.1.1 pkgbuild_1.2.0 xfun_0.25 sessioninfo_1.1.1
[19] cli_3.0.1 withr_2.4.2 ellipsis_0.3.2 remotes_2.4.0 yaml_2.2.1 rprojroot_2.0.2 lifecycle_1.0.0 crayon_1.4.1 processx_3.5.2
[28] purrr_0.3.4 callr_3.7.0 fs_1.5.0 ps_1.6.0 memoise_2.0.0 glue_1.4.2 cachem_1.0.5 stringi_1.7.3 compiler_4.1.1
[37] desc_1.3.0 prettyunits_1.1.1 renv_0.14.0
Thanks, Lewin