Skip to content

Commit fb86919

Browse files
authored
tempfile: Add basic test for new_anonymous (#287)
On general principle, but also specifically because I think this may not work on Windows; came out of some discussion in containers/image#1769
1 parent 663eb48 commit fb86919

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

cap-tempfile/src/tempfile.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ mod test {
235235
panic!("Could not determine process umask")
236236
}
237237

238+
/// Older Windows versions don't support removing open files
239+
fn os_supports_unlinked_tmp(d: &Dir) -> bool {
240+
if cfg!(not(windows)) {
241+
return true;
242+
}
243+
let name = "testfile";
244+
let _f = d.create(name).unwrap();
245+
d.remove_file(name).and_then(|_| d.create(name)).is_ok()
246+
}
247+
238248
#[test]
239249
fn test_tempfile() -> io::Result<()> {
240250
use crate::ambient_authority;
@@ -269,6 +279,17 @@ mod test {
269279

270280
assert_eq!(td.read("testfile")?, b"hello world");
271281

282+
if os_supports_unlinked_tmp(&td) {
283+
let mut tf = TempFile::new_anonymous(&td).unwrap();
284+
tf.write_all(b"hello world, I'm anonymous").unwrap();
285+
tf.seek(std::io::SeekFrom::Start(0)).unwrap();
286+
let mut buf = String::new();
287+
tf.read_to_string(&mut buf).unwrap();
288+
assert_eq!(&buf, "hello world, I'm anonymous");
289+
} else if cfg!(windows) {
290+
eprintln!("notice: Detected older Windows");
291+
}
292+
272293
td.close()
273294
}
274295
}

0 commit comments

Comments
 (0)