Skip to content

Commit f8c4af5

Browse files
committed
Auto merge of #13761 - weihanglo:pkgidspec, r=epage
test(schemas): Ensure tests cover the correct case Also, I am preparing an experiment of unidiff patch, which will introduce one more error kind.
2 parents 9f8adff + eacdfd2 commit f8c4af5

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

crates/cargo-util-schemas/src/core/package_id_spec.rs

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ enum ErrorKind {
318318

319319
#[cfg(test)]
320320
mod tests {
321+
use super::ErrorKind;
321322
use super::PackageIdSpec;
322323
use crate::core::{GitReference, SourceKind};
323324
use url::Url;
@@ -602,31 +603,53 @@ mod tests {
602603

603604
#[test]
604605
fn bad_parsing() {
605-
assert!(PackageIdSpec::parse("baz:").is_err());
606-
assert!(PackageIdSpec::parse("baz:*").is_err());
607-
assert!(PackageIdSpec::parse("baz@").is_err());
608-
assert!(PackageIdSpec::parse("baz@*").is_err());
609-
assert!(PackageIdSpec::parse("baz@^1.0").is_err());
610-
assert!(PackageIdSpec::parse("https://baz:1.0").is_err());
611-
assert!(PackageIdSpec::parse("https://#baz:1.0").is_err());
612-
assert!(
613-
PackageIdSpec::parse("foobar+https://github.com/rust-lang/crates.io-index").is_err()
614-
);
615-
assert!(PackageIdSpec::parse("path+https://github.com/rust-lang/crates.io-index").is_err());
606+
macro_rules! err {
607+
($spec:expr, $expected:pat) => {
608+
let err = PackageIdSpec::parse($spec).unwrap_err();
609+
let kind = err.0;
610+
assert!(
611+
matches!(kind, $expected),
612+
"`{}` parse error mismatch, got {kind:?}",
613+
$spec
614+
);
615+
};
616+
}
617+
618+
err!("baz:", ErrorKind::PartialVersion(_));
619+
err!("baz:*", ErrorKind::PartialVersion(_));
620+
err!("baz@", ErrorKind::PartialVersion(_));
621+
err!("baz@*", ErrorKind::PartialVersion(_));
622+
err!("baz@^1.0", ErrorKind::PartialVersion(_));
623+
err!("https://baz:1.0", ErrorKind::PartialVersion(_));
624+
err!("https://#baz:1.0", ErrorKind::PartialVersion(_));
625+
err!(
626+
"foobar+https://github.com/rust-lang/crates.io-index",
627+
ErrorKind::UnsupportedProtocol(_)
628+
);
629+
err!(
630+
"path+https://github.com/rust-lang/crates.io-index",
631+
ErrorKind::UnsupportedPathPlusScheme(_)
632+
);
616633

617634
// Only `git+` can use `?`
618-
assert!(PackageIdSpec::parse("file:///path/to/my/project/foo?branch=dev").is_err());
619-
assert!(PackageIdSpec::parse("path+file:///path/to/my/project/foo?branch=dev").is_err());
620-
assert!(PackageIdSpec::parse(
621-
"registry+https://github.com/rust-lang/cargo#0.52.0?branch=dev"
622-
)
623-
.is_err());
624-
assert!(PackageIdSpec::parse(
625-
"sparse+https://github.com/rust-lang/cargo#0.52.0?branch=dev"
626-
)
627-
.is_err());
628-
assert!(PackageIdSpec::parse("@1.2.3").is_err());
629-
assert!(PackageIdSpec::parse("registry+https://github.com").is_err());
630-
assert!(PackageIdSpec::parse("https://crates.io/1foo#1.2.3").is_err())
635+
err!(
636+
"file:///path/to/my/project/foo?branch=dev",
637+
ErrorKind::UnexpectedQueryString(_)
638+
);
639+
err!(
640+
"path+file:///path/to/my/project/foo?branch=dev",
641+
ErrorKind::UnexpectedQueryString(_)
642+
);
643+
err!(
644+
"registry+https://github.com/rust-lang/cargo?branch=dev#0.52.0",
645+
ErrorKind::UnexpectedQueryString(_)
646+
);
647+
err!(
648+
"sparse+https://github.com/rust-lang/cargo?branch=dev#0.52.0",
649+
ErrorKind::UnexpectedQueryString(_)
650+
);
651+
err!("@1.2.3", ErrorKind::NameValidation(_));
652+
err!("registry+https://github.com", ErrorKind::NameValidation(_));
653+
err!("https://crates.io/1foo#1.2.3", ErrorKind::NameValidation(_));
631654
}
632655
}

0 commit comments

Comments
 (0)