Skip to content

Commit 5ac705c

Browse files
committed
Create test urls from raw parts directly
The previous behavior introduced some subtle issues in test code, because fields of `gix_url::Url` instances created in with the `url` methods sometimes contained different values than originally specified.
1 parent 99f4447 commit 5ac705c

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

gix-url/src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,43 @@ impl Url {
253253
}
254254

255255
mod impls;
256+
257+
/// This module contains extensions to the [crate::Url] struct which are only intended to be used
258+
/// for testing code. Do not use this module in production! For all intends and purposes the APIs of
259+
/// all functions and types exposed by this module are considered unstable and are allowed to break
260+
/// even in patch releases!
261+
#[doc(hidden)]
262+
pub mod testing {
263+
use bstr::BString;
264+
265+
use crate::{Scheme, Url};
266+
267+
/// Additional functions for [crate::Url] which are only intended to be used for tests.
268+
pub trait TestUrlExtension {
269+
/// Create a new instance from the given parts without validating them.
270+
///
271+
/// This function is primarily intended for testing purposes. For production code please
272+
/// consider using [Url::from_parts] instead!
273+
fn from_parts_unchecked(
274+
scheme: Scheme,
275+
user: Option<String>,
276+
password: Option<String>,
277+
host: Option<String>,
278+
port: Option<u16>,
279+
path: BString,
280+
serialize_alternative_form: bool,
281+
) -> crate::Url {
282+
Url {
283+
scheme,
284+
user,
285+
password,
286+
host,
287+
port,
288+
path,
289+
serialize_alternative_form,
290+
}
291+
}
292+
}
293+
294+
impl TestUrlExtension for crate::Url {}
295+
}

gix-url/tests/parse/file.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,19 @@ fn shortest_possible_relative_path() -> crate::Result {
103103
assert_eq!(parsed, ".");
104104
let parsed = assert_url("..", url_alternate(Scheme::File, None, None, None, b".."))?.to_bstring();
105105
assert_eq!(parsed, "..");
106-
let parsed = assert_url("file://../", url(Scheme::File, None, None, None, b"../"))?.to_bstring();
107-
assert_eq!(parsed, "file://../");
108-
let parsed = assert_url("file://./", url(Scheme::File, None, None, None, b"./"))?.to_bstring();
109-
assert_eq!(parsed, "file://./");
110106
#[cfg(windows)]
111107
{
112108
let parsed = assert_url("file://.\\", url(Scheme::File, None, None, None, b".\\"))?.to_bstring();
113109
assert_eq!(parsed, "file://.\\");
114110
}
115-
let parsed = assert_url("file://a/", url(Scheme::File, None, None, None, b"a/"))?.to_bstring();
116-
assert_eq!(parsed, "file://a/");
111+
Ok(())
112+
}
113+
114+
#[test]
115+
fn no_relative_paths_if_protocol() -> crate::Result {
116+
assert_url_roundtrip("file://../", url(Scheme::File, None, "..", None, b"/"))?;
117+
assert_url_roundtrip("file://./", url(Scheme::File, None, ".", None, b"/"))?;
118+
assert_url_roundtrip("file://a/", url(Scheme::File, None, "a", None, b"/"))?;
117119
Ok(())
118120
}
119121

gix-url/tests/parse/http.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn username_expansion_is_unsupported() -> crate::Result {
1313
#[test]
1414
fn empty_user_cannot_roundtrip() -> crate::Result {
1515
let actual = gix_url::parse("http://@example.com/~byron/hello".into())?;
16-
let expected = url(Scheme::Http, "", "example.com", None, b"/~byron/hello");
16+
let expected = url(Scheme::Http, None, "example.com", None, b"/~byron/hello");
1717
assert_eq!(actual, expected);
1818
assert_eq!(
1919
actual.to_bstring(),
@@ -50,7 +50,7 @@ fn only_password() -> crate::Result {
5050
#[test]
5151
fn username_and_empty_password() -> crate::Result {
5252
let actual = gix_url::parse("http://user:@example.com/~byron/hello".into())?;
53-
let expected = url_with_pass(Scheme::Http, "user", "", "example.com", None, b"/~byron/hello");
53+
let expected = url(Scheme::Http, "user", "example.com", None, b"/~byron/hello");
5454
assert_eq!(actual, expected);
5555
assert_eq!(
5656
actual.to_bstring(),

gix-url/tests/parse/mod.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bstr::{BStr, ByteSlice};
2-
use gix_url::Scheme;
2+
use gix_url::{testing::TestUrlExtension, Scheme};
33

44
fn assert_url(url: &str, expected: gix_url::Url) -> Result<gix_url::Url, crate::Error> {
55
let actual = gix_url::parse(url.into())?;
@@ -33,7 +33,7 @@ fn url<'a, 'b>(
3333
port: impl Into<Option<u16>>,
3434
path: &[u8],
3535
) -> gix_url::Url {
36-
gix_url::Url::from_parts(
36+
gix_url::Url::from_parts_unchecked(
3737
protocol,
3838
user.into().map(Into::into),
3939
None,
@@ -42,7 +42,6 @@ fn url<'a, 'b>(
4242
path.into(),
4343
false,
4444
)
45-
.unwrap_or_else(|err| panic!("'{}' failed: {err:?}", path.as_bstr()))
4645
}
4746

4847
fn url_with_pass<'a, 'b>(
@@ -53,7 +52,7 @@ fn url_with_pass<'a, 'b>(
5352
port: impl Into<Option<u16>>,
5453
path: &[u8],
5554
) -> gix_url::Url {
56-
gix_url::Url::from_parts(
55+
gix_url::Url::from_parts_unchecked(
5756
protocol,
5857
user.into().map(Into::into),
5958
Some(password.into()),
@@ -62,7 +61,6 @@ fn url_with_pass<'a, 'b>(
6261
path.into(),
6362
false,
6463
)
65-
.unwrap_or_else(|err| panic!("'{}' failed: {err:?}", path.as_bstr()))
6664
}
6765

6866
fn url_alternate<'a, 'b>(
@@ -72,7 +70,7 @@ fn url_alternate<'a, 'b>(
7270
port: impl Into<Option<u16>>,
7371
path: &[u8],
7472
) -> gix_url::Url {
75-
let url = gix_url::Url::from_parts(
73+
gix_url::Url::from_parts_unchecked(
7674
protocol.clone(),
7775
user.into().map(Into::into),
7876
None,
@@ -81,9 +79,6 @@ fn url_alternate<'a, 'b>(
8179
path.into(),
8280
true,
8381
)
84-
.expect("valid");
85-
assert_eq!(url.scheme, protocol);
86-
url
8782
}
8883

8984
mod file;

gix-url/tests/parse/ssh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ fn scp_like_with_user_and_relative_path_keep_relative_path() -> crate::Result {
160160
#[test]
161161
fn strange_windows_paths_yield_meaningful_results() -> crate::Result {
162162
let url = assert_url(
163-
"[email protected]:42:C:/strange/absolute/path",
164-
url_alternate(Scheme::Ssh, "user", "host.xz", Some(42), b"C:/strange/absolute/path"),
163+
"[email protected]:C:/strange/absolute/path",
164+
url_alternate(Scheme::Ssh, "user", "host.xz", None, b"C:/strange/absolute/path"),
165165
)?
166166
.to_bstring();
167-
assert_eq!(url, "[email protected]:42:C:/strange/absolute/path");
167+
assert_eq!(url, "[email protected]:C:/strange/absolute/path");
168168
Ok(())
169169
}
170170

0 commit comments

Comments
 (0)