Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::*;
use std::collections::{HashMap, HashSet};
use std::fs::{create_dir, create_dir_all, read_dir, remove_dir_all, Metadata};
tuse std::io::ErrorKind as IoErrorKind;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

Expand Down Expand Up @@ -469,16 +470,16 @@ where
///
/// * `erase` - If set true and folder exist, then folder will be erased.
///
/// #Errors
/// # Errors
///
/// This function will return an error in the following situations,
/// but is not limited to just these cases:
///
/// * User lacks permissions to create directory at `path`.
///
/// * `path` already exists if `erase` set false.
/// If the directory already exists, no error will be returned.
///
/// #Examples
/// # Examples
///
/// ```rust,ignore
/// extern crate fs_extra;
Expand All @@ -493,7 +494,13 @@ where
if erase && path.as_ref().exists() {
remove(&path)?;
}
Ok(create_dir(&path)?)

match create_dir(&path) {
// if it fails because it exists, we ignore it
Err(err) if err.kind() == IoErrorKind::AlreadyExists => Ok(()),
Err(err) => Err(err.into()),
Ok(()) => Ok(()),
}
}

/// Recursively create a directory and all of its parent components if they are missing.
Expand All @@ -504,16 +511,16 @@ where
///
/// * `erase` - If set true and folder exist, then folder will be erased.
///
///#Errors
/// # Errors
///
/// This function will return an error in the following situations,
/// but is not limited to just these cases:
///
/// * User lacks permissions to create directory at `path`.
///
/// * `path` already exists if `erase` set false.
/// If the directory already exists, no error will be returned.
///
/// #Examples
/// # Examples
///
/// ```rust,ignore
/// extern crate fs_extra;
Expand Down
14 changes: 2 additions & 12 deletions tests/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,8 @@ fn it_create_exist_folder() {
fs_extra::file::write_all(&file_path, &content).unwrap();
assert!(file_path.exists());

match create(&test_dir, false) {
Ok(_) => panic!("Should be error!"),
Err(err) => match err.kind {
ErrorKind::AlreadyExists => {
assert!(test_dir.exists());
assert!(file_path.exists());
let new_content = fs_extra::file::read_to_string(file_path).unwrap();
assert_eq!(new_content, content);
}
_ => panic!("Wrong error"),
},
}
let result = create(&test_dir, false);
assert!(result.is_ok());
}

#[test]
Expand Down