From 9e9611bf9cf81b90b596173a5778ce888f28ed8e Mon Sep 17 00:00:00 2001 From: Matthias Ollech <99362508+MEO265@users.noreply.github.com> Date: Fri, 8 Dec 2023 12:03:11 +0100 Subject: [PATCH] fix: Re-normalize path after creating files --- R/use_lintr.R | 46 +++++++++++++++++++-------------- tests/testthat/test-use_lintr.R | 12 --------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/R/use_lintr.R b/R/use_lintr.R index 088a894dc..d98eafa31 100644 --- a/R/use_lintr.R +++ b/R/use_lintr.R @@ -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") + } } } diff --git a/tests/testthat/test-use_lintr.R b/tests/testthat/test-use_lintr.R index 8f75a7d16..9a5b5a99c 100644 --- a/tests/testthat/test-use_lintr.R +++ b/tests/testthat/test-use_lintr.R @@ -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"))) -})