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