|
| 1 | +// Copyright 2013 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 | +// xfail-fast windows doesn't like 'extern mod extra' |
| 12 | + |
| 13 | +extern mod extra; |
| 14 | + |
| 15 | +use extra::glob::*; |
| 16 | +use extra::tempfile; |
| 17 | +use std::unstable::finally::Finally; |
| 18 | +use std::{io, os, unstable}; |
| 19 | + |
| 20 | +pub fn main() { |
| 21 | + fn change_then_remove(p: &Path, f: &fn()) { |
| 22 | + do (|| { |
| 23 | + unstable::change_dir_locked(p, || f()); |
| 24 | + }).finally { |
| 25 | + os::remove_dir_recursive(p); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + fn mk_file(path: &str, directory: bool) { |
| 30 | + if directory { |
| 31 | + os::make_dir(&Path(path), 0xFFFF); |
| 32 | + } else { |
| 33 | + io::mk_file_writer(&Path(path), [io::Create]); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + fn abs_path(path: &str) -> Path { |
| 38 | + os::getcwd().push_many(Path(path).components) |
| 39 | + } |
| 40 | + |
| 41 | + fn glob_vec(pattern: &str) -> ~[Path] { |
| 42 | + glob(pattern).collect() |
| 43 | + } |
| 44 | + |
| 45 | + let root = tempfile::mkdtemp(&os::tmpdir(), "glob-tests"); |
| 46 | + let root = root.expect("Should have created a temp directory"); |
| 47 | + |
| 48 | + do change_then_remove(&root) { |
| 49 | + mk_file("aaa", true); |
| 50 | + mk_file("aaa/apple", true); |
| 51 | + mk_file("aaa/orange", true); |
| 52 | + mk_file("aaa/tomato", true); |
| 53 | + mk_file("aaa/tomato/tomato.txt", false); |
| 54 | + mk_file("aaa/tomato/tomoto.txt", false); |
| 55 | + mk_file("bbb", true); |
| 56 | + mk_file("bbb/specials", true); |
| 57 | + mk_file("bbb/specials/!", false); |
| 58 | + |
| 59 | + // windows does not allow `*` or `?` characters to exist in filenames |
| 60 | + if os::consts::FAMILY != os::consts::windows::FAMILY { |
| 61 | + mk_file("bbb/specials/*", false); |
| 62 | + mk_file("bbb/specials/?", false); |
| 63 | + } |
| 64 | + |
| 65 | + mk_file("bbb/specials/[", false); |
| 66 | + mk_file("bbb/specials/]", false); |
| 67 | + mk_file("ccc", true); |
| 68 | + mk_file("xyz", true); |
| 69 | + mk_file("xyz/x", false); |
| 70 | + mk_file("xyz/y", false); |
| 71 | + mk_file("xyz/z", false); |
| 72 | + |
| 73 | + assert_eq!(glob_vec(""), ~[]); |
| 74 | + assert_eq!(glob_vec("."), ~[]); |
| 75 | + assert_eq!(glob_vec(".."), ~[]); |
| 76 | + |
| 77 | + assert_eq!(glob_vec("aaa"), ~[abs_path("aaa")]); |
| 78 | + assert_eq!(glob_vec("aaa/"), ~[abs_path("aaa")]); |
| 79 | + assert_eq!(glob_vec("a"), ~[]); |
| 80 | + assert_eq!(glob_vec("aa"), ~[]); |
| 81 | + assert_eq!(glob_vec("aaaa"), ~[]); |
| 82 | + |
| 83 | + assert_eq!(glob_vec("aaa/apple"), ~[abs_path("aaa/apple")]); |
| 84 | + assert_eq!(glob_vec("aaa/apple/nope"), ~[]); |
| 85 | + |
| 86 | + // windows should support both / and \ as directory separators |
| 87 | + if os::consts::FAMILY == os::consts::windows::FAMILY { |
| 88 | + assert_eq!(glob_vec("aaa\\apple"), ~[abs_path("aaa/apple")]); |
| 89 | + } |
| 90 | + |
| 91 | + assert_eq!(glob_vec("???/"), ~[ |
| 92 | + abs_path("aaa"), |
| 93 | + abs_path("bbb"), |
| 94 | + abs_path("ccc"), |
| 95 | + abs_path("xyz")]); |
| 96 | + |
| 97 | + assert_eq!(glob_vec("aaa/tomato/tom?to.txt"), ~[ |
| 98 | + abs_path("aaa/tomato/tomato.txt"), |
| 99 | + abs_path("aaa/tomato/tomoto.txt")]); |
| 100 | + |
| 101 | + assert_eq!(glob_vec("xyz/?"), ~[ |
| 102 | + abs_path("xyz/x"), |
| 103 | + abs_path("xyz/y"), |
| 104 | + abs_path("xyz/z")]); |
| 105 | + |
| 106 | + assert_eq!(glob_vec("a*"), ~[abs_path("aaa")]); |
| 107 | + assert_eq!(glob_vec("*a*"), ~[abs_path("aaa")]); |
| 108 | + assert_eq!(glob_vec("a*a"), ~[abs_path("aaa")]); |
| 109 | + assert_eq!(glob_vec("aaa*"), ~[abs_path("aaa")]); |
| 110 | + assert_eq!(glob_vec("*aaa"), ~[abs_path("aaa")]); |
| 111 | + assert_eq!(glob_vec("*aaa*"), ~[abs_path("aaa")]); |
| 112 | + assert_eq!(glob_vec("*a*a*a*"), ~[abs_path("aaa")]); |
| 113 | + assert_eq!(glob_vec("aaa*/"), ~[abs_path("aaa")]); |
| 114 | + |
| 115 | + assert_eq!(glob_vec("aaa/*"), ~[ |
| 116 | + abs_path("aaa/apple"), |
| 117 | + abs_path("aaa/orange"), |
| 118 | + abs_path("aaa/tomato")]); |
| 119 | + |
| 120 | + assert_eq!(glob_vec("aaa/*a*"), ~[ |
| 121 | + abs_path("aaa/apple"), |
| 122 | + abs_path("aaa/orange"), |
| 123 | + abs_path("aaa/tomato")]); |
| 124 | + |
| 125 | + assert_eq!(glob_vec("*/*/*.txt"), ~[ |
| 126 | + abs_path("aaa/tomato/tomato.txt"), |
| 127 | + abs_path("aaa/tomato/tomoto.txt")]); |
| 128 | + |
| 129 | + assert_eq!(glob_vec("*/*/t[aob]m?to[.]t[!y]t"), ~[ |
| 130 | + abs_path("aaa/tomato/tomato.txt"), |
| 131 | + abs_path("aaa/tomato/tomoto.txt")]); |
| 132 | + |
| 133 | + assert_eq!(glob_vec("aa[a]"), ~[abs_path("aaa")]); |
| 134 | + assert_eq!(glob_vec("aa[abc]"), ~[abs_path("aaa")]); |
| 135 | + assert_eq!(glob_vec("a[bca]a"), ~[abs_path("aaa")]); |
| 136 | + assert_eq!(glob_vec("aa[b]"), ~[]); |
| 137 | + assert_eq!(glob_vec("aa[xyz]"), ~[]); |
| 138 | + assert_eq!(glob_vec("aa[]]"), ~[]); |
| 139 | + |
| 140 | + assert_eq!(glob_vec("aa[!b]"), ~[abs_path("aaa")]); |
| 141 | + assert_eq!(glob_vec("aa[!bcd]"), ~[abs_path("aaa")]); |
| 142 | + assert_eq!(glob_vec("a[!bcd]a"), ~[abs_path("aaa")]); |
| 143 | + assert_eq!(glob_vec("aa[!a]"), ~[]); |
| 144 | + assert_eq!(glob_vec("aa[!abc]"), ~[]); |
| 145 | + |
| 146 | + assert_eq!(glob_vec("bbb/specials/[[]"), ~[abs_path("bbb/specials/[")]); |
| 147 | + assert_eq!(glob_vec("bbb/specials/!"), ~[abs_path("bbb/specials/!")]); |
| 148 | + assert_eq!(glob_vec("bbb/specials/[]]"), ~[abs_path("bbb/specials/]")]); |
| 149 | + |
| 150 | + if os::consts::FAMILY != os::consts::windows::FAMILY { |
| 151 | + assert_eq!(glob_vec("bbb/specials/[*]"), ~[abs_path("bbb/specials/*")]); |
| 152 | + assert_eq!(glob_vec("bbb/specials/[?]"), ~[abs_path("bbb/specials/?")]); |
| 153 | + } |
| 154 | + |
| 155 | + if os::consts::FAMILY == os::consts::windows::FAMILY { |
| 156 | + |
| 157 | + assert_eq!(glob_vec("bbb/specials/[![]"), ~[ |
| 158 | + abs_path("bbb/specials/!"), |
| 159 | + abs_path("bbb/specials/]")]); |
| 160 | + |
| 161 | + assert_eq!(glob_vec("bbb/specials/[!]]"), ~[ |
| 162 | + abs_path("bbb/specials/!"), |
| 163 | + abs_path("bbb/specials/[")]); |
| 164 | + |
| 165 | + assert_eq!(glob_vec("bbb/specials/[!!]"), ~[ |
| 166 | + abs_path("bbb/specials/["), |
| 167 | + abs_path("bbb/specials/]")]); |
| 168 | + |
| 169 | + } else { |
| 170 | + |
| 171 | + assert_eq!(glob_vec("bbb/specials/[![]"), ~[ |
| 172 | + abs_path("bbb/specials/!"), |
| 173 | + abs_path("bbb/specials/*"), |
| 174 | + abs_path("bbb/specials/?"), |
| 175 | + abs_path("bbb/specials/]")]); |
| 176 | + |
| 177 | + assert_eq!(glob_vec("bbb/specials/[!]]"), ~[ |
| 178 | + abs_path("bbb/specials/!"), |
| 179 | + abs_path("bbb/specials/*"), |
| 180 | + abs_path("bbb/specials/?"), |
| 181 | + abs_path("bbb/specials/[")]); |
| 182 | + |
| 183 | + assert_eq!(glob_vec("bbb/specials/[!!]"), ~[ |
| 184 | + abs_path("bbb/specials/*"), |
| 185 | + abs_path("bbb/specials/?"), |
| 186 | + abs_path("bbb/specials/["), |
| 187 | + abs_path("bbb/specials/]")]); |
| 188 | + |
| 189 | + assert_eq!(glob_vec("bbb/specials/[!*]"), ~[ |
| 190 | + abs_path("bbb/specials/!"), |
| 191 | + abs_path("bbb/specials/?"), |
| 192 | + abs_path("bbb/specials/["), |
| 193 | + abs_path("bbb/specials/]")]); |
| 194 | + |
| 195 | + assert_eq!(glob_vec("bbb/specials/[!?]"), ~[ |
| 196 | + abs_path("bbb/specials/!"), |
| 197 | + abs_path("bbb/specials/*"), |
| 198 | + abs_path("bbb/specials/["), |
| 199 | + abs_path("bbb/specials/]")]); |
| 200 | + |
| 201 | + } |
| 202 | + }; |
| 203 | +} |
0 commit comments