-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #12938 - epage:source-refactor, r=weihanglo
refactor(source): Prepare for new PackageIDSpec syntax ### What does this PR try to resolve? This adds tests and refactors to prepare for #12933 ### How should we test and review this PR? ### Additional information
- Loading branch information
Showing
2 changed files
with
156 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,10 +180,13 @@ impl PackageIdSpec { | |
} | ||
} | ||
|
||
match self.url { | ||
Some(ref u) => u == package_id.source_id().url(), | ||
None => true, | ||
if let Some(u) = &self.url { | ||
if u != package_id.source_id().url() { | ||
return false; | ||
} | ||
} | ||
|
||
true | ||
} | ||
|
||
/// Checks a list of `PackageId`s to find 1 that matches this `PackageIdSpec`. If 0, 2, or | ||
|
@@ -331,7 +334,10 @@ mod tests { | |
fn ok(spec: &str, expected: PackageIdSpec, expected_rendered: &str) { | ||
let parsed = PackageIdSpec::parse(spec).unwrap(); | ||
assert_eq!(parsed, expected); | ||
assert_eq!(parsed.to_string(), expected_rendered); | ||
let rendered = parsed.to_string(); | ||
assert_eq!(rendered, expected_rendered); | ||
let reparsed = PackageIdSpec::parse(&rendered).unwrap(); | ||
assert_eq!(reparsed, expected); | ||
} | ||
|
||
ok( | ||
|
@@ -424,6 +430,98 @@ mod tests { | |
}, | ||
"[email protected]", | ||
); | ||
|
||
// pkgid-spec.md | ||
ok( | ||
"regex", | ||
PackageIdSpec { | ||
name: String::from("regex"), | ||
version: None, | ||
url: None, | ||
}, | ||
"regex", | ||
); | ||
ok( | ||
"[email protected]", | ||
PackageIdSpec { | ||
name: String::from("regex"), | ||
version: Some("1.4".parse().unwrap()), | ||
url: None, | ||
}, | ||
"[email protected]", | ||
); | ||
ok( | ||
"[email protected]", | ||
PackageIdSpec { | ||
name: String::from("regex"), | ||
version: Some("1.4.3".parse().unwrap()), | ||
url: None, | ||
}, | ||
"[email protected]", | ||
); | ||
ok( | ||
"https://github.com/rust-lang/crates.io-index#regex", | ||
PackageIdSpec { | ||
name: String::from("regex"), | ||
version: None, | ||
url: Some(Url::parse("https://github.com/rust-lang/crates.io-index").unwrap()), | ||
}, | ||
"https://github.com/rust-lang/crates.io-index#regex", | ||
); | ||
ok( | ||
"https://github.com/rust-lang/crates.io-index#[email protected]", | ||
PackageIdSpec { | ||
name: String::from("regex"), | ||
version: Some("1.4.3".parse().unwrap()), | ||
url: Some(Url::parse("https://github.com/rust-lang/crates.io-index").unwrap()), | ||
}, | ||
"https://github.com/rust-lang/crates.io-index#[email protected]", | ||
); | ||
ok( | ||
"https://github.com/rust-lang/cargo#0.52.0", | ||
PackageIdSpec { | ||
name: String::from("cargo"), | ||
version: Some("0.52.0".parse().unwrap()), | ||
url: Some(Url::parse("https://github.com/rust-lang/cargo").unwrap()), | ||
}, | ||
"https://github.com/rust-lang/cargo#0.52.0", | ||
); | ||
ok( | ||
"https://github.com/rust-lang/cargo#[email protected]", | ||
PackageIdSpec { | ||
name: String::from("cargo-platform"), | ||
version: Some("0.1.2".parse().unwrap()), | ||
url: Some(Url::parse("https://github.com/rust-lang/cargo").unwrap()), | ||
}, | ||
"https://github.com/rust-lang/cargo#[email protected]", | ||
); | ||
ok( | ||
"ssh://[email protected]/rust-lang/regex.git#[email protected]", | ||
PackageIdSpec { | ||
name: String::from("regex"), | ||
version: Some("1.4.3".parse().unwrap()), | ||
url: Some(Url::parse("ssh://[email protected]/rust-lang/regex.git").unwrap()), | ||
}, | ||
"ssh://[email protected]/rust-lang/regex.git#[email protected]", | ||
); | ||
ok( | ||
"file:///path/to/my/project/foo", | ||
PackageIdSpec { | ||
name: String::from("foo"), | ||
version: None, | ||
url: Some(Url::parse("file:///path/to/my/project/foo").unwrap()), | ||
}, | ||
"file:///path/to/my/project/foo", | ||
); | ||
ok( | ||
"file:///path/to/my/project/foo#1.1.8", | ||
PackageIdSpec { | ||
name: String::from("foo"), | ||
version: Some("1.1.8".parse().unwrap()), | ||
url: Some(Url::parse("file:///path/to/my/project/foo").unwrap()), | ||
}, | ||
"file:///path/to/my/project/foo#1.1.8", | ||
); | ||
} | ||
|
||
#[test] | ||
|
@@ -450,6 +548,12 @@ mod tests { | |
assert!(PackageIdSpec::parse("[email protected]").unwrap().matches(foo)); | ||
assert!(!PackageIdSpec::parse("[email protected]").unwrap().matches(foo)); | ||
assert!(PackageIdSpec::parse("[email protected]").unwrap().matches(foo)); | ||
assert!(PackageIdSpec::parse("https://example.com#[email protected]") | ||
.unwrap() | ||
.matches(foo)); | ||
assert!(!PackageIdSpec::parse("https://bob.com#[email protected]") | ||
.unwrap() | ||
.matches(foo)); | ||
|
||
let meta = PackageId::new("meta", "1.2.3+hello", sid).unwrap(); | ||
assert!(PackageIdSpec::parse("meta").unwrap().matches(meta)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters