]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/glob-std.rs
auto merge of #16659 : brson/rust/slowparse, r=alexcrichton
[rust.git] / src / test / run-pass / glob-std.rs
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 #![feature(macro_rules)]
14
15 extern crate glob;
16
17 use glob::glob;
18 use std::os;
19 use std::io;
20 use std::io::TempDir;
21
22 macro_rules! assert_eq ( ($e1:expr, $e2:expr) => (
23     if $e1 != $e2 {
24         fail!("{} != {}", stringify!($e1), stringify!($e2))
25     }
26 ) )
27
28 pub fn main() {
29     fn mk_file(path: &str, directory: bool) {
30         if directory {
31             io::fs::mkdir(&Path::new(path), io::UserRWX).unwrap();
32         } else {
33             io::File::create(&Path::new(path)).unwrap();
34         }
35     }
36
37     fn abs_path(path: &str) -> Path {
38         os::getcwd().join(&Path::new(path))
39     }
40
41     fn glob_vec(pattern: &str) -> Vec<Path> {
42         glob(pattern).collect()
43     }
44
45     let root = TempDir::new("glob-tests");
46     let root = root.expect("Should have created a temp directory");
47     assert!(os::change_dir(root.path()));
48
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 != "windows" {
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(""), Vec::new());
74     assert_eq!(glob_vec("."), vec!(os::getcwd()));
75     assert_eq!(glob_vec(".."), vec!(os::getcwd().join("..")));
76
77     assert_eq!(glob_vec("aaa"), vec!(abs_path("aaa")));
78     assert_eq!(glob_vec("aaa/"), vec!(abs_path("aaa")));
79     assert_eq!(glob_vec("a"), Vec::new());
80     assert_eq!(glob_vec("aa"), Vec::new());
81     assert_eq!(glob_vec("aaaa"), Vec::new());
82
83     assert_eq!(glob_vec("aaa/apple"), vec!(abs_path("aaa/apple")));
84     assert_eq!(glob_vec("aaa/apple/nope"), Vec::new());
85
86     // windows should support both / and \ as directory separators
87     if os::consts::FAMILY == "windows" {
88         assert_eq!(glob_vec("aaa\\apple"), vec!(abs_path("aaa/apple")));
89     }
90
91     assert_eq!(glob_vec("???/"), 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"), vec!(
98         abs_path("aaa/tomato/tomato.txt"),
99         abs_path("aaa/tomato/tomoto.txt")));
100
101     assert_eq!(glob_vec("xyz/?"), vec!(
102         abs_path("xyz/x"),
103         abs_path("xyz/y"),
104         abs_path("xyz/z")));
105
106     assert_eq!(glob_vec("a*"), vec!(abs_path("aaa")));
107     assert_eq!(glob_vec("*a*"), vec!(abs_path("aaa")));
108     assert_eq!(glob_vec("a*a"), vec!(abs_path("aaa")));
109     assert_eq!(glob_vec("aaa*"), vec!(abs_path("aaa")));
110     assert_eq!(glob_vec("*aaa"), vec!(abs_path("aaa")));
111     assert_eq!(glob_vec("*aaa*"), vec!(abs_path("aaa")));
112     assert_eq!(glob_vec("*a*a*a*"), vec!(abs_path("aaa")));
113     assert_eq!(glob_vec("aaa*/"), vec!(abs_path("aaa")));
114
115     assert_eq!(glob_vec("aaa/*"), vec!(
116         abs_path("aaa/apple"),
117         abs_path("aaa/orange"),
118         abs_path("aaa/tomato")));
119
120     assert_eq!(glob_vec("aaa/*a*"), vec!(
121         abs_path("aaa/apple"),
122         abs_path("aaa/orange"),
123         abs_path("aaa/tomato")));
124
125     assert_eq!(glob_vec("*/*/*.txt"), vec!(
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"), vec!(
130         abs_path("aaa/tomato/tomato.txt"),
131         abs_path("aaa/tomato/tomoto.txt")));
132
133     assert_eq!(glob_vec("./aaa"), vec!(abs_path("aaa")));
134     assert_eq!(glob_vec("./*"), glob_vec("*"));
135     assert_eq!(glob_vec("*/..").pop().unwrap(), abs_path("."));
136     assert_eq!(glob_vec("aaa/../bbb"), vec!(abs_path("bbb")));
137     assert_eq!(glob_vec("nonexistent/../bbb"), Vec::new());
138     assert_eq!(glob_vec("aaa/tomato/tomato.txt/.."), Vec::new());
139
140     assert_eq!(glob_vec("aaa/tomato/tomato.txt/"), Vec::new());
141
142     assert_eq!(glob_vec("aa[a]"), vec!(abs_path("aaa")));
143     assert_eq!(glob_vec("aa[abc]"), vec!(abs_path("aaa")));
144     assert_eq!(glob_vec("a[bca]a"), vec!(abs_path("aaa")));
145     assert_eq!(glob_vec("aa[b]"), Vec::new());
146     assert_eq!(glob_vec("aa[xyz]"), Vec::new());
147     assert_eq!(glob_vec("aa[]]"), Vec::new());
148
149     assert_eq!(glob_vec("aa[!b]"), vec!(abs_path("aaa")));
150     assert_eq!(glob_vec("aa[!bcd]"), vec!(abs_path("aaa")));
151     assert_eq!(glob_vec("a[!bcd]a"), vec!(abs_path("aaa")));
152     assert_eq!(glob_vec("aa[!a]"), Vec::new());
153     assert_eq!(glob_vec("aa[!abc]"), Vec::new());
154
155     assert_eq!(glob_vec("bbb/specials/[[]"), vec!(abs_path("bbb/specials/[")));
156     assert_eq!(glob_vec("bbb/specials/!"), vec!(abs_path("bbb/specials/!")));
157     assert_eq!(glob_vec("bbb/specials/[]]"), vec!(abs_path("bbb/specials/]")));
158
159     if os::consts::FAMILY != "windows" {
160         assert_eq!(glob_vec("bbb/specials/[*]"), vec!(abs_path("bbb/specials/*")));
161         assert_eq!(glob_vec("bbb/specials/[?]"), vec!(abs_path("bbb/specials/?")));
162     }
163
164     if os::consts::FAMILY == "windows" {
165
166         assert_eq!(glob_vec("bbb/specials/[![]"), vec!(
167             abs_path("bbb/specials/!"),
168             abs_path("bbb/specials/]")));
169
170         assert_eq!(glob_vec("bbb/specials/[!]]"), vec!(
171             abs_path("bbb/specials/!"),
172             abs_path("bbb/specials/[")));
173
174         assert_eq!(glob_vec("bbb/specials/[!!]"), vec!(
175             abs_path("bbb/specials/["),
176             abs_path("bbb/specials/]")));
177
178     } else {
179
180         assert_eq!(glob_vec("bbb/specials/[![]"), vec!(
181             abs_path("bbb/specials/!"),
182             abs_path("bbb/specials/*"),
183             abs_path("bbb/specials/?"),
184             abs_path("bbb/specials/]")));
185
186         assert_eq!(glob_vec("bbb/specials/[!]]"), vec!(
187             abs_path("bbb/specials/!"),
188             abs_path("bbb/specials/*"),
189             abs_path("bbb/specials/?"),
190             abs_path("bbb/specials/[")));
191
192         assert_eq!(glob_vec("bbb/specials/[!!]"), vec!(
193             abs_path("bbb/specials/*"),
194             abs_path("bbb/specials/?"),
195             abs_path("bbb/specials/["),
196             abs_path("bbb/specials/]")));
197
198         assert_eq!(glob_vec("bbb/specials/[!*]"), vec!(
199             abs_path("bbb/specials/!"),
200             abs_path("bbb/specials/?"),
201             abs_path("bbb/specials/["),
202             abs_path("bbb/specials/]")));
203
204         assert_eq!(glob_vec("bbb/specials/[!?]"), vec!(
205             abs_path("bbb/specials/!"),
206             abs_path("bbb/specials/*"),
207             abs_path("bbb/specials/["),
208             abs_path("bbb/specials/]")));
209
210     }
211 }