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