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
|
context("sanitize")
test_that("valid names", {
expect_equal(path_sanitize("the quick brown fox jumped over the lazy dog.mp3"), "the quick brown fox jumped over the lazy dog.mp3")
expect_equal(path_sanitize("résumé"), "résumé")
})
test_that("valid names", {
expect_equal(path_sanitize("the quick brown fox jumped over the lazy dog.mp3", "_"), "the quick brown fox jumped over the lazy dog.mp3")
expect_equal(path_sanitize("résumé", "_"), "résumé")
})
test_that("control characters", {
expect_equal(path_sanitize("hello\nworld"), "helloworld")
})
test_that("control characters", {
expect_equal(path_sanitize("hello\nworld", "_"), "hello_world")
})
test_that("restricted codes", {
expect_equal(path_sanitize("h?w"), "hw")
expect_equal(path_sanitize("h/w"), "hw")
expect_equal(path_sanitize("h*w"), "hw")
})
test_that("restricted codes", {
expect_equal(path_sanitize("h?w", "_"), "h_w")
expect_equal(path_sanitize("h/w", "_"), "h_w")
expect_equal(path_sanitize("h*w", "_"), "h_w")
})
# https://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx
test_that("restricted suffixes", {
expect_equal(path_sanitize("mr."), "mr")
expect_equal(path_sanitize("mr.."), "mr")
expect_equal(path_sanitize("mr "), "mr")
expect_equal(path_sanitize("mr "), "mr")
})
test_that("relative paths", {
expect_equal(path_sanitize("."), "")
expect_equal(path_sanitize(".."), "")
expect_equal(path_sanitize("./"), "")
expect_equal(path_sanitize("../"), "")
expect_equal(path_sanitize("/.."), "")
expect_equal(path_sanitize("/../"), "")
expect_equal(path_sanitize("*.|."), "")
})
test_that("relative path with replacement", {
expect_equal(path_sanitize("..", "_"), "_")
})
test_that("reserved filename in Windows", {
expect_equal(path_sanitize("con"), "")
expect_equal(path_sanitize("COM1"), "")
expect_equal(path_sanitize("PRN."), "")
expect_equal(path_sanitize("aux.txt"), "")
expect_equal(path_sanitize("LPT9.asdfasdf"), "")
expect_equal(path_sanitize("LPT10.txt"), "LPT10.txt")
})
test_that("reserved filename in Windows with replacement", {
expect_equal(path_sanitize("con", "_"), "_")
expect_equal(path_sanitize("COM1", "_"), "_")
expect_equal(path_sanitize("PRN.", "_"), "_")
expect_equal(path_sanitize("aux.txt", "_"), "_")
expect_equal(path_sanitize("LPT9.asdfasdf", "_"), "_")
expect_equal(path_sanitize("LPT10.txt", "_"), "LPT10.txt")
})
test_that("invalid replacement", {
expect_equal(path_sanitize(".", "."), "")
expect_equal(path_sanitize("foo?.txt", ">"), "foo.txt")
expect_equal(path_sanitize("con.txt", "aux"), "")
expect_equal(path_sanitize("valid.txt", "\\/:*?\"<>|"), "valid.txt")
})
test_string_fs <- function(str, tmpdir) {
sanitized <- path_sanitize(str)
if (sanitized == "") {
sanitized <- "default"
}
filepath <- path(tmpdir, sanitized)
# Should not contain any directories or relative paths
expect_equal(path_dir(path("/abs/path", sanitized)), "/abs/path")
# Should write and read file to disk
expect_equal(path_dir(path_tidy(filepath)), tmpdir)
expect_error_free(
writeLines("foobar", filepath))
expect_equal(readLines(filepath), "foobar")
expect_error_free(file_delete(filepath))
}
test_that("filesystems can read, write and delete sanitized files", {
skip_on_os("solaris")
strings <- c(
# Windows R functions cannot handle the unicode filenames or long filenames + paths correctly.
if (!is_windows()) {
readLines("blns.txt.xz", encoding = "UTF-8")
},
"the quick brown fox jumped over the lazy dog",
"résumé",
"hello\nworld",
"semi;colon.js",
";leading-semi.js",
"slash\\.js",
"slash/.js",
"col:on.js",
"star*.js",
"question?.js",
"quote\".js",
"singlequote'.js",
"brack<e>ts.js",
"p|pes.js",
"plus+.js",
"'five and six<seven'.js",
" space at front",
"space at end ",
".period",
"period.",
"relative/path/to/some/dir",
"/abs/path/to/some/dir",
"~/.notssh/authorized_keys",
"",
"h?w",
"h/w",
"h*w",
".",
"..",
"./",
"../",
"/..",
"/../",
"*.|.",
"./",
"./foobar",
"../foobar",
"../../foobar",
"./././foobar",
"|*.what",
"LPT9.asdf")
td <- dir_create(file_temp())
on.exit(dir_delete(td))
for (str in strings) {
test_string_fs(str, td)
}
})
|