diff --git a/NEWS.md b/NEWS.md index 32f21f36a..5c9089993 100644 --- a/NEWS.md +++ b/NEWS.md @@ -25,8 +25,10 @@ **Other changes** +* `.onLoad()` method no longer broken with {cli} >= 3.1 (#893). * Piped function without brackets `substitute(x %>% y)` don't get `()` added - anymore, as this can change outcome of the code (#876). + anymore for one level deep (not more yet, see #889), as this can change + outcome of the code (#876). * rules that add tokens don't break stylerignore sequences anymore (#891). * Add vignette on distributing style guide (#846, #861). * Alignment detection respects stylerignore (#850). diff --git a/R/zzz.R b/R/zzz.R index 4356d0a1c..d0a172c44 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -21,25 +21,29 @@ ask_to_switch_to_non_default_cache_root <- function(ask = interactive()) { if (ask && runif(1) > 0.9 && is.null(getOption("styler.cache_root"))) { - cli::cli_inform(paste0( - "The R option `styler.cache_root` is not set, which means the cache ", - "will get cleaned up after 6 days (and repeated styling will be slower).", - " To keep cache files longer, set ", - "the option to location within the {{R.cache}} cache where you want to ", - "store the cache, e.g. `\"styler-perm\"`.\n\n", - "options(styler.cache_root = \"styler-perm\")\n\n", - "in your .Rprofile. Note that the cache literally ", - "takes zero space on your disk, only the inode, and you can always ", - "manually clean up with `styler::cache_clear()`, and if you update the ", - "{{styler}} package, the cache is removed in any case. To ignore this ", - "message in the future, set the default explictly to \"styler\" with\n\n", - "options(styler.cache_root = \"styler\")\n\nin your `.Rprofile`. This ", - "message will only be displayed once in a while.\n" - )) + ask_to_switch_to_non_default_cache_root_impl() options(styler.cache_root = "styler") } } +ask_to_switch_to_non_default_cache_root_impl <- function() { + rlang::warn(paste0( + "The R option `styler.cache_root` is not set, which means the cache ", + "will get cleaned up after 6 days (and repeated styling will be slower).", + " To keep cache files longer, set ", + "the option to location within the {R.cache} cache where you want to ", + "store the cache, e.g. `\"styler-perm\"`.\n\n", + "options(styler.cache_root = \"styler-perm\")\n\n", + "in your .Rprofile. Note that the cache literally ", + "takes zero space on your disk, only the inode, and you can always ", + "manually clean up with `styler::cache_clear()`, and if you update the ", + "{styler} package, the cache is removed in any case. To ignore this ", + "message in the future, set the default explictly to \"styler\" with\n\n", + "options(styler.cache_root = \"styler\")\n\nin your `.Rprofile`. This ", + "message will only be displayed once in a while.\n" + )) +} + remove_old_cache_files <- function() { all_cached <- list.files( R.cache::getCachePath(c("styler", styler_version)), diff --git a/tests/testthat/test-public_api.R b/tests/testthat/test-public_api.R index d7653d3b2..e5abb00b2 100644 --- a/tests/testthat/test-public_api.R +++ b/tests/testthat/test-public_api.R @@ -475,3 +475,19 @@ test_that("Can properly determine style_after_saving", { expect_equal(op, FALSE) }) }) + +test_that("Can display warning on unset styler cache", { + withr::local_options(styler.cache_root = NULL) + withr::local_seed(7) + expect_warning( + ask_to_switch_to_non_default_cache_root(ask = TRUE), + 'options(styler.cache_root = "styler-perm")', + fixed = TRUE + ) +}) + +test_that("Can display warning on unset styler cache", { + withr::local_options(styler.cache_root = "styler-perm") + withr::local_seed(7) + expect_silent(ask_to_switch_to_non_default_cache_root(ask = TRUE)) +})