Skip to content

Commit 4ecb543

Browse files
committed
Compare version fields' equalities directly
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.
1 parent d226234 commit 4ecb543

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/cargo/util/semver_ext.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ impl OptVersionReq {
8282
match self {
8383
OptVersionReq::Any => true,
8484
OptVersionReq::Req(req) => req.matches(version),
85-
OptVersionReq::Locked(v, _) => VersionReq::exact(v).matches(version),
85+
OptVersionReq::Locked(v, _) => {
86+
v.major == version.major
87+
&& v.minor == version.minor
88+
&& v.patch == version.patch
89+
&& v.pre == version.pre
90+
}
8691
}
8792
}
8893
}
@@ -102,3 +107,40 @@ impl From<VersionReq> for OptVersionReq {
102107
OptVersionReq::Req(req)
103108
}
104109
}
110+
111+
#[cfg(test)]
112+
mod tests {
113+
use super::*;
114+
115+
#[test]
116+
fn locked_has_the_same_with_exact() {
117+
fn test_versions(target_ver: &str, vers: &[&str]) {
118+
let ver = Version::parse(target_ver).unwrap();
119+
let exact = OptVersionReq::exact(&ver);
120+
let mut locked = exact.clone();
121+
locked.lock_to(&ver);
122+
for v in vers {
123+
let v = Version::parse(v).unwrap();
124+
assert_eq!(exact.matches(&v), locked.matches(&v));
125+
}
126+
}
127+
128+
test_versions(
129+
"1.0.0",
130+
&["1.0.0", "1.0.1", "0.9.9", "0.10.0", "0.1.0", "1.0.0-pre"],
131+
);
132+
test_versions("0.9.0", &["0.9.0", "0.9.1", "1.9.0", "0.0.9", "0.9.0-pre"]);
133+
test_versions("0.0.2", &["0.0.2", "0.0.1", "0.0.3", "0.0.2-pre"]);
134+
test_versions(
135+
"0.1.0-beta2.a",
136+
&[
137+
"0.1.0-beta2.a",
138+
"0.9.1",
139+
"0.1.0",
140+
"0.1.1-beta2.a",
141+
"0.1.0-beta2",
142+
],
143+
);
144+
test_versions("0.1.0+meta", &["0.1.0", "0.1.0+meta", "0.1.0+any"]);
145+
}
146+
}

0 commit comments

Comments
 (0)