File: test-missing_package_linter.R

package info (click to toggle)
r-cran-lintr 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,396 kB
  • sloc: sh: 13; xml: 10; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,932 bytes parent folder | download
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
test_that("missing_package_linter skips allowed usages", {
  linter <- missing_package_linter()

  expect_lint("library(stats)", NULL, linter)
  expect_lint('library("stats")', NULL, linter)
  expect_lint("library('stats')", NULL, linter)
  expect_lint("library(`stats`)", NULL, linter)
  expect_lint("library(stats, quietly)", NULL, linter)
  expect_lint("library(stats, quietly = TRUE)", NULL, linter)
  expect_lint("require(stats)", NULL, linter)
  expect_lint("require(stats, quietly = TRUE)", NULL, linter)
  expect_lint('loadNamespace("stats")', NULL, linter)
  expect_lint('requireNamespace("stats")', NULL, linter)
})

test_that("missing_package_linter blocks disallowed usages", {
  linter <- missing_package_linter()
  lint_msg <- rex::rex("Package 'statts' is not installed.")

  expect_lint("require(statts)", lint_msg, linter)
  expect_lint("library(statts, quietly = TRUE)", lint_msg, linter)
  expect_lint("library(statts, quietly = TRUE)", lint_msg, linter)
  expect_lint('loadNamespace("statts")', lint_msg, linter)
  expect_lint('requireNamespace("statts")', lint_msg, linter)

  expect_lint(
    trim_some("
      library(utils)
      library(statts)
    "),
    list(lint_msg, line_number = 2L, line = "library(statts)"),
    linter
  )
})

test_that("loadNamespace and requireNamespace allow plain symbols", {
  linter <- missing_package_linter()

  expect_lint("loadNamespace(mypkg)", NULL, linter)
  expect_lint("requireNamespace(mypkg)", NULL, linter)
})

test_that("character.only=TRUE case is handled", {
  linter <- missing_package_linter()

  expect_lint("library(statts, character.only = TRUE)", NULL, linter)
  expect_lint("require(statts, character.only = TRUE)", NULL, linter)
  expect_lint('library("stats", character.only = TRUE)', NULL, linter)

  expect_lint(
    'library("statts", character.only = TRUE)',
    rex::rex("Package 'statts' is not installed."),
    missing_package_linter()
  )
})