File: test-nonportable_path_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 (75 lines) | stat: -rw-r--r-- 2,002 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
test_that("nonportable_path_linter skips allowed usages", {
  linter <- nonportable_path_linter(lax = FALSE)

  # various strings
  non_path_strings <- c(
    "foo",
    "https://cran.r-project.org/web/packages/lintr/",
    encodeString("hello\nthere!")
  )
  for (path in non_path_strings) {
    expect_lint(single_quote(path), NULL, linter)
    expect_lint(double_quote(path), NULL, linter)
  }

  expect_lint("\"'/foo'\"", NULL, linter) # nested quotes

  # system root
  root_path_strings <- c("/", "~", "c:", ".")
  for (path in root_path_strings) {
    expect_lint(single_quote(path), NULL, linter)
    expect_lint(double_quote(path), NULL, linter)
  }
})

test_that("nonportable_path_linter blocks disallowed usages", {
  linter <- nonportable_path_linter(lax = FALSE)
  lint_msg <- rex::escape("Use file.path() to construct portable file paths.")

  # paths with (back)slashes
  slash_path_strings <- c(
    "~/",
    "c:/",
    encodeString("D:\\"),
    "../",
    "/foo",
    "foo/",
    "foo/bar",
    encodeString("foo\\bar"),
    "/as:df",
    encodeString("/a\nsdf")
  )
  for (path in slash_path_strings) {
    expect_lint(single_quote(path), lint_msg, linter)
    expect_lint(double_quote(path), lint_msg, linter)
  }
})

test_that("nonportable_path_linter's lax argument works", {
  # lax mode: no check for strings that are likely not paths (too short or with special characters)
  linter <- nonportable_path_linter(lax = TRUE)

  unlikely_path_strings <- c(
    "/foo", encodeString("/a\nsdf/bar"), "/as:df/bar"
  )
  for (path in unlikely_path_strings) {
    expect_lint(single_quote(path), NULL, linter)
    expect_lint(double_quote(path), NULL, linter)
  }
})

test_that("lints vectorize", {
  lint_msg <- rex::escape("Use file.path() to construct portable file paths.")

  expect_lint(
    trim_some("{
      '~/'
      'C:/'
    }"),
    list(
      list(lint_msg, line_number = 2L),
      list(lint_msg, line_number = 3L)
    ),
    nonportable_path_linter(lax = FALSE)
  )
})