diff --git a/cmd/implants/eldritch/README.md b/cmd/implants/eldritch/README.md new file mode 100644 index 000000000..650810232 --- /dev/null +++ b/cmd/implants/eldritch/README.md @@ -0,0 +1,10 @@ +# Build +``` +cd ./cmd/implants/eldritch/ +cargo build +``` +# Test +``` +cd ./cmd/implants/eldritch/ +cargo test +``` diff --git a/cmd/implants/eldritch/src/file/append_impl.rs b/cmd/implants/eldritch/src/file/append_impl.rs index 06ded56fb..bd6501bc6 100644 --- a/cmd/implants/eldritch/src/file/append_impl.rs +++ b/cmd/implants/eldritch/src/file/append_impl.rs @@ -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") -} \ No newline at end of file + +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(()) + } +} diff --git a/docs/_data/eldritch_nav.yml b/docs/_data/eldritch_nav.yml index ef90175b9..fc25f7d81 100644 --- a/docs/_data/eldritch_nav.yml +++ b/docs/_data/eldritch_nav.yml @@ -12,7 +12,7 @@ - name: "file.append" signature: "file.append(path: str, content: str) -> None" link: stdlib_file_append - content: The file.append method is very cool, and will be even cooler when Nick documents it. + content: The file.append 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 @@ -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. - \ No newline at end of file +