Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 
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
hulto and Cictrone authored Mar 1, 2022
1 parent 646b571 commit 5943875
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
10 changes: 10 additions & 0 deletions cmd/implants/eldritch/README.md
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
```
79 changes: 76 additions & 3 deletions cmd/implants/eldritch/src/file/append_impl.rs
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(())
}
}
4 changes: 2 additions & 2 deletions docs/_data/eldritch_nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- name: "file.append"
signature: "file.append(path: str, content: str) -> None"
link: stdlib_file_append
content: The <b>file.append</b> method is very cool, and will be even cooler when Nick documents it.
content: The <b>file.append</b> Append content str to file at path. If no file exists at path create the file with the contents content.
- name: "file.copy"
signature: "file.copy(src: str, dst: str) -> None"
link: stdlib_file_copy
Expand Down Expand Up @@ -105,4 +105,4 @@
- name: Dev Docs
link: dev
content: I'm sure Nick would love help developing the language, but until he finishes this documentation he probably won't receive any.


0 comments on commit 5943875

Please sign in to comment.