]> git.proxmox.com Git - cargo.git/commitdiff
Compare version fields' equalities directly
authorWeihang Lo <me@weihanglo.tw>
Mon, 20 Sep 2021 03:31:41 +0000 (11:31 +0800)
committerWeihang Lo <me@weihanglo.tw>
Mon, 20 Sep 2021 03:32:55 +0000 (11:32 +0800)
Instead of creating another VersionReq, this can reduce some extra
efforts on version matching logic. Though it also may introduces
divergence from semver's matching logic. Accordingly, we borrow unit
tests from semver crate and compare the matching results to prevent
future breaks.

src/cargo/util/semver_ext.rs

index be86f209b46f73c03cb180fedadb5d38a76aeca8..de6d68e16aeb395b15065b34acbee467669932a7 100644 (file)
@@ -82,7 +82,12 @@ impl OptVersionReq {
         match self {
             OptVersionReq::Any => true,
             OptVersionReq::Req(req) => req.matches(version),
-            OptVersionReq::Locked(v, _) => VersionReq::exact(v).matches(version),
+            OptVersionReq::Locked(v, _) => {
+                v.major == version.major
+                    && v.minor == version.minor
+                    && v.patch == version.patch
+                    && v.pre == version.pre
+            }
         }
     }
 }
@@ -102,3 +107,40 @@ impl From<VersionReq> for OptVersionReq {
         OptVersionReq::Req(req)
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn locked_has_the_same_with_exact() {
+        fn test_versions(target_ver: &str, vers: &[&str]) {
+            let ver = Version::parse(target_ver).unwrap();
+            let exact = OptVersionReq::exact(&ver);
+            let mut locked = exact.clone();
+            locked.lock_to(&ver);
+            for v in vers {
+                let v = Version::parse(v).unwrap();
+                assert_eq!(exact.matches(&v), locked.matches(&v));
+            }
+        }
+
+        test_versions(
+            "1.0.0",
+            &["1.0.0", "1.0.1", "0.9.9", "0.10.0", "0.1.0", "1.0.0-pre"],
+        );
+        test_versions("0.9.0", &["0.9.0", "0.9.1", "1.9.0", "0.0.9", "0.9.0-pre"]);
+        test_versions("0.0.2", &["0.0.2", "0.0.1", "0.0.3", "0.0.2-pre"]);
+        test_versions(
+            "0.1.0-beta2.a",
+            &[
+                "0.1.0-beta2.a",
+                "0.9.1",
+                "0.1.0",
+                "0.1.1-beta2.a",
+                "0.1.0-beta2",
+            ],
+        );
+        test_versions("0.1.0+meta", &["0.1.0", "0.1.0+meta", "0.1.0+any"]);
+    }
+}