]>
git.proxmox.com Git - rustc.git/blob - src/vendor/tempfile/tests/tempdir.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.
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 extern crate tempfile
;
16 use std
::sync
::mpsc
::channel
;
19 use tempfile
::{Builder, TempDir}
;
25 Err(e
) => panic
!("error: {}", e
),
31 fn exists(&self) -> bool
;
32 fn is_dir(&self) -> bool
;
35 impl PathExt
for Path
{
36 fn exists(&self) -> bool
{
37 fs
::metadata(self).is_ok()
39 fn is_dir(&self) -> bool
{
40 fs
::metadata(self).map(|m
| m
.is_dir()).unwrap_or(false)
46 let p
= t
!(Builder
::new().prefix("foobar").tempdir_in(&Path
::new(".")));
48 assert
!(p
.to_str().unwrap().contains("foobar"));
51 assert
!(!path
.exists());
55 fn test_customnamed() {
56 let tmpfile
= Builder
::new()
62 let name
= tmpfile
.path().file_name().unwrap().to_str().unwrap();
63 assert
!(name
.starts_with("prefix"));
64 assert
!(name
.ends_with("suffix"));
65 assert_eq
!(name
.len(), 24);
68 fn test_rm_tempdir() {
69 let (tx
, rx
) = channel();
70 let f
= move || -> () {
71 let tmp
= t
!(TempDir
::new());
72 tx
.send(tmp
.path().to_path_buf()).unwrap();
73 panic
!("panic to unwind past `tmp`");
75 let _
= thread
::spawn(f
).join();
76 let path
= rx
.recv().unwrap();
77 assert
!(!path
.exists());
79 let tmp
= t
!(TempDir
::new());
80 let path
= tmp
.path().to_path_buf();
81 let f
= move || -> () {
83 panic
!("panic to unwind past `tmp`");
85 let _
= thread
::spawn(f
).join();
86 assert
!(!path
.exists());
90 let f
= move || t
!(TempDir
::new());
92 let tmp
= thread
::spawn(f
).join().unwrap();
93 path
= tmp
.path().to_path_buf();
94 assert
!(path
.exists());
96 assert
!(!path
.exists());
100 let tmp
= t
!(TempDir
::new());
101 path
= tmp
.into_path();
103 assert
!(path
.exists());
104 t
!(fs
::remove_dir_all(&path
));
105 assert
!(!path
.exists());
108 fn test_rm_tempdir_close() {
109 let (tx
, rx
) = channel();
110 let f
= move || -> () {
111 let tmp
= t
!(TempDir
::new());
112 tx
.send(tmp
.path().to_path_buf()).unwrap();
114 panic
!("panic when unwinding past `tmp`");
116 let _
= thread
::spawn(f
).join();
117 let path
= rx
.recv().unwrap();
118 assert
!(!path
.exists());
120 let tmp
= t
!(TempDir
::new());
121 let path
= tmp
.path().to_path_buf();
122 let f
= move || -> () {
125 panic
!("panic when unwinding past `tmp`");
127 let _
= thread
::spawn(f
).join();
128 assert
!(!path
.exists());
132 let f
= move || t
!(TempDir
::new());
134 let tmp
= thread
::spawn(f
).join().unwrap();
135 path
= tmp
.path().to_path_buf();
136 assert
!(path
.exists());
139 assert
!(!path
.exists());
143 let tmp
= t
!(TempDir
::new());
144 path
= tmp
.into_path();
146 assert
!(path
.exists());
147 t
!(fs
::remove_dir_all(&path
));
148 assert
!(!path
.exists());
151 // Ideally these would be in std::os but then core would need
153 fn recursive_mkdir_rel() {
154 let path
= Path
::new("frob");
155 let cwd
= env
::current_dir().unwrap();
157 "recursive_mkdir_rel: Making: {} in cwd {} [{}]",
162 t
!(fs
::create_dir(&path
));
163 assert
!(path
.is_dir());
164 t
!(fs
::create_dir_all(&path
));
165 assert
!(path
.is_dir());
168 fn recursive_mkdir_dot() {
169 let dot
= Path
::new(".");
170 t
!(fs
::create_dir_all(&dot
));
171 let dotdot
= Path
::new("..");
172 t
!(fs
::create_dir_all(&dotdot
));
175 fn recursive_mkdir_rel_2() {
176 let path
= Path
::new("./frob/baz");
177 let cwd
= env
::current_dir().unwrap();
179 "recursive_mkdir_rel_2: Making: {} in cwd {} [{}]",
184 t
!(fs
::create_dir_all(&path
));
185 assert
!(path
.is_dir());
186 assert
!(path
.parent().unwrap().is_dir());
187 let path2
= Path
::new("quux/blat");
189 "recursive_mkdir_rel_2: Making: {} in cwd {}",
193 t
!(fs
::create_dir("quux"));
194 t
!(fs
::create_dir_all(&path2
));
195 assert
!(path2
.is_dir());
196 assert
!(path2
.parent().unwrap().is_dir());
199 // Ideally this would be in core, but needs TempFile
200 pub fn test_remove_dir_all_ok() {
201 let tmpdir
= t
!(TempDir
::new());
202 let tmpdir
= tmpdir
.path();
203 let root
= tmpdir
.join("foo");
205 println
!("making {}", root
.display());
206 t
!(fs
::create_dir(&root
));
207 t
!(fs
::create_dir(&root
.join("foo")));
208 t
!(fs
::create_dir(&root
.join("foo").join("bar")));
209 t
!(fs
::create_dir(&root
.join("foo").join("bar").join("blat")));
210 t
!(fs
::remove_dir_all(&root
));
211 assert
!(!root
.exists());
212 assert
!(!root
.join("bar").exists());
213 assert
!(!root
.join("bar").join("blat").exists());
216 pub fn dont_double_panic() {
217 let r
: Result
<(), _
> = thread
::spawn(move || {
218 let tmpdir
= TempDir
::new().unwrap();
219 // Remove the temporary directory so that TempDir sees
221 t
!(fs
::remove_dir(tmpdir
.path()));
222 // Panic. If TempDir panics *again* due to the rmdir
223 // error then the process will abort.
229 fn in_tmpdir
<F
>(f
: F
)
233 let tmpdir
= t
!(TempDir
::new());
234 assert
!(env
::set_current_dir(tmpdir
.path()).is_ok());
239 pub fn pass_as_asref_path() {
240 let tempdir
= t
!(TempDir
::new());
241 takes_asref_path(&tempdir
);
243 fn takes_asref_path
<T
: AsRef
<Path
>>(path
: T
) {
244 let path
= path
.as_ref();
245 assert
!(path
.exists());
251 in_tmpdir(test_tempdir
);
252 in_tmpdir(test_rm_tempdir
);
253 in_tmpdir(test_rm_tempdir_close
);
254 in_tmpdir(recursive_mkdir_rel
);
255 in_tmpdir(recursive_mkdir_dot
);
256 in_tmpdir(recursive_mkdir_rel_2
);
257 in_tmpdir(test_remove_dir_all_ok
);
258 in_tmpdir(dont_double_panic
);
259 in_tmpdir(pass_as_asref_path
);