File: test-normalize_exclusions.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 (153 lines) | stat: -rw-r--r-- 4,685 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
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
withr::local_options(list(
  lintr.exclude = "#TeSt_NoLiNt",
  lintr.exclude_start = "#TeSt_NoLiNt_StArT",
  lintr.exclude_end = "#TeSt_NoLiNt_EnD"
))

a <- withr::local_tempfile()
b <- withr::local_tempfile()
c <- withr::local_tempfile(tmpdir = ".")
file.create(a, b, c)
a <- normalize_path(a)
b <- normalize_path(b)
c <- normalize_path(c)

test_that("it merges two NULL or empty objects as an empty list", {
  expect_identical(lintr:::normalize_exclusions(c(NULL, NULL)), list())
  expect_identical(lintr:::normalize_exclusions(c(NULL, list())), list())
  expect_identical(lintr:::normalize_exclusions(c(list(), NULL)), list())
  expect_identical(lintr:::normalize_exclusions(c(list(), list())), list())
})

test_that("it returns the object if the other is NULL", {
  t1 <- list()
  t1[[a]] <- list(1L:10L)
  expect_identical(lintr:::normalize_exclusions(c(t1, NULL)), t1)
  expect_identical(lintr:::normalize_exclusions(c(NULL, t1)), t1)
})

test_that("it returns the union of two non-overlapping lists", {
  t1 <- list()
  t1[[a]] <- list(1L:10L)
  t2 <- list()
  t2[[a]] <- list(20L:30L)
  res <- list()
  res[[a]] <- list(c(1L:10L, 20L:30L))
  expect_identical(lintr:::normalize_exclusions(c(t1, t2)), res)
})

test_that("it works with named lists", {
  t1 <- list()
  t1[[a]] <- list(1L:10L, my_linter = 1L:20L)
  t2 <- list()
  t2[[a]] <- list(11L:15L, my_linter = 21L:25L)
  res <- list()
  res[[a]] <- list(1L:15L, my_linter = 1L:25L)
  expect_identical(lintr:::normalize_exclusions(c(t1, t2)), res)
})

test_that("it returns the union of two overlapping lists", {
  t1 <- list()
  t1[[a]] <- list(1L:10L)
  t2 <- list()
  t2[[a]] <- list(5L:15L)
  res <- list()
  res[[a]] <- list(1L:15L)
  expect_identical(lintr:::normalize_exclusions(c(t1, t2)), res)
})

test_that("it adds names if needed", {
  t1 <- list()
  t1[[a]] <- list(1L:10L)
  t2 <- list()
  t2[[b]] <- list(5L:15L)
  res <- list()
  res[[a]] <- list(1L:10L)
  res[[b]] <- list(5L:15L)
  expect_identical(lintr:::normalize_exclusions(c(t1, t2)), res)
})

test_that("it handles full file exclusions", {
  res <- list()
  res[[a]] <- list(Inf)
  expect_identical(lintr:::normalize_exclusions(list(a)), res)

  t1 <- list()
  t1[[1L]] <- a
  t1[[b]] <- 1L
  res <- list()
  res[[a]] <- list(Inf)
  res[[b]] <- list(1L)
  expect_identical(lintr:::normalize_exclusions(t1), res)
})

test_that("it handles redundant lines", {
  t1 <- list()
  t1[[a]] <- list(c(1L, 1L, 1L:10L))
  res <- list()
  res[[a]] <- list(1L:10L)
  expect_identical(lintr:::normalize_exclusions(t1), res)

  t1 <- list()
  t1[[a]] <- list(c(1L, 1L, 1L:10L))
  t1[[b]] <- list(1L:10L)
  res <- list()
  res[[a]] <- list(1L:10L)
  res[[b]] <- list(1L:10L)
  expect_identical(lintr:::normalize_exclusions(t1), res)
})

test_that("it handles redundant linters", {
  t1 <- list()
  # nolint next: duplicate_argument_linter.
  t1[[a]] <- list(c(1L, 1L, 1L:10L), my_linter = c(1L, 1L, 1L, 2L), my_linter = 3L)
  res <- list()
  res[[a]] <- list(1L:10L, my_linter = 1L:3L)
  expect_identical(lintr:::normalize_exclusions(t1), res)

  t1 <- list()
  t1[[a]] <- list(c(1L, 1L, 1L:10L), my_linter = c(1L, 1L, 1L, 2L))
  # nolint next: duplicate_argument_linter.
  t1[[b]] <- list(1L:10L, my_linter = 1L:10L, my_linter = 11L:20L)
  res <- list()
  res[[a]] <- list(1L:10L, my_linter = 1L:2L)
  res[[b]] <- list(1L:10L, my_linter = 1L:20L)
  expect_identical(lintr:::normalize_exclusions(t1), res)
})

test_that("it handles redundant files", {
  t1 <- list(1L:10L, 10L:20L)
  names(t1) <- c(a, a)
  res <- list()
  res[[a]] <- list(1L:20L)
  expect_identical(lintr:::normalize_exclusions(t1), res)
})

test_that("it normalizes file paths, removing non-existing files", {
  t1 <- list()
  t1[[a]] <- 1L:10L
  t2 <- list()
  t2[["notafile"]] <- 5L:15L
  t3 <- list()
  t3[[c]] <- 5L:15L
  res <- list()
  res[[a]] <- list(1L:10L)
  res[[c]] <- list(5L:15L)
  expect_identical(lintr:::normalize_exclusions(c(t1, t2, t3)), res)

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

test_that("it errors for invalid specifications", {
  msg_full_files <- "Full file exclusions must be <character> vectors of length 1."
  expect_error(lintr:::normalize_exclusions(2L), msg_full_files)
  expect_error(lintr:::normalize_exclusions(list("a.R", 2L)), msg_full_files)
  expect_error(lintr:::normalize_exclusions(list(a.R = Inf, 2L)), msg_full_files)

  msg_full_lines <- "Full line exclusions must be <numeric> or <integer> vectors."
  expect_error(lintr:::normalize_exclusions(list(a.R = "Inf")), msg_full_lines)
})