Skip to content

Commit 7467f87

Browse files
committed
tests: Make runner output pretty, tabulated, visible when test hangs
Signed-off-by: Matej Hrica <[email protected]>
1 parent b081dad commit 7467f87

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

tests/runner/src/main.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,12 @@ fn setup_namespace_and_run(test_setup: TestSetup) -> anyhow::Result<()> {
133133
}
134134
}
135135

136-
fn run_single_test(test_case: &str, base_dir: &PathBuf, keep_all: bool) -> anyhow::Result<bool> {
136+
fn run_single_test(test_case: &str, base_dir: &PathBuf, keep_all: bool, max_name_len: usize) -> anyhow::Result<bool> {
137137
let executable = env::current_exe().context("Failed to detect current executable")?;
138138
let test_dir = base_dir.join(test_case);
139139
fs::create_dir(&test_dir).context("Failed to create test directory")?;
140140

141-
eprintln!(
142-
"[{}] running in dir {} ...",
143-
test_case,
144-
test_dir.display()
145-
);
141+
eprint!("[{test_case}] {:.<width$} ", "", width = max_name_len - test_case.len() + 3);
146142

147143
// Create log file for stderr
148144
let log_path = test_dir.join("log.txt");
@@ -168,14 +164,14 @@ fn run_single_test(test_case: &str, base_dir: &PathBuf, keep_all: bool) -> anyho
168164

169165
match result {
170166
Ok(()) => {
171-
println!("[{test_case}]: OK");
167+
eprintln!(" OK");
172168
if !keep_all {
173169
let _ = fs::remove_dir_all(&test_dir);
174170
}
175171
Ok(true)
176172
}
177173
Err(_e) => {
178-
println!("[{test_case}]: FAIL (dir {:?} kept)", test_dir);
174+
eprintln!(" FAIL");
179175
Ok(false)
180176
}
181177
}
@@ -195,37 +191,41 @@ fn run_tests(test_case: &str, base_dir: Option<PathBuf>, keep_all: bool) -> anyh
195191
}
196192
};
197193

198-
eprintln!("Test base directory: {}", base_dir.display());
199-
200194
let mut num_tests = 1;
201195
let mut num_ok: usize = 0;
202196

203197
if test_case == "all" {
204-
let test_cases = test_cases();
205-
num_tests = test_cases.len();
198+
let all_tests = test_cases();
199+
num_tests = all_tests.len();
200+
let max_name_len = all_tests.iter().map(|t| t.name.len()).max().unwrap_or(0);
206201

207202
for TestCase {
208203
name,
209204
test: _,
210205
requires_namespace: _,
211-
} in test_cases
206+
} in all_tests
212207
{
213-
num_ok += run_single_test(name, &base_dir, keep_all).context(name)? as usize;
208+
num_ok += run_single_test(name, &base_dir, keep_all, max_name_len).context(name)? as usize;
214209
}
215210
} else {
216-
num_ok += run_single_test(test_case, &base_dir, keep_all).context(test_case.to_string())? as usize;
211+
let max_name_len = test_case.len();
212+
num_ok += run_single_test(test_case, &base_dir, keep_all, max_name_len).context(test_case.to_string())? as usize;
217213
}
218214

219215
let num_failures = num_tests - num_ok;
220216
if num_failures > 0 {
221-
println!("\nFAIL (PASSED {num_ok}/{num_tests})");
217+
eprintln!("\nFAIL ({num_ok}/{num_tests} passed)");
218+
eprintln!("Test artifacts: {}", base_dir.display());
222219
anyhow::bail!("")
223220
} else {
224221
// Clean up base dir if all tests passed and it's empty
225222
if !keep_all {
226223
let _ = fs::remove_dir(&base_dir);
227224
}
228-
println!("\nOK (PASSED {num_ok}/{num_tests})");
225+
eprintln!("\nOK ({num_ok}/{num_tests} passed)");
226+
if keep_all {
227+
eprintln!("Test artifacts: {}", base_dir.display());
228+
}
229229
}
230230

231231
Ok(())

0 commit comments

Comments
 (0)