Skip to content

Commit

Permalink
fix: Re-normalize path after creating files
Browse files Browse the repository at this point in the history
  • Loading branch information
MEO265 committed Dec 8, 2023
1 parent 88acd40 commit 9e9611b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
46 changes: 27 additions & 19 deletions R/use_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,34 @@ use_lintr <- function(path = ".", type = c("tidyverse", "full")) {
)
write.dcf(the_config, config_file, width = Inf)

# Some OS can only normalize a path if the associated file or folder exists, so the path needs to be re-normalized
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = TRUE, winslash = "/")
pkg_path <- normalizePath(path, mustWork = TRUE, winslash = "/")
# Check if config_file is in package i.e. lintr_option("linter_file") != "../.lintr"
if (file.exists(file.path(path, "DESCRIPTION")) && startsWith(config_file, prefix = pkg_path)) {
# Skip a extra character for the leading `/`
rel_path <- substring(config_file, first = nchar(pkg_path) + 2L, last = nchar(config_file))
ignore_path <- file.path(pkg_path, ".Rbuildignore")
if (!file.exists(ignore_path)) file.create(ignore_path)
# Follow the same procedure as base R to see if the file is already ignored
ignore <- tryCatch({
trimws(readLines(ignore_path))
}, warning = function(e) {
cat(file = ignore_path, "\n", append = TRUE)
trimws(readLines(ignore_path))
if (file.exists(file.path(path, "DESCRIPTION"))) {
# Some OS can only normalize a path if the associated file or folder exists, so the path needs to be re-normalized
tryCatch({
pkg_path <- normalizePath(path, mustWork = TRUE, winslash = "/")
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = TRUE, winslash = "/")
}, error = function(e) {
stop("No entry could be added to the .Rbuildignore.", call. = FALSE)
})
ignore <- ignore[nzchar(ignore)]
if (!any(vapply(ignore, function(x) grepl(rel_path, pattern = x, perl = TRUE, ignore.case = TRUE), logical(1L)))) {
cat(file = ignore_path, rex::rex(start, rel_path, end), sep = "\n", append = TRUE)
message("Adding ", rel_path, " to .Rbuildignore")
# Check if config_file is in package i.e. lintr_option("linter_file") != "../.lintr"
if (startsWith(config_file, prefix = pkg_path)) {
# Skip a extra character for the leading `/`
rel_path <- substring(config_file, first = nchar(pkg_path) + 2L, last = nchar(config_file))
ignore_path <- file.path(pkg_path, ".Rbuildignore")
if (!file.exists(ignore_path)) file.create(ignore_path)
# Follow the same procedure as base R to see if the file is already ignored
ignore <- tryCatch({
trimws(readLines(ignore_path))
}, warning = function(e) {
cat(file = ignore_path, "\n", append = TRUE)
trimws(readLines(ignore_path))
})
ignore <- ignore[nzchar(ignore)]
already_ignored <-
any(vapply(ignore, FUN = grepl, x = rel_path, perl = TRUE, ignore.case = TRUE, FUN.VALUE = logical(1L)))
if (!already_ignored) {
cat(file = ignore_path, rex::rex(start, rel_path, end), sep = "\n", append = TRUE)
message("Adding ", rel_path, " to .Rbuildignore")
}
}
}

Expand Down
12 changes: 0 additions & 12 deletions tests/testthat/test-use_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,3 @@ test_that("use_lintr handles missing final new line", {
}, regexp = "Adding .* to .Rbuildignore")
expect_identical(readLines(ignore), c("^fu$", "^bar$", "^\\.lintr$"))
})

test_that("use_lintr handles missing final new line", {
path <- withr::local_tempdir()
file.create(file.path(path, "DESCRIPTION"))
file.create(file.path(path, lintr_option("linter_file")))
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = TRUE, winslash = "/")
pkg_path <- normalizePath(path, mustWork = TRUE, winslash = "/")
warning(config_file, call. = FALSE)
warning(pkg_path, call. = FALSE)
expect_true(startsWith(config_file, prefix = pkg_path))
expect_true(file.exists(file.path(path, "DESCRIPTION")))
})

0 comments on commit 9e9611b

Please sign in to comment.