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};
26 use std::sync::mpsc::channel;
30 let p = TempDir::new_in(&Path::new("."), "foobar").unwrap();
32 assert!(p.as_vec().ends_with(b"foobar"));
35 assert!(!path.exists());
38 fn test_rm_tempdir() {
39 let (tx, rx) = channel();
40 let f = move|:| -> () {
41 let tmp = TempDir::new("test_rm_tempdir").unwrap();
42 tx.send(tmp.path().clone()).unwrap();
43 panic!("panic to unwind past `tmp`");
46 let path = rx.recv().unwrap();
47 assert!(!path.exists());
49 let tmp = TempDir::new("test_rm_tempdir").unwrap();
50 let path = tmp.path().clone();
51 let f = move|:| -> () {
53 panic!("panic to unwind past `tmp`");
56 assert!(!path.exists());
61 TempDir::new("test_rm_tempdir").unwrap()
63 let tmp = task::try(f).ok().expect("test_rm_tmdir");
64 path = tmp.path().clone();
65 assert!(path.exists());
67 assert!(!path.exists());
71 let tmp = TempDir::new("test_rm_tempdir").unwrap();
74 assert!(path.exists());
75 fs::rmdir_recursive(&path);
76 assert!(!path.exists());
79 fn test_rm_tempdir_close() {
80 let (tx, rx) = channel();
81 let f = move|:| -> () {
82 let tmp = TempDir::new("test_rm_tempdir").unwrap();
83 tx.send(tmp.path().clone()).unwrap();
85 panic!("panic when unwinding past `tmp`");
88 let path = rx.recv().unwrap();
89 assert!(!path.exists());
91 let tmp = TempDir::new("test_rm_tempdir").unwrap();
92 let path = tmp.path().clone();
93 let f = move|:| -> () {
96 panic!("panic when unwinding past `tmp`");
99 assert!(!path.exists());
104 TempDir::new("test_rm_tempdir").unwrap()
106 let tmp = task::try(f).ok().expect("test_rm_tmdir");
107 path = tmp.path().clone();
108 assert!(path.exists());
111 assert!(!path.exists());
115 let tmp = TempDir::new("test_rm_tempdir").unwrap();
118 assert!(path.exists());
119 fs::rmdir_recursive(&path);
120 assert!(!path.exists());
123 // Ideally these would be in std::os but then core would need
125 fn recursive_mkdir_rel() {
126 let path = Path::new("frob");
127 let cwd = os::getcwd().unwrap();
128 println!("recursive_mkdir_rel: Making: {} in cwd {} [{}]", path.display(),
129 cwd.display(), path.exists());
130 fs::mkdir_recursive(&path, io::USER_RWX);
131 assert!(path.is_dir());
132 fs::mkdir_recursive(&path, io::USER_RWX);
133 assert!(path.is_dir());
136 fn recursive_mkdir_dot() {
137 let dot = Path::new(".");
138 fs::mkdir_recursive(&dot, io::USER_RWX);
139 let dotdot = Path::new("..");
140 fs::mkdir_recursive(&dotdot, io::USER_RWX);
143 fn recursive_mkdir_rel_2() {
144 let path = Path::new("./frob/baz");
145 let cwd = os::getcwd().unwrap();
146 println!("recursive_mkdir_rel_2: Making: {} in cwd {} [{}]", path.display(),
147 cwd.display(), path.exists());
148 fs::mkdir_recursive(&path, io::USER_RWX);
149 assert!(path.is_dir());
150 assert!(path.dir_path().is_dir());
151 let path2 = Path::new("quux/blat");
152 println!("recursive_mkdir_rel_2: Making: {} in cwd {}", path2.display(),
154 fs::mkdir_recursive(&path2, io::USER_RWX);
155 assert!(path2.is_dir());
156 assert!(path2.dir_path().is_dir());
159 // Ideally this would be in core, but needs TempFile
160 pub fn test_rmdir_recursive_ok() {
161 let rwx = io::USER_RWX;
163 let tmpdir = TempDir::new("test").ok().expect("test_rmdir_recursive_ok: \
164 couldn't create temp dir");
165 let tmpdir = tmpdir.path();
166 let root = tmpdir.join("foo");
168 println!("making {}", root.display());
169 fs::mkdir(&root, rwx);
170 fs::mkdir(&root.join("foo"), rwx);
171 fs::mkdir(&root.join("foo").join("bar"), rwx);
172 fs::mkdir(&root.join("foo").join("bar").join("blat"), rwx);
173 fs::rmdir_recursive(&root);
174 assert!(!root.exists());
175 assert!(!root.join("bar").exists());
176 assert!(!root.join("bar").join("blat").exists());
179 pub fn dont_double_panic() {
180 let r: Result<(), _> = task::try(move|| {
181 let tmpdir = TempDir::new("test").unwrap();
182 // Remove the temporary directory so that TempDir sees
184 fs::rmdir(tmpdir.path());
185 // Panic. If TempDir panics *again* due to the rmdir
186 // error then the process will abort.
192 fn in_tmpdir(f: ||) {
193 let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
194 assert!(os::change_dir(tmpdir.path()).is_ok());
200 in_tmpdir(test_tempdir);
201 in_tmpdir(test_rm_tempdir);
202 in_tmpdir(test_rm_tempdir_close);
203 in_tmpdir(recursive_mkdir_rel);
204 in_tmpdir(recursive_mkdir_dot);
205 in_tmpdir(recursive_mkdir_rel_2);
206 in_tmpdir(test_rmdir_recursive_ok);
207 in_tmpdir(dont_double_panic);