From e7f682c0ed4c1d56206b529e74e2de31d27b5939 Mon Sep 17 00:00:00 2001 From: Hulto Date: Tue, 1 Mar 2022 02:20:51 +0000 Subject: [PATCH 1/2] Added copy function, test, and docs. --- cmd/implants/eldritch/src/file/copy_impl.rs | 36 +++++++++++++++++++-- docs/_data/eldritch_nav.yml | 2 +- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/cmd/implants/eldritch/src/file/copy_impl.rs b/cmd/implants/eldritch/src/file/copy_impl.rs index cf448dbe3..a0a11083c 100644 --- a/cmd/implants/eldritch/src/file/copy_impl.rs +++ b/cmd/implants/eldritch/src/file/copy_impl.rs @@ -1,5 +1,35 @@ use anyhow::Result; +use std::fs; -pub fn copy(_src: String, _dst: String) -> Result<()> { - unimplemented!("Method unimplemented") -} \ No newline at end of file +pub fn copy(src: String, dst: String) -> Result<()> { + fs::copy(src, dst)?; + Ok(()) +} + + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File; + use std::io::prelude::*; + + #[test] + fn test_copy() -> std::io::Result<()>{ + // Create file + let mut file = File::create("/tmp/win_copy1")?; + // Write to file + file.write_all(b"Hello, world!")?; + + // Run our code + let _res = copy(String::from("/tmp/win_copy1"), String::from("/tmp/win_copy2")); + + // Open copied file + let mut winfile = File::open("/tmp/win_copy2")?; + // Read + let mut contents = String::new(); + winfile.read_to_string(&mut contents)?; + // Compare + assert_eq!(contents, "Hello, world!"); + Ok(()) + } +} diff --git a/docs/_data/eldritch_nav.yml b/docs/_data/eldritch_nav.yml index ef90175b9..1281ae31b 100644 --- a/docs/_data/eldritch_nav.yml +++ b/docs/_data/eldritch_nav.yml @@ -16,7 +16,7 @@ - name: "file.copy" signature: "file.copy(src: str, dst: str) -> None" link: stdlib_file_copy - content: The file.copy method is very cool, and will be even cooler when Nick documents it. + content: The file.copy copies a file from src path to dst path. If dst path doesn't exist it will be created. - name: "file.download" signature: "file.download(uri: str, dst: str) -> None" link: stdlib_file_download From 6f75fd5fd6a8ca1aa9cf1458721be3f54bbf360a Mon Sep 17 00:00:00 2001 From: Hulto Date: Tue, 1 Mar 2022 02:58:47 +0000 Subject: [PATCH 2/2] Fixing tests. --- cmd/implants/eldritch/src/file/copy_impl.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/implants/eldritch/src/file/copy_impl.rs b/cmd/implants/eldritch/src/file/copy_impl.rs index a0a11083c..7f9de535d 100644 --- a/cmd/implants/eldritch/src/file/copy_impl.rs +++ b/cmd/implants/eldritch/src/file/copy_impl.rs @@ -12,16 +12,20 @@ mod tests { use super::*; use std::fs::File; use std::io::prelude::*; + use std::fs::remove_file; #[test] - fn test_copy() -> std::io::Result<()>{ + fn test_copy() -> anyhow::Result<()>{ + let _ = remove_file(String::from("/tmp/win_copy1")); + let _ = remove_file(String::from("/tmp/win_copy2")); + // Create file let mut file = File::create("/tmp/win_copy1")?; // Write to file file.write_all(b"Hello, world!")?; // Run our code - let _res = copy(String::from("/tmp/win_copy1"), String::from("/tmp/win_copy2")); + copy(String::from("/tmp/win_copy1"), String::from("/tmp/win_copy2"))?; // Open copied file let mut winfile = File::open("/tmp/win_copy2")?; @@ -30,6 +34,9 @@ mod tests { winfile.read_to_string(&mut contents)?; // Compare assert_eq!(contents, "Hello, world!"); + + remove_file(String::from("/tmp/win_copy1"))?; + remove_file(String::from("/tmp/win_copy2"))?; Ok(()) } }