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
|
# styler: off
test_that("is_root_path", {
f <- lintr:::is_root_path
x <- character()
y <- logical()
expect_identical(f(x), y)
x <- c("", "foo", "http://rseek.org/", "./", " /", "/foo", "'/'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
expect_identical(f(x), y)
x <- c("/", "//")
y <- c(TRUE, FALSE)
expect_identical(f(x), y)
x <- c("~", "~/", "~//", "~bob2", "~foo_bar/")
y <- c(TRUE, TRUE, TRUE, TRUE, TRUE)
expect_identical(f(x), y)
x <- c("c:", "C:\\", "D:/", "C:\\\\", "D://")
y <- c(TRUE, TRUE, TRUE, FALSE, FALSE)
expect_identical(f(x), y)
x <- c("\\\\", "\\\\localhost", "\\\\localhost\\")
y <- c(TRUE, TRUE, TRUE)
expect_identical(f(x), y)
})
test_that("is_absolute_path", {
f <- lintr:::is_absolute_path
x <- character()
y <- logical()
expect_identical(f(x), y)
x <- c("/", "//", "/foo", "/foo/")
y <- c(TRUE, FALSE, TRUE, TRUE)
expect_identical(f(x), y)
x <- c("~", "~/foo", "~/foo/", "~'")
y <- c(TRUE, TRUE, TRUE, FALSE)
expect_identical(f(x), y)
x <- c("c:", "C:\\foo\\", "C:/foo/")
y <- c(TRUE, TRUE, TRUE)
expect_identical(f(x), y)
x <- c("\\\\", "\\\\localhost", "\\\\localhost\\c$", "\\\\localhost\\c$\\foo")
y <- c(TRUE, TRUE, TRUE, TRUE)
expect_identical(f(x), y)
})
test_that("is_relative_path", {
f <- lintr:::is_relative_path
x <- character()
y <- logical()
expect_identical(f(x), y)
x <- c("/", "c:\\", "~/", "foo", "http://rseek.org/", "'./'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
expect_identical(f(x), y)
x <- c("/foo", "foo/", "foo/bar", "foo//bar", "./foo", "../foo")
y <- c(FALSE, TRUE, TRUE, TRUE, TRUE, TRUE)
expect_identical(f(x), y)
x <- c("\\\\", "\\foo", "foo\\", "foo\\bar", ".\\foo", "..\\foo", ".", "..", "../")
y <- c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)
expect_identical(f(x), y)
})
test_that("is_path", {
f <- lintr:::is_path
x <- character()
y <- logical()
expect_identical(f(x), y)
x <- c("", "foo", "http://rseek.org/", "foo\nbar", "'foo/bar'", "'/'")
y <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
expect_identical(f(x), y)
x <- c("c:", "..", "foo/bar", "foo\\bar", "~", "\\\\localhost")
y <- c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)
expect_identical(f(x), y)
})
test_that("is_valid_path", {
f <- lintr:::is_valid_path
x <- character()
y <- logical()
expect_identical(f(x), y)
x <- c("C:/asdf", "C:/asd*f", "a\\s:df", "a\\\nsdf")
y <- c(TRUE, FALSE, FALSE, FALSE)
expect_identical(f(x), y)
x <- c("C:/asdf", "C:/asd*f", "a\\s:df", "a\\\nsdf")
y <- c(TRUE, FALSE, FALSE, FALSE)
expect_identical(f(x, lax = TRUE), y)
x <- c("/asdf", "/asd*f", "/as:df", "/a\nsdf")
y <- c(TRUE, TRUE, TRUE, TRUE)
expect_identical(f(x), y)
x <- c("/asdf", "/asd*f", "/as:df", "/a\nsdf")
y <- c(TRUE, FALSE, FALSE, FALSE)
expect_identical(f(x, lax = TRUE), y)
})
test_that("is_long_path", {
f <- lintr:::is_long_path
x <- character()
y <- logical()
expect_identical(f(x), y)
x <- c("foo/", "/foo", "n/a", "Z:\\foo", "foo/bar", "~/foo", "../foo")
y <- c(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
expect_identical(f(x), y)
})
# styler: on
test_that("returns the correct linting", {
lint_msg <- rex::escape("Do not use absolute paths.")
# strict mode
linter <- absolute_path_linter(lax = FALSE)
non_absolute_path_strings <- c(
"..",
"./blah",
encodeString("blah\\file.txt")
)
for (path in non_absolute_path_strings) {
expect_lint(single_quote(path), NULL, linter)
expect_lint(double_quote(path), NULL, linter)
}
expect_lint("\"'/'\"", NULL, linter) # nested quotes
absolute_path_strings <- c(
"/",
"/blah/file.txt",
encodeString("d:\\"),
"E:/blah/file.txt",
encodeString("\\\\"),
encodeString("\\\\server\\path"),
"~",
"~james.hester/blah/file.txt",
encodeString("/a\nsdf"),
"/as:df"
)
for (path in absolute_path_strings) {
expect_lint(single_quote(path), lint_msg, linter)
expect_lint(double_quote(path), lint_msg, linter)
}
# lax mode: no check for strings that are likely not paths (too short or with special characters)
linter <- absolute_path_linter(lax = TRUE)
unlikely_path_strings <- c(
"/",
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("raw strings are handled correctly", {
skip_if_not_r_version("4.0.0")
expect_lint('R"(./blah)"', NULL, absolute_path_linter(lax = FALSE))
expect_lint(
"R'--[/blah/file.txt]--'",
rex::rex("Do not use absolute paths."),
absolute_path_linter(lax = FALSE)
)
})
test_that("lints vectorize", {
lint_msg <- rex::rex("Do not use absolute paths.")
expect_lint(
trim_some("{
'/'
'/blah/file.txt'
'abcdefg'
'~'
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 3L),
list(lint_msg, line_number = 5L)
),
absolute_path_linter(lax = FALSE)
)
})
|