mirror of
https://github.com/denoland/deno.git
synced 2024-10-29 08:58:01 -04:00
fix(test): --junit-path should handle when the dir doesn't exist (#21044)
Closes https://github.com/denoland/deno/issues/21022
This commit is contained in:
parent
d42f154312
commit
02822d309f
3 changed files with 61 additions and 13 deletions
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
})?;
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue