Skip to content

Commit 3d60c02

Browse files
committed
Merge branch 'gix-url-fixture-improvements'
2 parents 60126d7 + 2488ad9 commit 3d60c02

File tree

2 files changed

+64
-33
lines changed

2 files changed

+64
-33
lines changed

gix-url/tests/baseline.rs

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,93 @@
11
use std::any::Any;
22

33
use bstr::ByteSlice;
4+
use gix_testtools::once_cell::sync::Lazy;
45

6+
/// To see all current failures run the following command or execute cargo-nextest directly with
7+
/// the below shown arguments.
8+
///
9+
/// ```bash
10+
/// just nt -p gix-url --test baseline --success-output immediate
11+
/// ``
512
#[test]
613
fn run() {
14+
// ensure the baseline is evaluated before we disable the panic hook, otherwise we swallow
15+
// errors inside the baseline generation
16+
Lazy::force(&baseline::URLS);
17+
718
let panic_hook = std::panic::take_hook();
819
std::panic::set_hook(Box::new(|_| {}));
920

10-
let mut count = 0;
21+
let mut test_count = 0;
1122
let mut failures = Vec::new();
12-
let (mut failed_roundtrips, mut serialized_url_does_not_match_input) = (0, 0);
23+
let (mut failure_count_roundtrips, mut failure_count_reserialization) = (0, 0);
1324
for (url, expected) in baseline::URLS.iter() {
14-
count += 1;
25+
test_count += 1;
1526
let actual = match gix_url::parse(url) {
1627
Ok(actual) => actual,
17-
Err(err) => {
18-
failures.push(err.to_string());
28+
Err(message) => {
29+
failures.push(format!("failure(parse): {message}"));
1930
continue;
2031
}
2132
};
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| {
33+
if let Err(message) = std::panic::catch_unwind(|| assert_urls_equal(expected, &actual)).map_err(|panic| {
2634
match downcast_panic_to_str(&panic) {
2735
Some(s) => format!("{url}: {s}\nexpected: {expected:?}\nactual: {actual:?}"),
2836
None => format!("{url}: expected: {expected:?}\nactual: {actual:?}"),
2937
}
30-
});
31-
if let Err(message) = result {
32-
failures.push(message);
38+
}) {
39+
failures.push(format!("failure(compare): {message}"));
40+
continue;
3341
}
42+
// perform additional checks only after we determined that we parsed the url correctly
43+
let url_serialized_again = actual.to_bstring();
44+
failure_count_reserialization += usize::from(url_serialized_again != *url);
45+
failure_count_roundtrips +=
46+
usize::from(gix_url::parse(url_serialized_again.as_ref()).ok().as_ref() != Some(&actual));
3447
}
3548

3649
std::panic::set_hook(panic_hook);
37-
assert_ne!(count, 0, "the baseline is never empty");
50+
51+
assert_ne!(test_count, 0, "the baseline is never empty");
3852
if failures.is_empty() {
3953
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");
4054
}
55+
56+
let failure_count = failures.len();
57+
let passed_count = test_count - failure_count;
58+
let expected_failure_count = baseline::Kind::new().max_num_failures();
59+
60+
eprintln!("failed {failure_count}/{test_count} tests ({passed_count} passed)");
61+
4162
for message in &failures {
42-
eprintln!("{message}");
63+
// print messages to out instead of err to separate them from general test information
64+
println!("{message}");
65+
}
66+
67+
use core::cmp::Ordering;
68+
match Ord::cmp(&failure_count, &expected_failure_count) {
69+
Ordering::Equal => {
70+
eprintln!("the number of failing tests is as expected");
71+
}
72+
Ordering::Less => {
73+
panic!(
74+
"{} more passing tests than expected. Great work! Please change the expected number of failures to {failure_count} to make this panic go away",
75+
expected_failure_count - failure_count,
76+
)
77+
}
78+
Ordering::Greater => {
79+
panic!(
80+
"{} more failing tests than expected! This should get better, not worse. Please check your changes manually for any regressions",
81+
failure_count - expected_failure_count,
82+
)
83+
}
4384
}
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-
);
5385

54-
let kind = baseline::Kind::new();
55-
assert_eq!(failed_roundtrips, 0);
5686
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-
)
87+
failure_count_reserialization <= 42,
88+
"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}"
89+
);
90+
assert_eq!(failure_count_roundtrips, 0, "there should be no roundtrip errors");
6291
}
6392

6493
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)