Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion R/exclude.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ normalize_exclusions <- function(x, normalize_path=TRUE, dir_prefix = NULL) {
# Paths to excluded files are specified relative-to the package- or
# directory-root (when running lint_dir or lint_package)
if (!is.null(dir_prefix)) {
names(x) <- file.path(dir_prefix, names(x))
relative_paths <- !is_absolute_path(names(x))
names(x)[relative_paths] <- file.path(dir_prefix, names(x)[relative_paths])
}
if (normalize_path) {
x <- x[file.exists(names(x))] # remove exclusions for non-existing files
Expand Down
2 changes: 1 addition & 1 deletion R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ lint_package <- function(path = ".", relative_path = TRUE, ..., exclusions = lis
read_settings(path)
on.exit(clear_settings, add = TRUE)

exclusions <- normalize_exclusions(c(exclusions, settings$exclusions), FALSE)
exclusions <- normalize_exclusions(c(exclusions, settings$exclusions), dir_prefix = path)

lints <- lint_dir(file.path(path, c("R", "tests", "inst")), relative_path = FALSE, exclusions = exclusions, parse_settings = FALSE, ...)

Expand Down
18 changes: 16 additions & 2 deletions tests/testthat/test-exclusions.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,25 @@ test_that("it normalizes file paths, removing non-existing files", {
t1 <- list(); t1[[a]] <- 1:10
t2 <- list(); t2[["notafile"]] <- 5:15
t3 <- list(); t3[[c]] <- 5:15
t4 <- list(); t4[[basename(c)]] <- 5:15
pkg_root <- "../.."
prefix <- file.path("tests", "testthat")
res <- list(); res[[a]] <- 1:10; res[[normalizePath(c)]] <- 5:15
expect_equal(normalize_exclusions(c(t1, t2, t3)), res)
expect_equal(withr::with_dir(pkg_root, normalize_exclusions(c(t1, t2, t4),
dir_prefix=prefix)),
res)

res <- list(); res[[a]] <- 1:10; res[["notafile"]] <- 5:15; res[[c]] <- 5:15
expect_equal(normalize_exclusions(c(t1, t2, t3), normalize_path=FALSE), res)

res <- list(); res[[a]] <- 1:10; res[[file.path(prefix, "notafile")]] <- 5:15
res[[file.path(prefix, basename(c))]] <- 5:15
expect_equal(withr::with_dir(pkg_root,
normalize_exclusions(c(t1, t2, t4),
normalize_path=FALSE,
dir_prefix=prefix)),
res)
})

unlink(c(a, b, c))
Expand All @@ -187,7 +201,7 @@ context("exclude")
test_that("it excludes properly", {
read_settings(NULL)

t1 <- lint("exclusions-test")
t1 <- lint("exclusions-test", exclusions = list())

expect_equal(length(t1), 2)

Expand All @@ -202,7 +216,7 @@ test_that("it excludes properly", {
cache_path <- file.path(tempdir(), "lintr_cache")
clear_cache("exclusions-test", cache_path)
for (info in sprintf("caching: pass %s", 1:4)) {
t4 <- lint("exclusions-test", cache = cache_path)
t4 <- lint("exclusions-test", cache = cache_path, exclusions = list())

expect_equal(length(t4), 2, info = info)
}
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ test_that("single check", {
})

test_that("multiple checks", {
expect_success(expect_lint(file = "exclusions-test", checks = as.list(rep(msg, 6L)), linters = linter))
expect_success(expect_lint(file = "exclusions-test", exclusions = list(), checks = as.list(rep(msg, 6L)),
linters = linter))

expect_success(expect_lint("a=1; b=2", list(msg, msg), linter))
expect_success(expect_lint("a=1; b=2", list(c(message = msg), c(message = msg)), linter))
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-lint_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,25 @@ test_that(
)
})

test_that(
"`lint_package` does not depend on path to pkg - with exclusions argument", {
# The test checks the results of lint_package when the excluded regions are
# given as an argument of the function and are specified using absolute and
# relative paths

pkg_path <- file.path("dummy_packages", "assignmentLinter")

# Create list of exclusions relative to the whole of `abc.R` and the first
# line of `jkl.R`
exclusions <- list(normalizePath(file.path(pkg_path, 'R/abc.R')),
'R/jkl.R' = 1)

expected_lines <- c("mno = 789")
lints_from_outside <- lint_package(
pkg_path, linters = list(assignment_linter), exclusions = exclusions
)

expect_equal(
as.data.frame(lints_from_outside)[["line"]], expected_lines
)
})