From 02822d309f6a3ca1a092670905605dd72f15b384 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 1 Nov 2023 15:59:51 -0400 Subject: fix(test): --junit-path should handle when the dir doesn't exist (#21044) Closes https://github.com/denoland/deno/issues/21022 --- cli/tests/integration/test_tests.rs | 17 +++++++++++++ cli/tools/test/reporters/junit.rs | 8 +++--- cli/util/fs.rs | 49 ++++++++++++++++++++++++++++++------- 3 files changed, 61 insertions(+), 13 deletions(-) (limited to 'cli') diff --git a/cli/tests/integration/test_tests.rs b/cli/tests/integration/test_tests.rs index bcf3e4ef7..4560f95b6 100644 --- a/cli/tests/integration/test_tests.rs +++ b/cli/tests/integration/test_tests.rs @@ -283,6 +283,23 @@ itest!(junit { output: "test/pass.junit.out", }); +#[test] +fn junit_path() { + let context = TestContextBuilder::new().use_temp_cwd().build(); + let temp_dir = context.temp_dir(); + temp_dir.write("test.js", "Deno.test('does test', () => {});"); + let output = context + .new_command() + .args("test --junit-path=sub_dir/output.xml test.js") + .run(); + output.skip_output_check(); + output.assert_exit_code(0); + temp_dir + .path() + .join("sub_dir/output.xml") + .assert_matches_text(">( 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>( 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>( } } } - 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>( 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 { + 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>( filename: &Path, data: T, -- cgit v1.2.3