Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): --junit-path should handle when the dir doesn't exist #21044

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cli/tests/integration/test_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions cli/tools/test/reporters/junit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use std::path::PathBuf;

use super::*;

pub struct JunitTestReporter {
Expand Down Expand Up @@ -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)
})?;
Expand Down
49 changes: 40 additions & 9 deletions cli/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, _| {
Expand All @@ -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() {
Expand All @@ -95,14 +88,52 @@ 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(())
}

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,
Expand Down