Skip to content

Commit 9e9611b

Browse files
committed
fix: Re-normalize path after creating files
1 parent 88acd40 commit 9e9611b

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

R/use_lintr.R

+27-19
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,34 @@ use_lintr <- function(path = ".", type = c("tidyverse", "full")) {
4444
)
4545
write.dcf(the_config, config_file, width = Inf)
4646

47-
# Some OS can only normalize a path if the associated file or folder exists, so the path needs to be re-normalized
48-
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = TRUE, winslash = "/")
49-
pkg_path <- normalizePath(path, mustWork = TRUE, winslash = "/")
50-
# Check if config_file is in package i.e. lintr_option("linter_file") != "../.lintr"
51-
if (file.exists(file.path(path, "DESCRIPTION")) && startsWith(config_file, prefix = pkg_path)) {
52-
# Skip a extra character for the leading `/`
53-
rel_path <- substring(config_file, first = nchar(pkg_path) + 2L, last = nchar(config_file))
54-
ignore_path <- file.path(pkg_path, ".Rbuildignore")
55-
if (!file.exists(ignore_path)) file.create(ignore_path)
56-
# Follow the same procedure as base R to see if the file is already ignored
57-
ignore <- tryCatch({
58-
trimws(readLines(ignore_path))
59-
}, warning = function(e) {
60-
cat(file = ignore_path, "\n", append = TRUE)
61-
trimws(readLines(ignore_path))
47+
if (file.exists(file.path(path, "DESCRIPTION"))) {
48+
# Some OS can only normalize a path if the associated file or folder exists, so the path needs to be re-normalized
49+
tryCatch({
50+
pkg_path <- normalizePath(path, mustWork = TRUE, winslash = "/")
51+
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = TRUE, winslash = "/")
52+
}, error = function(e) {
53+
stop("No entry could be added to the .Rbuildignore.", call. = FALSE)
6254
})
63-
ignore <- ignore[nzchar(ignore)]
64-
if (!any(vapply(ignore, function(x) grepl(rel_path, pattern = x, perl = TRUE, ignore.case = TRUE), logical(1L)))) {
65-
cat(file = ignore_path, rex::rex(start, rel_path, end), sep = "\n", append = TRUE)
66-
message("Adding ", rel_path, " to .Rbuildignore")
55+
# Check if config_file is in package i.e. lintr_option("linter_file") != "../.lintr"
56+
if (startsWith(config_file, prefix = pkg_path)) {
57+
# Skip a extra character for the leading `/`
58+
rel_path <- substring(config_file, first = nchar(pkg_path) + 2L, last = nchar(config_file))
59+
ignore_path <- file.path(pkg_path, ".Rbuildignore")
60+
if (!file.exists(ignore_path)) file.create(ignore_path)
61+
# Follow the same procedure as base R to see if the file is already ignored
62+
ignore <- tryCatch({
63+
trimws(readLines(ignore_path))
64+
}, warning = function(e) {
65+
cat(file = ignore_path, "\n", append = TRUE)
66+
trimws(readLines(ignore_path))
67+
})
68+
ignore <- ignore[nzchar(ignore)]
69+
already_ignored <-
70+
any(vapply(ignore, FUN = grepl, x = rel_path, perl = TRUE, ignore.case = TRUE, FUN.VALUE = logical(1L)))
71+
if (!already_ignored) {
72+
cat(file = ignore_path, rex::rex(start, rel_path, end), sep = "\n", append = TRUE)
73+
message("Adding ", rel_path, " to .Rbuildignore")
74+
}
6775
}
6876
}
6977

tests/testthat/test-use_lintr.R

-12
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,3 @@ test_that("use_lintr handles missing final new line", {
8888
}, regexp = "Adding .* to .Rbuildignore")
8989
expect_identical(readLines(ignore), c("^fu$", "^bar$", "^\\.lintr$"))
9090
})
91-
92-
test_that("use_lintr handles missing final new line", {
93-
path <- withr::local_tempdir()
94-
file.create(file.path(path, "DESCRIPTION"))
95-
file.create(file.path(path, lintr_option("linter_file")))
96-
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = TRUE, winslash = "/")
97-
pkg_path <- normalizePath(path, mustWork = TRUE, winslash = "/")
98-
warning(config_file, call. = FALSE)
99-
warning(pkg_path, call. = FALSE)
100-
expect_true(startsWith(config_file, prefix = pkg_path))
101-
expect_true(file.exists(file.path(path, "DESCRIPTION")))
102-
})

0 commit comments

Comments
 (0)