-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hulto eldritch file append (#4) * eldritch append works + testing * Remove file after test. Added docs. * Missed append. Removing test file. * Added test existing. * Update README.md * Removed uneeded match case. * Update eldritch_nav.yml Co-authored-by: Nicholas OBrien <ndo9903@rit.edu>
- Loading branch information
Showing
3 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
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,10 @@ | ||
# Build | ||
``` | ||
cd ./cmd/implants/eldritch/ | ||
cargo build | ||
``` | ||
# Test | ||
``` | ||
cd ./cmd/implants/eldritch/ | ||
cargo test | ||
``` |
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,78 @@ | ||
use anyhow::Result; | ||
use std::fs::OpenOptions; | ||
use std::io::prelude::*; | ||
|
||
pub fn append(_path: String, _content: String) -> Result<()> { | ||
unimplemented!("Method unimplemented") | ||
} | ||
|
||
pub fn append(path: String, content: String) -> Result<()> { | ||
let mut file = OpenOptions::new() | ||
.create(true) //Do we want to create the file if it doesn't exist? - Yes! | ||
.write(true) | ||
.append(true) | ||
.open(path)?; | ||
|
||
writeln!(file, "{}", content)?; | ||
Ok(()) | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::io::BufReader; | ||
use std::fs::File; | ||
use std::fs::remove_file; | ||
|
||
#[test] | ||
fn test_append_nonexisting() -> anyhow::Result<()> { | ||
//Remove the test file | ||
let _ = remove_file(String::from("/tmp/win")); | ||
// Run our code | ||
append(String::from("/tmp/win"), String::from("Hi2!"))?; | ||
// Read the file | ||
let file = BufReader::new(File::open("/tmp/win").unwrap()); | ||
// Reverse the file lines | ||
let mut lines: Vec<_> = file.lines().map(|line| { line.unwrap() }).collect(); | ||
lines.reverse(); | ||
|
||
//Make sure not empty | ||
assert_eq!((lines.len() > 0), true); | ||
|
||
// Make sure the last line equals == Hi2! | ||
for line in lines.iter() { | ||
println!("{}", line); | ||
assert_eq!(line, "Hi2!"); | ||
// Last line so just break. | ||
break; | ||
} | ||
remove_file(String::from("/tmp/win"))?; | ||
Ok(()) | ||
} | ||
#[test] | ||
fn test_append_existing() -> anyhow::Result<()> { | ||
//Remove the test file | ||
let _remove_res = remove_file(String::from("/tmp/win")); | ||
let mut file = File::create("/tmp/win").unwrap(); | ||
file.write_all(b"Hello, world!\n")?; | ||
|
||
// Run our code | ||
append(String::from("/tmp/win"), String::from("Hi2!"))?; | ||
// Read the file | ||
let file = BufReader::new(File::open("/tmp/win").unwrap()); | ||
// Reverse the file lines | ||
let mut lines: Vec<_> = file.lines().map(|line| { line.unwrap() }).collect(); | ||
lines.reverse(); | ||
|
||
//Make sure not empty | ||
assert_eq!((lines.len() > 0), true); | ||
|
||
// Make sure the last line equals == Hi2! | ||
for line in lines.iter() { | ||
println!("{}", line); | ||
assert_eq!(line, "Hi2!"); | ||
// Last line so just break. | ||
break; | ||
} | ||
remove_file(String::from("/tmp/win"))?; | ||
Ok(()) | ||
} | ||
} |
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