Debian Codegraph @

debian/rust-vendor/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
16 use tempfile::{Builder, TempDir};
17
18 /// For the wasi platforms, `std::env::temp_dir` will panic. For those targets, configure the /tmp
19 /// directory instead as the base directory for temp files.
20 fn configure_wasi_temp_dir() {
21 if cfg!(target_os = "wasi") {
22 let _ = tempfile::env::override_temp_dir(std::path::Path::new("/tmp"));
23 }
24 }
25
26 #[test]
27 fn test_tempdir() {
28 configure_wasi_temp_dir();
29
30 let path = {
31 let p = Builder::new().prefix("foobar").tempdir().unwrap();
32 let p = p.path();
33 assert!(p.to_str().unwrap().contains("foobar"));
34 p.to_path_buf()
35 };
36 assert!(!path.exists());
37 }
38
39 #[test]
40 fn test_prefix() {
41 configure_wasi_temp_dir();
42
43 let tmpfile = TempDir::with_prefix("prefix").unwrap();
44 let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
45 assert!(name.starts_with("prefix"));
46 }
47
48 #[test]
49 fn test_suffix() {
50 configure_wasi_temp_dir();
51
52 let tmpfile = TempDir::with_suffix("suffix").unwrap();
53 let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
54 assert!(name.ends_with("suffix"));
55 }
56
57 #[test]
58 fn test_customnamed() {
59 configure_wasi_temp_dir();
60
61 let tmpfile = Builder::new()
62 .prefix("prefix")
63 .suffix("suffix")
64 .rand_bytes(12)
65 .tempdir()
66 .unwrap();
67 let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
68 assert!(name.starts_with("prefix"));
69 assert!(name.ends_with("suffix"));
70 assert_eq!(name.len(), 24);
71 }
72
73 #[test]
74 #[cfg_attr(target_os = "wasi", ignore = "thread::spawn is not supported")]
75 fn test_rm_tempdir_threading() {
76 configure_wasi_temp_dir();
77
78 use std::sync::mpsc::channel;
79 use std::thread;
80
81 let (tx, rx) = channel();
82 let f = move || {
83 let tmp = TempDir::new().unwrap();
84 tx.send(tmp.path().to_path_buf()).unwrap();
85 panic!("panic to unwind past `tmp`");
86 };
87 let _ = thread::spawn(f).join();
88 let path = rx.recv().unwrap();
89 assert!(!path.exists());
90
91 let tmp = TempDir::new().unwrap();
92 let path = tmp.path().to_path_buf();
93 let f = move || {
94 let _tmp = tmp;
95 panic!("panic to unwind past `tmp`");
96 };
97 let _ = thread::spawn(f).join();
98 assert!(!path.exists());
99
100 let path;
101 {
102 let f = move || TempDir::new().unwrap();
103
104 let tmp = thread::spawn(f).join().unwrap();
105 path = tmp.path().to_path_buf();
106 assert!(path.exists());
107 }
108 assert!(!path.exists());
109 }
110
111 #[test]
112 fn test_tempdir_keep() {
113 configure_wasi_temp_dir();
114
115 let path = {
116 let tmp = TempDir::new().unwrap();
117 tmp.keep()
118 };
119 assert!(path.exists());
120 fs::remove_dir_all(&path).unwrap();
121 assert!(!path.exists());
122 }
123
124 #[test]
125 fn test_tmpdir_close() {
126 configure_wasi_temp_dir();
127
128 let tmp = TempDir::new().unwrap();
129 let path = tmp.path().to_path_buf();
130 assert!(path.exists());
131 tmp.close().unwrap();
132 assert!(!path.exists());
133 }
134
135 #[test]
136 #[cfg_attr(target_os = "wasi", ignore = "unwinding is not supported")]
137 fn dont_double_panic() {
138 configure_wasi_temp_dir();
139
140 use std::thread;
141 let r: Result<(), _> = thread::spawn(move || {
142 let tmpdir = TempDir::new().unwrap();
143 // Remove the temporary directory so that TempDir sees
144 // an error on drop
145 fs::remove_dir(tmpdir.path()).unwrap();
146 // Panic. If TempDir panics *again* due to the rmdir
147 // error then the process will abort.
148 panic!();
149 })
150 .join();
151 assert!(r.is_err());
152 }
153
154 #[test]
155 fn pass_as_asref_path() {
156 configure_wasi_temp_dir();
157
158 let tempdir = TempDir::new().unwrap();
159 takes_asref_path(&tempdir);
160
161 fn takes_asref_path<T: AsRef<Path>>(path: T) {
162 let path = path.as_ref();
163 assert!(path.exists());
164 }
165 }
166
167 #[test]
168 fn test_disable_cleanup() {
169 configure_wasi_temp_dir();
170
171 // Case 0: never mark as "disable cleanup"
172 // Case 1: enable "disable cleanup" in the builder, don't touch it after.
173 // Case 2: enable "disable cleanup" in the builder, turn it off after.
174 // Case 3: don't enable disable cleanup in the builder, turn it on after.
175 for case in 0..4 {
176 let in_builder = case & 1 > 0;
177 let toggle = case & 2 > 0;
178 let mut tmpdir = Builder::new()
179 .disable_cleanup(in_builder)
180 .tempdir()
181 .unwrap();
182 if toggle {
183 tmpdir.disable_cleanup(!in_builder);
184 }
185
186 let path = tmpdir.path().to_owned();
187 drop(tmpdir);
188
189 if in_builder ^ toggle {
190 assert!(path.exists());
191 fs::remove_dir(path).unwrap();
192 } else {
193 assert!(!path.exists(), "tempdir wasn't deleted");
194 }
195 }
196 }