| 1 |
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution and at |
| 3 |
// http://rust-lang.org/COPYRIGHT. |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 |
// option. This file may not be copied, modified, or distributed |
| 9 |
// except according to those terms. |
| 10 |
|
| 11 |
// ignore-windows TempDir may cause IoError on windows: #10462 |
| 12 |
|
| 13 |
#![cfg_attr(test, deny(warnings))] |
| 14 |
|
| 15 |
extern crate glob; |
| 16 |
extern crate tempdir; |
| 17 |
|
| 18 |
use glob::{glob, glob_with}; |
| 19 |
use std::env; |
| 20 |
use std::fs; |
| 21 |
use std::path::PathBuf; |
| 22 |
use tempdir::TempDir; |
| 23 |
|
| 24 |
#[test] |
| 25 |
fn main() { |
| 26 |
fn mk_file(path: &str, directory: bool) { |
| 27 |
if directory { |
| 28 |
fs::create_dir(path).unwrap(); |
| 29 |
} else { |
| 30 |
fs::File::create(path).unwrap(); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
fn mk_symlink_file(original: &str, link: &str) { |
| 35 |
#[cfg(unix)] |
| 36 |
{ |
| 37 |
use std::os::unix::fs::symlink; |
| 38 |
symlink(original, link).unwrap(); |
| 39 |
} |
| 40 |
#[cfg(windows)] |
| 41 |
{ |
| 42 |
use std::os::windows::fs::symlink_file; |
| 43 |
symlink_file(original, link).unwrap(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
fn mk_symlink_dir(original: &str, link: &str) { |
| 48 |
#[cfg(unix)] |
| 49 |
{ |
| 50 |
use std::os::unix::fs::symlink; |
| 51 |
symlink(original, link).unwrap(); |
| 52 |
} |
| 53 |
#[cfg(windows)] |
| 54 |
{ |
| 55 |
use std::os::windows::fs::symlink_dir; |
| 56 |
symlink_dir(original, link).unwrap(); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
fn glob_vec(pattern: &str) -> Vec<PathBuf> { |
| 61 |
glob(pattern).unwrap().map(|r| r.unwrap()).collect() |
| 62 |
} |
| 63 |
|
| 64 |
fn glob_with_vec(pattern: &str, options: glob::MatchOptions) -> Vec<PathBuf> { |
| 65 |
glob_with(pattern, options) |
| 66 |
.unwrap() |
| 67 |
.map(|r| r.unwrap()) |
| 68 |
.collect() |
| 69 |
} |
| 70 |
|
| 71 |
let root = TempDir::new("glob-tests"); |
| 72 |
let root = root.ok().expect("Should have created a temp directory"); |
| 73 |
assert!(env::set_current_dir(root.path()).is_ok()); |
| 74 |
|
| 75 |
mk_file("aaa", true); |
| 76 |
mk_file("aaa/apple", true); |
| 77 |
mk_file("aaa/orange", true); |
| 78 |
mk_file("aaa/tomato", true); |
| 79 |
mk_file("aaa/tomato/tomato.txt", false); |
| 80 |
mk_file("aaa/tomato/tomoto.txt", false); |
| 81 |
mk_file("bbb", true); |
| 82 |
mk_file("bbb/specials", true); |
| 83 |
mk_file("bbb/specials/!", false); |
| 84 |
// a valid symlink |
| 85 |
mk_symlink_file("aaa/apple", "aaa/green_apple"); |
| 86 |
// a broken symlink |
| 87 |
mk_symlink_file("aaa/setsuna", "aaa/kazusa"); |
| 88 |
|
| 89 |
// windows does not allow `*` or `?` characters to exist in filenames |
| 90 |
if env::consts::FAMILY != "windows" { |
| 91 |
mk_file("bbb/specials/*", false); |
| 92 |
mk_file("bbb/specials/?", false); |
| 93 |
} |
| 94 |
|
| 95 |
mk_file("bbb/specials/[", false); |
| 96 |
mk_file("bbb/specials/]", false); |
| 97 |
mk_file("ccc", true); |
| 98 |
mk_file("xyz", true); |
| 99 |
mk_file("xyz/x", false); |
| 100 |
mk_file("xyz/y", false); |
| 101 |
mk_file("xyz/z", false); |
| 102 |
|
| 103 |
mk_file("r", true); |
| 104 |
mk_file("r/current_dir.md", false); |
| 105 |
mk_file("r/one", true); |
| 106 |
mk_file("r/one/a.md", false); |
| 107 |
mk_file("r/one/another", true); |
| 108 |
mk_file("r/one/another/a.md", false); |
| 109 |
mk_file("r/one/another/deep", true); |
| 110 |
mk_file("r/one/another/deep/spelunking.md", false); |
| 111 |
mk_file("r/another", true); |
| 112 |
mk_file("r/another/a.md", false); |
| 113 |
mk_file("r/two", true); |
| 114 |
mk_file("r/two/b.md", false); |
| 115 |
mk_file("r/three", true); |
| 116 |
mk_file("r/three/c.md", false); |
| 117 |
|
| 118 |
mk_file("dirsym", true); |
| 119 |
mk_symlink_dir(root.path().join("r").to_str().unwrap(), "dirsym/link"); |
| 120 |
|
| 121 |
assert_eq!( |
| 122 |
glob_vec("dirsym/**/*.md"), |
| 123 |
vec!( |
| 124 |
PathBuf::from("dirsym/link/another/a.md"), |
| 125 |
PathBuf::from("dirsym/link/current_dir.md"), |
| 126 |
PathBuf::from("dirsym/link/one/a.md"), |
| 127 |
PathBuf::from("dirsym/link/one/another/a.md"), |
| 128 |
PathBuf::from("dirsym/link/one/another/deep/spelunking.md"), |
| 129 |
PathBuf::from("dirsym/link/three/c.md"), |
| 130 |
PathBuf::from("dirsym/link/two/b.md") |
| 131 |
) |
| 132 |
); |
| 133 |
|
| 134 |
// all recursive entities |
| 135 |
assert_eq!( |
| 136 |
glob_vec("r/**"), |
| 137 |
vec!( |
| 138 |
PathBuf::from("r/another"), |
| 139 |
PathBuf::from("r/one"), |
| 140 |
PathBuf::from("r/one/another"), |
| 141 |
PathBuf::from("r/one/another/deep"), |
| 142 |
PathBuf::from("r/three"), |
| 143 |
PathBuf::from("r/two") |
| 144 |
) |
| 145 |
); |
| 146 |
|
| 147 |
// std-canonicalized windows verbatim disk paths should work |
| 148 |
if env::consts::FAMILY == "windows" { |
| 149 |
let r_verbatim = PathBuf::from("r").canonicalize().unwrap(); |
| 150 |
assert_eq!( |
| 151 |
glob_vec(&format!("{}\\**", r_verbatim.display().to_string())) |
| 152 |
.into_iter() |
| 153 |
.map(|p| p.strip_prefix(&r_verbatim).unwrap().to_owned()) |
| 154 |
.collect::<Vec<_>>(), |
| 155 |
vec!( |
| 156 |
PathBuf::from("another"), |
| 157 |
PathBuf::from("one"), |
| 158 |
PathBuf::from("one\\another"), |
| 159 |
PathBuf::from("one\\another\\deep"), |
| 160 |
PathBuf::from("three"), |
| 161 |
PathBuf::from("two") |
| 162 |
) |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
// collapse consecutive recursive patterns |
| 167 |
assert_eq!( |
| 168 |
glob_vec("r/**/**"), |
| 169 |
vec!( |
| 170 |
PathBuf::from("r/another"), |
| 171 |
PathBuf::from("r/one"), |
| 172 |
PathBuf::from("r/one/another"), |
| 173 |
PathBuf::from("r/one/another/deep"), |
| 174 |
PathBuf::from("r/three"), |
| 175 |
PathBuf::from("r/two") |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
assert_eq!( |
| 180 |
glob_vec("r/**/*"), |
| 181 |
vec!( |
| 182 |
PathBuf::from("r/another"), |
| 183 |
PathBuf::from("r/another/a.md"), |
| 184 |
PathBuf::from("r/current_dir.md"), |
| 185 |
PathBuf::from("r/one"), |
| 186 |
PathBuf::from("r/one/a.md"), |
| 187 |
PathBuf::from("r/one/another"), |
| 188 |
PathBuf::from("r/one/another/a.md"), |
| 189 |
PathBuf::from("r/one/another/deep"), |
| 190 |
PathBuf::from("r/one/another/deep/spelunking.md"), |
| 191 |
PathBuf::from("r/three"), |
| 192 |
PathBuf::from("r/three/c.md"), |
| 193 |
PathBuf::from("r/two"), |
| 194 |
PathBuf::from("r/two/b.md") |
| 195 |
) |
| 196 |
); |
| 197 |
|
| 198 |
// followed by a wildcard |
| 199 |
assert_eq!( |
| 200 |
glob_vec("r/**/*.md"), |
| 201 |
vec!( |
| 202 |
PathBuf::from("r/another/a.md"), |
| 203 |
PathBuf::from("r/current_dir.md"), |
| 204 |
PathBuf::from("r/one/a.md"), |
| 205 |
PathBuf::from("r/one/another/a.md"), |
| 206 |
PathBuf::from("r/one/another/deep/spelunking.md"), |
| 207 |
PathBuf::from("r/three/c.md"), |
| 208 |
PathBuf::from("r/two/b.md") |
| 209 |
) |
| 210 |
); |
| 211 |
|
| 212 |
// followed by a precise pattern |
| 213 |
assert_eq!( |
| 214 |
glob_vec("r/one/**/a.md"), |
| 215 |
vec!( |
| 216 |
PathBuf::from("r/one/a.md"), |
| 217 |
PathBuf::from("r/one/another/a.md") |
| 218 |
) |
| 219 |
); |
| 220 |
|
| 221 |
// followed by another recursive pattern |
| 222 |
// collapses consecutive recursives into one |
| 223 |
assert_eq!( |
| 224 |
glob_vec("r/one/**/**/a.md"), |
| 225 |
vec!( |
| 226 |
PathBuf::from("r/one/a.md"), |
| 227 |
PathBuf::from("r/one/another/a.md") |
| 228 |
) |
| 229 |
); |
| 230 |
|
| 231 |
// followed by two precise patterns |
| 232 |
assert_eq!( |
| 233 |
glob_vec("r/**/another/a.md"), |
| 234 |
vec!( |
| 235 |
PathBuf::from("r/another/a.md"), |
| 236 |
PathBuf::from("r/one/another/a.md") |
| 237 |
) |
| 238 |
); |
| 239 |
|
| 240 |
assert_eq!(glob_vec(""), Vec::<PathBuf>::new()); |
| 241 |
assert_eq!(glob_vec("."), vec!(PathBuf::from("."))); |
| 242 |
assert_eq!(glob_vec(".."), vec!(PathBuf::from(".."))); |
| 243 |
|
| 244 |
assert_eq!(glob_vec("aaa"), vec!(PathBuf::from("aaa"))); |
| 245 |
assert_eq!(glob_vec("aaa/"), vec!(PathBuf::from("aaa"))); |
| 246 |
assert_eq!(glob_vec("a"), Vec::<PathBuf>::new()); |
| 247 |
assert_eq!(glob_vec("aa"), Vec::<PathBuf>::new()); |
| 248 |
assert_eq!(glob_vec("aaaa"), Vec::<PathBuf>::new()); |
| 249 |
|
| 250 |
assert_eq!(glob_vec("aaa/apple"), vec!(PathBuf::from("aaa/apple"))); |
| 251 |
assert_eq!(glob_vec("aaa/apple/nope"), Vec::<PathBuf>::new()); |
| 252 |
|
| 253 |
// windows should support both / and \ as directory separators |
| 254 |
if env::consts::FAMILY == "windows" { |
| 255 |
assert_eq!(glob_vec("aaa\\apple"), vec!(PathBuf::from("aaa/apple"))); |
| 256 |
} |
| 257 |
|
| 258 |
assert_eq!( |
| 259 |
glob_vec("???/"), |
| 260 |
vec!( |
| 261 |
PathBuf::from("aaa"), |
| 262 |
PathBuf::from("bbb"), |
| 263 |
PathBuf::from("ccc"), |
| 264 |
PathBuf::from("xyz") |
| 265 |
) |
| 266 |
); |
| 267 |
|
| 268 |
assert_eq!( |
| 269 |
glob_vec("aaa/tomato/tom?to.txt"), |
| 270 |
vec!( |
| 271 |
PathBuf::from("aaa/tomato/tomato.txt"), |
| 272 |
PathBuf::from("aaa/tomato/tomoto.txt") |
| 273 |
) |
| 274 |
); |
| 275 |
|
| 276 |
assert_eq!( |
| 277 |
glob_vec("xyz/?"), |
| 278 |
vec!( |
| 279 |
PathBuf::from("xyz/x"), |
| 280 |
PathBuf::from("xyz/y"), |
| 281 |
PathBuf::from("xyz/z") |
| 282 |
) |
| 283 |
); |
| 284 |
|
| 285 |
assert_eq!(glob_vec("a*"), vec!(PathBuf::from("aaa"))); |
| 286 |
assert_eq!(glob_vec("*a*"), vec!(PathBuf::from("aaa"))); |
| 287 |
assert_eq!(glob_vec("a*a"), vec!(PathBuf::from("aaa"))); |
| 288 |
assert_eq!(glob_vec("aaa*"), vec!(PathBuf::from("aaa"))); |
| 289 |
assert_eq!(glob_vec("*aaa"), vec!(PathBuf::from("aaa"))); |
| 290 |
assert_eq!(glob_vec("*aaa*"), vec!(PathBuf::from("aaa"))); |
| 291 |
assert_eq!(glob_vec("*a*a*a*"), vec!(PathBuf::from("aaa"))); |
| 292 |
assert_eq!(glob_vec("aaa*/"), vec!(PathBuf::from("aaa"))); |
| 293 |
|
| 294 |
assert_eq!( |
| 295 |
glob_vec("aaa/*"), |
| 296 |
vec!( |
| 297 |
PathBuf::from("aaa/apple"), |
| 298 |
PathBuf::from("aaa/green_apple"), |
| 299 |
PathBuf::from("aaa/kazusa"), |
| 300 |
PathBuf::from("aaa/orange"), |
| 301 |
PathBuf::from("aaa/tomato"), |
| 302 |
) |
| 303 |
); |
| 304 |
|
| 305 |
assert_eq!( |
| 306 |
glob_vec("aaa/*a*"), |
| 307 |
vec!( |
| 308 |
PathBuf::from("aaa/apple"), |
| 309 |
PathBuf::from("aaa/green_apple"), |
| 310 |
PathBuf::from("aaa/kazusa"), |
| 311 |
PathBuf::from("aaa/orange"), |
| 312 |
PathBuf::from("aaa/tomato") |
| 313 |
) |
| 314 |
); |
| 315 |
|
| 316 |
assert_eq!( |
| 317 |
glob_vec("*/*/*.txt"), |
| 318 |
vec!( |
| 319 |
PathBuf::from("aaa/tomato/tomato.txt"), |
| 320 |
PathBuf::from("aaa/tomato/tomoto.txt") |
| 321 |
) |
| 322 |
); |
| 323 |
|
| 324 |
assert_eq!( |
| 325 |
glob_vec("*/*/t[aob]m?to[.]t[!y]t"), |
| 326 |
vec!( |
| 327 |
PathBuf::from("aaa/tomato/tomato.txt"), |
| 328 |
PathBuf::from("aaa/tomato/tomoto.txt") |
| 329 |
) |
| 330 |
); |
| 331 |
|
| 332 |
assert_eq!(glob_vec("./aaa"), vec!(PathBuf::from("aaa"))); |
| 333 |
assert_eq!(glob_vec("./*"), glob_vec("*")); |
| 334 |
assert_eq!(glob_vec("*/..").pop().unwrap(), PathBuf::from("xyz/..")); |
| 335 |
assert_eq!(glob_vec("aaa/../bbb"), vec!(PathBuf::from("aaa/../bbb"))); |
| 336 |
assert_eq!(glob_vec("nonexistent/../bbb"), Vec::<PathBuf>::new()); |
| 337 |
assert_eq!(glob_vec("aaa/tomato/tomato.txt/.."), Vec::<PathBuf>::new()); |
| 338 |
|
| 339 |
assert_eq!(glob_vec("aaa/tomato/tomato.txt/"), Vec::<PathBuf>::new()); |
| 340 |
|
| 341 |
// Ensure to find a broken symlink. |
| 342 |
assert_eq!(glob_vec("aaa/kazusa"), vec!(PathBuf::from("aaa/kazusa"))); |
| 343 |
|
| 344 |
assert_eq!(glob_vec("aa[a]"), vec!(PathBuf::from("aaa"))); |
| 345 |
assert_eq!(glob_vec("aa[abc]"), vec!(PathBuf::from("aaa"))); |
| 346 |
assert_eq!(glob_vec("a[bca]a"), vec!(PathBuf::from("aaa"))); |
| 347 |
assert_eq!(glob_vec("aa[b]"), Vec::<PathBuf>::new()); |
| 348 |
assert_eq!(glob_vec("aa[xyz]"), Vec::<PathBuf>::new()); |
| 349 |
assert_eq!(glob_vec("aa[]]"), Vec::<PathBuf>::new()); |
| 350 |
|
| 351 |
assert_eq!(glob_vec("aa[!b]"), vec!(PathBuf::from("aaa"))); |
| 352 |
assert_eq!(glob_vec("aa[!bcd]"), vec!(PathBuf::from("aaa"))); |
| 353 |
assert_eq!(glob_vec("a[!bcd]a"), vec!(PathBuf::from("aaa"))); |
| 354 |
assert_eq!(glob_vec("aa[!a]"), Vec::<PathBuf>::new()); |
| 355 |
assert_eq!(glob_vec("aa[!abc]"), Vec::<PathBuf>::new()); |
| 356 |
|
| 357 |
assert_eq!( |
| 358 |
glob_vec("bbb/specials/[[]"), |
| 359 |
vec!(PathBuf::from("bbb/specials/[")) |
| 360 |
); |
| 361 |
assert_eq!( |
| 362 |
glob_vec("bbb/specials/!"), |
| 363 |
vec!(PathBuf::from("bbb/specials/!")) |
| 364 |
); |
| 365 |
assert_eq!( |
| 366 |
glob_vec("bbb/specials/[]]"), |
| 367 |
vec!(PathBuf::from("bbb/specials/]")) |
| 368 |
); |
| 369 |
|
| 370 |
mk_file("i", true); |
| 371 |
mk_file("i/qwe", true); |
| 372 |
mk_file("i/qwe/.aaa", false); |
| 373 |
mk_file("i/qwe/.bbb", true); |
| 374 |
mk_file("i/qwe/.bbb/ccc", false); |
| 375 |
mk_file("i/qwe/.bbb/.ddd", false); |
| 376 |
mk_file("i/qwe/eee", false); |
| 377 |
|
| 378 |
let options = glob::MatchOptions { |
| 379 |
case_sensitive: false, |
| 380 |
require_literal_separator: true, |
| 381 |
require_literal_leading_dot: true, |
| 382 |
}; |
| 383 |
assert_eq!(glob_with_vec("i/**/*a*", options), Vec::<PathBuf>::new()); |
| 384 |
assert_eq!(glob_with_vec("i/**/*c*", options), Vec::<PathBuf>::new()); |
| 385 |
assert_eq!(glob_with_vec("i/**/*d*", options), Vec::<PathBuf>::new()); |
| 386 |
assert_eq!( |
| 387 |
glob_with_vec("i/**/*e*", options), |
| 388 |
vec!(PathBuf::from("i/qwe"), PathBuf::from("i/qwe/eee")) |
| 389 |
); |
| 390 |
|
| 391 |
if env::consts::FAMILY != "windows" { |
| 392 |
assert_eq!( |
| 393 |
glob_vec("bbb/specials/[*]"), |
| 394 |
vec!(PathBuf::from("bbb/specials/*")) |
| 395 |
); |
| 396 |
assert_eq!( |
| 397 |
glob_vec("bbb/specials/[?]"), |
| 398 |
vec!(PathBuf::from("bbb/specials/?")) |
| 399 |
); |
| 400 |
} |
| 401 |
|
| 402 |
if env::consts::FAMILY == "windows" { |
| 403 |
assert_eq!( |
| 404 |
glob_vec("bbb/specials/[![]"), |
| 405 |
vec!( |
| 406 |
PathBuf::from("bbb/specials/!"), |
| 407 |
PathBuf::from("bbb/specials/]") |
| 408 |
) |
| 409 |
); |
| 410 |
|
| 411 |
assert_eq!( |
| 412 |
glob_vec("bbb/specials/[!]]"), |
| 413 |
vec!( |
| 414 |
PathBuf::from("bbb/specials/!"), |
| 415 |
PathBuf::from("bbb/specials/[") |
| 416 |
) |
| 417 |
); |
| 418 |
|
| 419 |
assert_eq!( |
| 420 |
glob_vec("bbb/specials/[!!]"), |
| 421 |
vec!( |
| 422 |
PathBuf::from("bbb/specials/["), |
| 423 |
PathBuf::from("bbb/specials/]") |
| 424 |
) |
| 425 |
); |
| 426 |
} else { |
| 427 |
assert_eq!( |
| 428 |
glob_vec("bbb/specials/[![]"), |
| 429 |
vec!( |
| 430 |
PathBuf::from("bbb/specials/!"), |
| 431 |
PathBuf::from("bbb/specials/*"), |
| 432 |
PathBuf::from("bbb/specials/?"), |
| 433 |
PathBuf::from("bbb/specials/]") |
| 434 |
) |
| 435 |
); |
| 436 |
|
| 437 |
assert_eq!( |
| 438 |
glob_vec("bbb/specials/[!]]"), |
| 439 |
vec!( |
| 440 |
PathBuf::from("bbb/specials/!"), |
| 441 |
PathBuf::from("bbb/specials/*"), |
| 442 |
PathBuf::from("bbb/specials/?"), |
| 443 |
PathBuf::from("bbb/specials/[") |
| 444 |
) |
| 445 |
); |
| 446 |
|
| 447 |
assert_eq!( |
| 448 |
glob_vec("bbb/specials/[!!]"), |
| 449 |
vec!( |
| 450 |
PathBuf::from("bbb/specials/*"), |
| 451 |
PathBuf::from("bbb/specials/?"), |
| 452 |
PathBuf::from("bbb/specials/["), |
| 453 |
PathBuf::from("bbb/specials/]") |
| 454 |
) |
| 455 |
); |
| 456 |
|
| 457 |
assert_eq!( |
| 458 |
glob_vec("bbb/specials/[!*]"), |
| 459 |
vec!( |
| 460 |
PathBuf::from("bbb/specials/!"), |
| 461 |
PathBuf::from("bbb/specials/?"), |
| 462 |
PathBuf::from("bbb/specials/["), |
| 463 |
PathBuf::from("bbb/specials/]") |
| 464 |
) |
| 465 |
); |
| 466 |
|
| 467 |
assert_eq!( |
| 468 |
glob_vec("bbb/specials/[!?]"), |
| 469 |
vec!( |
| 470 |
PathBuf::from("bbb/specials/!"), |
| 471 |
PathBuf::from("bbb/specials/*"), |
| 472 |
PathBuf::from("bbb/specials/["), |
| 473 |
PathBuf::from("bbb/specials/]") |
| 474 |
) |
| 475 |
); |
| 476 |
} |
| 477 |
} |