Skip to content

Commit

Permalink
Compare version fields' equalities directly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weihanglo committed Sep 20, 2021
1 parent d226234 commit 4ecb543
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand All @@ -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"]);
}
}

0 comments on commit 4ecb543

Please sign in to comment.