summaryrefslogtreecommitdiff
path: root/cli/tools/test/mod.rs
diff options
context:
space:
mode:
authorHajime-san <41257923+Hajime-san@users.noreply.github.com>2024-08-20 10:27:36 +0900
committerGitHub <noreply@github.com>2024-08-20 01:27:36 +0000
commit19bcb40059f6ba730b6d05d8edf005c6b40f6ff8 (patch)
treee7c60d8957a8609199a3dad24455518cc36fac32 /cli/tools/test/mod.rs
parent4f49f703c10afcde7155baac2b494fa6670c0115 (diff)
feat(cli/tools): add a subcommand `--hide-stacktraces` for test (#24095)
Diffstat (limited to 'cli/tools/test/mod.rs')
-rw-r--r--cli/tools/test/mod.rs82
1 files changed, 50 insertions, 32 deletions
diff --git a/cli/tools/test/mod.rs b/cli/tools/test/mod.rs
index 4fbd0423e..0dc213350 100644
--- a/cli/tools/test/mod.rs
+++ b/cli/tools/test/mod.rs
@@ -288,6 +288,11 @@ impl From<&TestDescription> for TestFailureDescription {
}
}
+#[derive(Debug, Default, Clone, PartialEq)]
+pub struct TestFailureFormatOptions {
+ pub hide_stacktraces: bool,
+}
+
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -302,52 +307,55 @@ pub enum TestFailure {
HasSanitizersAndOverlaps(IndexSet<String>), // Long names of overlapped tests
}
-impl std::fmt::Display for TestFailure {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+impl TestFailure {
+ pub fn format(
+ &self,
+ options: &TestFailureFormatOptions,
+ ) -> Cow<'static, str> {
match self {
TestFailure::JsError(js_error) => {
- write!(f, "{}", format_test_error(js_error))
+ Cow::Owned(format_test_error(js_error, options))
+ }
+ TestFailure::FailedSteps(1) => Cow::Borrowed("1 test step failed."),
+ TestFailure::FailedSteps(n) => {
+ Cow::Owned(format!("{} test steps failed.", n))
}
- TestFailure::FailedSteps(1) => write!(f, "1 test step failed."),
- TestFailure::FailedSteps(n) => write!(f, "{n} test steps failed."),
TestFailure::IncompleteSteps => {
- write!(f, "Completed while steps were still running. Ensure all steps are awaited with `await t.step(...)`.")
+ Cow::Borrowed("Completed while steps were still running. Ensure all steps are awaited with `await t.step(...)`.")
}
TestFailure::Incomplete => {
- write!(
- f,
- "Didn't complete before parent. Await step with `await t.step(...)`."
- )
+ Cow::Borrowed("Didn't complete before parent. Await step with `await t.step(...)`.")
}
TestFailure::Leaked(details, trailer_notes) => {
- write!(f, "Leaks detected:")?;
+ let mut f = String::new();
+ write!(f, "Leaks detected:").unwrap();
for detail in details {
- write!(f, "\n - {}", detail)?;
+ write!(f, "\n - {}", detail).unwrap();
}
for trailer in trailer_notes {
- write!(f, "\n{}", trailer)?;
+ write!(f, "\n{}", trailer).unwrap();
}
- Ok(())
+ Cow::Owned(f)
}
TestFailure::OverlapsWithSanitizers(long_names) => {
- write!(f, "Started test step while another test step with sanitizers was running:")?;
+ let mut f = String::new();
+ write!(f, "Started test step while another test step with sanitizers was running:").unwrap();
for long_name in long_names {
- write!(f, "\n * {}", long_name)?;
+ write!(f, "\n * {}", long_name).unwrap();
}
- Ok(())
+ Cow::Owned(f)
}
TestFailure::HasSanitizersAndOverlaps(long_names) => {
- write!(f, "Started test step with sanitizers while another test step was running:")?;
+ let mut f = String::new();
+ write!(f, "Started test step with sanitizers while another test step was running:").unwrap();
for long_name in long_names {
- write!(f, "\n * {}", long_name)?;
+ write!(f, "\n * {}", long_name).unwrap();
}
- Ok(())
+ Cow::Owned(f)
}
}
}
-}
-impl TestFailure {
pub fn overview(&self) -> String {
match self {
TestFailure::JsError(js_error) => js_error.exception_message.clone(),
@@ -369,10 +377,6 @@ impl TestFailure {
}
}
- pub fn detail(&self) -> String {
- self.to_string()
- }
-
fn format_label(&self) -> String {
match self {
TestFailure::Incomplete => colors::gray("INCOMPLETE").to_string(),
@@ -512,6 +516,7 @@ struct TestSpecifiersOptions {
specifier: TestSpecifierOptions,
reporter: TestReporterConfig,
junit_path: Option<String>,
+ hide_stacktraces: bool,
}
#[derive(Debug, Default, Clone)]
@@ -545,23 +550,31 @@ impl TestSummary {
fn get_test_reporter(options: &TestSpecifiersOptions) -> Box<dyn TestReporter> {
let parallel = options.concurrent_jobs.get() > 1;
+ let failure_format_options = TestFailureFormatOptions {
+ hide_stacktraces: options.hide_stacktraces,
+ };
let reporter: Box<dyn TestReporter> = match &options.reporter {
- TestReporterConfig::Dot => {
- Box::new(DotTestReporter::new(options.cwd.clone()))
- }
+ TestReporterConfig::Dot => Box::new(DotTestReporter::new(
+ options.cwd.clone(),
+ failure_format_options,
+ )),
TestReporterConfig::Pretty => Box::new(PrettyTestReporter::new(
parallel,
options.log_level != Some(Level::Error),
options.filter,
false,
options.cwd.clone(),
+ failure_format_options,
+ )),
+ TestReporterConfig::Junit => Box::new(JunitTestReporter::new(
+ options.cwd.clone(),
+ "-".to_string(),
+ failure_format_options,
)),
- TestReporterConfig::Junit => {
- Box::new(JunitTestReporter::new(options.cwd.clone(), "-".to_string()))
- }
TestReporterConfig::Tap => Box::new(TapTestReporter::new(
options.cwd.clone(),
options.concurrent_jobs > NonZeroUsize::new(1).unwrap(),
+ failure_format_options,
)),
};
@@ -569,6 +582,9 @@ fn get_test_reporter(options: &TestSpecifiersOptions) -> Box<dyn TestReporter> {
let junit = Box::new(JunitTestReporter::new(
options.cwd.clone(),
junit_path.to_string(),
+ TestFailureFormatOptions {
+ hide_stacktraces: options.hide_stacktraces,
+ },
));
return Box::new(CompoundTestReporter::new(vec![reporter, junit]));
}
@@ -1807,6 +1823,7 @@ pub async fn run_tests(
filter: workspace_test_options.filter.is_some(),
reporter: workspace_test_options.reporter,
junit_path: workspace_test_options.junit_path,
+ hide_stacktraces: workspace_test_options.hide_stacktraces,
specifier: TestSpecifierOptions {
filter: TestFilter::from_flag(&workspace_test_options.filter),
shuffle: workspace_test_options.shuffle,
@@ -1973,6 +1990,7 @@ pub async fn run_tests_with_watch(
filter: workspace_test_options.filter.is_some(),
reporter: workspace_test_options.reporter,
junit_path: workspace_test_options.junit_path,
+ hide_stacktraces: workspace_test_options.hide_stacktraces,
specifier: TestSpecifierOptions {
filter: TestFilter::from_flag(&workspace_test_options.filter),
shuffle: workspace_test_options.shuffle,