@@ -9,6 +9,7 @@ fn run() {
9
9
10
10
let mut count = 0 ;
11
11
let mut failures = Vec :: new ( ) ;
12
+ let ( mut failed_roundtrips, mut serialized_url_does_not_match_input) = ( 0 , 0 ) ;
12
13
for ( url, expected) in baseline:: URLS . iter ( ) {
13
14
count += 1 ;
14
15
let actual = match gix_url:: parse ( url) {
@@ -18,9 +19,12 @@ fn run() {
18
19
continue ;
19
20
}
20
21
} ;
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) ) ;
21
25
let result = std:: panic:: catch_unwind ( || assert_urls_equal ( expected, & actual) ) . map_err ( |panic| {
22
26
match downcast_panic_to_str ( & panic) {
23
- Some ( s) => format ! ( "{url}: {s}\n expected: {expected:?}\n actual: {actual:?}" ) . into ( ) ,
27
+ Some ( s) => format ! ( "{url}: {s}\n expected: {expected:?}\n actual: {actual:?}" ) ,
24
28
None => format ! ( "{url}: expected: {expected:?}\n actual: {actual:?}" ) ,
25
29
}
26
30
} ) ;
@@ -42,7 +46,13 @@ fn run() {
42
46
failures. len( ) ,
43
47
count - failures. len( )
44
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
+ ) ;
53
+
45
54
let kind = baseline:: Kind :: new ( ) ;
55
+ assert_eq ! ( failed_roundtrips, 0 ) ;
46
56
assert ! (
47
57
failures. len( ) <= kind. max_num_failures( ) ,
48
58
"Expected no more than {} failures, but got {} - this should get better, not worse" ,
@@ -70,30 +80,29 @@ fn downcast_panic_to_str<'a>(panic: &'a Box<dyn Any + Send + 'static>) -> Option
70
80
71
81
fn assert_urls_equal ( expected : & baseline:: GitDiagUrl < ' _ > , actual : & gix_url:: Url ) {
72
82
assert_eq ! (
83
+ actual. scheme,
73
84
gix_url:: Scheme :: from( expected. protocol. to_str( ) . unwrap( ) ) ,
74
- actual. scheme
75
85
) ;
76
86
77
87
match expected. host {
78
88
baseline:: GitDiagHost :: NonSsh { host_and_port } => match host_and_port {
79
- Some ( host_and_port ) if !host_and_port . is_empty ( ) => {
89
+ Some ( expected_host_and_port ) if !expected_host_and_port . is_empty ( ) => {
80
90
assert ! ( actual. host( ) . is_some( ) ) ;
81
91
82
- let mut gix_host_and_port = String :: with_capacity ( host_and_port. len ( ) ) ;
83
-
92
+ let mut actual_host_and_port = String :: new ( ) ;
84
93
if let Some ( user) = actual. user ( ) {
85
- gix_host_and_port . push_str ( user) ;
86
- gix_host_and_port . push ( '@' ) ;
94
+ actual_host_and_port . push_str ( user) ;
95
+ actual_host_and_port . push ( '@' ) ;
87
96
}
88
97
89
- gix_host_and_port . push_str ( actual. host ( ) . unwrap ( ) ) ;
98
+ actual_host_and_port . push_str ( actual. host ( ) . unwrap ( ) ) ;
90
99
91
100
if let Some ( port) = actual. port {
92
- gix_host_and_port . push ( ':' ) ;
93
- gix_host_and_port . push_str ( & port. to_string ( ) ) ;
101
+ actual_host_and_port . push ( ':' ) ;
102
+ actual_host_and_port . push_str ( & port. to_string ( ) ) ;
94
103
}
95
104
96
- assert_eq ! ( host_and_port , gix_host_and_port ) ;
105
+ assert_eq ! ( actual_host_and_port , expected_host_and_port ) ;
97
106
}
98
107
_ => {
99
108
assert ! ( actual. host( ) . is_none( ) ) ;
@@ -102,44 +111,28 @@ fn assert_urls_equal(expected: &baseline::GitDiagUrl<'_>, actual: &gix_url::Url)
102
111
} ,
103
112
baseline:: GitDiagHost :: Ssh { user_and_host, port } => {
104
113
match user_and_host {
105
- Some ( user_and_host ) => {
114
+ Some ( expected_user_and_host ) => {
106
115
assert ! ( actual. host( ) . is_some( ) ) ;
107
116
108
- let mut gix_user_and_host = String :: with_capacity ( user_and_host . len ( ) ) ;
117
+ let mut actual_user_and_host = String :: new ( ) ;
109
118
if let Some ( user) = actual. user ( ) {
110
- gix_user_and_host . push_str ( user) ;
111
- gix_user_and_host . push ( '@' ) ;
119
+ actual_user_and_host . push_str ( user) ;
120
+ actual_user_and_host . push ( '@' ) ;
112
121
}
113
- gix_user_and_host . push_str ( actual. host ( ) . unwrap ( ) ) ;
122
+ actual_user_and_host . push_str ( actual. host ( ) . unwrap ( ) ) ;
114
123
115
- assert_eq ! ( user_and_host , gix_user_and_host ) ;
124
+ assert_eq ! ( actual_user_and_host , expected_user_and_host ) ;
116
125
}
117
126
None => {
118
127
assert ! ( actual. host( ) . is_none( ) ) ;
119
128
assert ! ( actual. user( ) . is_none( ) ) ;
120
129
}
121
130
}
122
- match port {
123
- Some ( port) => {
124
- assert ! ( actual. port. is_some( ) ) ;
125
- assert_eq ! ( port, actual. port. unwrap( ) . to_string( ) ) ;
126
- }
127
- None => {
128
- assert ! ( actual. port. is_none( ) ) ;
129
- }
130
- }
131
+ assert_eq ! ( actual. port. map( |p| p. to_string( ) ) , port. map( ToString :: to_string) ) ;
131
132
}
132
133
}
133
134
134
- match expected. path {
135
- Some ( path) => {
136
- assert_eq ! ( path, actual. path) ;
137
- }
138
- None => {
139
- // I guess? This case does not happen a single time in the current fixtures...
140
- assert ! ( actual. path. is_empty( ) ) ;
141
- }
142
- }
135
+ assert_eq ! ( actual. path, expected. path. unwrap_or_default( ) ) ;
143
136
}
144
137
145
138
mod baseline {
@@ -177,20 +170,20 @@ mod baseline {
177
170
178
171
static BASELINE : Lazy < BString > = Lazy :: new ( || {
179
172
let base = gix_testtools:: scripted_fixture_read_only ( "make_baseline.sh" ) . unwrap ( ) ;
180
- BString :: from (
181
- std :: fs :: read ( base . join ( format ! ( "git-baseline.{}" , Kind :: new ( ) . extension ( ) ) ) ) . expect ( "fixture file exists" ) ,
182
- )
173
+ std :: fs :: read ( base . join ( format ! ( "git-baseline.{}" , Kind :: new ( ) . extension ( ) ) ) )
174
+ . expect ( "fixture file exists" )
175
+ . into ( )
183
176
} ) ;
184
177
185
178
pub static URLS : Lazy < Vec < ( & ' static BStr , GitDiagUrl < ' static > ) > > = Lazy :: new ( || {
186
179
let mut out = Vec :: new ( ) ;
187
180
188
- let url_block = BASELINE
181
+ let blocks = BASELINE
189
182
. split ( |c| c == & b';' )
190
- . filter ( |url | !url . is_empty ( ) )
183
+ . filter ( |block | !block . is_empty ( ) )
191
184
. map ( ByteSlice :: trim) ;
192
185
193
- for block in url_block {
186
+ for block in blocks {
194
187
let ( url, diag_url) = GitDiagUrl :: parse ( block. as_bstr ( ) ) ;
195
188
out. push ( ( url, diag_url) ) ;
196
189
}
@@ -208,8 +201,15 @@ mod baseline {
208
201
/// Parses the given string into a [GitDiagUrl] according to the format
209
202
/// specified in [Git's `connect.c`][git_src].
210
203
///
211
- /// [git_src]: https://github.com/git/git/blob/master /connect.c#L1415
204
+ /// [git_src]: https://github.com/git/git/blob/bcb6cae2966cc407ca1afc77413b3ef11103c175 /connect.c#L1415
212
205
fn parse ( diag_url : & BStr ) -> ( & ' _ BStr , GitDiagUrl < ' _ > ) {
206
+ fn null_is_none ( input : & BStr ) -> Option < & BStr > {
207
+ if input == "NULL" || input == "NONE" {
208
+ None
209
+ } else {
210
+ Some ( input)
211
+ }
212
+ }
213
213
let mut lines = diag_url. lines ( ) . map ( ByteSlice :: trim) ;
214
214
let mut next_attr = |name : & str | {
215
215
lines
@@ -227,21 +227,13 @@ mod baseline {
227
227
let user_and_host = next_attr ( "userandhost" ) ;
228
228
let port = next_attr ( "port" ) ;
229
229
GitDiagHost :: Ssh {
230
- user_and_host : if user_and_host == "NULL" {
231
- None
232
- } else {
233
- Some ( user_and_host)
234
- } ,
235
- port : if port == "NONE" { None } else { Some ( port) } ,
230
+ user_and_host : null_is_none ( user_and_host) ,
231
+ port : null_is_none ( port) ,
236
232
}
237
233
} else {
238
234
let host_and_port = next_attr ( "hostandport" ) ;
239
235
GitDiagHost :: NonSsh {
240
- host_and_port : if host_and_port == "NULL" {
241
- None
242
- } else {
243
- Some ( host_and_port)
244
- } ,
236
+ host_and_port : null_is_none ( host_and_port) ,
245
237
}
246
238
} ;
247
239
@@ -252,7 +244,7 @@ mod baseline {
252
244
GitDiagUrl {
253
245
protocol,
254
246
host,
255
- path : if path == "NULL" { None } else { Some ( path) } ,
247
+ path : null_is_none ( path) ,
256
248
} ,
257
249
)
258
250
}
0 commit comments