njtierney / yahtsee

Yet Another Hierachical Time Series Extension and Expansion
http://yahtsee.njtierney.com/
Other
2 stars 1 forks source link

Add `sitrep` function for people to test if they have all system requirements installed #50

Closed njtierney closed 2 years ago

njtierney commented 3 years ago

As stated here: https://ropensci.org/blog/2021/09/30/ropensci-news-digest-september-2021/#package-development-corner by Maëlle:

...[consider] adding some sort of sitrep (situation report) function...the devtools package has devtools::dev_sitrep() e.g. reports on the package development situation, with clear hints given if something is not quite right. The usethis package has usethis::git_sitrep() (using the gert package under the hood!), blogdown has blogdown::check_site(). Good candidates for checks are common pain points, so finding them might require some sort of external perspective on your package.

This could check if the INLA packages are installed, perhaps:

check_sitrep()
# or
check_deps()
njtierney commented 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:

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()
  )