Skip to content

Commit

Permalink
fix(pkgid): Allow open namespaces in PackageIdSpec's
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 28, 2024
1 parent c81e82d commit f4c7ed1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
72 changes: 60 additions & 12 deletions crates/cargo-util-schemas/src/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ impl PackageIdSpec {
}

fn parse_spec(spec: &str) -> Result<Option<(String, Option<PartialVersion>)>> {
let Some((name, ver)) = spec.split_once([':', '@']) else {
let Some((name, ver)) = spec
.rsplit_once('@')
.or_else(|| spec.rsplit_once(':').filter(|(n, _)| !n.ends_with(':')))
else {
return Ok(None);
};
let name = name.to_owned();
Expand Down Expand Up @@ -438,7 +441,16 @@ mod tests {
},
"foo",
);
err!("foo::bar", ErrorKind::PartialVersion(_));
ok(
"foo::bar",
PackageIdSpec {
name: String::from("foo::bar"),
version: None,
url: None,
kind: None,
},
"foo::bar",
);
ok(
"foo:1.2.3",
PackageIdSpec {
Expand All @@ -449,7 +461,16 @@ mod tests {
},
"[email protected]",
);
err!("foo::bar:1.2.3", ErrorKind::PartialVersion(_));
ok(
"foo::bar:1.2.3",
PackageIdSpec {
name: String::from("foo::bar"),
version: Some("1.2.3".parse().unwrap()),
url: None,
kind: None,
},
"foo::[email protected]",
);
ok(
"[email protected]",
PackageIdSpec {
Expand All @@ -460,7 +481,16 @@ mod tests {
},
"[email protected]",
);
err!("foo::[email protected]", ErrorKind::PartialVersion(_));
ok(
"foo::[email protected]",
PackageIdSpec {
name: String::from("foo::bar"),
version: Some("1.2.3".parse().unwrap()),
url: None,
kind: None,
},
"foo::[email protected]",
);
ok(
"[email protected]",
PackageIdSpec {
Expand Down Expand Up @@ -635,9 +665,15 @@ mod tests {
},
"path+file:///path/to/my/project/foo#bar",
);
err!(
ok(
"path+file:///path/to/my/project/foo#foo::bar",
PackageIdSpec {
name: String::from("foo::bar"),
version: None,
url: Some(Url::parse("file:///path/to/my/project/foo").unwrap()),
kind: Some(SourceKind::Path),
},
"path+file:///path/to/my/project/foo#foo::bar",
ErrorKind::PartialVersion(_)
);
ok(
"path+file:///path/to/my/project/foo#bar:1.1.8",
Expand All @@ -649,9 +685,15 @@ mod tests {
},
"path+file:///path/to/my/project/foo#[email protected]",
);
err!(
ok(
"path+file:///path/to/my/project/foo#foo::bar:1.1.8",
ErrorKind::PartialVersion(_)
PackageIdSpec {
name: String::from("foo::bar"),
version: Some("1.1.8".parse().unwrap()),
url: Some(Url::parse("file:///path/to/my/project/foo").unwrap()),
kind: Some(SourceKind::Path),
},
"path+file:///path/to/my/project/foo#foo::[email protected]",
);
ok(
"path+file:///path/to/my/project/foo#[email protected]",
Expand All @@ -663,9 +705,15 @@ mod tests {
},
"path+file:///path/to/my/project/foo#[email protected]",
);
err!(
ok(
"path+file:///path/to/my/project/foo#foo::[email protected]",
PackageIdSpec {
name: String::from("foo::bar"),
version: Some("1.1.8".parse().unwrap()),
url: Some(Url::parse("file:///path/to/my/project/foo").unwrap()),
kind: Some(SourceKind::Path),
},
"path+file:///path/to/my/project/foo#foo::[email protected]",
ErrorKind::PartialVersion(_)
);
}

Expand All @@ -676,8 +724,8 @@ mod tests {
err!("baz@", ErrorKind::PartialVersion(_));
err!("baz@*", ErrorKind::PartialVersion(_));
err!("baz@^1.0", ErrorKind::PartialVersion(_));
err!("https://baz:1.0", ErrorKind::PartialVersion(_));
err!("https://#baz:1.0", ErrorKind::PartialVersion(_));
err!("https://baz:1.0", ErrorKind::NameValidation(_));
err!("https://#baz:1.0", ErrorKind::NameValidation(_));
err!(
"foobar+https://github.com/rust-lang/crates.io-index",
ErrorKind::UnsupportedProtocol(_)
Expand Down
14 changes: 2 additions & 12 deletions tests/testsuite/open_namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,9 @@ fn update_spec_accepts_namespaced_name() {
.run();
p.cargo("update foo::bar")
.masquerade_as_nightly_cargo(&["open-namespaces"])
.with_status(101)
.with_stdout_data(str![""])
.with_stderr_data(str![[r#"
[ERROR] invalid package ID specification: `foo::bar`
Did you mean `foo::bar`?
Caused by:
expected a version like "1.32"
[LOCKING] 0 packages to latest compatible versions
"#]])
.run()
Expand Down Expand Up @@ -415,13 +409,9 @@ fn update_spec_accepts_namespaced_pkgid() {
.run();
p.cargo(&format!("update path+{}#foo::[email protected]", p.url()))
.masquerade_as_nightly_cargo(&["open-namespaces"])
.with_status(101)
.with_stdout_data(str![""])
.with_stderr_data(str![[r#"
[ERROR] invalid package ID specification: `path+[ROOTURL]/foo#foo::[email protected]`
Caused by:
expected a version like "1.32"
[LOCKING] 0 packages to latest compatible versions
"#]])
.run()
Expand Down

0 comments on commit f4c7ed1

Please sign in to comment.