1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
# When called from inside a package:
# > lint_package(".")
# .. should give the same results as when called from outside the package
# with:
# > lint_package(path_to_package)
# Template packages for use in testing are stored in
# tests/testthat/dummy_packages/<pkgName>
# These packages should not have a .lintr file: Hardcoding a .lintr in a
# dummy package throws problems during `R CMD check` (they are flagged as
# hidden files, but can't be added to RBuildIgnore since they should be
# available during `R CMD check` tests)
test_that(
"`lint_package` does not depend on path to pkg - no excluded files",
{
withr::local_options(lintr.linter_file = "lintr_test_config")
# This dummy package does not have a .lintr file, so no files / lines should
# be excluded from analysis
pkg_path <- test_path("dummy_packages", "assignmentLinter")
expected_lines <- c(
# from abc.R
"abc = 123",
# from jkl.R
"jkl = 456",
"mno = 789",
# from exec/script.R
"x = 1:4"
)
lints_from_outside <- lint_package(
pkg_path,
linters = list(assignment_linter())
)
lints_from_pkg_root <- withr::with_dir(
pkg_path,
lint_package(".", linters = list(assignment_linter()), parse_settings = FALSE)
)
lints_from_a_subdir <- withr::with_dir(
file.path(pkg_path, "R"),
lint_package("..", linters = list(assignment_linter()), parse_settings = FALSE)
)
expect_identical(
as.data.frame(lints_from_outside)[["line"]],
expected_lines
)
expect_identical(
as.data.frame(lints_from_outside),
as.data.frame(lints_from_pkg_root),
info = paste(
"lint_package() finds the same lints from pkg-root as from outside a pkg",
"(no .lintr config present)"
)
)
expect_identical(
as.data.frame(lints_from_outside),
as.data.frame(lints_from_a_subdir),
info = paste(
"lint_package() finds the same lints from a subdir as from outside a pkg",
"(no .lintr config present)"
)
)
}
)
test_that(
"`lint_package` does not depend on path to pkg - with excluded files",
{
# Since excluded regions can be specified in two ways
# list(
# filename = line_numbers, # approach 1
# filename # approach 2
# ),
# the test checks both approaches
pkg_path <- test_path("dummy_packages", "assignmentLinter")
# Add a .lintr that excludes the whole of `abc.R` and the first line of
# `jkl.R` (and remove it on finishing this test)
local_config(pkg_path, "exclusions: list('R/abc.R', 'R/jkl.R' = 1)")
expected_lines <- c("mno = 789", "x = 1:4")
lints_from_outside <- lint_package(
pkg_path,
linters = list(assignment_linter())
)
lints_from_pkg_root <- withr::with_dir(
pkg_path,
lint_package(".", linters = list(assignment_linter()))
)
lints_from_a_subdir <- withr::with_dir(
file.path(pkg_path, "R"),
lint_package(".", linters = list(assignment_linter()))
)
lints_from_a_subsubdir <- withr::with_dir(
file.path(pkg_path, "tests", "testthat"),
lint_package(".", linters = list(assignment_linter()))
)
expect_identical(
as.data.frame(lints_from_outside)[["line"]],
expected_lines
)
expect_identical(
as.data.frame(lints_from_outside),
as.data.frame(lints_from_pkg_root),
info = paste(
"lint_package() finds the same lints from pkg-root as from outside a pkg",
"(.lintr config present)"
)
)
expect_identical(
as.data.frame(lints_from_outside),
as.data.frame(lints_from_a_subdir),
info = paste(
"lint_package() finds the same lints from a subdir as from outside a pkg",
"(.lintr config present)"
)
)
expect_identical(
as.data.frame(lints_from_outside),
as.data.frame(lints_from_a_subsubdir),
info = paste(
"lint_package() finds the same lints from a sub-subdir as from outside a pkg",
"(.lintr config present)"
)
)
}
)
test_that("lint_package returns early if no package is found", {
temp_pkg <- withr::local_tempdir("dir")
expect_warning(
{
l <- lint_package(temp_pkg)
},
"Didn't find any R package",
fixed = TRUE
)
expect_null(l)
# ignore a folder named DESCRIPTION, #702
file.copy(test_path("dummy_packages", "desc_dir_pkg"), temp_pkg, recursive = TRUE)
expect_warning(
lint_package(file.path(temp_pkg, "desc_dir_pkg", "DESCRIPTION", "R")),
"Didn't find any R package",
fixed = TRUE
)
})
test_that("length(path)>1 is not supported", {
expect_error(lint_package(letters), "one package at a time", fixed = TRUE)
})
test_that(
"`lint_package` will use a `.lintr` file in `.github/linters/` directory the same as the package root",
{
withr::local_options(lintr.linter_file = "lintr_test_config")
pkg_path <- test_path("dummy_packages", "github_lintr_file")
# First, ensure that the package has lint messages in the absence of a
# custom configuration:
pkg_lints_before <- withr::with_dir(
pkg_path,
lint_package(".", linters = list(quotes_linter()))
)
expect_identical(
as.data.frame(pkg_lints_before)[["line"]],
c("'abc'", "'abc'"),
"linting the `github_lintr_file` package should fail"
)
# In `github/linters`add a `.lintr` file
dir.create(
path = file.path(pkg_path, ".github", "linters/"),
recursive = TRUE
)
on.exit(unlink(file.path(pkg_path, ".github"), recursive = TRUE), add = TRUE)
local_config(
file.path(pkg_path, ".github", "linters"),
"linters: linters_with_defaults(quotes_linter(\"'\"))",
filename = "lintr_test_config"
)
pkg_lints <- withr::with_dir(pkg_path, lint_package("."))
expect_length(pkg_lints, 0L)
subdir_lints <- withr::with_dir(pkg_path, lint_dir("tests/testthat"))
expect_length(subdir_lints, 0L)
}
)
test_that("package using .lintr.R config lints correctly", {
withr::local_options(lintr.linter_file = "lintr_test_config")
r_config_pkg <- test_path("dummy_packages", "RConfig")
lints <- as.data.frame(lint_package(r_config_pkg))
expect_identical(unique(basename(lints$filename)), "lint_me.R")
expect_identical(lints$linter, c("infix_spaces_linter", "any_duplicated_linter"))
# config has bad R syntax
expect_error(
lint_package(test_path("dummy_packages", "RConfigInvalid")),
"Malformed config file (lintr_test_config.R), ensure it is valid R syntax",
fixed = TRUE
)
# config produces unused variables
withr::local_options(lintr.linter_file = "lintr_test_config_extraneous")
expect_warning(
expect_length(lint_package(r_config_pkg), 2L),
"Found unused settings in config file",
fixed = TRUE
)
# R is preferred if multiple matched configs
withr::local_options(lintr.linter_file = "lintr_test_config_conflict")
lints <- as.data.frame(lint_package(r_config_pkg))
expect_identical(unique(basename(lints$filename)), "testthat.R")
expect_identical(lints$linter, c("expect_null_linter", "trailing_blank_lines_linter"))
})
test_that("lintr need not be attached for .lintr.R configs to use lintr functions", {
exprs <- paste(
'options(lintr.linter_file = "lintr_test_config")',
sprintf('lints <- lintr::lint_package("%s")', test_path("dummy_packages", "RConfig")),
# simplify output to be read from stdout
'cat(paste(as.data.frame(lints)$linter, collapse = "|"), "\n", sep = "")',
sep = "; "
)
if (tolower(Sys.info()[["sysname"]]) == "windows") {
rscript <- file.path(R.home("bin"), "Rscript.exe")
} else {
rscript <- file.path(R.home("bin"), "Rscript")
}
expect_identical(
system2(rscript, c("-e", shQuote(exprs)), stdout = TRUE),
"infix_spaces_linter|any_duplicated_linter"
)
})
|