Skip to content

Commit f6b6b3b

Browse files
rename and document
1 parent db26b5f commit f6b6b3b

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

R/zzz.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@
2323
invisible()
2424
}
2525

26-
delete_temp_directory_if_empty <- function(path) {
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) {
2735
path <- normalizePath(path)
2836
if (getRversion() < package_version("4.0.0")) {
2937
return(FALSE)
3038
}
31-
3239
designated_cache_path <- normalizePath(tools::R_user_dir("R.cache", which = "cache"))
3340
is_in_tools_cache <- startsWith(path, designated_cache_path)
3441
temp_dir <- normalizePath(Sys.getenv("TMPDIR", Sys.getenv("TMP")))
@@ -43,13 +50,8 @@ delete_temp_directory_if_empty <- function(path) {
4350
unlink(path, recursive = TRUE)
4451
return(TRUE)
4552
}
46-
return(FALSE)
47-
} else {
48-
rlang::abort(c(
49-
"Can only delete absolute paths under `tools::R_user_dir('R.cache') (",
50-
designated_cache_path, ") not ", path
51-
))
5253
}
54+
FALSE
5355
}
5456

5557

@@ -85,7 +87,7 @@ remove_old_cache_files <- function() {
8587
)
8688
purrr::walk(
8789
paths,
88-
delete_temp_directory_if_empty
90+
delete_if_cache_directory
8991
)
9092
}
9193

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ test_that("clear Cache", {
1616
})
1717

1818

19-
test_that("can delete empty directory", {
19+
test_that("can delete empty cache directory", {
2020
skip_if(getRversion() < package_version("4.0.0"))
2121
skip_on_cran()
2222
tmpdir <- withr::local_tempdir()
2323
withr::local_dir(tmpdir)
2424
dir.create("xxx")
25-
expect_true(delete_temp_directory_if_empty(file.path(getwd(), "xxx")))
25+
expect_true(delete_if_cache_directory(file.path(getwd(), "xxx")))
2626
dir.create("xxx")
2727
file.create("xxx/yyy")
2828
list.files("xxx")
29-
expect_false(delete_temp_directory_if_empty(file.path(getwd(), "xxx")))
29+
expect_false(delete_if_cache_directory(file.path(getwd(), "xxx")))
3030
expect_true(file.exists(tmpdir))
3131
})

0 commit comments

Comments
 (0)