Closed njtierney closed 2 years ago
I think something like this should work:
have_inla <- function(){
x <- requireNamespace("INLA", quietly = TRUE)
x
}
have_inla()
yahtsee_sitrep <- function(){
cli::cli_process_start("checking if {.pkg INLA} available")
# if the software is detected
if (!have_inla()) {
cli::cli_process_failed(
msg_failed = "{.pkg INLA} not available"
)
cli::cli_alert(
text = "{.pkg INLA} is not available, you can install it \\
by running the helper function:
{.code install_yahtsee_deps()}
or by running:
{.code install.packages('INLA', repos = 'https://inla.r-inla-download.org/R/testing')}"
)
if (software_available) {
# if it has a version and ideal version
cli::cli_process_done(
msg_done = "{.pkg INLA} available"
)
}
}
check_if_inla_available()
The next steps will be to:
yahtsee_sitrep()
yahtsee_sitrep()
- see the greta_sitrep()
tests for an example. The idea with mocking is that we need a way to test how code behaves under specific conditions, but replicating those conditions is hard.
In our case, we want to test what happens with INLA isn't installed - but we don't want to go through the pain of removing INLA and then testing this condition. Instead, we can make have_inla
return FALSE, which should trigger the warning message we've provided in the function.
The test code will probably look something like this:
test_that("yahtsee_sitrep warns when have_inla is FALSE", {
mockery::stub(yahtsee_sitrep, 'have_yahtsee', FALSE)
# I am not sure if you need to add "2" here - this tells it how far down to go into the function
# mockery::stub(yahtsee_sitrep, 'have_yahtsee', FALSE, 2)
expect_snapshot(
yahtsee_sitrep()
)
As stated here: https://ropensci.org/blog/2021/09/30/ropensci-news-digest-september-2021/#package-development-corner by Maëlle:
This could check if the INLA packages are installed, perhaps: