generated from aboutcode-org/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for CargoVersion and CargoVersionRange
Signed-off-by: ziadhany <[email protected]>
- Loading branch information
Showing
4 changed files
with
307 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import pytest | ||
|
||
from univers.versions import CargoVersion | ||
|
||
|
||
def test_compare(): | ||
""" | ||
1.2.3 := >=1.2.3, <2.0.0 | ||
1.2 := >=1.2.0, <2.0.0 | ||
1 := >=1.0.0, <2.0.0 | ||
0.2.3 := >=0.2.3, <0.3.0 | ||
0.2 := >=0.2.0, <0.3.0 | ||
0.0.3 := >=0.0.3, <0.0.4 | ||
0.0 := >=0.0.0, <0.1.0 | ||
0 := >=0.0.0, <1.0.0 | ||
""" | ||
|
||
assert CargoVersion("1.2.3") >= CargoVersion("1.2.3") | ||
assert CargoVersion("1.2.3") < CargoVersion("2.0.0") | ||
|
||
assert CargoVersion("1.2") >= CargoVersion("1.2.0") | ||
assert CargoVersion("1.2") < CargoVersion("2.0.0") | ||
|
||
assert CargoVersion("1") >= CargoVersion("1.0.0") | ||
assert CargoVersion("1") < CargoVersion("2.0.0") | ||
|
||
assert CargoVersion("0.2.3") >= CargoVersion("0.2.3") | ||
assert CargoVersion("0.2.3") < CargoVersion("0.3.0") | ||
|
||
assert CargoVersion("0.2") >= CargoVersion("0.2.0") | ||
assert CargoVersion("0.2") < CargoVersion("0.3.0") | ||
|
||
assert CargoVersion("0.0.3") >= CargoVersion("0.0.3") | ||
assert CargoVersion("0.0.3") < CargoVersion("0.0.4") | ||
|
||
assert CargoVersion("0.0") >= CargoVersion("0.0.0") | ||
assert CargoVersion("0.0") < CargoVersion("0.1.0") | ||
|
||
assert CargoVersion("0") >= CargoVersion("0.0.0") | ||
assert CargoVersion("0") < CargoVersion("1.0.0") | ||
|
||
|
||
version_list = [ | ||
("1.2.3", 1, 2, 3, (), ()), | ||
("1.2.3-alpha1", 1, 2, 3, ("alpha1",), ()), | ||
("1.2.3+build5", 1, 2, 3, (), ("build5",)), | ||
("1.2.3+5build", 1, 2, 3, (), ("5build",)), | ||
("1.2.3-alpha1+build5", 1, 2, 3, ("alpha1",), ("build5",)), | ||
( | ||
"1.2.3-1.alpha1.9+build5.7.3aedf", | ||
1, | ||
2, | ||
3, | ||
( | ||
"1", | ||
"alpha1", | ||
"9", | ||
), | ||
( | ||
"build5", | ||
"7", | ||
"3aedf", | ||
), | ||
), | ||
( | ||
"1.2.3-0a.alpha1.9+05build.7.3aedf", | ||
1, | ||
2, | ||
3, | ||
( | ||
"0a", | ||
"alpha1", | ||
"9", | ||
), | ||
( | ||
"05build", | ||
"7", | ||
"3aedf", | ||
), | ||
), | ||
( | ||
"0.4.0-beta.1+0851523", | ||
0, | ||
4, | ||
0, | ||
( | ||
"beta", | ||
"1", | ||
), | ||
("0851523",), | ||
), | ||
("1.1.0-beta-10", 1, 1, 0, ("beta-10",), ()), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"version, expected_major, expected_minor, expected_patch, expected_prerelease, expected_build", | ||
version_list, | ||
) | ||
def test_cargo( | ||
version, expected_major, expected_minor, expected_patch, expected_prerelease, expected_build | ||
): | ||
# https://github.com/dtolnay/semver/blob/master/tests/test_version.rs : | ||
v1 = CargoVersion(version) | ||
assert v1.major == expected_major | ||
assert v1.minor == expected_minor | ||
assert v1.patch == expected_patch | ||
assert v1.prerelease == expected_prerelease | ||
assert v1.build == expected_build | ||
|
||
|
||
def test_cargo1(): | ||
assert CargoVersion("1.2.3") == CargoVersion("1.2.3") | ||
assert CargoVersion("1.2.3-alpha1") == CargoVersion("1.2.3-alpha1") | ||
assert CargoVersion("1.2.3+build.42") == CargoVersion("1.2.3+build.42") | ||
assert CargoVersion("1.2.3-alpha1+42") == CargoVersion("1.2.3-alpha1+42") | ||
assert CargoVersion("0.0.0") != CargoVersion("0.0.1") | ||
assert CargoVersion("0.0.0") != CargoVersion("0.1.0") | ||
assert CargoVersion("0.0.0") != CargoVersion("1.0.0") | ||
assert CargoVersion("1.2.3-alpha") != CargoVersion("1.2.3-beta") | ||
assert CargoVersion("1.2.3+23") != CargoVersion("1.2.3+42") | ||
|
||
assert CargoVersion("0.0.0") < CargoVersion("1.2.3-alpha2") | ||
assert CargoVersion("1.0.0") < CargoVersion("1.2.3-alpha2") | ||
assert CargoVersion("1.2.0") < CargoVersion("1.2.3-alpha2") | ||
assert CargoVersion("1.2.3-alpha1") < CargoVersion("1.2.3") | ||
assert CargoVersion("1.2.3-alpha1") < CargoVersion("1.2.3-alpha2") | ||
assert not (CargoVersion("1.2.3-alpha2") < CargoVersion("1.2.3-alpha2")) | ||
assert CargoVersion("1.2.3+23") < CargoVersion("1.2.3+42") | ||
|
||
assert CargoVersion("0.0.0") <= CargoVersion("1.2.3-alpha2") | ||
assert CargoVersion("1.0.0") <= CargoVersion("1.2.3-alpha2") | ||
assert CargoVersion("1.2.0") <= CargoVersion("1.2.3-alpha2") | ||
assert CargoVersion("1.2.3-alpha1") <= CargoVersion("1.2.3-alpha2") | ||
assert CargoVersion("1.2.3-alpha2") <= CargoVersion("1.2.3-alpha2") | ||
assert CargoVersion("1.2.3+23") <= CargoVersion("1.2.3+42") | ||
|
||
assert CargoVersion("1.2.3-alpha2") > CargoVersion("0.0.0") | ||
assert CargoVersion("1.2.3-alpha2") > CargoVersion("1.0.0") | ||
assert CargoVersion("1.2.3-alpha2") > CargoVersion("1.2.0") | ||
assert CargoVersion("1.2.3-alpha2") > CargoVersion("1.2.3-alpha1") | ||
assert CargoVersion("1.2.3") > CargoVersion("1.2.3-alpha2") | ||
assert not (CargoVersion("1.2.3-alpha2") > CargoVersion("1.2.3-alpha2")) | ||
assert not (CargoVersion("1.2.3+23") > CargoVersion("1.2.3+42")) | ||
|
||
assert CargoVersion("1.2.3-alpha2") >= CargoVersion("0.0.0") | ||
assert CargoVersion("1.2.3-alpha2") >= CargoVersion("1.0.0") | ||
assert CargoVersion("1.2.3-alpha2") >= CargoVersion("1.2.0") | ||
assert CargoVersion("1.2.3-alpha2") >= CargoVersion("1.2.3-alpha1") | ||
assert CargoVersion("1.2.3-alpha2") >= CargoVersion("1.2.3-alpha2") | ||
assert not (CargoVersion("1.2.3+23") >= CargoVersion("1.2.3+42")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pytest | ||
|
||
from univers.version_range import CargoVersionRange | ||
from univers.version_range import InvalidVersionRange | ||
from univers.versions import CargoVersion | ||
|
||
values = [ | ||
# https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html | ||
# caret | ||
["^1.2.3", [[["=", "1.2.3"]]], ["1.2.3"], ["1.2.4"]], | ||
# tilde | ||
["~1.2.3", [[[">=", "1.2.3"], ["<", "1.3.0"]]], ["1.2.4"], ["2.0.1"]], | ||
["~1.2", [[[">=", "1.2.0"], ["<", "1.3.0"]]], ["1.2.5"], ["1.3.1"]], | ||
[ | ||
"~1", | ||
[[[">=", "1.0.0"], ["<", "2.0.0"]]], | ||
["1.3.0", "1.8.1"], | ||
["2.1.0", "2.2"], | ||
], # tilde increment the major | ||
# wildcard | ||
["*", [[[">=", "0.0.0"]]], ["1.0.0", "2.0.0"], []], | ||
# ["1.*", [[[">=", "1.0.0"]]], ["1.0.0"], ["2", "1.0.1"]], | ||
# ["1.2.*", [[[">=", "1.2.0"], ["<", "1.3.0"]]], ["1.2", "1.2.1"], ["2.1.0", "2.2"]], | ||
# https://github.com/dtolnay/semver/blob/master/tests/test_version_req.rs : | ||
["=1.0.0", [["=", "1.0.0"]], ["1.0.0"], ["1.0.1", "0.9.9", "0.10.0", "0.1.0", "1.0.0-pre"]], | ||
["=0.9.0", [["=", "0.9.0"]], ["0.9.0"], ["0.9.1", "1.9.0", "0.0.9", "0.9.0-pre"]], | ||
["=0.0.2", [["=", "0.0.2"]], ["0.0.2"], ["0.0.1", "0.0.3", "0.0.2-pre"]], | ||
[ | ||
"=0.1.0-beta2.a", | ||
[["=", "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"], | ||
], | ||
# ["=0.1.0+meta", [["=", "0.1.0+meta"]], ["0.1.0", "0.1.0+meta", "0.1.0+any"], []], | ||
# ["<1.0.0", [["<", "1.0.0"]], ["0.1.0", "0.0.1"], ["1.0.0", "1.0.0-beta", "1.0.1", "0.9.9-alpha"]], | ||
# [ | ||
# "<= 2.1.0-alpha2", | ||
# [["<", "2.1.0-alpha2"], ["=", "2.1.0-alpha2"]], | ||
# ["2.1.0-alpha2", "2.1.0-alpha1", "2.0.0", "1.0.0"], | ||
# ["2.1.0", "2.2.0-alpha1", "2.0.0-alpha2", "1.0.0-alpha2"], | ||
# ], | ||
# [">1.0.0-alpha, <1.0.0", [[[">", "2.1.0-alpha2"], ["<", "1.0.0"]]], ["1.0.0-beta"], []], | ||
# [">1.0.0-alpha, <1.0", [[[">", "1.0.0-alpha"], ["<", "1.0"]]], ["1.0.0-beta"], []], | ||
# [">1.0.0-alpha, <1", [[[">", "1.0.0-alpha"], ["<", "1"]]], ["1.0.0-beta"], []], | ||
# [ | ||
# ">=0.5.1-alpha3, <0.6", | ||
# [[[">", "0.5.1-alpha3"], ["=", "0.5.1-alpha3"], ["<", "0.6"]]], | ||
# ["0.5.1-alpha3", "0.5.1-alpha4", "0.5.1-beta", "0.5.1", "0.5.5"], | ||
# ["0.5.1-alpha1", "0.5.2-alpha3", "0.5.5-pre", "0.5.0-pre"], | ||
# ], | ||
["~1", [], ["1.0.0", "1.0.1", "1.1.1"], ["0.9.1", "2.9.0", "0.0.9"]], | ||
["~1.2", [], ["1.2.0", "1.2.1"], ["1.1.1", "1.3.0", "0.0.9"]], | ||
["~1.2.2", [], ["1.2.2", "1.2.4"], ["1.2.1", "1.9.0", "1.0.9", "2.0.1", "0.1.3"]], | ||
# [ | ||
# "~1.2.3-beta.2", | ||
# [], | ||
# ["1.2.3", "1.2.4", "1.2.3-beta.2", "1.2.3-beta.4"], | ||
# ["1.3.3", "1.1.4", "1.2.3-beta.1", "1.2.4-beta.2"], | ||
# ], | ||
] | ||
|
||
error_list = [ | ||
"> 0.1.0,", | ||
"> 0.3.0, ,", | ||
# "1.2.3 - 2.3.4", | ||
# "> 0.0.9 <= 2.5.3", | ||
# "=1.2.3 || =2.3.4", | ||
# "1.1 || =1.2.3", | ||
# "6.* || 8.* || >= 10.*", | ||
# ">= >= 0.0.2", | ||
# ">== 0.0.2", | ||
# "a.0.0", | ||
# "1.0.0-", | ||
# ">=", | ||
# "*.1", | ||
# "1.*.1", | ||
# ">=1.*.1", | ||
# "*, 0.20.0-any", | ||
# "0.20.0-any, *" "0.20.0-any, *, 1.0", | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("version_range, conditions, versions_in, versions_out", values) | ||
def test_range(version_range, conditions, versions_in, versions_out): | ||
r = CargoVersionRange.from_native(version_range) | ||
# TODO test Version Constraints | ||
|
||
for v in versions_in: | ||
assert CargoVersion(v) in r | ||
|
||
for v in versions_out: | ||
assert CargoVersion(v) not in r | ||
|
||
|
||
def test_error(): | ||
for i in error_list: | ||
with pytest.raises(InvalidVersionRange): | ||
CargoVersionRange.from_native(i) |