How to use spell_check_test() in a package?

I'm developing my first package and I noticed that usethis has a function, use_spell_check(), that adds a spell check to a package's tests. After running the function, it adds spelling.R to the tests folder and it contains the following:

if(requireNamespace('spelling', quietly = TRUE))
  spelling::spell_check_test(vignettes = TRUE, error = FALSE,
                             skip_on_cran = TRUE)

This all looks right, but I'm having trouble getting spelling::spell_check_test() to return any output. When I run it in the console, I get NULL. When I run devtools::check(), there is a green check next to spelling.R that indicates the script ran, but even with spelling mistakes, it doesn't seem to find them or print results. I know the spelling package is working correctly because it works if I run the following:

> spelling::spell_check_package()
  WORD  FOUND IN
aa   README.md:12
     README.Rmd:22

Is there a setting in devtools::check() I need to enable for the spell check to work as part of my tests?

By any chance, have you already configured a NOT_CRAN environment variable ?

  spelling::spell_check_test(vignettes = TRUE, error = FALSE,
                             skip_on_cran = TRUE)

has skip_on_cran = TRUE. This will check the behavior of NOT_CRAN env var before running any spell check. If not set to c("1", "TRUE", "true", "YES"), it will return NULL and not check for spelling. If you use skip_on_cran = FALSE, it will check spelling in any case.

This is just a hint that could explain the behavior...

There is a small say about that in the help page

The spell_check_setup function adds a unit test to your package which automatically runs a spell check on documentation and vignettes during R CMD check if the environment variable NOT_CRAN is set to TRUE . By default this unit test never fails; it merely prints potential spelling errors to the console.

spell_check_setup is the function called by usethis that set skip_on_cran = TRUE by default. This is on purpose so that this test is not executed on CRAN but only during dev check where you explicitly setup the NOT_CRAN variable. Otherwise, with skip_on_cran = FALSE, it will always run, even on CRAN.

It is clearer by looking at the code to see this:

spell_check_test <- function (vignettes = TRUE, error = FALSE, lang = NULL, skip_on_cran = TRUE) 
{
  if (isTRUE(skip_on_cran)) {
    not_cran <- Sys.getenv("NOT_CRAN")
    if (is.na(match(not_cran, c("1", "TRUE", "true", "YES")))) 
      return(NULL)
  }
...
}

Ah, I see. I hadn't set NOT_CRAN = TRUE as an environment variable. I think I was confused when reading the devtools docs:

devtools sets a number of environmental variables to ensure consistent between the current R session and the new session, and to ensure that everything behaves the same across systems. It also suppresses a common warning on windows, and sets NOT_CRAN so you can tell that your code is not running on CRAN. If NOT_CRAN has been set externally, it is not overwritten.

I thought that devtools automatically sets NOT_CRAN = TRUE when running check(). After setting the environment variable locally, the spell check works as expected. Thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.