I'm working on a project, where I load a json file. By default, I start the project at the root of the project, however when testing, the path changes. Therefore I included the following statement:
if (grepl("testthat", getwd())) {
template <- jsonlite::read_json("../../templates/initial_status.json")
}else{
template <- jsonlite::read_json("./templates/initial_status.json")
}
This works, however, when using covr::gitlab()
the file cannot be opened. I guess the working directory changed and therefore the file is not found:
Error in `open.connection(con, "rb")`: kann Verbindung nicht öffnen
Backtrace:
x
1. \-MyPackage::api_create(...) at test-API.R:40:2
2. \-MyPackage:::get_initial_status(initial_status)
3. \-jsonlite::read_json("../../templates/initial_status.json")
Of course a working around is to use setwd(...)
however I don't like this, because I want to publish the package and therefore user may place the package in different locations.
What's the best solution here?
It seems to execute from a random temporary directory like this: "/private/var/folders/wr/_qxy_jbd2kg1t72cr7d89_jc0000gn/T/RtmptlUqd8/R_LIBS171fb7c114d3c/pkg_name/pkg_name-tests/testthat"
I stumbled upon this because it quite un-intuitively makes tests fail which do not fail in context if they rely on something with a path (e.g. a call to load in global variables from a file, etc.)
I just tried this by adding a test like:
and upon running
covr::package_coverage()
got:Echoing
here::here()
instead will return something like/private/var/folders/wr/_qxy_jbd2kg1t72cr7d89_jc0000gn/T/RtmptlUqd8/R_LIBS171fb7c114d3c/pkg_name
so I assume it copies the package directory in entirety somewhere and maybe it can help you.Unfortunately it doesn't help at all if you rely on resources in a folder above your package folder.