Repository navigation
Display modification times of input files in context and unified diff #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
42eb15b
9ff8f89
043c5f9
80c9944
88a7568
5b814f8
a3a372f
61fb065
72da7fc
f6eb083
00e18a6
86bd05c
0a77fe1
900e1c3
33783d0
ba7cb0a
54c02bd
aedd068
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,8 @@ pub fn do_write_line( | |
| } | ||
| } | ||
|
|
||
| /// Retrieves the modification time of the input file specified by file path | ||
| /// If an error occurs, it returns the current system time | ||
| pub fn get_modification_time(file_path: &str) -> String { | ||
|
oSoMoN marked this conversation as resolved.
|
||
| use chrono::{DateTime, Local}; | ||
| use std::fs; | ||
|
|
@@ -132,4 +134,40 @@ mod tests { | |
| assert_line_written("foo bar\tbaz", true, 8, "foo bar baz"); | ||
| } | ||
| } | ||
|
|
||
| mod modification_time { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn set_time() { | ||
| use chrono::{DateTime, Local}; | ||
| use std::fs::File; | ||
| use std::time::SystemTime; | ||
|
|
||
| let> | ||
| let _ = std::fs::create_dir(target); | ||
| let filename = &format!("{target}/foo"); | ||
| let temp = File::create(filename).unwrap(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use a tempfile.NamedTempFile instead?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there are APIs to set modification time for a
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
use tempfile::NamedTempFile;
let temp = NamedTempFile::new().unwrap();
let current = SystemTime::now();
let _ = temp.as_file().set_modified(current);
…
assert_eq!(current, get_modification_time(&temp.path().to_string_lossy())); |
||
|
|
||
| // set file modification time equal to current time | ||
| let current = SystemTime::now(); | ||
| let _ = temp.set_modified(current); | ||
|
|
||
| // format current time | ||
| let current: DateTime<Local> = current.into(); | ||
| let current: String = current.format("%Y-%m-%d %H:%M:%S%.9f %z").to_string(); | ||
|
|
||
| // verify | ||
| assert_eq!(current, get_modification_time(filename)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_file() { | ||
| let invalid_file = "target/utils/invalid-file"; | ||
|
|
||
| let m_time = get_modification_time(invalid_file); | ||
|
|
||
| assert!(!m_time.is_empty()); | ||
|
TanmayPatil105 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.