Debian Codegraph @

third_party/rust/tempfile/tests/tempdir.rs

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