Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
34 changes: 19 additions & 15 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-public_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})