Skip to content

Commit d50ecb3

Browse files
authored
feat: remove parallelism concept, add optional duration reporting, and improve reporter (#25)
1 parent 54a6e06 commit d50ecb3

File tree

6 files changed

+213
-397
lines changed

6 files changed

+213
-397
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2018-2025 the Deno authors. MIT license.
22

33
pub mod collection;
4-
pub mod parallelism;
54
pub mod reporter;
65
mod runner;
76
mod utils;

src/parallelism.rs

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/reporter.rs

Lines changed: 88 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ pub trait Reporter<TData = ()>: Send + Sync {
4343
result: &TestResult,
4444
context: &ReporterContext,
4545
);
46-
fn report_long_running_test(&self, test_name: &str);
46+
/// Reports all the currently running tests every 1 second until this method
47+
/// returns `true` for the test or the test is no longer running.
48+
///
49+
/// This can be useful to report a test has been running for too long
50+
/// or to update a progress bar with running tests.
51+
fn report_running_test(&self, test_name: &str, duration: Duration) -> bool;
4752
fn report_failures(
4853
&self,
4954
failures: &[ReporterFailure<TData>],
@@ -109,36 +114,48 @@ impl LogReporter {
109114
sub_tests: &[SubTestResult],
110115
) -> std::io::Result<()> {
111116
for sub_test in sub_tests {
117+
let duration_display = sub_test
118+
.result
119+
.duration()
120+
.map(|d| format!(" {}", format_duration(d)))
121+
.unwrap_or_default();
112122
match &sub_test.result {
113-
TestResult::Passed => {
123+
TestResult::Passed { .. } => {
114124
writeln!(
115125
writer,
116-
"{}{} {}",
126+
"{}{} {}{}",
117127
indent,
118128
sub_test.name,
119129
colors::green_bold("ok"),
130+
duration_display,
120131
)?;
121132
}
122133
TestResult::Ignored => {
123134
writeln!(
124135
writer,
125-
"{}{} {}",
136+
"{}{} {}{}",
126137
indent,
127138
sub_test.name,
128139
colors::gray("ignored"),
140+
duration_display,
129141
)?;
130142
}
131143
TestResult::Failed { .. } => {
132144
writeln!(
133145
writer,
134-
"{}{} {}",
146+
"{}{} {}{}",
135147
indent,
136148
sub_test.name,
137-
colors::red_bold("fail")
149+
colors::red_bold("fail"),
150+
duration_display,
138151
)?;
139152
}
140-
TestResult::SubTests(sub_tests) => {
141-
writeln!(writer, "{}{}", indent, sub_test.name)?;
153+
TestResult::SubTests { sub_tests, .. } => {
154+
writeln!(
155+
writer,
156+
"{}{}{}",
157+
indent, sub_test.name, duration_display
158+
)?;
142159
if sub_tests.is_empty() {
143160
writeln!(
144161
writer,
@@ -156,9 +173,9 @@ impl LogReporter {
156173
}
157174

158175
let duration_display =
159-
colors::gray(format!("({}ms)", duration.as_millis()));
176+
format_duration(result.duration().unwrap_or(duration));
160177
match result {
161-
TestResult::Passed => {
178+
TestResult::Passed { .. } => {
162179
writeln!(writer, "{} {}", colors::green_bold("ok"), duration_display)?;
163180
}
164181
TestResult::Ignored => {
@@ -167,7 +184,7 @@ impl LogReporter {
167184
TestResult::Failed { .. } => {
168185
writeln!(writer, "{} {}", colors::red_bold("fail"), duration_display)?;
169186
}
170-
TestResult::SubTests(sub_tests) => {
187+
TestResult::SubTests { sub_tests, .. } => {
171188
writeln!(writer, "{}", duration_display)?;
172189
output_sub_tests(writer, " ", sub_tests)?;
173190
}
@@ -183,7 +200,7 @@ impl LogReporter {
183200
writeln!(
184201
writer,
185202
"test {} has been running for more than 60 seconds",
186-
test_name
203+
test_name,
187204
)?;
188205
Ok(())
189206
}
@@ -272,11 +289,16 @@ impl<TData> Reporter<TData> for LogReporter {
272289
);
273290
}
274291

275-
fn report_long_running_test(&self, test_name: &str) {
276-
let _ = LogReporter::write_report_long_running_test(
277-
&mut std::io::stderr(),
278-
test_name,
279-
);
292+
fn report_running_test(&self, test_name: &str, duration: Duration) -> bool {
293+
if duration.as_secs() > 60 {
294+
let _ = LogReporter::write_report_long_running_test(
295+
&mut std::io::stderr(),
296+
test_name,
297+
);
298+
true
299+
} else {
300+
false // keep reporting until hit
301+
}
280302
}
281303

282304
fn report_failures(
@@ -292,6 +314,10 @@ impl<TData> Reporter<TData> for LogReporter {
292314
}
293315
}
294316

317+
fn format_duration(duration: Duration) -> colors::Style<String> {
318+
colors::gray(format!("({}ms)", duration.as_millis()))
319+
}
320+
295321
#[cfg(test)]
296322
mod test {
297323
use deno_terminal::colors;
@@ -311,7 +337,7 @@ mod test {
311337
fn test_build_end_test_message_passed() {
312338
assert_eq!(
313339
build_end_test_message(
314-
&super::TestResult::Passed,
340+
&super::TestResult::Passed { duration: None },
315341
std::time::Duration::from_millis(100),
316342
),
317343
format!("{} {}\n", colors::green_bold("ok"), colors::gray("(100ms)"))
@@ -323,6 +349,7 @@ mod test {
323349
let message = build_end_test_message(
324350
&super::TestResult::Failed {
325351
output: b"error".to_vec(),
352+
duration: None,
326353
},
327354
std::time::Duration::from_millis(100),
328355
);
@@ -346,50 +373,63 @@ mod test {
346373
#[test]
347374
fn test_build_end_test_message_sub_tests() {
348375
let message = build_end_test_message(
349-
&super::TestResult::SubTests(vec![
350-
super::SubTestResult {
351-
name: "step1".to_string(),
352-
result: super::TestResult::Passed,
353-
},
354-
super::SubTestResult {
355-
name: "step2".to_string(),
356-
result: super::TestResult::Failed {
357-
output: b"error1".to_vec(),
376+
&super::TestResult::SubTests {
377+
duration: None,
378+
sub_tests: vec![
379+
super::SubTestResult {
380+
name: "step1".to_string(),
381+
result: super::TestResult::Passed {
382+
duration: Some(Duration::from_millis(20)),
383+
},
358384
},
359-
},
360-
super::SubTestResult {
361-
name: "step3".to_string(),
362-
result: super::TestResult::Failed {
363-
output: b"error2".to_vec(),
385+
super::SubTestResult {
386+
name: "step2".to_string(),
387+
result: super::TestResult::Failed {
388+
duration: None,
389+
output: b"error1".to_vec(),
390+
},
364391
},
365-
},
366-
super::SubTestResult {
367-
name: "step4".to_string(),
368-
result: super::TestResult::SubTests(vec![
369-
super::SubTestResult {
370-
name: "sub-step1".to_string(),
371-
result: super::TestResult::Passed,
392+
super::SubTestResult {
393+
name: "step3".to_string(),
394+
result: super::TestResult::Failed {
395+
duration: Some(Duration::from_millis(200)),
396+
output: b"error2".to_vec(),
372397
},
373-
super::SubTestResult {
374-
name: "sub-step2".to_string(),
375-
result: super::TestResult::Failed {
376-
output: b"error3".to_vec(),
377-
},
398+
},
399+
super::SubTestResult {
400+
name: "step4".to_string(),
401+
result: super::TestResult::SubTests {
402+
duration: None,
403+
sub_tests: vec![
404+
super::SubTestResult {
405+
name: "sub-step1".to_string(),
406+
result: super::TestResult::Passed { duration: None },
407+
},
408+
super::SubTestResult {
409+
name: "sub-step2".to_string(),
410+
result: super::TestResult::Failed {
411+
duration: None,
412+
output: b"error3".to_vec(),
413+
},
414+
},
415+
],
378416
},
379-
]),
380-
},
381-
]),
417+
},
418+
],
419+
},
382420
std::time::Duration::from_millis(10),
383421
);
384422

385423
assert_eq!(
386424
message,
387425
format!(
388-
"{}\n step1 {}\n step2 {}\n step3 {}\n step4\n sub-step1 {}\n sub-step2 {}\n",
426+
"{}\n step1 {} {}\n step2 {}\n step3 {} {}\n step4\n sub-step1 {}\n sub-step2 {}\n",
389427
colors::gray("(10ms)"),
390428
colors::green_bold("ok"),
429+
colors::gray("(20ms)"),
391430
colors::red_bold("fail"),
392431
colors::red_bold("fail"),
432+
colors::gray("(200ms)"),
393433
colors::green_bold("ok"),
394434
colors::red_bold("fail"),
395435
)

0 commit comments

Comments
 (0)