-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement file.mkdir * remove extra remove_dir * documentation of file.mkdir * Adding error cases to documentation --------- Co-authored-by: 1nv8rZim <[email protected]>
- Loading branch information
Showing
2 changed files
with
51 additions
and
3 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
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,53 @@ | ||
use anyhow::Result; | ||
use std::fs; | ||
|
||
pub fn mkdir(_path: String) -> Result<()> { | ||
unimplemented!("Method unimplemented") | ||
pub fn mkdir(path: String) -> Result<()> { | ||
match fs::create_dir(&path) { | ||
Ok(_) => return Ok(()), | ||
Err(_) => return Err(anyhow::anyhow!(format!("Failed to create directory at path: {}", path))), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs; | ||
use anyhow::Ok; | ||
use tempfile::tempdir; | ||
use std::path::Path; | ||
|
||
#[test] | ||
fn test_successful_mkdir() -> Result<()> { | ||
let tmp_dir_parent = tempdir()?; | ||
let path_dir = String::from(tmp_dir_parent.path().to_str().unwrap()).clone(); | ||
tmp_dir_parent.close()?; | ||
|
||
let result = mkdir(path_dir.clone()); | ||
assert!(result.is_ok(), "Expected mkdir to succeed, but it failed: {:?}", result); | ||
|
||
let binding = path_dir.clone(); | ||
let res = Path::new(&binding); | ||
assert!(res.is_dir(), "Directory not created successfully."); | ||
|
||
fs::remove_dir_all(path_dir.clone()).ok(); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_error_mkdir() -> Result<()>{ | ||
let tmp_dir_parent = tempdir()?; | ||
let path_dir = String::from(tmp_dir_parent.path().to_str().unwrap()).clone(); | ||
tmp_dir_parent.close()?; | ||
|
||
let result = mkdir(format!("{}/{}", path_dir, "dir".to_string())); | ||
|
||
assert!( | ||
result.is_err(), | ||
"Expected mkdir to fail, but it succeeded: {:?}", | ||
result | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |