| 1 |
test_that("valid names", { |
| 2 |
expect_equal( |
| 3 |
path_sanitize("the quick brown fox jumped over the lazy dog.mp3"), |
| 4 |
"the quick brown fox jumped over the lazy dog.mp3" |
| 5 |
) |
| 6 |
expect_equal(path_sanitize("résumé"), "résumé") |
| 7 |
}) |
| 8 |
|
| 9 |
test_that("valid names", { |
| 10 |
expect_equal( |
| 11 |
path_sanitize("the quick brown fox jumped over the lazy dog.mp3", "_"), |
| 12 |
"the quick brown fox jumped over the lazy dog.mp3" |
| 13 |
) |
| 14 |
expect_equal(path_sanitize("résumé", "_"), "résumé") |
| 15 |
}) |
| 16 |
|
| 17 |
test_that("control characters", { |
| 18 |
expect_equal(path_sanitize("hello\nworld"), "helloworld") |
| 19 |
}) |
| 20 |
|
| 21 |
test_that("control characters", { |
| 22 |
expect_equal(path_sanitize("hello\nworld", "_"), "hello_world") |
| 23 |
}) |
| 24 |
|
| 25 |
test_that("restricted codes", { |
| 26 |
expect_equal(path_sanitize("h?w"), "hw") |
| 27 |
expect_equal(path_sanitize("h/w"), "hw") |
| 28 |
expect_equal(path_sanitize("h*w"), "hw") |
| 29 |
}) |
| 30 |
|
| 31 |
test_that("restricted codes", { |
| 32 |
expect_equal(path_sanitize("h?w", "_"), "h_w") |
| 33 |
expect_equal(path_sanitize("h/w", "_"), "h_w") |
| 34 |
expect_equal(path_sanitize("h*w", "_"), "h_w") |
| 35 |
}) |
| 36 |
|
| 37 |
# https://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx |
| 38 |
test_that("restricted suffixes", { |
| 39 |
expect_equal(path_sanitize("mr."), "mr") |
| 40 |
expect_equal(path_sanitize("mr.."), "mr") |
| 41 |
expect_equal(path_sanitize("mr "), "mr") |
| 42 |
expect_equal(path_sanitize("mr "), "mr") |
| 43 |
}) |
| 44 |
|
| 45 |
test_that("relative paths", { |
| 46 |
expect_equal(path_sanitize("."), "") |
| 47 |
expect_equal(path_sanitize(".."), "") |
| 48 |
expect_equal(path_sanitize("./"), "") |
| 49 |
expect_equal(path_sanitize("../"), "") |
| 50 |
expect_equal(path_sanitize("/.."), "") |
| 51 |
expect_equal(path_sanitize("/../"), "") |
| 52 |
expect_equal(path_sanitize("*.|."), "") |
| 53 |
}) |
| 54 |
|
| 55 |
test_that("relative path with replacement", { |
| 56 |
expect_equal(path_sanitize("..", "_"), "_") |
| 57 |
}) |
| 58 |
|
| 59 |
test_that("reserved filename in Windows", { |
| 60 |
expect_equal(path_sanitize("con"), "") |
| 61 |
expect_equal(path_sanitize("COM1"), "") |
| 62 |
expect_equal(path_sanitize("PRN."), "") |
| 63 |
expect_equal(path_sanitize("aux.txt"), "") |
| 64 |
expect_equal(path_sanitize("LPT9.asdfasdf"), "") |
| 65 |
expect_equal(path_sanitize("LPT10.txt"), "LPT10.txt") |
| 66 |
}) |
| 67 |
|
| 68 |
test_that("reserved filename in Windows with replacement", { |
| 69 |
expect_equal(path_sanitize("con", "_"), "_") |
| 70 |
expect_equal(path_sanitize("COM1", "_"), "_") |
| 71 |
expect_equal(path_sanitize("PRN.", "_"), "_") |
| 72 |
expect_equal(path_sanitize("aux.txt", "_"), "_") |
| 73 |
expect_equal(path_sanitize("LPT9.asdfasdf", "_"), "_") |
| 74 |
expect_equal(path_sanitize("LPT10.txt", "_"), "LPT10.txt") |
| 75 |
}) |
| 76 |
|
| 77 |
test_that("invalid replacement", { |
| 78 |
expect_equal(path_sanitize(".", "."), "") |
| 79 |
expect_equal(path_sanitize("foo?.txt", ">"), "foo.txt") |
| 80 |
expect_equal(path_sanitize("con.txt", "aux"), "") |
| 81 |
expect_equal(path_sanitize("valid.txt", "\\/:*?\"<>|"), "valid.txt") |
| 82 |
}) |
| 83 |
|
| 84 |
test_string_fs <- function(str, tmpdir) { |
| 85 |
sanitized <- path_sanitize(str) |
| 86 |
if (sanitized == "") { |
| 87 |
sanitized <- "default" |
| 88 |
} |
| 89 |
filepath <- path(tmpdir, sanitized) |
| 90 |
|
| 91 |
# Should not contain any directories or relative paths |
| 92 |
expect_equal(path_dir(path("/abs/path", sanitized)), "/abs/path") |
| 93 |
|
| 94 |
# Should write and read file to disk |
| 95 |
expect_equal(path_dir(path_tidy(filepath)), as.character(tmpdir)) |
| 96 |
expect_error_free( |
| 97 |
writeLines("foobar", filepath) |
| 98 |
) |
| 99 |
expect_equal(readLines(filepath), "foobar") |
| 100 |
expect_error_free(file_delete(filepath)) |
| 101 |
} |
| 102 |
|
| 103 |
test_that("filesystems can read, write and delete sanitized files", { |
| 104 |
skip_if_not_utf8() |
| 105 |
skip_on_os("solaris") |
| 106 |
|
| 107 |
strings <- c( |
| 108 |
# Windows R functions cannot handle the unicode filenames or long filenames + paths correctly. |
| 109 |
if (!is_windows()) { |
| 110 |
readLines(testthat::test_path("blns.txt.xz"), encoding = "UTF-8") |
| 111 |
}, |
| 112 |
"the quick brown fox jumped over the lazy dog", |
| 113 |
"résumé", |
| 114 |
"hello\nworld", |
| 115 |
"semi;colon.js", |
| 116 |
";leading-semi.js", |
| 117 |
"mode_tslash\\.js", |
| 118 |
"slash/.js", |
| 119 |
"col:on.js", |
| 120 |
"star*.js", |
| 121 |
"question?.js", |
| 122 |
"quote\".js", |
| 123 |
"singlequote'.js", |
| 124 |
"brack<e>ts.js", |
| 125 |
"p|pes.js", |
| 126 |
"plus+.js", |
| 127 |
"'five and six<seven'.js", |
| 128 |
" space at front", |
| 129 |
"space at end ", |
| 130 |
".period", |
| 131 |
"period.", |
| 132 |
"relative/path/to/some/dir", |
| 133 |
"/abs/path/to/some/dir", |
| 134 |
"~/.notssh/authorized_keys", |
| 135 |
"", |
| 136 |
"h?w", |
| 137 |
"h/w", |
| 138 |
"h*w", |
| 139 |
".", |
| 140 |
"..", |
| 141 |
"./", |
| 142 |
"../", |
| 143 |
"/..", |
| 144 |
"/../", |
| 145 |
"*.|.", |
| 146 |
"./", |
| 147 |
"./foobar", |
| 148 |
"../foobar", |
| 149 |
"../../foobar", |
| 150 |
"./././foobar", |
| 151 |
"|*.what", |
| 152 |
"LPT9.asdf" |
| 153 |
) |
| 154 |
|
| 155 |
td <- dir_create(file_temp()) |
| 156 |
on.exit(dir_delete(td)) |
| 157 |
for (str in strings) { |
| 158 |
test_string_fs(str, td) |
| 159 |
} |
| 160 |
}) |