| 1 |
describe("path", { |
| 2 |
it("returns paths UTF-8 encoded", { |
| 3 |
skip_on_os("solaris") |
| 4 |
expect_equal(Encoding(path("föö")), "UTF-8") |
| 5 |
}) |
| 6 |
|
| 7 |
it("returns paths UTF-8 encoded", { |
| 8 |
skip_on_os("solaris") |
| 9 |
expect_equal(Encoding(path("\U4F60\U597D.R")), "UTF-8") |
| 10 |
}) |
| 11 |
|
| 12 |
it("returns empty strings for empty inputs", { |
| 13 |
expect_equal(path(""), fs_path("")) |
| 14 |
expect_equal(path(character()), fs_path(character())) |
| 15 |
expect_equal(path("foo", character(), "bar"), fs_path(character())) |
| 16 |
}) |
| 17 |
|
| 18 |
it("propagates NA strings", { |
| 19 |
expect_equal(path(NA_character_), fs_path(NA_character_)) |
| 20 |
expect_equal(path("foo", NA_character_), fs_path(NA_character_)) |
| 21 |
expect_equal( |
| 22 |
path(c("foo", "bar"), c("baz", NA_character_)), |
| 23 |
fs_path(c("foo/baz", NA_character_)) |
| 24 |
) |
| 25 |
}) |
| 26 |
|
| 27 |
it("appends a ext if provided", { |
| 28 |
expect_equal(path("foo", ext = "bar"), fs_path("foo.bar")) |
| 29 |
expect_equal( |
| 30 |
path(c("foo", "baz"), ext = "bar"), |
| 31 |
fs_path(c("foo.bar", "baz.bar")) |
| 32 |
) |
| 33 |
expect_equal( |
| 34 |
path(c("foo", "baz", NA_character_), ext = "bar"), |
| 35 |
fs_path(c("foo.bar", "baz.bar", NA_character_)) |
| 36 |
) |
| 37 |
}) |
| 38 |
|
| 39 |
it("does not double paths", { |
| 40 |
expect_equal(path("/", "foo"), fs_path("/foo")) |
| 41 |
expect_equal(path("\\", "foo"), fs_path("/foo")) |
| 42 |
expect_equal(path("", "foo"), fs_path("/foo")) |
| 43 |
expect_equal(path("foo/", "bar"), fs_path("foo/bar")) |
| 44 |
expect_equal(path("foo\\", "bar"), fs_path("foo/bar")) |
| 45 |
expect_equal(path("foo//", "bar"), fs_path("foo/bar")) |
| 46 |
|
| 47 |
# This could be a UNC path, so we keep the doubled path. |
| 48 |
expect_equal(path("//", "foo"), fs_path("//foo")) |
| 49 |
}) |
| 50 |
|
| 51 |
it("errors on paths which are too long", { |
| 52 |
expect_snapshot( |
| 53 |
error = TRUE, |
| 54 |
{ |
| 55 |
path(paste(rep("a", 100000), collapse = "")) |
| 56 |
do.call(path, as.list(rep("a", 100000))) |
| 57 |
}, |
| 58 |
transform = transform_path_max |
| 59 |
) |
| 60 |
}) |
| 61 |
|
| 62 |
it("follows recycling rules", { |
| 63 |
expect_equal(path("foo", character()), fs_path(character())) |
| 64 |
expect_equal(path("foo", "bar"), fs_path("foo/bar")) |
| 65 |
expect_equal( |
| 66 |
path("foo", c("bar", "baz")), |
| 67 |
fs_path(c("foo/bar", "foo/baz")) |
| 68 |
) |
| 69 |
expect_equal( |
| 70 |
path(c("foo", "qux"), c("bar", "baz")), |
| 71 |
fs_path(c("foo/bar", "qux/baz")) |
| 72 |
) |
| 73 |
|
| 74 |
expect_error( |
| 75 |
path(c("foo", "qux", "foo2"), c("bar", "baz")), |
| 76 |
"Arguments must have consistent lengths", |
| 77 |
class = "invalid_argument" |
| 78 |
) |
| 79 |
|
| 80 |
expect_equal(path(ext = character()), fs_path(character())) |
| 81 |
expect_equal(path("foo", ext = character()), fs_path(character())) |
| 82 |
expect_equal(path("foo", ext = "bar"), fs_path("foo.bar")) |
| 83 |
expect_equal( |
| 84 |
path("foo", ext = c("bar", "baz")), |
| 85 |
fs_path(c("foo.bar", "foo.baz")) |
| 86 |
) |
| 87 |
expect_equal( |
| 88 |
path(c("foo", "qux"), ext = c("bar", "baz")), |
| 89 |
fs_path(c("foo.bar", "qux.baz")) |
| 90 |
) |
| 91 |
|
| 92 |
expect_error( |
| 93 |
path(c("foo", "qux", "foo2"), ext = c("bar", "baz")), |
| 94 |
"Arguments must have consistent lengths", |
| 95 |
class = "invalid_argument" |
| 96 |
) |
| 97 |
}) |
| 98 |
}) |
| 99 |
|
| 100 |
describe("path_wd", { |
| 101 |
it("returns an absolute path from the working directory", { |
| 102 |
x <- path_wd() |
| 103 |
expect_true(is_absolute_path(x)) |
| 104 |
|
| 105 |
expect_equal(x, path(getwd())) |
| 106 |
}) |
| 107 |
}) |
| 108 |
|
| 109 |
describe("path_real", { |
| 110 |
it("returns the real path for symbolic links", { |
| 111 |
with_dir_tree(list("foo/bar" = "test"), { |
| 112 |
link_create(path_real("foo"), "foo2") |
| 113 |
expect_equal(path_real("foo2"), path_real("foo")) |
| 114 |
}) |
| 115 |
}) |
| 116 |
|
| 117 |
it("works with indirect symlinks", { |
| 118 |
skip_on_os("windows") |
| 119 |
|
| 120 |
with_dir_tree("foo", { |
| 121 |
wd <- path_wd() |
| 122 |
link_create(path("..", path_file(wd)), "self") |
| 123 |
link_create("self", "link") |
| 124 |
expect_equal(path_real("link"), wd) |
| 125 |
}) |
| 126 |
}) |
| 127 |
|
| 128 |
it("works with parent symlinks", { |
| 129 |
skip_on_os("windows") |
| 130 |
|
| 131 |
# If there are symlinks in the parents of relative paths we need to resolve |
| 132 |
# them. |
| 133 |
# e.g. working directory is /usr/doc with 'doc' being a symlink to |
| 134 |
# /usr/share/doc. If we call `path_real("a")` we should return |
| 135 |
# /usr/share/doc/a. |
| 136 |
with_dir_tree("y", { |
| 137 |
wd <- path_wd() |
| 138 |
link_create("y", "k") |
| 139 |
withr::with_dir("k", { |
| 140 |
file_create("a") |
| 141 |
expect_equal(path_real("a"), path(wd, "/y/a")) |
| 142 |
unlink("a") |
| 143 |
}) |
| 144 |
}) |
| 145 |
}) |
| 146 |
|
| 147 |
it("resolves paths before normalizing", { |
| 148 |
skip_on_os("windows") |
| 149 |
|
| 150 |
# if we have the following hierarchy: a/k/y |
| 151 |
# and a symbolic link 'link-y' pointing to 'y' in directory 'a', |
| 152 |
# then `path_real("link-y/..")` should return 'k', not 'a'. |
| 153 |
with_dir_tree("a/k/y", { |
| 154 |
link_create("a/k/y", "link-y") |
| 155 |
|
| 156 |
expect_equal(path_real("link-y/.."), path_real("a/k")) |
| 157 |
}) |
| 158 |
}) |
| 159 |
|
| 160 |
it("resolves paths before normalizing", { |
| 161 |
skip_on_os("windows") |
| 162 |
|
| 163 |
# if we have the following hierarchy: a/k/y |
| 164 |
# and a symbolic link 'link-y' pointing to 'y' in directory 'a', |
| 165 |
# then `path_real("link-y/..")` should return 'k', not 'a'. |
| 166 |
with_dir_tree("k", { |
| 167 |
wd <- path_wd() |
| 168 |
link_create(wd, "link") |
| 169 |
|
| 170 |
withr::with_dir(path_dir(wd), { |
| 171 |
base <- path_file(wd) |
| 172 |
expect_equal(path_real(path(base, "link")), wd) |
| 173 |
expect_equal(path_real(path(base, "link/k")), path(wd, "k")) |
| 174 |
}) |
| 175 |
}) |
| 176 |
}) |
| 177 |
|
| 178 |
it("propagates NAs", { |
| 179 |
with_dir_tree(list("foo/bar" = "test"), { |
| 180 |
link_create(path_real("foo"), fs_path("foo2")) |
| 181 |
expect_equal(path_real(NA_character_), fs_path(NA_character_)) |
| 182 |
expect_equal( |
| 183 |
path_real(c("foo2", NA_character_)), |
| 184 |
fs_path(c(path_real("foo"), NA_character_)) |
| 185 |
) |
| 186 |
}) |
| 187 |
}) |
| 188 |
|
| 189 |
it("converts inputs to character if required", { |
| 190 |
with_dir_tree(list("foo/bar" = "test"), { |
| 191 |
link_create(path_real("foo"), "2") |
| 192 |
expect_equal(path_real(NA), fs_path(NA_character_)) ## NA (logical) coerced to NA_character_ |
| 193 |
expect_equal(path_real(2), path_real("2")) |
| 194 |
}) |
| 195 |
}) |
| 196 |
}) |
| 197 |
|
| 198 |
describe("path_split", { |
| 199 |
it("returns the path split", { |
| 200 |
expect_equal(path_split("foo/bar")[[1]], c("foo", "bar")) |
| 201 |
expect_equal( |
| 202 |
path_split(c("foo/bar", "foo/baz")), |
| 203 |
list(c("foo", "bar"), c("foo", "baz")) |
| 204 |
) |
| 205 |
}) |
| 206 |
|
| 207 |
it("does not split the root path", { |
| 208 |
expect_equal(path_split("/usr/bin")[[1]], c("/", "usr", "bin")) |
| 209 |
expect_equal(path_split("c:/usr/bin")[[1]], c("C:", "usr", "bin")) |
| 210 |
expect_equal(path_split("X:/usr/bin")[[1]], c("X:", "usr", "bin")) |
| 211 |
expect_equal( |
| 212 |
path_split("//server/usr/bin")[[1]], |
| 213 |
c("//server", "usr", "bin") |
| 214 |
) |
| 215 |
expect_equal( |
| 216 |
path_split("\\\\server\\usr\\bin")[[1]], |
| 217 |
c("//server", "usr", "bin") |
| 218 |
) |
| 219 |
}) |
| 220 |
}) |
| 221 |
|
| 222 |
describe("path_tidy", { |
| 223 |
it("does not expand ~", { |
| 224 |
expect_equal(path_tidy("~/foo"), fs_path("~/foo")) |
| 225 |
expect_equal(path_tidy("~/foo/"), fs_path("~/foo")) |
| 226 |
expect_equal(path_tidy("~//foo"), fs_path("~/foo")) |
| 227 |
expect_equal(path_tidy("~//foo"), fs_path("~/foo")) |
| 228 |
expect_equal(path_tidy("~\\foo\\"), fs_path("~/foo")) |
| 229 |
}) |
| 230 |
|
| 231 |
it("always uses / for delimiting, never multiple / or trailing /", { |
| 232 |
expect_equal(path_tidy("foo/bar/baz"), fs_path("foo/bar/baz")) |
| 233 |
expect_equal(path_tidy("foo/bar/baz/"), fs_path("foo/bar/baz")) |
| 234 |
expect_equal(path_tidy("foo//bar//baz"), fs_path("foo/bar/baz")) |
| 235 |
expect_equal(path_tidy("foo//bar//baz//"), fs_path("foo/bar/baz")) |
| 236 |
expect_equal(path_tidy("foo\\bar\\baz"), fs_path("foo/bar/baz")) |
| 237 |
expect_equal(path_tidy("foo\\\\bar\\\\baz"), fs_path("foo/bar/baz")) |
| 238 |
expect_equal(path_tidy("//foo\\\\bar\\\\baz"), fs_path("//foo/bar/baz")) |
| 239 |
expect_equal(path_tidy("foo\\\\bar\\\\baz\\"), fs_path("foo/bar/baz")) |
| 240 |
expect_equal(path_tidy("foo\\\\bar\\\\baz\\\\"), fs_path("foo/bar/baz")) |
| 241 |
}) |
| 242 |
|
| 243 |
it("always capitalizes and appends windows root paths with /", { |
| 244 |
expect_equal(path_tidy("C:"), fs_path("C:/")) |
| 245 |
expect_equal(path_tidy("c:"), fs_path("C:/")) |
| 246 |
expect_equal(path_tidy("X:"), fs_path("X:/")) |
| 247 |
expect_equal(path_tidy("x:"), fs_path("X:/")) |
| 248 |
expect_equal(path_tidy("c:/"), fs_path("C:/")) |
| 249 |
expect_equal(path_tidy("c://"), fs_path("C:/")) |
| 250 |
expect_equal(path_tidy("c:\\"), fs_path("C:/")) |
| 251 |
expect_equal(path_tidy("c:\\\\"), fs_path("C:/")) |
| 252 |
}) |
| 253 |
|
| 254 |
it("passes NA along", { |
| 255 |
expect_equal(path_tidy(NA_character_), fs_path(NA_character_)) |
| 256 |
expect_equal( |
| 257 |
path_tidy(c("foo/bar", NA_character_)), |
| 258 |
fs_path(c("foo/bar", NA_character_)) |
| 259 |
) |
| 260 |
}) |
| 261 |
|
| 262 |
it("works for non-UTF8 path", { |
| 263 |
x <- "folder//fa\xE7ile.txt/" |
| 264 |
Encoding(x) <- "latin1" |
| 265 |
out <- fs::path_tidy(x) |
| 266 |
expect_equal(Encoding(out), "UTF-8") |
| 267 |
expect_equal(out, fs_path("folder/façile.txt")) |
| 268 |
}) |
| 269 |
|
| 270 |
it("converts inputs to character if required", { |
| 271 |
expect_equal( |
| 272 |
path_tidy(c("foo/bar", NA)), |
| 273 |
fs_path(c("foo/bar", NA_character_)) |
| 274 |
) |
| 275 |
expect_equal(path_tidy(1), fs_path("1")) |
| 276 |
}) |
| 277 |
}) |
| 278 |
|
| 279 |
describe("path_temp", { |
| 280 |
it("returned tidies tempdir()", { |
| 281 |
expect_equal(path_temp(), path_tidy(tempdir())) |
| 282 |
}) |
| 283 |
}) |
| 284 |
|
| 285 |
describe("path_ext", { |
| 286 |
it("returns 0 length outputs for 0 length inputs", { |
| 287 |
expect_equal(path_ext(character()), character()) |
| 288 |
}) |
| 289 |
|
| 290 |
it("returns the path extension, or \"\" if one does not exist", { |
| 291 |
expect_equal(path_ext("foo.bar"), "bar") |
| 292 |
expect_equal(path_ext("foo.boo.bar"), "bar") |
| 293 |
expect_equal(path_ext("foo.boo.biff.bar"), "bar") |
| 294 |
expect_equal(path_ext(".csh.rc"), "rc") |
| 295 |
expect_equal(path_ext("nodots"), "") |
| 296 |
expect_equal(path_ext(".cshrc"), "") |
| 297 |
expect_equal(path_ext("...manydots"), "") |
| 298 |
expect_equal(path_ext("...manydots.ext"), "ext") |
| 299 |
expect_equal(path_ext("manydots..."), "") |
| 300 |
expect_equal(path_ext("manydots...ext"), "ext") |
| 301 |
expect_equal(path_ext("."), "") |
| 302 |
expect_equal(path_ext(".."), "") |
| 303 |
expect_equal(path_ext("........"), "") |
| 304 |
expect_equal(path_ext(""), "") |
| 305 |
expect_equal(path_ext(".bar"), "") |
| 306 |
expect_equal(path_ext("foo/.bar"), "") |
| 307 |
expect_equal(path_ext(c("foo.bar", NA_character_)), c("bar", NA_character_)) |
| 308 |
expect_equal(path_ext("foo.bar/baz"), "") |
| 309 |
}) |
| 310 |
it("works with non-ASCII inputs", { |
| 311 |
skip_if_not_utf8() |
| 312 |
|
| 313 |
expect_equal(path_ext("f\U00F6\U00F6.txt"), "txt") |
| 314 |
expect_equal(path_ext("f\U00F6\U00F6.t\U00E4r"), "t\U00E4r") |
| 315 |
}) |
| 316 |
it("returns a normal character vector", { |
| 317 |
expect_equal(class(path_dir("foo.sh")), "character") |
| 318 |
}) |
| 319 |
}) |
| 320 |
|
| 321 |
describe("path_ext_remove", { |
| 322 |
it("removes the path extension", { |
| 323 |
expect_equal(path_ext_remove("foo.bar"), "foo") |
| 324 |
expect_equal(path_ext_remove("foo.boo.bar"), "foo.boo") |
| 325 |
expect_equal(path_ext_remove("foo.boo.biff.bar"), "foo.boo.biff") |
| 326 |
expect_equal(path_ext_remove(".csh.rc"), ".csh") |
| 327 |
expect_equal(path_ext_remove("nodots"), "nodots") |
| 328 |
expect_equal(path_ext_remove(".cshrc"), ".cshrc") |
| 329 |
expect_equal(path_ext_remove("...manydots"), "...manydots") |
| 330 |
expect_equal(path_ext_remove("...manydots.ext"), "...manydots") |
| 331 |
expect_equal(path_ext_remove("manydots..."), "manydots...") |
| 332 |
expect_equal(path_ext_remove("manydots...ext"), "manydots") |
| 333 |
expect_equal(path_ext_remove("."), ".") |
| 334 |
expect_equal(path_ext_remove(".."), "..") |
| 335 |
expect_equal(path_ext_remove("........"), "........") |
| 336 |
expect_equal(path_ext_remove(""), "") |
| 337 |
expect_equal(path_ext_remove(NA_character_), NA_character_) |
| 338 |
expect_equal( |
| 339 |
path_ext_remove(c("foo.bar", NA_character_)), |
| 340 |
c("foo", NA_character_) |
| 341 |
) |
| 342 |
expect_equal(path_ext_remove(".bar"), ".bar") |
| 343 |
expect_equal(path_ext_remove("foo/.bar"), "foo/.bar") |
| 344 |
expect_equal(path_ext_remove("foo.bar/abc.123"), "foo.bar/abc") |
| 345 |
expect_equal(path_ext_remove("foo.bar/abc"), "foo.bar/abc") |
| 346 |
}) |
| 347 |
it("works with non-ASCII inputs", { |
| 348 |
skip_if_not_utf8() |
| 349 |
|
| 350 |
expect_equal(path_ext_remove("f\U00F6\U00F6.txt"), "f\U00F6\U00F6") |
| 351 |
expect_equal(path_ext_remove("f\U00F6\U00F6.t\U00E4r"), "f\U00F6\U00F6") |
| 352 |
}) |
| 353 |
}) |
| 354 |
|
| 355 |
describe("path_ext_set", { |
| 356 |
it("replaces the path extension", { |
| 357 |
expect_equal(path_ext_set("foo.bar", "baz"), fs_path("foo.baz")) |
| 358 |
expect_equal(path_ext_set("foo.boo.bar", "baz"), fs_path("foo.boo.baz")) |
| 359 |
expect_equal( |
| 360 |
path_ext_set("foo.boo.biff.bar", "baz"), |
| 361 |
fs_path("foo.boo.biff.baz") |
| 362 |
) |
| 363 |
expect_equal(path_ext_set(".csh.rc", "gz"), fs_path(".csh.gz")) |
| 364 |
expect_equal(path_ext_set("nodots", "bar"), fs_path("nodots.bar")) |
| 365 |
expect_equal(path_ext_set(".cshrc", "bar"), fs_path(".cshrc.bar")) |
| 366 |
expect_equal( |
| 367 |
path_ext_set("...manydots", "bar"), |
| 368 |
fs_path("...manydots.bar") |
| 369 |
) |
| 370 |
expect_equal( |
| 371 |
path_ext_set("...manydots.ext", "bar"), |
| 372 |
fs_path("...manydots.bar") |
| 373 |
) |
| 374 |
expect_equal(path_ext_set(".", "bar"), fs_path("..bar")) |
| 375 |
expect_equal(path_ext_set("..", "bar"), fs_path("...bar")) |
| 376 |
expect_equal(path_ext_set("........", "bar"), fs_path(".........bar")) |
| 377 |
expect_equal(path_ext_set("", "bar"), fs_path(".bar")) |
| 378 |
expect_equal(path_ext_set(NA_character_, "bar"), fs_path(NA_character_)) |
| 379 |
expect_equal( |
| 380 |
path_ext_set(c("foo", NA_character_), "bar"), |
| 381 |
fs_path(c("foo.bar", NA_character_)) |
| 382 |
) |
| 383 |
expect_equal(path_ext_set(".bar", "baz"), fs_path(".bar.baz")) |
| 384 |
expect_equal(path_ext_set("foo/.bar", "baz"), fs_path("foo/.bar.baz")) |
| 385 |
expect_equal(path_ext_set("foo", ""), fs_path("foo")) |
| 386 |
}) |
| 387 |
it("works the same with and without a leading . for ext", { |
| 388 |
expect_equal(path_ext_set("foo", "bar"), fs_path("foo.bar")) |
| 389 |
expect_equal(path_ext_set("foo", ".bar"), fs_path("foo.bar")) |
| 390 |
}) |
| 391 |
it("only removes a leading . from the extension", { |
| 392 |
expect_equal(path_ext_set("foo", "b.ar"), fs_path("foo.b.ar")) |
| 393 |
expect_equal(path_ext_set("foo", ".b.ar"), fs_path("foo.b.ar")) |
| 394 |
}) |
| 395 |
it("works with multiple paths (#205)", { |
| 396 |
multiple_paths <- c("a", "b") |
| 397 |
expect_equal( |
| 398 |
path_ext_set(multiple_paths, "csv"), |
| 399 |
fs_path(c("a.csv", "b.csv")) |
| 400 |
) |
| 401 |
}) |
| 402 |
it("works with multiple extensions (#250)", { |
| 403 |
multiple_paths <- c("a", "b") |
| 404 |
multiple_exts <- c("csv", "tsv") |
| 405 |
expect_equal( |
| 406 |
path_ext_set(multiple_paths, multiple_exts), |
| 407 |
fs_path(c("a.csv", "b.tsv")) |
| 408 |
) |
| 409 |
|
| 410 |
expect_error( |
| 411 |
path_ext_set(multiple_paths, c(multiple_exts, "xls")), |
| 412 |
class = "fs_error", |
| 413 |
"consistent lengths" |
| 414 |
) |
| 415 |
}) |
| 416 |
it("works with non-ASCII inputs", { |
| 417 |
skip_if_not_utf8() |
| 418 |
|
| 419 |
expect_equal( |
| 420 |
path_ext_set("f\U00F6\U00F6.txt", "bar"), |
| 421 |
fs_path("f\U00F6\U00F6.bar") |
| 422 |
) |
| 423 |
expect_equal( |
| 424 |
path_ext_set("f\U00F6\U00F6.t\U00E4r", "bar"), |
| 425 |
fs_path("f\U00F6\U00F6.bar") |
| 426 |
) |
| 427 |
}) |
| 428 |
}) |
| 429 |
|
| 430 |
describe("path_ext<-", { |
| 431 |
it("replaces the path extension", { |
| 432 |
x <- "...manydots" |
| 433 |
path_ext(x) <- "bar" |
| 434 |
expect_equal(x, fs_path("...manydots.bar")) |
| 435 |
}) |
| 436 |
}) |
| 437 |
|
| 438 |
# test cases derived from https://github.com/python/cpython/blob/6f0eb93183519024cb360162bdd81b9faec97ba6/Lib/test/test_posixpath.py#L276 |
| 439 |
|
| 440 |
describe("path_norm", { |
| 441 |
it("works with POSIX paths", { |
| 442 |
expect_equal(path_norm(""), fs_path(".")) |
| 443 |
expect_equal(path_norm(""), fs_path(".")) |
| 444 |
expect_equal(path_norm(".."), fs_path("..")) |
| 445 |
expect_equal(path_norm("/../../.."), fs_path("/")) |
| 446 |
expect_equal(path_norm("/../.././.."), fs_path("/")) |
| 447 |
expect_equal(path_norm("../.././.."), fs_path("../../..")) |
| 448 |
expect_equal(path_norm("/"), fs_path("/")) |
| 449 |
expect_equal(path_norm("//"), fs_path("/")) |
| 450 |
expect_equal(path_norm("///"), fs_path("/")) |
| 451 |
expect_equal(path_norm("//foo"), fs_path("//foo")) |
| 452 |
expect_equal(path_norm("//foo/.//bar//"), fs_path("//foo/bar")) |
| 453 |
expect_equal(path_norm("/foo/.//bar//.//..//.//baz"), fs_path("/foo/baz")) |
| 454 |
expect_equal(path_norm("/..//./foo/.//bar"), fs_path("/foo/bar")) |
| 455 |
}) |
| 456 |
|
| 457 |
it("works with POSIX paths", { |
| 458 |
expect_equal(path_norm("A//////././//.//B"), fs_path("A/B")) |
| 459 |
expect_equal(path_norm("A/./B"), fs_path("A/B")) |
| 460 |
expect_equal(path_norm("A/foo/../B"), fs_path("A/B")) |
| 461 |
expect_equal(path_norm("C:A//B"), fs_path("C:A/B")) |
| 462 |
expect_equal(path_norm("D:A/./B"), fs_path("D:A/B")) |
| 463 |
expect_equal(path_norm("e:A/foo/../B"), fs_path("E:A/B")) |
| 464 |
|
| 465 |
expect_equal(path_norm("C:///A//B"), fs_path("C:/A/B")) |
| 466 |
expect_equal(path_norm("D:///A/./B"), fs_path("D:/A/B")) |
| 467 |
expect_equal(path_norm("e:///A/foo/../B"), fs_path("E:/A/B")) |
| 468 |
|
| 469 |
expect_equal(path_norm(".."), fs_path("..")) |
| 470 |
expect_equal(path_norm("."), fs_path(".")) |
| 471 |
expect_equal(path_norm(""), fs_path(".")) |
| 472 |
expect_equal(path_norm("/"), fs_path("/")) |
| 473 |
expect_equal(path_norm("c:/"), fs_path("C:/")) |
| 474 |
expect_equal(path_norm("/../.././.."), fs_path("/")) |
| 475 |
expect_equal(path_norm("c:/../../.."), fs_path("C:/")) |
| 476 |
expect_equal(path_norm("../.././.."), fs_path("../../..")) |
| 477 |
expect_equal(path_norm("C:////a/b"), fs_path("C:/a/b")) |
| 478 |
expect_equal( |
| 479 |
path_norm("//machine/share//a/b"), |
| 480 |
fs_path("//machine/share/a/b") |
| 481 |
) |
| 482 |
|
| 483 |
expect_equal(path_norm("\\\\?\\D:/XY\\Z"), fs_path("//?/D:/XY/Z")) |
| 484 |
}) |
| 485 |
|
| 486 |
it("works with missing values", { |
| 487 |
expect_equal(path_norm(NA), fs_path(NA_character_)) |
| 488 |
expect_equal(path_norm(c("foo", NA)), fs_path(c("foo", NA))) |
| 489 |
expect_equal( |
| 490 |
path_norm(c(NA, NA)), |
| 491 |
fs_path(c(NA_character_, NA_character_)) |
| 492 |
) |
| 493 |
}) |
| 494 |
}) |
| 495 |
|
| 496 |
# Test cases derived from https://github.com/python/cpython/blob/6f0eb93183519024cb360162bdd81b9faec97ba6/Lib/test/test_posixpath.py |
| 497 |
|
| 498 |
describe("path_common", { |
| 499 |
it("finds the common path", { |
| 500 |
expect_error(path_common(c("/usr", "usr")), "Can't mix", class = "fs_error") |
| 501 |
expect_error(path_common(c("usr", "/usr")), "Can't mix", class = "fs_error") |
| 502 |
|
| 503 |
expect_equal(path_common(c("/usr/local")), fs_path("/usr/local")) |
| 504 |
expect_equal( |
| 505 |
path_common(c("/usr/local", "/usr/local")), |
| 506 |
fs_path("/usr/local") |
| 507 |
) |
| 508 |
expect_equal( |
| 509 |
path_common(c("/usr/local/", "/usr/local")), |
| 510 |
fs_path("/usr/local") |
| 511 |
) |
| 512 |
expect_equal( |
| 513 |
path_common(c("/usr/local/", "/usr/local/")), |
| 514 |
fs_path("/usr/local") |
| 515 |
) |
| 516 |
expect_equal( |
| 517 |
path_common(c("/usr//local/bin", "/usr/local//bin")), |
| 518 |
fs_path("/usr/local/bin") |
| 519 |
) |
| 520 |
expect_equal( |
| 521 |
path_common(c("/usr/./local", "/./usr/local")), |
| 522 |
fs_path("/usr/local") |
| 523 |
) |
| 524 |
expect_equal( |
| 525 |
path_common(c("/", "/dev")), |
| 526 |
fs_path("/") |
| 527 |
) |
| 528 |
expect_equal( |
| 529 |
path_common(c("/usr", "/dev")), |
| 530 |
fs_path("/") |
| 531 |
) |
| 532 |
expect_equal( |
| 533 |
path_common(c("/usr/lib/", "/usr/lib/python3")), |
| 534 |
fs_path("/usr/lib") |
| 535 |
) |
| 536 |
expect_equal( |
| 537 |
path_common(c("/usr/lib/", "/usr/lib64/")), |
| 538 |
fs_path("/usr") |
| 539 |
) |
| 540 |
|
| 541 |
expect_equal( |
| 542 |
path_common(c("/usr/lib", "/usr/lib64")), |
| 543 |
fs_path("/usr") |
| 544 |
) |
| 545 |
expect_equal( |
| 546 |
path_common(c("/usr/lib/", "/usr/lib64")), |
| 547 |
fs_path("/usr") |
| 548 |
) |
| 549 |
|
| 550 |
expect_equal( |
| 551 |
path_common(c("spam")), |
| 552 |
fs_path("spam") |
| 553 |
) |
| 554 |
expect_equal( |
| 555 |
path_common(c("spam", "spam")), |
| 556 |
fs_path("spam") |
| 557 |
) |
| 558 |
expect_equal( |
| 559 |
path_common(c("spam", "alot")), |
| 560 |
fs_path("") |
| 561 |
) |
| 562 |
expect_equal( |
| 563 |
path_common(c("and/jam", "and/spam")), |
| 564 |
fs_path("and") |
| 565 |
) |
| 566 |
expect_equal( |
| 567 |
path_common(c("and//jam", "and/spam//")), |
| 568 |
fs_path("and") |
| 569 |
) |
| 570 |
expect_equal( |
| 571 |
path_common(c("and/./jam", "./and/spam")), |
| 572 |
fs_path("and") |
| 573 |
) |
| 574 |
expect_equal(path_common(c("and/jam", "and/spam", "alot")), fs_path("")) |
| 575 |
expect_equal(path_common(c("and/jam", "and/spam", "and")), fs_path("and")) |
| 576 |
|
| 577 |
expect_equal(path_common(c("")), fs_path("")) |
| 578 |
expect_equal(path_common(c("", "spam/alot")), fs_path("")) |
| 579 |
|
| 580 |
expect_error( |
| 581 |
path_common(c("", "/spam/alot")), |
| 582 |
"Can't mix", |
| 583 |
class = "fs_error" |
| 584 |
) |
| 585 |
}) |
| 586 |
|
| 587 |
it("returns NA if any input is NA", { |
| 588 |
expect_equal(path_common(NA), fs_path(NA_character_)) |
| 589 |
expect_equal(path_common(c("and/jam", NA)), fs_path(NA_character_)) |
| 590 |
expect_equal(path_common(c("and/jam", NA, "and")), fs_path(NA_character_)) |
| 591 |
}) |
| 592 |
}) |
| 593 |
|
| 594 |
describe("path_has_parent", { |
| 595 |
it("works on single paths", { |
| 596 |
expect_false(path_has_parent("foo", "bar")) |
| 597 |
expect_false(path_has_parent("foo", "foo/bar")) |
| 598 |
|
| 599 |
expect_false(path_has_parent("/usr/var2/log", "/usr/var")) |
| 600 |
|
| 601 |
expect_true(path_has_parent("foo/bar", "foo")) |
| 602 |
expect_true(path_has_parent( |
| 603 |
"path/myfiles/myfile", |
| 604 |
"path/to/files/../../myfiles" |
| 605 |
)) |
| 606 |
|
| 607 |
# expands path |
| 608 |
expect_true(path_has_parent("~/a", path_expand("~/a"))) |
| 609 |
expect_true(path_has_parent(path_expand("~/a"), "~/a")) |
| 610 |
}) |
| 611 |
it("works with multiple paths", { |
| 612 |
expect_equal(path_has_parent(c("/a/b/c", "x/y"), "/a/b"), c(TRUE, FALSE)) |
| 613 |
|
| 614 |
expect_equal(path_has_parent("/a/b/c", c("/a/b", "/x/y")), c(TRUE, FALSE)) |
| 615 |
|
| 616 |
expect_error( |
| 617 |
path_has_parent(c("/a/b/c", "x/y"), c("/a/b", "x/y", "foo/bar")), |
| 618 |
"consistent lengths", |
| 619 |
class = "invalid_argument" |
| 620 |
) |
| 621 |
}) |
| 622 |
}) |
| 623 |
|
| 624 |
describe("path_join", { |
| 625 |
it("converts inputs to character if required", { |
| 626 |
expect_equal(path_join(c("a", NA)), path_join(c("a", NA_character_))) |
| 627 |
expect_equal(path_join(c("a", 1)), path_join(c("a", "1"))) |
| 628 |
expect_equal( |
| 629 |
path_join(list(c("a", 1), c("a/b", 2))), |
| 630 |
fs_path(c(path_join(c("a", "1")), path_join(c("a/b", "2")))) |
| 631 |
) |
| 632 |
}) |
| 633 |
it("works with list inputs", { |
| 634 |
expect_equal( |
| 635 |
path_join(list(c("foo", "bar"), c("a/b", "c"))), |
| 636 |
fs_path(c(path_join(c("foo", "bar")), path_join(c("a/b", "c")))) |
| 637 |
) |
| 638 |
expect_equal( |
| 639 |
path_join(list(NA, c("a", 1), c("a/b", 2))), |
| 640 |
fs_path(c( |
| 641 |
path_join(NA_character_), |
| 642 |
path_join(c("a", "1")), |
| 643 |
path_join(c("a/b", "2")) |
| 644 |
)) |
| 645 |
) |
| 646 |
}) |
| 647 |
}) |
| 648 |
|
| 649 |
# derived from https://github.com/python/cpython/blob/6f0eb93183519024cb360162bdd81b9faec97ba6/Lib/test/test_posixpath.py#L483 |
| 650 |
describe("path_rel", { |
| 651 |
it("works for POSIX paths", { |
| 652 |
cur_dir <- path_file(getwd()) |
| 653 |
expect_equal(path_rel("a"), fs_path("a")) |
| 654 |
expect_equal(path_rel(path_abs("a")), fs_path("a")) |
| 655 |
expect_equal(path_rel("a/b"), fs_path("a/b")) |
| 656 |
expect_equal(path_rel("../a/b"), fs_path("../a/b")) |
| 657 |
expect_equal(path_rel("a", "../b"), path_join(c("..", cur_dir, "a"))) |
| 658 |
expect_equal(path_rel("a/b", "../c"), path_join(c("..", cur_dir, "a", "b"))) |
| 659 |
expect_equal(path_rel("a", "b/c"), fs_path("../../a")) |
| 660 |
expect_equal(path_rel("a", "a"), fs_path(".")) |
| 661 |
expect_equal( |
| 662 |
path_rel("/foo/bar/bat", "/x/y/z"), |
| 663 |
fs_path("../../../foo/bar/bat") |
| 664 |
) |
| 665 |
expect_equal(path_rel("/foo/bar/bat", "/foo/bar"), fs_path("bat")) |
| 666 |
expect_equal(path_rel("/foo/bar/bat", "/"), fs_path("foo/bar/bat")) |
| 667 |
expect_equal(path_rel("/", "/foo/bar/bat"), fs_path("../../..")) |
| 668 |
expect_equal(path_rel("/foo/bar/bat", "/x"), fs_path("../foo/bar/bat")) |
| 669 |
expect_equal(path_rel("/x", "/foo/bar/bat"), fs_path("../../../x")) |
| 670 |
expect_equal(path_rel("/", "/"), fs_path(".")) |
| 671 |
expect_equal(path_rel("/a", "/a"), fs_path(".")) |
| 672 |
expect_equal(path_rel("/a/b", "/a/b"), fs_path(".")) |
| 673 |
|
| 674 |
expect_equal( |
| 675 |
path_rel(c("a", "a/b", "a/b/c"), "a/b"), |
| 676 |
fs_path(c("..", ".", "c")) |
| 677 |
) |
| 678 |
expect_snapshot( |
| 679 |
error = TRUE, |
| 680 |
path_rel(c("a", "a/b", "a/b/c"), c("a/b", "a")) |
| 681 |
) |
| 682 |
}) |
| 683 |
|
| 684 |
it("works for windows paths", { |
| 685 |
expect_equal( |
| 686 |
path_rel("c:/foo/bar/bat", "c:/x/y"), |
| 687 |
fs_path("../../foo/bar/bat") |
| 688 |
) |
| 689 |
expect_equal( |
| 690 |
path_rel("//conky/mountpoint/a", "//conky/mountpoint/b/c"), |
| 691 |
fs_path("../../a") |
| 692 |
) |
| 693 |
expect_equal( |
| 694 |
path_rel("d:/Users/a", "D:/Users/b"), |
| 695 |
fs_path("../a") |
| 696 |
) |
| 697 |
}) |
| 698 |
|
| 699 |
it("expands path before computing relativity", { |
| 700 |
expect_equal( |
| 701 |
path_rel("/foo/bar/baz", "~"), |
| 702 |
path_rel("/foo/bar/baz", path_expand("~")) |
| 703 |
) |
| 704 |
expect_equal(path_rel("~/foo/bar/baz", "~"), fs_path("foo/bar/baz")) |
| 705 |
}) |
| 706 |
|
| 707 |
it("propagates NAs", { |
| 708 |
expect_equal(path_rel(NA_character_), fs_path(NA_character_)) |
| 709 |
expect_equal( |
| 710 |
path_rel("/foo/bar/baz", NA_character_), |
| 711 |
fs_path(NA_character_) |
| 712 |
) |
| 713 |
}) |
| 714 |
|
| 715 |
it("can be reversed by path_abs", { |
| 716 |
skip_on_os("windows") |
| 717 |
|
| 718 |
f <- file_temp() |
| 719 |
expect_equal(path_abs(path_rel(f)), f) |
| 720 |
|
| 721 |
home <- path_home() |
| 722 |
home_f <- path_abs(path_home("../../foo/bar/baz")) |
| 723 |
|
| 724 |
expect_equal(path_abs(path_rel(home_f, start = home), start = home), home_f) |
| 725 |
}) |
| 726 |
}) |
| 727 |
|
| 728 |
describe("path_abs", { |
| 729 |
it("uses the start parameter", { |
| 730 |
expect_equal(path_abs("b", "a"), path_abs("a/b")) |
| 731 |
}) |
| 732 |
}) |
| 733 |
|
| 734 |
describe("path_home", { |
| 735 |
# The trailing slash is needed to ensure we get path expansion |
| 736 |
# on POSIX systems when readline support is not built in. (#60) |
| 737 |
it("is equivalent to path_expand(\"~/\")", { |
| 738 |
expect_equal(path_home(), path_tidy(path_expand("~/"))) |
| 739 |
}) |
| 740 |
}) |
| 741 |
|
| 742 |
describe("path_dir", { |
| 743 |
it("works like dirname for normal paths", { |
| 744 |
expect_equal(path_dir("foo/bar"), "foo") |
| 745 |
expect_equal(path_dir("bar"), ".") |
| 746 |
expect_equal(path_dir(c("foo/bar", "baz")), c("foo", ".")) |
| 747 |
}) |
| 748 |
it("propagates NAs", { |
| 749 |
expect_equal(path_dir(NA_character_), NA_character_) |
| 750 |
expect_equal(path_dir(c("foo/bar", NA)), c("foo", NA_character_)) |
| 751 |
}) |
| 752 |
it("returns a normal character vector", { |
| 753 |
expect_equal(class(path_dir("foo/bar")), "character") |
| 754 |
}) |
| 755 |
}) |
| 756 |
|
| 757 |
describe("path_file", { |
| 758 |
it("works like dirname for normal paths", { |
| 759 |
expect_equal(path_file("foo/bar"), "bar") |
| 760 |
expect_equal(path_file("bar"), "bar") |
| 761 |
expect_equal(path_file(c("foo/bar", "baz")), c("bar", "baz")) |
| 762 |
}) |
| 763 |
it("propagates NAs", { |
| 764 |
expect_equal(path_file(NA_character_), NA_character_) |
| 765 |
expect_equal(path_file(c("foo/bar", NA)), c("bar", NA_character_)) |
| 766 |
}) |
| 767 |
it("returns a normal character vector", { |
| 768 |
expect_equal(class(path_dir("foo/bar")), "character") |
| 769 |
}) |
| 770 |
}) |
| 771 |
|
| 772 |
# These tests were adapted from |
| 773 |
# https://github.com/python/cpython/blob/48e8c82fc63d2ddcddce8aa637a892839b551619/Lib/test/test_ntpath.py, |
| 774 |
# hence the flying circus names, but I like dead parrots too, so keeping them. |
| 775 |
describe("path_expand", { |
| 776 |
it("works on windows", { |
| 777 |
withr::local_envvar(c(FS_IS_WINDOWS = "TRUE")) |
| 778 |
withr::with_envvar( |
| 779 |
c("USERPROFILE" = NA, "HOMEDRIVE" = NA, "HOMEPATH" = NA), |
| 780 |
{ |
| 781 |
expect_equal(path_expand("~test"), fs_path("~test")) |
| 782 |
} |
| 783 |
) |
| 784 |
withr::with_envvar( |
| 785 |
c("USERPROFILE" = NA, "HOMEDRIVE" = "C:\\", "HOMEPATH" = "eric\\idle"), |
| 786 |
{ |
| 787 |
expect_equal(path_expand("~"), fs_path("C:/eric/idle")) |
| 788 |
expect_equal(path_expand("~test"), fs_path("C:/eric/test")) |
| 789 |
} |
| 790 |
) |
| 791 |
withr::with_envvar( |
| 792 |
c("USERPROFILE" = NA, "HOMEDRIVE" = NA, "HOMEPATH" = "eric/idle"), |
| 793 |
{ |
| 794 |
expect_equal(path_expand("~"), fs_path("eric/idle")) |
| 795 |
expect_equal(path_expand("~test"), fs_path("eric/test")) |
| 796 |
} |
| 797 |
) |
| 798 |
withr::with_envvar(c("USERPROFILE" = "C:\\idle\\eric"), { |
| 799 |
expect_equal(path_expand("~"), fs_path("C:/idle/eric")) |
| 800 |
expect_equal(path_expand("~test"), fs_path("C:/idle/test")) |
| 801 |
|
| 802 |
expect_equal( |
| 803 |
path_expand("~test/foo/bar"), |
| 804 |
fs_path("C:/idle/test/foo/bar") |
| 805 |
) |
| 806 |
expect_equal( |
| 807 |
path_expand("~test/foo/bar/"), |
| 808 |
fs_path("C:/idle/test/foo/bar") |
| 809 |
) |
| 810 |
expect_equal( |
| 811 |
path_expand("~test\\foo\\bar"), |
| 812 |
fs_path("C:/idle/test/foo/bar") |
| 813 |
) |
| 814 |
expect_equal( |
| 815 |
path_expand("~test\\foo\\bar\\"), |
| 816 |
fs_path("C:/idle/test/foo/bar") |
| 817 |
) |
| 818 |
expect_equal( |
| 819 |
path_expand("~/foo/bar"), |
| 820 |
fs_path("C:/idle/eric/foo/bar") |
| 821 |
) |
| 822 |
expect_equal( |
| 823 |
path_expand("~\\foo\\bar"), |
| 824 |
fs_path("C:/idle/eric/foo/bar") |
| 825 |
) |
| 826 |
}) |
| 827 |
withr::with_envvar( |
| 828 |
c("USERPROFILE" = "C:\\idle\\eric", "R_FS_HOME" = "C:\\john\\cleese"), |
| 829 |
{ |
| 830 |
# R_FS_HOME overrides userprofile |
| 831 |
expect_equal(path_expand("~"), fs_path("C:/john/cleese")) |
| 832 |
expect_equal(path_expand("~test"), fs_path("C:/john/test")) |
| 833 |
} |
| 834 |
) |
| 835 |
}) |
| 836 |
it("repects R_FS_HOME", { |
| 837 |
withr::with_envvar(c("R_FS_HOME" = "/foo/bar"), { |
| 838 |
expect_equal(path_expand("~"), fs_path("/foo/bar")) |
| 839 |
expect_equal(path_expand("~test"), fs_path("/foo/test")) |
| 840 |
}) |
| 841 |
}) |
| 842 |
}) |