-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp_file.rs
72 lines (61 loc) · 1.52 KB
/
temp_file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::{fs, iter};
use std::io::{Read, Seek, SeekFrom, Write};
use mktemp::TempFile;
macro_rules! assert_ok {
($e:expr) => {
assert!($e.is_ok(), $e.unwrap_or_else(|e| panic!("{}", e)));
};
}
#[test]
fn tf_create_neither() {
let tf = TempFile::new("", "");
assert_ok!(tf);
}
#[test]
fn tf_create_prefix() {
let tf = TempFile::new("prefix-", "");
assert_ok!(tf);
}
#[test]
fn tf_create_suffix() {
let tf = TempFile::new("", "-suffix");
assert_ok!(tf);
}
#[test]
fn tf_create_both() {
let tf = TempFile::new("prefix-", "-suffix");
assert_ok!(tf);
}
#[test]
#[should_panic(expected = "No such file or directory")]
fn tf_drop() {
let path;
{
let tf = TempFile::new("", "").unwrap();
path = tf.path().to_string();
}
let md = fs::metadata(&path);
assert_ok!(md);
}
#[test]
fn tf_read_write() {
let mut tf = TempFile::new("", "").unwrap();
let data = b"Hello world!";
let res = tf.write(data);
assert_ok!(res);
let res = tf.seek(SeekFrom::Start(0));
assert_ok!(res);
let mut buf = [0u8; 12];
let res = tf.read(&mut buf);
assert_ok!(res);
}
#[test]
#[should_panic(expected = "Suffix length must be less than std::i32::MAX")]
#[ignore] // takes a few minutes and a lot of RAM on debug, recommend using --release
fn tf_suffix_overflow() {
let super_long_string = iter::repeat("ab")
.take((std::i32::MAX as usize + 1) / 2)
.collect::<String>();
let tf = TempFile::new("", &super_long_string);
assert_ok!(tf);
}