Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add .lintr to .Rbuildignore by the use_lintr function #1895

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Suggests:
testthat (>= 3.0.0),
tibble,
tufte,
withr (>= 2.5.0)
withr (>= 2.5.0),
xfun
VignetteBuilder:
knitr
Config/Needs/website: tidyverse/tidytemplate
Expand Down
24 changes: 24 additions & 0 deletions R/use_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,29 @@ use_lintr <- function(path = ".", type = c("tidyverse", "full")) {
)
)
write.dcf(the_config, config_file, width = Inf)

# If this is an R package and if available, add .lintr file to .Rbuildignore
if (file.exists("DESCRIPTION") && file.exists(".Rbuildignore")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test looks in the current working directory, not where the config was placed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no guarantee that DESCRIPTION and .lintr will be placed in the same directory, I believe that the current working directory can should be used.

try(
add_build_ignore(config_file),
silent = TRUE
)
}

invisible(config_file)
}

add_build_ignore <- function(path) {
path_build_ignore <- file.path(".", ".Rbuildignore")
existing_lines <- xfun::read_utf8(path_build_ignore)

escape_path <- rex::rex(start, rex::re_substitutes(path, list(start, "./") %or% list("/", end), ""), end)

new_lines <- c(existing_lines, setdiff(escape_path, existing_lines))

xfun::write_utf8(new_lines, path_build_ignore)

message("Add '", escape_path, "' to ", path_build_ignore)

invisible(NULL)
}
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/use_lintr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# use_lintr add .lintr to .Rbuildignore for packages

Code
cat(brio::read_file(file.path(tmp_package_dir, ".Rbuildignore")))
Output
^lintr\.Rproj$
^\.Rproj\.user$
^\.lintr$

12 changes: 12 additions & 0 deletions tests/testthat/test-use_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ test_that("use_lintr with type = full also works", {
lints <- lint_dir(tmp)
expect_length(lints, 0L)
})

test_that("use_lintr add .lintr to .Rbuildignore for packages", {
tmp <- withr::local_tempdir()
tmp_package_dir <- paste0(tmp, "/package")
package_dir <- test_path("dummy_packages", "package")
dir.create(tmp_package_dir)
file.copy(package_dir, tmp, recursive = TRUE)
setwd(tmp_package_dir)
lintr_file <- use_lintr()
expect_true(file.exists(lintr_file))
expect_snapshot(cat(brio::read_file(file.path(tmp_package_dir, ".Rbuildignore"))))
})