Skip to content

Commit 93bc73d

Browse files
Bromeonlilizoey
authored andcommitted
Test runner: print test name before execution
1 parent 558a6d3 commit 93bc73d

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

godot-core/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,21 @@ pub mod private {
6969
match std::panic::catch_unwind(code) {
7070
Ok(result) => Some(result),
7171
Err(err) => {
72+
// Flush, to make sure previous Rust output (e.g. test announcement, or debug prints during app) have been printed
73+
// TODO write custom panic handler and move this there, before panic backtrace printing
74+
flush_stdout();
75+
7276
log::godot_error!("Rust function panicked. Context: {}", error_context());
7377
print_panic(err);
7478
None
7579
}
7680
}
7781
}
82+
83+
pub fn flush_stdout() {
84+
use std::io::Write;
85+
std::io::stdout().flush().expect("flush stdout");
86+
}
7887
}
7988

8089
#[cfg(feature = "trace")]

itest/godot/TestSuite.gd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ func assert_that(what: bool, message: String = "") -> bool:
1414
return true
1515

1616
_assertion_failed = true
17+
18+
printerr() # previous line not yet broken
1719
if message:
18-
print("assertion failed: %s" % message)
20+
push_error("GDScript assertion failed: %s" % message)
1921
else:
20-
print("assertion failed")
22+
push_error("GDScript assertion failed.")
2123
return false
2224

2325
func assert_eq(left, right, message: String = "") -> bool:
2426
if left == right:
2527
return true
2628

2729
_assertion_failed = true
30+
31+
printerr() # previous line not yet broken
2832
if message:
29-
print("assertion failed: %s\n left: %s\n right: %s" % [message, left, right])
33+
push_error("GDScript assertion failed: %s\n left: %s\n right: %s" % [message, left, right])
3034
else:
31-
print("assertion failed: `(left == right)`\n left: %s\n right: %s" % [left, right])
35+
push_error("GDScript assertion failed: `(left == right)`\n left: %s\n right: %s" % [left, right])
3236
return false

itest/rust/src/runner.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,29 @@ impl IntegrationTests {
7171

7272
let mut last_file = None;
7373
for test in tests {
74+
print_test_pre(test.name, test.file.to_string(), &mut last_file, false);
7475
let outcome = run_rust_test(&test, &ctx);
7576

7677
self.update_stats(&outcome);
77-
print_test(test.file.to_string(), test.name, outcome, &mut last_file);
78+
print_test_post(test.name, outcome);
7879
}
7980
}
8081

8182
fn run_gdscript_tests(&mut self, tests: VariantArray) {
8283
let mut last_file = None;
8384
for test in tests.iter_shared() {
85+
let test_file = get_property(&test, "suite_name");
86+
let test_case = get_property(&test, "method_name");
87+
88+
print_test_pre(&test_case, test_file, &mut last_file, true);
8489
let result = test.call("run", &[]);
8590
let success = result.try_to::<bool>().unwrap_or_else(|_| {
8691
panic!("GDScript test case {test} returned non-bool: {result}")
8792
});
88-
89-
let test_file = get_property(&test, "suite_name");
90-
let test_case = get_property(&test, "method_name");
9193
let outcome = TestOutcome::from_bool(success);
9294

9395
self.update_stats(&outcome);
94-
print_test(test_file, &test_case, outcome, &mut last_file);
96+
print_test_post(&test_case, outcome);
9597
}
9698
}
9799

@@ -178,16 +180,7 @@ fn run_rust_test(test: &RustTestCase, ctx: &TestContext) -> TestOutcome {
178180
TestOutcome::from_bool(success.is_some())
179181
}
180182

181-
/// Prints a test name and its outcome.
182-
///
183-
/// Note that this is run after a test run, so stdout/stderr output during the test will be printed before.
184-
/// It would be possible to print the test name before and the outcome after, but that would split or duplicate the line.
185-
fn print_test(
186-
test_file: String,
187-
test_case: &str,
188-
outcome: TestOutcome,
189-
last_file: &mut Option<String>,
190-
) {
183+
fn print_test_pre(test_case: &str, test_file: String, last_file: &mut Option<String>, flush: bool) {
191184
// Check if we need to open a new category for a file
192185
let print_file = last_file
193186
.as_ref()
@@ -203,12 +196,30 @@ fn print_test(
203196
println!("\n {file_subtitle}:");
204197
}
205198

206-
println!(" -- {test_case} ... {outcome}");
199+
print!(" -- {test_case} ... ");
200+
if flush {
201+
// Flush in GDScript, because its own print may come sooner than Rust prints otherwise
202+
// (strictly speaking, this can also happen from Rust, when Godot prints something. So far, it didn't though...
203+
godot::private::flush_stdout();
204+
}
207205

208206
// State update for file-category-print
209207
*last_file = Some(test_file);
210208
}
211209

210+
/// Prints a test name and its outcome.
211+
///
212+
/// Note that this is run after a test run, so stdout/stderr output during the test will be printed before.
213+
/// It would be possible to print the test name before and the outcome after, but that would split or duplicate the line.
214+
fn print_test_post(test_case: &str, outcome: TestOutcome) {
215+
// If test failed, something was printed (e.g. assertion), so we can print the entire line again; otherwise just outcome on same line.
216+
if matches!(outcome, TestOutcome::Failed) {
217+
println!(" -- {test_case} ... {outcome}");
218+
} else {
219+
println!("{outcome}");
220+
}
221+
}
222+
212223
fn get_property(test: &Variant, property: &str) -> String {
213224
test.call("get", &[property.to_variant()]).to::<String>()
214225
}

0 commit comments

Comments
 (0)