Skip to content

Commit a530037

Browse files
committed
Improve output of the gix-url baseline test
1 parent 60126d7 commit a530037

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

gix-url/tests/baseline.rs

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,87 @@ use std::any::Any;
22

33
use bstr::ByteSlice;
44

5+
/// To see all current failures run the following command or execute cargo-nextest directly with
6+
/// the below shown arguments.
7+
///
8+
/// ```bash
9+
/// just nt -p gix-url --test baseline --success-output immediate
10+
/// ``
511
#[test]
612
fn run() {
713
let panic_hook = std::panic::take_hook();
814
std::panic::set_hook(Box::new(|_| {}));
915

10-
let mut count = 0;
16+
let mut test_count = 0;
1117
let mut failures = Vec::new();
12-
let (mut failed_roundtrips, mut serialized_url_does_not_match_input) = (0, 0);
18+
let (mut failure_count_roundtrips, mut failure_count_reserialization) = (0, 0);
1319
for (url, expected) in baseline::URLS.iter() {
14-
count += 1;
20+
test_count += 1;
1521
let actual = match gix_url::parse(url) {
1622
Ok(actual) => actual,
17-
Err(err) => {
18-
failures.push(err.to_string());
23+
Err(message) => {
24+
failures.push(format!("failure(parse): {message}"));
1925
continue;
2026
}
2127
};
22-
let url_as_string = actual.to_bstring();
23-
serialized_url_does_not_match_input += usize::from(url_as_string != *url);
24-
failed_roundtrips += usize::from(gix_url::parse(url_as_string.as_ref()).ok().as_ref() != Some(&actual));
25-
let result = std::panic::catch_unwind(|| assert_urls_equal(expected, &actual)).map_err(|panic| {
28+
if let Err(message) = std::panic::catch_unwind(|| assert_urls_equal(expected, &actual)).map_err(|panic| {
2629
match downcast_panic_to_str(&panic) {
2730
Some(s) => format!("{url}: {s}\nexpected: {expected:?}\nactual: {actual:?}"),
2831
None => format!("{url}: expected: {expected:?}\nactual: {actual:?}"),
2932
}
30-
});
31-
if let Err(message) = result {
32-
failures.push(message);
33+
}) {
34+
failures.push(format!("failure(compare): {message}"));
35+
continue;
3336
}
37+
// perform additional checks only after we determined that we parsed the url correctly
38+
let url_serialized_again = actual.to_bstring();
39+
failure_count_reserialization += usize::from(url_serialized_again != *url);
40+
failure_count_roundtrips +=
41+
usize::from(gix_url::parse(url_serialized_again.as_ref()).ok().as_ref() != Some(&actual));
3442
}
3543

3644
std::panic::set_hook(panic_hook);
37-
assert_ne!(count, 0, "the baseline is never empty");
45+
46+
assert_ne!(test_count, 0, "the baseline is never empty");
3847
if failures.is_empty() {
3948
todo!("The baseline is currently meddling with hooks, thats not needed anymore since the failure rate is 0: move this into a module of the normal tests");
4049
}
50+
51+
let failure_count = failures.len();
52+
let passed_count = test_count - failure_count;
53+
let expected_failure_count = baseline::Kind::new().max_num_failures();
54+
55+
eprintln!("failed {failure_count}/{test_count} tests ({passed_count} passed)");
56+
4157
for message in &failures {
42-
eprintln!("{message}");
58+
// print messages to out instead of err to separate them from general test information
59+
println!("{message}");
60+
}
61+
62+
use core::cmp::Ordering;
63+
match Ord::cmp(&failure_count, &expected_failure_count) {
64+
Ordering::Equal => {
65+
eprintln!("the number of failing tests is as expected");
66+
}
67+
Ordering::Less => {
68+
panic!(
69+
"{} more passing tests than expected. Great work! Please change the expected number of failures to {failure_count} to make this panic go away",
70+
expected_failure_count - failure_count,
71+
)
72+
}
73+
Ordering::Greater => {
74+
panic!(
75+
"{} more failing tests than expected! This should get better, not worse. Please check your changes manually for any regressions",
76+
failure_count - expected_failure_count,
77+
)
78+
}
4379
}
44-
eprintln!(
45-
"{} failed out of {count} tests ({} passed)",
46-
failures.len(),
47-
count - failures.len()
48-
);
49-
assert!(
50-
serialized_url_does_not_match_input <= 126,
51-
"we shouldn't get worse when serializing to match our input URL"
52-
);
5380

54-
let kind = baseline::Kind::new();
55-
assert_eq!(failed_roundtrips, 0);
5681
assert!(
57-
failures.len() <= kind.max_num_failures(),
58-
"Expected no more than {} failures, but got {} - this should get better, not worse",
59-
kind.max_num_failures(),
60-
failures.len(),
61-
)
82+
failure_count_reserialization <= 42,
83+
"the number of reserialization errors should ideally get better, not worse - if this panic is not due to regressions but to new passing test cases, you can set this check to {failure_count_reserialization}"
84+
);
85+
assert_eq!(failure_count_roundtrips, 0, "there should be no roundtrip errors");
6286
}
6387

6488
fn downcast_panic_to_str<'a>(panic: &'a Box<dyn Any + Send + 'static>) -> Option<&'a str> {

gix-url/tests/fixtures/make_baseline.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ tests_unix=()
88
# urls only intended for testing on Windows
99
tests_windows=()
1010

11-
# The contents and structure of this loop are a adaption
11+
# The contents and structure of this loop are an adaption
1212
# from git's own test suite (t/t5500-fetch-pack.sh).
1313
# Please do not change this loop and instead add additional
1414
# test cases at the bottom of this file.
1515
for path in "repo" "re:po" "re/po"; do
16+
# normal urls
1617
for protocol in "ssh+git" "git+ssh" "git" "ssh"; do
1718
for host in "host" "user@host" "user@[::1]" "user@::1"; do
1819
for port_separator in "" ":"; do
@@ -25,6 +26,7 @@ for path in "repo" "re:po" "re/po"; do
2526
tests+=("$protocol://$host:22/$path")
2627
done
2728
done
29+
# file protocol urls
2830
for protocol in "file"; do
2931
tests_unix+=("$protocol://$host/$path")
3032

@@ -34,14 +36,14 @@ for path in "repo" "re:po" "re/po"; do
3436
tests_unix+=("$protocol://$host/~$path")
3537
tests_windows+=("$protocol://$host/~$path")
3638
done
39+
# local paths
3740
for host in "nohost" "nohost:12" "[::1]" "[::1]:23" "[" "[:aa"; do
3841
tests+=("./$host:$path")
3942
tests+=("./$protocol:$host/~$path")
4043
done
41-
protocol="ssh"
44+
# SCP like urls
4245
for host in "host" "[::1]"; do
4346
tests+=("$host:$path")
44-
4547
tests+=("$host:/~$path")
4648
done
4749
done

0 commit comments

Comments
 (0)