Skip to content

Commit

Permalink
Auto merge of #13761 - weihanglo:pkgidspec, r=epage
Browse files Browse the repository at this point in the history
test(schemas): Ensure tests cover the correct case

Also, I am preparing an experiment of unidiff patch, which will introduce one more error kind.
  • Loading branch information
bors committed Apr 16, 2024
2 parents 9f8adff + eacdfd2 commit f8c4af5
Showing 1 changed file with 47 additions and 24 deletions.
71 changes: 47 additions & 24 deletions crates/cargo-util-schemas/src/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ enum ErrorKind {

#[cfg(test)]
mod tests {
use super::ErrorKind;
use super::PackageIdSpec;
use crate::core::{GitReference, SourceKind};
use url::Url;
Expand Down Expand Up @@ -602,31 +603,53 @@ mod tests {

#[test]
fn bad_parsing() {
assert!(PackageIdSpec::parse("baz:").is_err());
assert!(PackageIdSpec::parse("baz:*").is_err());
assert!(PackageIdSpec::parse("baz@").is_err());
assert!(PackageIdSpec::parse("baz@*").is_err());
assert!(PackageIdSpec::parse("baz@^1.0").is_err());
assert!(PackageIdSpec::parse("https://baz:1.0").is_err());
assert!(PackageIdSpec::parse("https://#baz:1.0").is_err());
assert!(
PackageIdSpec::parse("foobar+https://github.com/rust-lang/crates.io-index").is_err()
);
assert!(PackageIdSpec::parse("path+https://github.com/rust-lang/crates.io-index").is_err());
macro_rules! err {
($spec:expr, $expected:pat) => {
let err = PackageIdSpec::parse($spec).unwrap_err();
let kind = err.0;
assert!(
matches!(kind, $expected),
"`{}` parse error mismatch, got {kind:?}",
$spec
);
};
}

err!("baz:", ErrorKind::PartialVersion(_));
err!("baz:*", ErrorKind::PartialVersion(_));
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!(
"foobar+https://github.com/rust-lang/crates.io-index",
ErrorKind::UnsupportedProtocol(_)
);
err!(
"path+https://github.com/rust-lang/crates.io-index",
ErrorKind::UnsupportedPathPlusScheme(_)
);

// Only `git+` can use `?`
assert!(PackageIdSpec::parse("file:///path/to/my/project/foo?branch=dev").is_err());
assert!(PackageIdSpec::parse("path+file:///path/to/my/project/foo?branch=dev").is_err());
assert!(PackageIdSpec::parse(
"registry+https://github.com/rust-lang/cargo#0.52.0?branch=dev"
)
.is_err());
assert!(PackageIdSpec::parse(
"sparse+https://github.com/rust-lang/cargo#0.52.0?branch=dev"
)
.is_err());
assert!(PackageIdSpec::parse("@1.2.3").is_err());
assert!(PackageIdSpec::parse("registry+https://github.com").is_err());
assert!(PackageIdSpec::parse("https://crates.io/1foo#1.2.3").is_err())
err!(
"file:///path/to/my/project/foo?branch=dev",
ErrorKind::UnexpectedQueryString(_)
);
err!(
"path+file:///path/to/my/project/foo?branch=dev",
ErrorKind::UnexpectedQueryString(_)
);
err!(
"registry+https://github.com/rust-lang/cargo?branch=dev#0.52.0",
ErrorKind::UnexpectedQueryString(_)
);
err!(
"sparse+https://github.com/rust-lang/cargo?branch=dev#0.52.0",
ErrorKind::UnexpectedQueryString(_)
);
err!("@1.2.3", ErrorKind::NameValidation(_));
err!("registry+https://github.com", ErrorKind::NameValidation(_));
err!("https://crates.io/1foo#1.2.3", ErrorKind::NameValidation(_));
}
}

0 comments on commit f8c4af5

Please sign in to comment.