diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/tests/integration/test_tests.rs | 17 | ||||
-rw-r--r-- | cli/tools/test/reporters/junit.rs | 8 | ||||
-rw-r--r-- | cli/util/fs.rs | 49 |
3 files changed, 61 insertions, 13 deletions
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("<?xml [WILDCARD]"); +} + itest!(clear_timeout { args: "test test/clear_timeout.ts", exit_code: 0, diff --git a/cli/tools/test/reporters/junit.rs b/cli/tools/test/reporters/junit.rs index a62c32dab..c345f3697 100644 --- a/cli/tools/test/reporters/junit.rs +++ b/cli/tools/test/reporters/junit.rs @@ -1,5 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use std::path::PathBuf; + use super::*; pub struct JunitTestReporter { @@ -191,10 +193,8 @@ impl TestReporter for JunitTestReporter { .serialize(std::io::stdout()) .with_context(|| "Failed to write JUnit report to stdout")?; } else { - let file = - std::fs::File::create(self.path.clone()).with_context(|| { - format!("Failed to open JUnit report file {}", self.path) - })?; + let file = crate::util::fs::create_file(&PathBuf::from(&self.path)) + .context("Failed to open JUnit report file.")?; report.serialize(file).with_context(|| { format!("Failed to write JUnit report to {}", self.path) })?; 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, |