Skip to content

Commit c7098f7

Browse files
committed
Change rules about how we handle pre-release
1 parent 0080ed9 commit c7098f7

File tree

2 files changed

+73
-130
lines changed

2 files changed

+73
-130
lines changed

src/eval.rs

Lines changed: 42 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -42,135 +42,67 @@ fn matches_impl(cmp: &Comparator, ver: &Version) -> bool {
4242
}
4343

4444
fn matches_exact(cmp: &Comparator, ver: &Version) -> bool {
45-
if ver.major != cmp.major {
46-
return false;
47-
}
48-
49-
if let Some(minor) = cmp.minor {
50-
if ver.minor != minor {
51-
return false;
52-
}
53-
}
54-
55-
if let Some(patch) = cmp.patch {
56-
if ver.patch != patch {
57-
return false;
58-
}
59-
}
60-
61-
ver.pre == cmp.pre
45+
ver.major == cmp.major
46+
&& cmp.minor.map_or(true, |minor| ver.minor == minor)
47+
&& cmp.patch.map_or(true, |patch| ver.patch == patch)
48+
&& ver.pre == cmp.pre
6249
}
6350

6451
fn matches_greater(cmp: &Comparator, ver: &Version) -> bool {
6552
if ver.major != cmp.major {
66-
return ver.major > cmp.major;
53+
ver.major > cmp.major
54+
} else if let Some(minor) = cmp.minor.filter(|&minor| ver.minor != minor) {
55+
ver.minor > minor
56+
} else if let Some(patch) = cmp.patch.filter(|&patch| ver.patch != patch) {
57+
ver.patch > patch
58+
} else {
59+
ver.pre > cmp.pre
6760
}
68-
69-
match cmp.minor {
70-
None => return false,
71-
Some(minor) => {
72-
if ver.minor != minor {
73-
return ver.minor > minor;
74-
}
75-
}
76-
}
77-
78-
match cmp.patch {
79-
None => return false,
80-
Some(patch) => {
81-
if ver.patch != patch {
82-
return ver.patch > patch;
83-
}
84-
}
85-
}
86-
87-
ver.pre > cmp.pre
8861
}
8962

9063
fn matches_less(cmp: &Comparator, ver: &Version) -> bool {
9164
if ver.major != cmp.major {
92-
return ver.major < cmp.major;
65+
ver.major < cmp.major
66+
} else if let Some(minor) = cmp.minor.filter(|&minor| ver.minor != minor) {
67+
ver.minor < minor
68+
} else if let Some(patch) = cmp.patch.filter(|&patch| ver.patch != patch) {
69+
ver.patch < patch
70+
} else {
71+
ver.pre < cmp.pre
9372
}
94-
95-
match cmp.minor {
96-
None => return false,
97-
Some(minor) => {
98-
if ver.minor != minor {
99-
return ver.minor < minor;
100-
}
101-
}
102-
}
103-
104-
match cmp.patch {
105-
None => return false,
106-
Some(patch) => {
107-
if ver.patch != patch {
108-
return ver.patch < patch;
109-
}
110-
}
111-
}
112-
113-
ver.pre < cmp.pre
11473
}
11574

11675
fn matches_tilde(cmp: &Comparator, ver: &Version) -> bool {
117-
if ver.major != cmp.major {
118-
return false;
119-
}
120-
121-
if let Some(minor) = cmp.minor {
122-
if ver.minor != minor {
123-
return false;
124-
}
125-
}
126-
127-
if let Some(patch) = cmp.patch {
128-
if ver.patch != patch {
129-
return ver.patch > patch;
130-
}
76+
if !ver.pre.is_empty() || !cmp.pre.is_empty() {
77+
matches_exact(cmp, ver)
78+
} else if ver.major != cmp.major {
79+
false
80+
} else if cmp.minor.map_or(false, |minor| ver.minor != minor) {
81+
false
82+
} else if let Some(patch) = cmp.patch.filter(|&patch| ver.patch != patch) {
83+
ver.patch > patch
84+
} else {
85+
true
13186
}
132-
133-
ver.pre >= cmp.pre
13487
}
13588

13689
fn matches_caret(cmp: &Comparator, ver: &Version) -> bool {
137-
if ver.major != cmp.major {
138-
return false;
139-
}
140-
141-
let minor = match cmp.minor {
142-
None => return true,
143-
Some(minor) => minor,
144-
};
145-
146-
let patch = match cmp.patch {
147-
None => {
148-
if cmp.major > 0 {
149-
return ver.minor >= minor;
150-
} else {
151-
return ver.minor == minor;
152-
}
90+
if !ver.pre.is_empty() || !cmp.pre.is_empty() {
91+
matches_exact(cmp, ver)
92+
} else if ver.major != cmp.major {
93+
false
94+
} else if let Some(minor) = cmp.minor.filter(|&minor| ver.minor != minor) {
95+
// if major is 0 than minor is considered as major
96+
if cmp.major == 0 {
97+
false
98+
} else {
99+
ver.minor > minor
153100
}
154-
Some(patch) => patch,
155-
};
156-
157-
if cmp.major > 0 {
158-
if ver.minor != minor {
159-
return ver.minor > minor;
160-
} else if ver.patch != patch {
161-
return ver.patch > patch;
162-
}
163-
} else if minor > 0 {
164-
if ver.minor != minor {
165-
return false;
166-
} else if ver.patch != patch {
167-
return ver.patch > patch;
168-
}
169-
} else if ver.minor != minor || ver.patch != patch {
170-
return false;
101+
} else if let Some(patch) = cmp.patch.filter(|&patch| ver.patch != patch) {
102+
ver.patch > patch
103+
} else {
104+
true
171105
}
172-
173-
ver.pre >= cmp.pre
174106
}
175107

176108
fn pre_is_compatible(cmp: &Comparator, ver: &Version) -> bool {

tests/test_version_req.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ use semver::VersionReq;
2121
fn assert_match_all(req: &VersionReq, versions: &[&str]) {
2222
for string in versions {
2323
let parsed = version(string);
24-
assert!(req.matches(&parsed), "did not match {}", string);
24+
assert!(req.matches(&parsed), "{} did not match {}", req, string);
2525
}
2626
}
2727

2828
#[cfg_attr(not(no_track_caller), track_caller)]
2929
fn assert_match_none(req: &VersionReq, versions: &[&str]) {
3030
for string in versions {
3131
let parsed = version(string);
32-
assert!(!req.matches(&parsed), "matched {}", string);
32+
assert!(!req.matches(&parsed), "{} matched {}", req, string);
3333
}
3434
}
3535

@@ -105,14 +105,18 @@ pub fn test_less_than() {
105105
&["2.1.0", "2.2.0-alpha1", "2.0.0-alpha2", "1.0.0-alpha2"],
106106
);
107107

108+
let ref r = req("<1.0");
109+
assert_match_none(r, &["1.0.0-beta"]);
110+
111+
// All the following should probably be in test_multiple
108112
let ref r = req(">1.0.0-alpha, <1.0.0");
109113
assert_match_all(r, &["1.0.0-beta"]);
110114

111115
let ref r = req(">1.0.0-alpha, <1.0");
112-
assert_match_none(r, &["1.0.0-beta"]);
116+
assert_match_all(r, &["1.0.0-beta"]);
113117

114118
let ref r = req(">1.0.0-alpha, <1");
115-
assert_match_none(r, &["1.0.0-beta"]);
119+
assert_match_all(r, &["1.0.0-beta"]);
116120
}
117121

118122
#[test]
@@ -192,8 +196,19 @@ pub fn test_tilde() {
192196
assert_match_none(r, &["1.2.1", "1.9.0", "1.0.9", "2.0.1", "0.1.3"]);
193197

194198
let ref r = req("~1.2.3-beta.2");
195-
assert_match_all(r, &["1.2.3", "1.2.4", "1.2.3-beta.2", "1.2.3-beta.4"]);
196-
assert_match_none(r, &["1.3.3", "1.1.4", "1.2.3-beta.1", "1.2.4-beta.2"]);
199+
assert_match_all(r, &["1.2.3-beta.2"]);
200+
assert_match_none(
201+
r,
202+
&[
203+
"1.1.4",
204+
"1.2.3",
205+
"1.2.4",
206+
"1.2.3-beta.1",
207+
"1.2.3-beta.4",
208+
"1.2.4-beta.2",
209+
"1.3.3",
210+
],
211+
);
197212
}
198213

199214
#[test]
@@ -218,21 +233,16 @@ pub fn test_caret() {
218233
assert_match_none(r, &["0.1.2-beta", "0.1.3-alpha", "0.2.0-pre"]);
219234

220235
let ref r = req("^0.5.1-alpha3");
221-
assert_match_all(
236+
assert_match_all(r, &["0.5.1-alpha3"]);
237+
assert_match_none(
222238
r,
223239
&[
224-
"0.5.1-alpha3",
240+
"0.5.1-alpha1",
241+
"0.5.2-alpha3",
225242
"0.5.1-alpha4",
226243
"0.5.1-beta",
227244
"0.5.1",
228245
"0.5.5",
229-
],
230-
);
231-
assert_match_none(
232-
r,
233-
&[
234-
"0.5.1-alpha1",
235-
"0.5.2-alpha3",
236246
"0.5.5-pre",
237247
"0.5.0-pre",
238248
"0.6.0",
@@ -252,18 +262,19 @@ pub fn test_caret() {
252262
assert_match_none(r, &["2.9.0", "1.1.1"]);
253263

254264
let ref r = req("^1.4.2-beta.5");
255-
assert_match_all(
256-
r,
257-
&["1.4.2", "1.4.3", "1.4.2-beta.5", "1.4.2-beta.6", "1.4.2-c"],
258-
);
265+
assert_match_all(r, &["1.4.2-beta.5"]);
259266
assert_match_none(
260267
r,
261268
&[
262269
"0.9.9",
263-
"2.0.0",
270+
"1.4.2",
271+
"1.4.3",
264272
"1.4.2-alpha",
265273
"1.4.2-beta.4",
274+
"1.4.2-beta.6",
275+
"1.4.2-c",
266276
"1.4.3-beta.5",
277+
"2.0.0",
267278
],
268279
);
269280
}

0 commit comments

Comments
 (0)