]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/tempfile.rs
476278405ca8a88a8a00eacd6c0f2c5709e501b1
[rust.git] / src / test / run-pass / tempfile.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: #10463
12
13 // These tests are here to exercise the functionality of the `tempfile` module.
14 // One might expect these tests to be located in that module, but sadly they
15 // cannot. The tests need to invoke `os::change_dir` which cannot be done in the
16 // normal test infrastructure. If the tests change the current working
17 // directory, then *all* tests which require relative paths suddenly break b/c
18 // they're in a different location than before. Hence, these tests are all run
19 // serially here.
20
21 use std::io::fs::PathExtensions;
22 use std::io::{fs, TempDir};
23 use std::io;
24 use std::os;
25 use std::task;
26
27 fn test_tempdir() {
28     let path = {
29         let p = TempDir::new_in(&Path::new("."), "foobar").unwrap();
30         let p = p.path();
31         assert!(p.as_vec().ends_with(b"foobar"));
32         p.clone()
33     };
34     assert!(!path.exists());
35 }
36
37 fn test_rm_tempdir() {
38     let (tx, rx) = channel();
39     let f: proc():Send = proc() {
40         let tmp = TempDir::new("test_rm_tempdir").unwrap();
41         tx.send(tmp.path().clone());
42         panic!("panic to unwind past `tmp`");
43     };
44     task::try(f);
45     let path = rx.recv();
46     assert!(!path.exists());
47
48     let tmp = TempDir::new("test_rm_tempdir").unwrap();
49     let path = tmp.path().clone();
50     let f: proc():Send = proc() {
51         let _tmp = tmp;
52         panic!("panic to unwind past `tmp`");
53     };
54     task::try(f);
55     assert!(!path.exists());
56
57     let path;
58     {
59         let f = proc() {
60             TempDir::new("test_rm_tempdir").unwrap()
61         };
62         let tmp = task::try(f).ok().expect("test_rm_tmdir");
63         path = tmp.path().clone();
64         assert!(path.exists());
65     }
66     assert!(!path.exists());
67
68     let path;
69     {
70         let tmp = TempDir::new("test_rm_tempdir").unwrap();
71         path = tmp.unwrap();
72     }
73     assert!(path.exists());
74     fs::rmdir_recursive(&path);
75     assert!(!path.exists());
76 }
77
78 fn test_rm_tempdir_close() {
79     let (tx, rx) = channel();
80     let f: proc():Send = proc() {
81         let tmp = TempDir::new("test_rm_tempdir").unwrap();
82         tx.send(tmp.path().clone());
83         tmp.close();
84         panic!("panic when unwinding past `tmp`");
85     };
86     task::try(f);
87     let path = rx.recv();
88     assert!(!path.exists());
89
90     let tmp = TempDir::new("test_rm_tempdir").unwrap();
91     let path = tmp.path().clone();
92     let f: proc():Send = proc() {
93         let tmp = tmp;
94         tmp.close();
95         panic!("panic when unwinding past `tmp`");
96     };
97     task::try(f);
98     assert!(!path.exists());
99
100     let path;
101     {
102         let f = proc() {
103             TempDir::new("test_rm_tempdir").unwrap()
104         };
105         let tmp = task::try(f).ok().expect("test_rm_tmdir");
106         path = tmp.path().clone();
107         assert!(path.exists());
108         tmp.close();
109     }
110     assert!(!path.exists());
111
112     let path;
113     {
114         let tmp = TempDir::new("test_rm_tempdir").unwrap();
115         path = tmp.unwrap();
116     }
117     assert!(path.exists());
118     fs::rmdir_recursive(&path);
119     assert!(!path.exists());
120 }
121
122 // Ideally these would be in std::os but then core would need
123 // to depend on std
124 fn recursive_mkdir_rel() {
125     let path = Path::new("frob");
126     let cwd = os::getcwd();
127     println!("recursive_mkdir_rel: Making: {} in cwd {} [{}]", path.display(),
128            cwd.display(), path.exists());
129     fs::mkdir_recursive(&path, io::USER_RWX);
130     assert!(path.is_dir());
131     fs::mkdir_recursive(&path, io::USER_RWX);
132     assert!(path.is_dir());
133 }
134
135 fn recursive_mkdir_dot() {
136     let dot = Path::new(".");
137     fs::mkdir_recursive(&dot, io::USER_RWX);
138     let dotdot = Path::new("..");
139     fs::mkdir_recursive(&dotdot, io::USER_RWX);
140 }
141
142 fn recursive_mkdir_rel_2() {
143     let path = Path::new("./frob/baz");
144     let cwd = os::getcwd();
145     println!("recursive_mkdir_rel_2: Making: {} in cwd {} [{}]", path.display(),
146            cwd.display(), path.exists());
147     fs::mkdir_recursive(&path, io::USER_RWX);
148     assert!(path.is_dir());
149     assert!(path.dir_path().is_dir());
150     let path2 = Path::new("quux/blat");
151     println!("recursive_mkdir_rel_2: Making: {} in cwd {}", path2.display(),
152            cwd.display());
153     fs::mkdir_recursive(&path2, io::USER_RWX);
154     assert!(path2.is_dir());
155     assert!(path2.dir_path().is_dir());
156 }
157
158 // Ideally this would be in core, but needs TempFile
159 pub fn test_rmdir_recursive_ok() {
160     let rwx = io::USER_RWX;
161
162     let tmpdir = TempDir::new("test").ok().expect("test_rmdir_recursive_ok: \
163                                                    couldn't create temp dir");
164     let tmpdir = tmpdir.path();
165     let root = tmpdir.join("foo");
166
167     println!("making {}", root.display());
168     fs::mkdir(&root, rwx);
169     fs::mkdir(&root.join("foo"), rwx);
170     fs::mkdir(&root.join("foo").join("bar"), rwx);
171     fs::mkdir(&root.join("foo").join("bar").join("blat"), rwx);
172     fs::rmdir_recursive(&root);
173     assert!(!root.exists());
174     assert!(!root.join("bar").exists());
175     assert!(!root.join("bar").join("blat").exists());
176 }
177
178 pub fn dont_double_panic() {
179     let r: Result<(), _> = task::try(proc() {
180         let tmpdir = TempDir::new("test").unwrap();
181         // Remove the temporary directory so that TempDir sees
182         // an error on drop
183         fs::rmdir(tmpdir.path());
184         // Panic. If TempDir panics *again* due to the rmdir
185         // error then the process will abort.
186         panic!();
187     });
188     assert!(r.is_err());
189 }
190
191 fn in_tmpdir(f: ||) {
192     let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
193     assert!(os::change_dir(tmpdir.path()));
194
195     f();
196 }
197
198 pub fn main() {
199     in_tmpdir(test_tempdir);
200     in_tmpdir(test_rm_tempdir);
201     in_tmpdir(test_rm_tempdir_close);
202     in_tmpdir(recursive_mkdir_rel);
203     in_tmpdir(recursive_mkdir_dot);
204     in_tmpdir(recursive_mkdir_rel_2);
205     in_tmpdir(test_rmdir_recursive_ok);
206     in_tmpdir(dont_double_panic);
207 }