From 99b4a49530b37681551e090f5607595d4c120f03 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 15 Apr 2024 19:46:08 -0400 Subject: [PATCH 1/2] test: assert error kind for pkgidspec parsing --- .../src/core/package_id_spec.rs | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/crates/cargo-util-schemas/src/core/package_id_spec.rs b/crates/cargo-util-schemas/src/core/package_id_spec.rs index 3032d205aa6..18e1e8211d6 100644 --- a/crates/cargo-util-schemas/src/core/package_id_spec.rs +++ b/crates/cargo-util-schemas/src/core/package_id_spec.rs @@ -318,6 +318,7 @@ enum ErrorKind { #[cfg(test)] mod tests { + use super::ErrorKind; use super::PackageIdSpec; use crate::core::{GitReference, SourceKind}; use url::Url; @@ -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(_)); } } From eacdfd2917ee851d861c19072a1c32772aee2652 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 15 Apr 2024 19:50:43 -0400 Subject: [PATCH 2/2] test: fix tests to assert unxpected querystrring error --- crates/cargo-util-schemas/src/core/package_id_spec.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/cargo-util-schemas/src/core/package_id_spec.rs b/crates/cargo-util-schemas/src/core/package_id_spec.rs index 18e1e8211d6..72d72149e2a 100644 --- a/crates/cargo-util-schemas/src/core/package_id_spec.rs +++ b/crates/cargo-util-schemas/src/core/package_id_spec.rs @@ -641,12 +641,12 @@ mod tests { ErrorKind::UnexpectedQueryString(_) ); err!( - "registry+https://github.com/rust-lang/cargo#0.52.0?branch=dev", - ErrorKind::PartialVersion(_) + "registry+https://github.com/rust-lang/cargo?branch=dev#0.52.0", + ErrorKind::UnexpectedQueryString(_) ); err!( - "sparse+https://github.com/rust-lang/cargo#0.52.0?branch=dev", - ErrorKind::PartialVersion(_) + "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(_));