-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial touch function with two tests for file creation and timestamp…
… update
- Loading branch information
Showing
6 changed files
with
197 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ sysinfo = "0.17" | |
dirs = "3.0" | ||
chrono = "0.4" | ||
termion = "*" | ||
fs-set-times = "0.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod shellname; | ||
pub mod tokenizer; | ||
pub mod touch; | ||
|
||
use crate::shellname::*; | ||
use crate::tokenizer::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use fs_set_times::{set_mtime, SystemTimeSpec}; | ||
use std::fs; | ||
use std::io::Result; | ||
use std::path::Path; | ||
use std::thread; | ||
use std::time::SystemTime; | ||
|
||
fn touch(option: char, file: &str) -> Result<()> { | ||
if Path::new(file).exists() { | ||
let now = SystemTime::now(); | ||
set_mtime(file, SystemTimeSpec::from(now))?; | ||
} else { | ||
fs::File::create(file)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use core::time; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn create_file() { | ||
let _ = touch('a', "test001.txt"); | ||
assert!(Path::new("test.txt").exists()); | ||
if let Err(_) = fs::remove_file("test.txt") { | ||
eprintln!("Can't remove test.txt"); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_updated_timestamp() { | ||
let _ = touch('a', "test002.txt"); | ||
let mut metadata = fs::metadata("test002.txt").unwrap(); | ||
let init_time = metadata.modified().unwrap(); | ||
|
||
thread::sleep(time::Duration::from_secs(3)); | ||
|
||
let _ = touch('a', "test002.txt"); | ||
metadata = fs::metadata("test002.txt").unwrap(); | ||
let modified_time = metadata.modified().unwrap(); | ||
|
||
let diff = modified_time.duration_since(init_time).unwrap().as_secs(); | ||
assert_eq!(diff, time::Duration::from_secs(3).as_secs()); | ||
|
||
if let Err(_) = fs::remove_file("test002.txt") { | ||
eprintln!("Can't remove test.txt"); | ||
} | ||
} | ||
} |