1 #![deny(rust_2018_idioms)]
2 
3 use std::env;
4 use std::fs::File;
5 use std::io::{Read, Seek, SeekFrom, Write};
6 use std::path::Path;
7 use tempfile::{Builder, NamedTempFile};
8 
exists<P: AsRef<Path>>(path: P) -> bool9 fn exists<P: AsRef<Path>>(path: P) -> bool {
10     std::fs::metadata(path.as_ref()).is_ok()
11 }
12 
13 #[test]
test_basic()14 fn test_basic() {
15     let mut tmpfile = NamedTempFile::new().unwrap();
16     write!(tmpfile, "abcde").unwrap();
17     tmpfile.seek(SeekFrom::Start(0)).unwrap();
18     let mut buf = String::new();
19     tmpfile.read_to_string(&mut buf).unwrap();
20     assert_eq!("abcde", buf);
21 }
22 
23 #[test]
test_deleted()24 fn test_deleted() {
25     let tmpfile = NamedTempFile::new().unwrap();
26     let path = tmpfile.path().to_path_buf();
27     assert!(exists(&path));
28     drop(tmpfile);
29     assert!(!exists(&path));
30 }
31 
32 #[test]
test_persist()33 fn test_persist() {
34     let mut tmpfile = NamedTempFile::new().unwrap();
35     let old_path = tmpfile.path().to_path_buf();
36     let persist_path = env::temp_dir().join("persisted_temporary_file");
37     write!(tmpfile, "abcde").unwrap();
38     {
39         assert!(exists(&old_path));
40         let mut f = tmpfile.persist(&persist_path).unwrap();
41         assert!(!exists(&old_path));
42 
43         // Check original file
44         f.seek(SeekFrom::Start(0)).unwrap();
45         let mut buf = String::new();
46         f.read_to_string(&mut buf).unwrap();
47         assert_eq!("abcde", buf);
48     }
49 
50     {
51         // Try opening it at the new path.
52         let mut f = File::open(&persist_path).unwrap();
53         f.seek(SeekFrom::Start(0)).unwrap();
54         let mut buf = String::new();
55         f.read_to_string(&mut buf).unwrap();
56         assert_eq!("abcde", buf);
57     }
58     std::fs::remove_file(&persist_path).unwrap();
59 }
60 
61 #[test]
test_persist_noclobber()62 fn test_persist_noclobber() {
63     let mut tmpfile = NamedTempFile::new().unwrap();
64     let old_path = tmpfile.path().to_path_buf();
65     let persist_target = NamedTempFile::new().unwrap();
66     let persist_path = persist_target.path().to_path_buf();
67     write!(tmpfile, "abcde").unwrap();
68     assert!(exists(&old_path));
69     {
70         tmpfile = tmpfile.persist_noclobber(&persist_path).unwrap_err().into();
71         assert!(exists(&old_path));
72         std::fs::remove_file(&persist_path).unwrap();
73         drop(persist_target);
74     }
75     tmpfile.persist_noclobber(&persist_path).unwrap();
76     // Try opening it at the new path.
77     let mut f = File::open(&persist_path).unwrap();
78     f.seek(SeekFrom::Start(0)).unwrap();
79     let mut buf = String::new();
80     f.read_to_string(&mut buf).unwrap();
81     assert_eq!("abcde", buf);
82     std::fs::remove_file(&persist_path).unwrap();
83 }
84 
85 #[test]
test_customnamed()86 fn test_customnamed() {
87     let tmpfile = Builder::new()
88         .prefix("tmp")
89         .suffix(&".rs".to_string())
90         .rand_bytes(12)
91         .tempfile()
92         .unwrap();
93     let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
94     assert!(name.starts_with("tmp"));
95     assert!(name.ends_with(".rs"));
96     assert_eq!(name.len(), 18);
97 }
98 
99 #[test]
test_append()100 fn test_append() {
101     let mut tmpfile = Builder::new().append(true).tempfile().unwrap();
102     tmpfile.write(b"a").unwrap();
103     tmpfile.seek(SeekFrom::Start(0)).unwrap();
104     tmpfile.write(b"b").unwrap();
105 
106     tmpfile.seek(SeekFrom::Start(0)).unwrap();
107     let mut buf = vec![0u8; 1];
108     tmpfile.read_exact(&mut buf).unwrap();
109     assert_eq!(buf, b"a");
110 }
111 
112 #[test]
test_reopen()113 fn test_reopen() {
114     let source = NamedTempFile::new().unwrap();
115     let mut first = source.reopen().unwrap();
116     let mut second = source.reopen().unwrap();
117     drop(source);
118 
119     write!(first, "abcde").expect("write failed");
120     let mut buf = String::new();
121     second.read_to_string(&mut buf).unwrap();
122     assert_eq!("abcde", buf);
123 }
124 
125 #[test]
test_into_file()126 fn test_into_file() {
127     let mut file = NamedTempFile::new().unwrap();
128     let path = file.path().to_owned();
129     write!(file, "abcde").expect("write failed");
130 
131     assert!(path.exists());
132     let mut file = file.into_file();
133     assert!(!path.exists());
134 
135     file.seek(SeekFrom::Start(0)).unwrap();
136     let mut buf = String::new();
137     file.read_to_string(&mut buf).unwrap();
138     assert_eq!("abcde", buf);
139 }
140 
141 #[test]
test_immut()142 fn test_immut() {
143     let tmpfile = NamedTempFile::new().unwrap();
144     (&tmpfile).write_all(b"abcde").unwrap();
145     (&tmpfile).seek(SeekFrom::Start(0)).unwrap();
146     let mut buf = String::new();
147     (&tmpfile).read_to_string(&mut buf).unwrap();
148     assert_eq!("abcde", buf);
149 }
150 
151 #[test]
test_temppath()152 fn test_temppath() {
153     let mut tmpfile = NamedTempFile::new().unwrap();
154     write!(tmpfile, "abcde").unwrap();
155 
156     let path = tmpfile.into_temp_path();
157     assert!(path.is_file());
158 }
159 
160 #[test]
test_temppath_persist()161 fn test_temppath_persist() {
162     let mut tmpfile = NamedTempFile::new().unwrap();
163     write!(tmpfile, "abcde").unwrap();
164 
165     let tmppath = tmpfile.into_temp_path();
166 
167     let old_path = tmppath.to_path_buf();
168     let persist_path = env::temp_dir().join("persisted_temppath_file");
169 
170     {
171         assert!(exists(&old_path));
172         tmppath.persist(&persist_path).unwrap();
173         assert!(!exists(&old_path));
174     }
175 
176     {
177         // Try opening it at the new path.
178         let mut f = File::open(&persist_path).unwrap();
179         f.seek(SeekFrom::Start(0)).unwrap();
180         let mut buf = String::new();
181         f.read_to_string(&mut buf).unwrap();
182         assert_eq!("abcde", buf);
183     }
184 
185     std::fs::remove_file(&persist_path).unwrap();
186 }
187 
188 #[test]
test_temppath_persist_noclobber()189 fn test_temppath_persist_noclobber() {
190     let mut tmpfile = NamedTempFile::new().unwrap();
191     write!(tmpfile, "abcde").unwrap();
192 
193     let mut tmppath = tmpfile.into_temp_path();
194 
195     let old_path = tmppath.to_path_buf();
196     let persist_target = NamedTempFile::new().unwrap();
197     let persist_path = persist_target.path().to_path_buf();
198 
199     assert!(exists(&old_path));
200 
201     {
202         tmppath = tmppath.persist_noclobber(&persist_path).unwrap_err().into();
203         assert!(exists(&old_path));
204         std::fs::remove_file(&persist_path).unwrap();
205         drop(persist_target);
206     }
207 
208     tmppath.persist_noclobber(&persist_path).unwrap();
209 
210     // Try opening it at the new path.
211     let mut f = File::open(&persist_path).unwrap();
212     f.seek(SeekFrom::Start(0)).unwrap();
213     let mut buf = String::new();
214     f.read_to_string(&mut buf).unwrap();
215     assert_eq!("abcde", buf);
216     std::fs::remove_file(&persist_path).unwrap();
217 }
218 
219 #[test]
test_write_after_close()220 fn test_write_after_close() {
221     let path = NamedTempFile::new().unwrap().into_temp_path();
222     File::create(path).unwrap().write_all(b"test").unwrap();
223 }
224 
225 #[test]
test_change_dir()226 fn test_change_dir() {
227     env::set_current_dir(env::temp_dir()).unwrap();
228     let tmpfile = NamedTempFile::new_in(".").unwrap();
229     let path = env::current_dir().unwrap().join(tmpfile.path());
230     env::set_current_dir("/").unwrap();
231     drop(tmpfile);
232     assert!(!exists(path))
233 }
234 
235 #[test]
test_into_parts()236 fn test_into_parts() {
237     let mut file = NamedTempFile::new().unwrap();
238     write!(file, "abcd").expect("write failed");
239 
240     let (mut file, temp_path) = file.into_parts();
241 
242     let path = temp_path.to_path_buf();
243 
244     assert!(path.exists());
245     drop(temp_path);
246     assert!(!path.exists());
247 
248     write!(file, "efgh").expect("write failed");
249 
250     file.seek(SeekFrom::Start(0)).unwrap();
251     let mut buf = String::new();
252     file.read_to_string(&mut buf).unwrap();
253     assert_eq!("abcdefgh", buf);
254 }
255 
256 #[test]
test_keep()257 fn test_keep() {
258     let mut tmpfile = NamedTempFile::new().unwrap();
259     write!(tmpfile, "abcde").unwrap();
260     let (mut f, temp_path) = tmpfile.into_parts();
261     let path;
262     {
263         assert!(exists(&temp_path));
264         path = temp_path.keep().unwrap();
265         assert!(exists(&path));
266 
267         // Check original file
268         f.seek(SeekFrom::Start(0)).unwrap();
269         let mut buf = String::new();
270         f.read_to_string(&mut buf).unwrap();
271         assert_eq!("abcde", buf);
272     }
273 
274     {
275         // Try opening it again.
276         let mut f = File::open(&path).unwrap();
277         f.seek(SeekFrom::Start(0)).unwrap();
278         let mut buf = String::new();
279         f.read_to_string(&mut buf).unwrap();
280         assert_eq!("abcde", buf);
281     }
282     std::fs::remove_file(&path).unwrap();
283 }
284