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.
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.
11 // ignore-windows TempDir may cause IoError on windows: #10463
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
21 use std::io::fs::PathExtensions;
22 use std::io::{fs, TempDir};
29 let p = TempDir::new_in(&Path::new("."), "foobar").unwrap();
31 assert!(p.as_vec().ends_with(b"foobar"));
34 assert!(!path.exists());
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`");
46 assert!(!path.exists());
48 let tmp = TempDir::new("test_rm_tempdir").unwrap();
49 let path = tmp.path().clone();
50 let f: proc():Send = proc() {
52 panic!("panic to unwind past `tmp`");
55 assert!(!path.exists());
60 TempDir::new("test_rm_tempdir").unwrap()
62 let tmp = task::try(f).ok().expect("test_rm_tmdir");
63 path = tmp.path().clone();
64 assert!(path.exists());
66 assert!(!path.exists());
70 let tmp = TempDir::new("test_rm_tempdir").unwrap();
73 assert!(path.exists());
74 fs::rmdir_recursive(&path);
75 assert!(!path.exists());
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());
84 panic!("panic when unwinding past `tmp`");
88 assert!(!path.exists());
90 let tmp = TempDir::new("test_rm_tempdir").unwrap();
91 let path = tmp.path().clone();
92 let f: proc():Send = proc() {
95 panic!("panic when unwinding past `tmp`");
98 assert!(!path.exists());
103 TempDir::new("test_rm_tempdir").unwrap()
105 let tmp = task::try(f).ok().expect("test_rm_tmdir");
106 path = tmp.path().clone();
107 assert!(path.exists());
110 assert!(!path.exists());
114 let tmp = TempDir::new("test_rm_tempdir").unwrap();
117 assert!(path.exists());
118 fs::rmdir_recursive(&path);
119 assert!(!path.exists());
122 // Ideally these would be in std::os but then core would need
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());
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);
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(),
153 fs::mkdir_recursive(&path2, io::USER_RWX);
154 assert!(path2.is_dir());
155 assert!(path2.dir_path().is_dir());
158 // Ideally this would be in core, but needs TempFile
159 pub fn test_rmdir_recursive_ok() {
160 let rwx = io::USER_RWX;
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");
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());
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
183 fs::rmdir(tmpdir.path());
184 // Panic. If TempDir panics *again* due to the rmdir
185 // error then the process will abort.
191 fn in_tmpdir(f: ||) {
192 let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
193 assert!(os::change_dir(tmpdir.path()));
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);