Skip to content

Commit 1584683

Browse files
Merge pull request #1119 from r-lib/fix-cran
Delete cache directories if empty
2 parents 8e9ac82 + ed16b6e commit 1584683

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

.github/workflows/check-full.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ jobs:
5656
- uses: r-lib/actions/check-r-package@v2
5757
with:
5858
upload-snapshots: true
59-
error-on: '"note"'
59+
error-on: 'ifelse(getRversion() > 3.6, "warning", "note")'
6060
env:
6161
_R_CHECK_FORCE_SUGGESTS_: false

R/zzz.R

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@
2323
invisible()
2424
}
2525

26+
#' Delete a cache or temp directory
27+
#'
28+
#' For safety, `path` is only deleted if it is a sub-directory of a temporary
29+
#' directory or user cache. Since this function relies on `tools::R_user_dir()`,
30+
#' it early returns `FALSE` on `R < 4.0.0`.
31+
#' @param path Absolute path to a directory to delete.
32+
#' @returns `TRUE` if anything was deleted, `FALSE` otherwise.
33+
#' @keywords internal
34+
delete_if_cache_directory <- function(path) {
35+
path <- normalizePath(path)
36+
if (getRversion() < package_version("4.0.0")) {
37+
return(FALSE)
38+
}
39+
designated_cache_path <- normalizePath(tools::R_user_dir("R.cache", which = "cache"))
40+
is_in_tools_cache <- startsWith(path, designated_cache_path)
41+
temp_dir <- normalizePath(Sys.getenv("TMPDIR", Sys.getenv("TMP")))
42+
is_in_generic_cache <- startsWith(path, temp_dir)
43+
if (is_in_tools_cache || is_in_generic_cache) {
44+
all_files <- list.files(path,
45+
full.names = TRUE,
46+
recursive = TRUE,
47+
all.files = FALSE
48+
)
49+
if (length(all_files) < 1L) {
50+
unlink(path, recursive = TRUE)
51+
return(TRUE)
52+
}
53+
}
54+
FALSE
55+
}
56+
2657

2758
ask_to_switch_to_non_default_cache_root <- function(ask = interactive()) {
2859
if (ask && stats::runif(1L) > 0.9 && is.null(getOption("styler.cache_root"))) {
@@ -40,14 +71,24 @@ ask_to_switch_to_non_default_cache_root_impl <- function() {
4071
}
4172

4273
remove_old_cache_files <- function() {
74+
path_version_specific <- R.cache::getCachePath(c("styler", styler_version))
4375
all_cached <- list.files(
44-
R.cache::getCachePath(c("styler", styler_version)),
76+
path_version_specific,
4577
full.names = TRUE, recursive = TRUE
4678
)
4779
date_boundary <- Sys.time() - 60L * 60L * 24L * 6L
4880
file.remove(
4981
all_cached[file.info(all_cached)$mtime < date_boundary]
5082
)
83+
path_styler_specific <- dirname(path_version_specific)
84+
path_r_cache_specific <- dirname(path_styler_specific)
85+
paths <- normalizePath(
86+
c(path_version_specific, path_styler_specific, path_r_cache_specific)
87+
)
88+
purrr::walk(
89+
paths,
90+
delete_if_cache_directory
91+
)
5192
}
5293

5394

man/delete_if_cache_directory.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-zzz.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,18 @@ test_that("clear Cache", {
1414
length(list.dirs(R.cache::getCachePath("styler"))) == 1L
1515
)
1616
})
17+
18+
19+
test_that("can delete empty cache directory", {
20+
skip_if(getRversion() < package_version("4.0.0"))
21+
skip_on_cran()
22+
tmpdir <- withr::local_tempdir()
23+
withr::local_dir(tmpdir)
24+
dir.create("xxx")
25+
expect_true(delete_if_cache_directory(file.path(getwd(), "xxx")))
26+
dir.create("xxx")
27+
file.create("xxx/yyy")
28+
list.files("xxx")
29+
expect_false(delete_if_cache_directory(file.path(getwd(), "xxx")))
30+
expect_true(file.exists(tmpdir))
31+
})

0 commit comments

Comments
 (0)