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 #![deny(rust_2018_idioms)]
12
13 use std::fs;
14 use std::path::Path;
15 use std::sync::mpsc::channel;
16 use std::thread;
17
18 use tempfile::{Builder, TempDir};
19
test_tempdir()20 fn test_tempdir() {
21 let path = {
22 let p = Builder::new().prefix("foobar").tempdir_in(".").unwrap();
23 let p = p.path();
24 assert!(p.to_str().unwrap().contains("foobar"));
25 p.to_path_buf()
26 };
27 assert!(!path.exists());
28 }
29
test_prefix()30 fn test_prefix() {
31 let tmpfile = TempDir::with_prefix_in("prefix", ".").unwrap();
32 let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
33 assert!(name.starts_with("prefix"));
34 }
35
test_customnamed()36 fn test_customnamed() {
37 let tmpfile = Builder::new()
38 .prefix("prefix")
39 .suffix("suffix")
40 .rand_bytes(12)
41 .tempdir()
42 .unwrap();
43 let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
44 assert!(name.starts_with("prefix"));
45 assert!(name.ends_with("suffix"));
46 assert_eq!(name.len(), 24);
47 }
48
test_rm_tempdir()49 fn test_rm_tempdir() {
50 let (tx, rx) = channel();
51 let f = move || {
52 let tmp = TempDir::new().unwrap();
53 tx.send(tmp.path().to_path_buf()).unwrap();
54 panic!("panic to unwind past `tmp`");
55 };
56 let _ = thread::spawn(f).join();
57 let path = rx.recv().unwrap();
58 assert!(!path.exists());
59
60 let tmp = TempDir::new().unwrap();
61 let path = tmp.path().to_path_buf();
62 let f = move || {
63 let _tmp = tmp;
64 panic!("panic to unwind past `tmp`");
65 };
66 let _ = thread::spawn(f).join();
67 assert!(!path.exists());
68
69 let path;
70 {
71 let f = move || TempDir::new().unwrap();
72
73 let tmp = thread::spawn(f).join().unwrap();
74 path = tmp.path().to_path_buf();
75 assert!(path.exists());
76 }
77 assert!(!path.exists());
78
79 let path;
80 {
81 let tmp = TempDir::new().unwrap();
82 path = tmp.into_path();
83 }
84 assert!(path.exists());
85 fs::remove_dir_all(&path).unwrap();
86 assert!(!path.exists());
87 }
88
test_rm_tempdir_close()89 fn test_rm_tempdir_close() {
90 let (tx, rx) = channel();
91 let f = move || {
92 let tmp = TempDir::new().unwrap();
93 tx.send(tmp.path().to_path_buf()).unwrap();
94 tmp.close().unwrap();
95 panic!("panic when unwinding past `tmp`");
96 };
97 let _ = thread::spawn(f).join();
98 let path = rx.recv().unwrap();
99 assert!(!path.exists());
100
101 let tmp = TempDir::new().unwrap();
102 let path = tmp.path().to_path_buf();
103 let f = move || {
104 let tmp = tmp;
105 tmp.close().unwrap();
106 panic!("panic when unwinding past `tmp`");
107 };
108 let _ = thread::spawn(f).join();
109 assert!(!path.exists());
110
111 let path;
112 {
113 let f = move || TempDir::new().unwrap();
114
115 let tmp = thread::spawn(f).join().unwrap();
116 path = tmp.path().to_path_buf();
117 assert!(path.exists());
118 tmp.close().unwrap();
119 }
120 assert!(!path.exists());
121
122 let path;
123 {
124 let tmp = TempDir::new().unwrap();
125 path = tmp.into_path();
126 }
127 assert!(path.exists());
128 fs::remove_dir_all(&path).unwrap();
129 assert!(!path.exists());
130 }
131
dont_double_panic()132 fn dont_double_panic() {
133 let r: Result<(), _> = thread::spawn(move || {
134 let tmpdir = TempDir::new().unwrap();
135 // Remove the temporary directory so that TempDir sees
136 // an error on drop
137 fs::remove_dir(tmpdir.path()).unwrap();
138 // Panic. If TempDir panics *again* due to the rmdir
139 // error then the process will abort.
140 panic!();
141 })
142 .join();
143 assert!(r.is_err());
144 }
145
in_tmpdir<F>(f: F) where F: FnOnce(),146 fn in_tmpdir<F>(f: F)
147 where
148 F: FnOnce(),
149 {
150 let tmpdir = TempDir::new().unwrap();
151 assert!(std::env::set_current_dir(tmpdir.path()).is_ok());
152
153 f();
154 }
155
pass_as_asref_path()156 fn pass_as_asref_path() {
157 let tempdir = TempDir::new().unwrap();
158 takes_asref_path(&tempdir);
159
160 fn takes_asref_path<T: AsRef<Path>>(path: T) {
161 let path = path.as_ref();
162 assert!(path.exists());
163 }
164 }
165
test_keep()166 fn test_keep() {
167 let tmpdir = Builder::new().keep(true).tempdir().unwrap();
168 let path = tmpdir.path().to_owned();
169 drop(tmpdir);
170 assert!(path.exists());
171 fs::remove_dir(path).unwrap();
172 }
173
174 #[test]
main()175 fn main() {
176 in_tmpdir(test_tempdir);
177 in_tmpdir(test_prefix);
178 in_tmpdir(test_customnamed);
179 in_tmpdir(test_rm_tempdir);
180 in_tmpdir(test_rm_tempdir_close);
181 in_tmpdir(dont_double_panic);
182 in_tmpdir(pass_as_asref_path);
183 in_tmpdir(test_keep);
184 }
185