@@ -171,14 +171,17 @@ test_that("package_hooks_linter blocks attaching namespaces", {
171171 )
172172})
173173
174- test_that(" package_hooks_linter skips valid .onDetach() and .Last.lib()" , {
174+ test_that(" package_hooks_linter skips valid .onDetach(), .Last.lib(), and .onUnload ()" , {
175175 linter <- package_hooks_linter()
176176
177177 expect_lint(" .onDetach <- function(lib) { }" , NULL , linter )
178178 expect_lint(" .onDetach <- function(libname) { }" , NULL , linter )
179179
180180 expect_lint(" .Last.lib <- function(lib) { }" , NULL , linter )
181181 expect_lint(" .Last.lib <- function(libname) { }" , NULL , linter )
182+
183+ expect_lint(" .onUnload <- function(lib) { }" , NULL , linter )
184+ expect_lint(" .onUnload <- function(libpath) { }" , NULL , linter )
182185})
183186
184187test_that(" package_hooks_linter catches usage of library.dynam.unload()" , {
@@ -202,7 +205,7 @@ test_that("package_hooks_linter catches usage of library.dynam.unload()", {
202205 )
203206})
204207
205- test_that(" package_hooks_linter detects bad argument names in .onDetach()/.Last.lib()" , {
208+ test_that(" package_hooks_linter detects bad argument names in .onDetach()/.Last.lib()/.onUnload() " , {
206209 linter <- package_hooks_linter()
207210 lint_msg_part <- " should take one argument starting with 'lib'"
208211
@@ -216,6 +219,11 @@ test_that("package_hooks_linter detects bad argument names in .onDetach()/.Last.
216219 rex :: rex(" .Last.lib()" , lint_msg_part ),
217220 linter
218221 )
222+ expect_lint(
223+ " .onUnload <- function(pkg) { }" ,
224+ rex :: rex(" .onUnload()" , lint_msg_part ),
225+ linter
226+ )
219227
220228 # exactly one argument required.
221229 # NB: QC.R allows ... arguments to be passed, but disallow this flexibility in the linter.
@@ -234,6 +242,21 @@ test_that("package_hooks_linter detects bad argument names in .onDetach()/.Last.
234242 rex :: rex(" .onDetach()" , lint_msg_part ),
235243 linter
236244 )
245+ expect_lint(
246+ " .onUnload <- function() { }" ,
247+ rex :: rex(" .onUnload()" , lint_msg_part ),
248+ linter
249+ )
250+ expect_lint(
251+ " .onUnload <- function(lib, pkg) { }" ,
252+ rex :: rex(" .onUnload()" , lint_msg_part ),
253+ linter
254+ )
255+ expect_lint(
256+ " .onUnload <- function(...) { }" ,
257+ rex :: rex(" .onUnload()" , lint_msg_part ),
258+ linter
259+ )
237260})
238261
239262test_that(" function shorthand is handled" , {
@@ -265,6 +288,16 @@ test_that("function shorthand is handled", {
265288 rex :: rex(" .onDetach() should take one argument starting with 'lib'." ),
266289 linter
267290 )
291+ expect_lint(
292+ " .onUnload <- \\ () { }" ,
293+ rex :: rex(" .onUnload() should take one argument starting with 'lib'." ),
294+ linter
295+ )
296+ expect_lint(
297+ " .onUnload <- \\ (libpath) { }" ,
298+ NULL ,
299+ linter
300+ )
268301})
269302
270303test_that(" lints vectorize" , {
@@ -279,4 +312,16 @@ test_that("lints vectorize", {
279312 ),
280313 package_hooks_linter()
281314 )
315+
316+ expect_lint(
317+ trim_some(" {
318+ .onDetach <- function(xxx) { }
319+ .onUnload <- function(yyy) { }
320+ }" ),
321+ list (
322+ list (" .onDetach" , line_number = 2L ),
323+ list (" .onUnload" , line_number = 3L )
324+ ),
325+ package_hooks_linter()
326+ )
282327})
0 commit comments