Skip to content

Commit

Permalink
implement file.mkdir (#252)
Browse files Browse the repository at this point in the history
* 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
2 people authored and hulto committed Sep 30, 2023
1 parent ee5696a commit afa5094
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Here is an example of the Dict layout:
### file.mkdir
`file.mkdir(path: str) -> None`

The <b>file.mkdir</b> method is very cool, and will be even cooler when Nick documents it.
The <b>file.mkdir</b> method is make a new dirctory at `path`. If the parent directory does not exist or the directory cannot be otherwise be created, it will creat an error.

### file.moveto
`file.moveto(src: str, dst: str) -> None`
Expand Down
52 changes: 50 additions & 2 deletions implants/lib/eldritch/src/file/mkdir_impl.rs
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(())
}
}

0 comments on commit afa5094

Please sign in to comment.