Skip to content

Commit 0a77fe1

Browse files
Add tests for get_modification_time function
1 parent 86bd05c commit 0a77fe1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/utils.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub fn do_write_line(
5252
}
5353
}
5454

55+
/// Retrieves the modification time of the input file specified by file path
56+
/// If an error occurs, it returns the current system time
5557
pub fn get_modification_time(file_path: &str) -> String {
5658
use chrono::{DateTime, Local};
5759
use std::fs;
@@ -132,4 +134,40 @@ mod tests {
132134
assert_line_written("foo bar\tbaz", true, 8, "foo bar baz");
133135
}
134136
}
137+
138+
mod modification_time {
139+
use super::*;
140+
141+
#[test]
142+
fn set_time() {
143+
use chrono::{DateTime, Local};
144+
use std::fs::File;
145+
use std::time::SystemTime;
146+
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+
152+
// set file modification time equal to current time
153+
let current = SystemTime::now();
154+
let _ = temp.set_modified(current);
155+
156+
// format current time
157+
let current: DateTime<Local> = current.into();
158+
let current: String = current.format("%Y-%m-%d %H:%M:%S%.9f %z").to_string();
159+
160+
// verify
161+
assert_eq!(current, get_modification_time(filename));
162+
}
163+
164+
#[test]
165+
fn invalid_file() {
166+
let invalid_file = "target/utils/invalid-file";
167+
168+
let m_time = get_modification_time(invalid_file);
169+
170+
assert!(!m_time.is_empty());
171+
}
172+
}
135173
}

0 commit comments

Comments
 (0)