diff --git a/src/dir.rs b/src/dir.rs index a5d1112..f2e4b2c 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -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; @@ -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; @@ -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. @@ -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; diff --git a/tests/dir.rs b/tests/dir.rs index 2647066..b411956 100644 --- a/tests/dir.rs +++ b/tests/dir.rs @@ -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]