Skip to content

Commit

Permalink
Test for ~ files, to avoid home deletion
Browse files Browse the repository at this point in the history
Closes #120.
  • Loading branch information
gaborcsardi committed Sep 9, 2021
1 parent 8f2a041 commit 5782a7a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
26 changes: 25 additions & 1 deletion R/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@
build_package <- function(path, tmpdir, build_args, libpath, quiet) {
path <- normalizePath(path)

check_for_tilde_file(path)

dir.create(tmpdir, recursive = TRUE, showWarnings = FALSE)
tmpdir <- normalizePath(tmpdir)

if (file.info(path)$isdir) {
if (!quiet) cat_head("R CMD build")

desc <- desc(path)
clean_doc <- as_flag(desc$get("Config/build/clean-inst-doc"), NULL)

with_envvar(
c("R_LIBS_USER" = paste(libpath, collapse = .Platform$path.sep)), {
proc <- pkgbuild_process$new(path, tmpdir, args = build_args)
proc <- pkgbuild_process$new(
path,
tmpdir,
args = build_args,
clean_doc = clean_doc
)
on.exit(proc$kill(), add = TRUE)

callback <- detect_callback()
while (proc$is_incomplete_output() || proc$is_incomplete_error()) {
proc$poll_io(-1)
Expand All @@ -38,3 +50,15 @@ build_package <- function(path, tmpdir, build_args, libpath, quiet) {
dest
}
}

check_for_tilde_file <- function(path) {
lst <- dir(path)
if ("~" %in% lst) {
stop(
"This package contains a file or directory named `~`. ",
"Because of a bug in older R versions (before R 4.0.0), ",
"building this package might delete your entire home directory!",
"It is best to (carefully!) remove the file. rcmdcehck will exit now."
)
}
}
8 changes: 8 additions & 0 deletions tests/testthat/test-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ test_that("different packages in the same dir are fine", {
expect_equal(readLines(res), "baz")
expect_equal(readLines(f1), "foobar")
})

test_that("protection against ~ deletion", {
mockery::stub(check_for_tilde_file, "dir", c("foo", "~", "bar"))
expect_error(
check_for_tilde_file(tempfile()),
"delete your entire home directory"
)
})

0 comments on commit 5782a7a

Please sign in to comment.