Skip to content

Commit

Permalink
initial touch function with two tests for file creation and timestamp…
Browse files Browse the repository at this point in the history
… update
  • Loading branch information
bexxmodd committed Jul 22, 2021
1 parent fc2e900 commit ef355f7
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 6 deletions.
137 changes: 133 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ sysinfo = "0.17"
dirs = "3.0"
chrono = "0.4"
termion = "*"
fs-set-times = "0.6"
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ TODO:
- [x] Allow chain of commands when `&&` is supplied
- [ ] Handle `&` symbol to send command as a background process
- [ ] Handle arrow, home, end keyboard inputs and cursor movement
- [ ] Implement `history` functionality
- [ ] Implement dot/source function
- [ ] Implement `source` function
- [ ] Implement `history` function
- [ ] Implement `dot/source` function
1 change: 1 addition & 0 deletions src/main.rs
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::*;
Expand Down
7 changes: 7 additions & 0 deletions src/shellname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ mod test {
let sh = ShellName::new("home");
assert_eq!("home".to_string(), sh.current_dir);
}

#[test]
fn test_dir_change() {
let mut sh = ShellName::new("home");
sh.set_current_dir("~");
assert_eq!("~".to_string(), sh.current_dir);
}
}
52 changes: 52 additions & 0 deletions src/touch.rs
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");
}
}
}

0 comments on commit ef355f7

Please sign in to comment.