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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
test_that("package_hooks_linter skips allowed usages of packageStartupMessage() & library.dynam()", {
linter <- package_hooks_linter()
# allowed in .onAttach, not .onLoad
expect_lint(".onAttach <- function(lib, pkg) packageStartupMessage('hi')", NULL, linter)
# allowed in .onLoad, not .onAttach
expect_lint(".onLoad <- function(lib, pkg) library.dynam()", NULL, linter)
})
test_that("package_hooks_linter blocks simple disallowed usages of packageStartupMessage() & library.dynam()", {
linter <- package_hooks_linter()
# inline version
expect_lint(
".onLoad <- function(lib, pkg) packageStartupMessage('hi')",
rex::rex("Put packageStartupMessage() calls in .onAttach()"),
linter
)
# multiline version
expect_lint(
trim_some("
.onAttach <- function(libname, pkgname) {
library.dynam()
}
"),
rex::rex("Put library.dynam() calls in .onLoad, not .onAttach()."),
linter
)
# found at deeper nesting too
expect_lint(
trim_some("
.onLoad <- function(libname, pkgname) {
foo(bar(baz(packageStartupMessage('hi'))))
}
"),
rex::rex("Put packageStartupMessage() calls in .onAttach()"),
linter
)
})
test_that("package_hooks_linter blocks simple disallowed usages of other blocked messaging functions", {
linter <- package_hooks_linter()
# inline version
expect_lint(
".onLoad <- function(lib, pkg) cat('hi')",
rex::rex("Don't use cat() in .onLoad()"),
linter
)
# multiline version
expect_lint(
trim_some("
.onAttach <- function(libname, pkgname) {
writeLines('hi')
}
"),
rex::rex("Don't use writeLines() in .onAttach()"),
linter
)
expect_lint(
trim_some("
.onLoad <- function(libname, pkgname) {
print('hi')
}
"),
rex::rex("Don't use print() in .onLoad()"),
linter
)
# found at deeper nesting too
expect_lint(
trim_some("
.onAttach <- function(libname, pkgname) {
foo(bar(baz(message('hi'))))
}
"),
rex::rex("Don't use message() in .onAttach()"),
linter
)
})
test_that("package_hooks_linter skips valid .onLoad() and .onAttach() arguments", {
linter <- package_hooks_linter()
expect_lint(".onAttach <- function(lib, pkg) { }", NULL, linter)
expect_lint(".onLoad <- function(lib, pkg) { }", NULL, linter)
# args only need to start with those characters
expect_lint(".onAttach <- function(libname, pkgpath) { }", NULL, linter)
expect_lint(".onLoad <- function(libXXXX, pkgYYYY) { }", NULL, linter)
})
test_that("package_hooks_linter blocks invalid .onLoad() / .onAttach() arguments", {
linter <- package_hooks_linter()
onload_msg <- rex::rex(".onLoad() should take two arguments")
expect_lint(
".onAttach <- function(xxx, pkg) { }",
rex::rex(".onAttach() should take two arguments"),
linter
)
expect_lint(".onLoad <- function(lib, yyy) { }", onload_msg, linter)
# only one lint if both are wrong
expect_lint(".onLoad <- function(xxx, yyy) { }", onload_msg, linter)
# exactly two arguments required.
# NB: QC.R allows ... arguments to be passed, but disallow this flexibility in the linter.
expect_lint(".onLoad <- function() { }", onload_msg, linter)
expect_lint(".onLoad <- function(lib) { }", onload_msg, linter)
expect_lint(".onLoad <- function(lib, pkg, third) { }", onload_msg, linter)
expect_lint(".onLoad <- function(lib, ...) { }", onload_msg, linter)
})
test_that("package_hooks_linter skips valid namespace loading", {
linter <- package_hooks_linter()
expect_lint(".onAttach <- function(lib, pkg) { requireNamespace('foo') }", NULL, linter)
expect_lint(".onLoad <- function(lib, pkg) { requireNamespace('foo') }", NULL, linter)
})
test_that("package_hooks_linter blocks attaching namespaces", {
linter <- package_hooks_linter()
expect_lint(
".onAttach <- function(lib, pkg) { require(foo) }",
rex::rex("Don't alter the search() path in .onAttach() by calling require()."),
linter
)
expect_lint(
".onLoad <- function(lib, pkg) { library(foo) }",
rex::rex("Don't alter the search() path in .onLoad() by calling library()."),
linter
)
expect_lint(
".onLoad <- function(lib, pkg) { installed.packages() }",
rex::rex("Don't slow down package load by running installed.packages() in .onLoad()."),
linter
)
# find at further nesting too
expect_lint(
".onAttach <- function(lib, pkg) { a(b(c(require(foo)))) }",
rex::rex("Don't alter the search() path in .onAttach() by calling require()."),
linter
)
expect_lint(
".onLoad <- function(lib, pkg) { d(e(f(library(foo)))) }",
rex::rex("Don't alter the search() path in .onLoad() by calling library()."),
linter
)
expect_lint(
".onLoad <- function(lib, pkg) { g(h(i(installed.packages()))) }",
rex::rex("Don't slow down package load by running installed.packages() in .onLoad()."),
linter
)
# also find when used as names
expect_lint(
".onAttach <- function(lib, pkg) { sapply(c('a', 'b', 'c'), require, character.only = TRUE) }",
rex::rex("Don't alter the search() path in .onAttach() by calling require()."),
linter
)
expect_lint(
".onAttach <- function(lib, pkg) { lapply(c('a', 'b', 'c'), library, character.only = TRUE) }",
rex::rex("Don't alter the search() path in .onAttach() by calling library()"),
linter
)
})
test_that("package_hooks_linter skips valid .onDetach() and .Last.lib()", {
linter <- package_hooks_linter()
expect_lint(".onDetach <- function(lib) { }", NULL, linter)
expect_lint(".onDetach <- function(libname) { }", NULL, linter)
expect_lint(".Last.lib <- function(lib) { }", NULL, linter)
expect_lint(".Last.lib <- function(libname) { }", NULL, linter)
})
test_that("package_hooks_linter catches usage of library.dynam.unload()", {
linter <- package_hooks_linter()
expect_lint(
".onDetach <- function(lib) { library.dynam.unload() }",
rex::rex("Use library.dynam.unload() calls in .onUnload(), not .onDetach()."),
linter
)
expect_lint(
".Last.lib <- function(lib) { library.dynam.unload() }",
rex::rex("Use library.dynam.unload() calls in .onUnload(), not .Last.lib()."),
linter
)
# expected usage is in .onUnload
expect_lint(
".onUnload <- function(lib) { library.dynam.unload() }",
NULL,
linter
)
})
test_that("package_hooks_linter detects bad argument names in .onDetach()/.Last.lib()", {
linter <- package_hooks_linter()
lint_msg_part <- " should take one argument starting with 'lib'"
expect_lint(
".onDetach <- function(xxx) { }",
rex::rex(".onDetach()", lint_msg_part),
linter
)
expect_lint(
".Last.lib <- function(yyy) { }",
rex::rex(".Last.lib()", lint_msg_part),
linter
)
# exactly one argument required.
# NB: QC.R allows ... arguments to be passed, but disallow this flexibility in the linter.
expect_lint(
".onDetach <- function() { }",
rex::rex(".onDetach()", lint_msg_part),
linter
)
expect_lint(
".Last.lib <- function(lib, pkg) { }",
rex::rex(".Last.lib()", lint_msg_part),
linter
)
expect_lint(
".onDetach <- function(...) { }",
rex::rex(".onDetach()", lint_msg_part),
linter
)
})
test_that("function shorthand is handled", {
skip_if_not_r_version("4.1.0")
linter <- package_hooks_linter()
expect_lint(
".onLoad <- \\(lib, pkg) packageStartupMessage('hi')",
rex::rex("Put packageStartupMessage() calls in .onAttach()"),
linter
)
expect_lint(
".onAttach <- \\(xxx, pkg) { }",
rex::rex(".onAttach() should take two arguments"),
linter
)
expect_lint(
".onAttach <- \\(lib, pkg) { require(foo) }",
rex::rex("Don't alter the search() path in .onAttach() by calling require()."),
linter
)
expect_lint(
".onDetach <- \\(lib) { library.dynam.unload() }",
rex::rex("Use library.dynam.unload() calls in .onUnload(), not .onDetach()."),
linter
)
expect_lint(
".onDetach <- \\(xxx) { }",
rex::rex(".onDetach() should take one argument starting with 'lib'."),
linter
)
})
test_that("lints vectorize", {
expect_lint(
trim_some("{
.onLoad <- function(xxx, yyy) { }
.onAttach <- function(aaa, bbb) { }
}"),
list(
list(".onLoad", line_number = 2L),
list(".onAttach", line_number = 3L)
),
package_hooks_linter()
)
})
|