Skip to content

Commit 33efecf

Browse files
authored
Add .lintr to .Rbuildignore in use_lintr() (#2926)
1 parent fcb12d2 commit 33efecf

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Imports:
3535
rex,
3636
stats,
3737
utils,
38+
xfun,
3839
xml2 (>= 1.0.0),
3940
xmlparsedata (>= 1.0.5)
4041
Suggests:
@@ -51,7 +52,7 @@ Suggests:
5152
withr (>= 2.5.0)
5253
Enhances:
5354
data.table
54-
VignetteBuilder:
55+
VignetteBuilder:
5556
knitr
5657
Config/Needs/website: tidyverse/tidytemplate
5758
Config/Needs/development: pkgload, testthat, patrick

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* `object_usage_linter()` lints missing packages that may cause false positives (#2872, @AshesITR)
4949
* New argument `include_s4_slots` for the `xml_find_function_calls()` entry in the `get_source_expressions()` to govern whether calls of the form `s4Obj@fun()` are included in the result (#2820, @MichaelChirico).
5050
* `sprintf_linter()` lints `sprintf()` and `gettextf()` calls when a constant string is passed to `fmt` (#2894, @Bisaloo).
51+
* `use_lintr()` adds the created `.lintr` file to the `.Rbuildignore` if run in a package (#2926, initial work by @MEO265, finalized by @Bisaloo).
5152

5253
### New linters
5354

R/use_lintr.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,32 @@ use_lintr <- function(path = ".", type = c("tidyverse", "full")) {
4242
)
4343
)
4444
write.dcf(the_config, config_file, width = Inf)
45+
46+
pkg_path <- find_package(path)
47+
48+
if (is.null(pkg_path)) {
49+
return(invisible(config_file))
50+
}
51+
52+
rbuildignore_path <- file.path(pkg_path, ".Rbuildignore")
53+
rel_path <- xfun::relative_path(config_file, pkg_path)
54+
escaped_config_path <- rex::rex(start, rel_path, end)
55+
56+
if (!file.exists(rbuildignore_path)) {
57+
writeLines(escaped_config_path, rbuildignore_path)
58+
cli_inform("Added {.val {escaped_config_path}} to {.code .Rbuildignore}.")
59+
return(invisible(config_file))
60+
}
61+
62+
ignored <- readLines(rbuildignore_path, warn = FALSE)
63+
64+
if (escaped_config_path %in% ignored) {
65+
cli_inform("Configuration file {.val {escaped_config_path}} is already ignored in {.code .Rbuildignore}.")
66+
return(invisible(config_file))
67+
}
68+
69+
writeLines(c(ignored, escaped_config_path), rbuildignore_path)
70+
cli_inform("Added {.val {escaped_config_path}} to {.code .Rbuildignore}.")
71+
4572
invisible(config_file)
4673
}

tests/testthat/test-use_lintr.R

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,57 @@ test_that("use_lintr with type = full also works", {
3636
lints <- lint_dir(tmp)
3737
expect_length(lints, 0L)
3838
})
39+
40+
test_that("No .Rbuildignore is created of packages", {
41+
tmp <- withr::local_tempdir()
42+
43+
lintr_file <- use_lintr(path = tmp, type = "full")
44+
expect_false(file.exists(file.path(tmp, ".Rbuildignore")))
45+
})
46+
47+
test_that("No .Rbuildignore is filled outside of packages", {
48+
tmp <- withr::local_tempdir()
49+
ignore_path <- file.path(tmp, ".Rbuildignore")
50+
file.create(ignore_path)
51+
52+
lintr_file <- use_lintr(path = tmp, type = "full")
53+
expect_identical(readLines(ignore_path), character())
54+
})
55+
56+
test_that("No .Rbuildignore is filled if pattern already present", {
57+
tmp <- withr::local_tempdir()
58+
writeLines(
59+
"Package: test",
60+
file.path(tmp, "DESCRIPTION")
61+
)
62+
ignore_path <- file.path(tmp, ".Rbuildignore")
63+
ignore <- c("^fu$", "^\\.lintr$", "^bar$")
64+
writeLines(
65+
ignore,
66+
ignore_path
67+
)
68+
69+
lintr_file <- use_lintr(path = tmp, type = "full")
70+
expect_identical(readLines(ignore_path), ignore)
71+
})
72+
73+
test_that("use_lintr creates the correct regex", {
74+
tmp <- withr::local_tempdir()
75+
writeLines(
76+
"Package: test",
77+
file.path(tmp, "DESCRIPTION")
78+
)
79+
ignore_path <- file.path(tmp, ".Rbuildignore")
80+
writeLines(
81+
c("^fu$", "^bar$"),
82+
ignore_path
83+
)
84+
85+
expect_message(
86+
{
87+
lintr_file <- use_lintr(path = tmp, type = "full")
88+
},
89+
regexp = rex::rex("Added ^\\.lintr$ to `.Rbuildignore`")
90+
)
91+
expect_identical(readLines(ignore_path), c("^fu$", "^bar$", "^\\.lintr$"))
92+
})

0 commit comments

Comments
 (0)