-
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.
Added copy function, test, and docs. (#5) * Added copy function, test, and docs.
- Loading branch information
Showing
2 changed files
with
41 additions
and
4 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 |
---|---|---|
@@ -1,5 +1,42 @@ | ||
use anyhow::Result; | ||
use std::fs; | ||
|
||
pub fn copy(_src: String, _dst: String) -> Result<()> { | ||
unimplemented!("Method unimplemented") | ||
} | ||
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::*; | ||
use std::fs::remove_file; | ||
|
||
#[test] | ||
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 | ||
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!"); | ||
|
||
remove_file(String::from("/tmp/win_copy1"))?; | ||
remove_file(String::from("/tmp/win_copy2"))?; | ||
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