Skip to content

Commit 54c02bd

Browse files
Use NamedTempFile instead of manually creating files
1 parent ba7cb0a commit 54c02bd

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/utils.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,20 @@ mod tests {
141141
#[test]
142142
fn set_time() {
143143
use chrono::{DateTime, Local};
144-
use std::fs::File;
145144
use std::time::SystemTime;
145+
use tempfile::NamedTempFile;
146146

147-
let target = "target/utils";
148-
let _ = std::fs::create_dir(target);
149-
let filename = &format!("{target}/foo");
150-
let temp = File::create(filename).unwrap();
151-
147+
let temp = NamedTempFile::new().unwrap();
152148
// set file modification time equal to current time
153149
let current = SystemTime::now();
154-
let _ = temp.set_modified(current);
150+
let _ = temp.as_file().set_modified(current);
155151

156152
// format current time
157153
let current: DateTime<Local> = current.into();
158154
let current: String = current.format("%Y-%m-%d %H:%M:%S%.9f %z").to_string();
159155

160156
// verify
161-
assert_eq!(current, get_modification_time(filename));
157+
assert_eq!(current, get_modification_time(&temp.path().to_string_lossy()));
162158
}
163159

164160
#[test]

0 commit comments

Comments
 (0)