Skip to content

Commit

Permalink
test: assert error kind for pkgidspec parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Apr 16, 2024
1 parent 48eca1b commit 99b4a49
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#0.52.0?branch=dev",
ErrorKind::PartialVersion(_)
);
err!(
"sparse+https://github.com/rust-lang/cargo#0.52.0?branch=dev",
ErrorKind::PartialVersion(_)
);
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 99b4a49

Please sign in to comment.