2014-02-07 13:08:32 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-09-13 11:37:45 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-07 13:08:32 -06:00
|
|
|
// ignore-win32 TempDir may cause IoError on windows: #10462
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-04-07 07:47:04 -05:00
|
|
|
#![feature(macro_rules)]
|
2014-02-28 03:23:06 -06:00
|
|
|
|
2014-02-14 12:10:06 -06:00
|
|
|
extern crate glob;
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-01-28 20:51:33 -06:00
|
|
|
use glob::glob;
|
2014-04-07 07:47:04 -05:00
|
|
|
use std::os;
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io;
|
2014-03-14 13:16:10 -05:00
|
|
|
use std::io::TempDir;
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-02-28 03:23:06 -06:00
|
|
|
macro_rules! assert_eq ( ($e1:expr, $e2:expr) => (
|
|
|
|
if $e1 != $e2 {
|
|
|
|
fail!("{} != {}", stringify!($e1), stringify!($e2))
|
|
|
|
}
|
|
|
|
) )
|
|
|
|
|
2013-09-13 11:37:45 -05:00
|
|
|
pub fn main() {
|
|
|
|
fn mk_file(path: &str, directory: bool) {
|
|
|
|
if directory {
|
2014-04-07 07:47:04 -05:00
|
|
|
io::fs::mkdir(&Path::new(path), io::UserRWX).unwrap();
|
2013-09-13 11:37:45 -05:00
|
|
|
} else {
|
2014-04-07 07:47:04 -05:00
|
|
|
io::File::create(&Path::new(path)).unwrap();
|
2013-09-13 11:37:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn abs_path(path: &str) -> Path {
|
2013-12-03 21:15:12 -06:00
|
|
|
os::getcwd().join(&Path::new(path))
|
2013-09-13 11:37:45 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
fn glob_vec(pattern: &str) -> Vec<Path> {
|
2013-09-13 11:37:45 -05:00
|
|
|
glob(pattern).collect()
|
|
|
|
}
|
|
|
|
|
2013-10-11 08:55:37 -05:00
|
|
|
let root = TempDir::new("glob-tests");
|
2013-09-13 11:37:45 -05:00
|
|
|
let root = root.expect("Should have created a temp directory");
|
2013-10-11 08:55:37 -05:00
|
|
|
assert!(os::change_dir(root.path()));
|
|
|
|
|
|
|
|
mk_file("aaa", true);
|
|
|
|
mk_file("aaa/apple", true);
|
|
|
|
mk_file("aaa/orange", true);
|
|
|
|
mk_file("aaa/tomato", true);
|
|
|
|
mk_file("aaa/tomato/tomato.txt", false);
|
|
|
|
mk_file("aaa/tomato/tomoto.txt", false);
|
|
|
|
mk_file("bbb", true);
|
|
|
|
mk_file("bbb/specials", true);
|
|
|
|
mk_file("bbb/specials/!", false);
|
|
|
|
|
|
|
|
// windows does not allow `*` or `?` characters to exist in filenames
|
2014-04-23 00:01:31 -05:00
|
|
|
if os::consts::FAMILY != "windows" {
|
2013-10-11 08:55:37 -05:00
|
|
|
mk_file("bbb/specials/*", false);
|
|
|
|
mk_file("bbb/specials/?", false);
|
|
|
|
}
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2013-10-11 08:55:37 -05:00
|
|
|
mk_file("bbb/specials/[", false);
|
|
|
|
mk_file("bbb/specials/]", false);
|
|
|
|
mk_file("ccc", true);
|
|
|
|
mk_file("xyz", true);
|
|
|
|
mk_file("xyz/x", false);
|
|
|
|
mk_file("xyz/y", false);
|
|
|
|
mk_file("xyz/z", false);
|
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec(""), Vec::new());
|
2014-04-07 07:47:04 -05:00
|
|
|
assert_eq!(glob_vec("."), vec!(os::getcwd()));
|
|
|
|
assert_eq!(glob_vec(".."), vec!(os::getcwd().join("..")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("aaa"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("aaa/"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("a"), Vec::new());
|
|
|
|
assert_eq!(glob_vec("aa"), Vec::new());
|
|
|
|
assert_eq!(glob_vec("aaaa"), Vec::new());
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("aaa/apple"), vec!(abs_path("aaa/apple")));
|
|
|
|
assert_eq!(glob_vec("aaa/apple/nope"), Vec::new());
|
2013-10-11 08:55:37 -05:00
|
|
|
|
|
|
|
// windows should support both / and \ as directory separators
|
2014-04-23 00:01:31 -05:00
|
|
|
if os::consts::FAMILY == "windows" {
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("aaa\\apple"), vec!(abs_path("aaa/apple")));
|
2013-10-11 08:55:37 -05:00
|
|
|
}
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("???/"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("aaa"),
|
|
|
|
abs_path("bbb"),
|
|
|
|
abs_path("ccc"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("xyz")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("aaa/tomato/tom?to.txt"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("aaa/tomato/tomato.txt"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("aaa/tomato/tomoto.txt")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("xyz/?"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("xyz/x"),
|
|
|
|
abs_path("xyz/y"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("xyz/z")));
|
|
|
|
|
|
|
|
assert_eq!(glob_vec("a*"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("*a*"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("a*a"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("aaa*"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("*aaa"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("*aaa*"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("*a*a*a*"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("aaa*/"), vec!(abs_path("aaa")));
|
|
|
|
|
|
|
|
assert_eq!(glob_vec("aaa/*"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("aaa/apple"),
|
|
|
|
abs_path("aaa/orange"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("aaa/tomato")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("aaa/*a*"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("aaa/apple"),
|
|
|
|
abs_path("aaa/orange"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("aaa/tomato")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("*/*/*.txt"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("aaa/tomato/tomato.txt"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("aaa/tomato/tomoto.txt")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("*/*/t[aob]m?to[.]t[!y]t"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("aaa/tomato/tomato.txt"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("aaa/tomato/tomoto.txt")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-04-07 07:47:04 -05:00
|
|
|
assert_eq!(glob_vec("./aaa"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("./*"), glob_vec("*"));
|
|
|
|
assert_eq!(glob_vec("*/..").pop().unwrap(), abs_path("."));
|
|
|
|
assert_eq!(glob_vec("aaa/../bbb"), vec!(abs_path("bbb")));
|
|
|
|
assert_eq!(glob_vec("nonexistent/../bbb"), Vec::new());
|
|
|
|
assert_eq!(glob_vec("aaa/tomato/tomato.txt/.."), Vec::new());
|
|
|
|
|
2014-04-07 11:24:06 -05:00
|
|
|
assert_eq!(glob_vec("aaa/tomato/tomato.txt/"), Vec::new());
|
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("aa[a]"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("aa[abc]"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("a[bca]a"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("aa[b]"), Vec::new());
|
|
|
|
assert_eq!(glob_vec("aa[xyz]"), Vec::new());
|
|
|
|
assert_eq!(glob_vec("aa[]]"), Vec::new());
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("aa[!b]"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("aa[!bcd]"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("a[!bcd]a"), vec!(abs_path("aaa")));
|
|
|
|
assert_eq!(glob_vec("aa[!a]"), Vec::new());
|
|
|
|
assert_eq!(glob_vec("aa[!abc]"), Vec::new());
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[[]"), vec!(abs_path("bbb/specials/[")));
|
|
|
|
assert_eq!(glob_vec("bbb/specials/!"), vec!(abs_path("bbb/specials/!")));
|
|
|
|
assert_eq!(glob_vec("bbb/specials/[]]"), vec!(abs_path("bbb/specials/]")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
if os::consts::FAMILY != "windows" {
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[*]"), vec!(abs_path("bbb/specials/*")));
|
|
|
|
assert_eq!(glob_vec("bbb/specials/[?]"), vec!(abs_path("bbb/specials/?")));
|
2013-10-11 08:55:37 -05:00
|
|
|
}
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
if os::consts::FAMILY == "windows" {
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[![]"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("bbb/specials/!"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("bbb/specials/]")));
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[!]]"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("bbb/specials/!"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("bbb/specials/[")));
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[!!]"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("bbb/specials/["),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("bbb/specials/]")));
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2013-10-11 08:55:37 -05:00
|
|
|
} else {
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[![]"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("bbb/specials/!"),
|
|
|
|
abs_path("bbb/specials/*"),
|
|
|
|
abs_path("bbb/specials/?"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("bbb/specials/]")));
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[!]]"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("bbb/specials/!"),
|
|
|
|
abs_path("bbb/specials/*"),
|
|
|
|
abs_path("bbb/specials/?"),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("bbb/specials/[")));
|
2013-09-13 11:37:45 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[!!]"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("bbb/specials/*"),
|
|
|
|
abs_path("bbb/specials/?"),
|
|
|
|
abs_path("bbb/specials/["),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("bbb/specials/]")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[!*]"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("bbb/specials/!"),
|
|
|
|
abs_path("bbb/specials/?"),
|
|
|
|
abs_path("bbb/specials/["),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("bbb/specials/]")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
assert_eq!(glob_vec("bbb/specials/[!?]"), vec!(
|
2013-10-11 08:55:37 -05:00
|
|
|
abs_path("bbb/specials/!"),
|
|
|
|
abs_path("bbb/specials/*"),
|
|
|
|
abs_path("bbb/specials/["),
|
2014-03-05 16:02:44 -06:00
|
|
|
abs_path("bbb/specials/]")));
|
2013-10-11 08:55:37 -05:00
|
|
|
|
|
|
|
}
|
2013-09-13 11:37:45 -05:00
|
|
|
}
|