diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-11-01 15:59:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-01 19:59:51 +0000 |
commit | 02822d309f6a3ca1a092670905605dd72f15b384 (patch) | |
tree | bee2e50ac74f5c0e99cd73bba63f076048cd833e /cli/util/fs.rs | |
parent | d42f1543121e7245789a96a485d1ef7645cb5fba (diff) |
fix(test): --junit-path should handle when the dir doesn't exist (#21044)
Closes https://github.com/denoland/deno/issues/21022
Diffstat (limited to 'cli/util/fs.rs')
-rw-r--r-- | cli/util/fs.rs | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/cli/util/fs.rs b/cli/util/fs.rs index 9aeeb62cc..c1fd00f5e 100644 --- a/cli/util/fs.rs +++ b/cli/util/fs.rs @@ -49,13 +49,6 @@ pub fn atomic_write_file<T: AsRef<[u8]>>( Ok(()) } - fn add_file_context(file_path: &Path, err: Error) -> Error { - Error::new( - err.kind(), - format!("{:#} (for '{}')", err, file_path.display()), - ) - } - fn inner(file_path: &Path, data: &[u8], mode: u32) -> std::io::Result<()> { let temp_file_path = { let rand: String = (0..4).fold(String::new(), |mut output, _| { @@ -79,7 +72,7 @@ pub fn atomic_write_file<T: AsRef<[u8]>>( data, mode, ) - .map_err(|err| add_file_context(file_path, err)); + .map_err(|err| add_file_context_to_err(file_path, err)); } Err(create_err) => { if !parent_dir_path.exists() { @@ -95,7 +88,7 @@ pub fn atomic_write_file<T: AsRef<[u8]>>( } } } - return Err(add_file_context(file_path, write_err)); + return Err(add_file_context_to_err(file_path, write_err)); } Ok(()) } @@ -103,6 +96,44 @@ pub fn atomic_write_file<T: AsRef<[u8]>>( inner(file_path, data.as_ref(), mode) } +/// Creates a std::fs::File handling if the parent does not exist. +pub fn create_file(file_path: &Path) -> std::io::Result<std::fs::File> { + match std::fs::File::create(file_path) { + Ok(file) => Ok(file), + Err(err) => { + if err.kind() == ErrorKind::NotFound { + let parent_dir_path = file_path.parent().unwrap(); + match std::fs::create_dir_all(parent_dir_path) { + Ok(()) => { + return std::fs::File::create(file_path) + .map_err(|err| add_file_context_to_err(file_path, err)); + } + Err(create_err) => { + if !parent_dir_path.exists() { + return Err(Error::new( + create_err.kind(), + format!( + "{:#} (for '{}')\nCheck the permission of the directory.", + create_err, + parent_dir_path.display() + ), + )); + } + } + } + } + Err(add_file_context_to_err(file_path, err)) + } + } +} + +fn add_file_context_to_err(file_path: &Path, err: Error) -> Error { + Error::new( + err.kind(), + format!("{:#} (for '{}')", err, file_path.display()), + ) +} + pub fn write_file<T: AsRef<[u8]>>( filename: &Path, data: T, |