Skip to content

Commit

Permalink
Merge pull request #120 from nexB/117-fix-npm-caret-range
Browse files Browse the repository at this point in the history
Handle npm prerelease caret range expression
  • Loading branch information
keshav-space authored Sep 6, 2023
2 parents 0113d67 + da287b1 commit f28c4b0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
25 changes: 20 additions & 5 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,26 @@ def from_native(cls, string):
VersionConstraint(comparator=comparator, version=vrc(constraint))
)
else:
if (
constraint.endswith(".x")
or constraint.startswith("~")
or constraint.startswith("^")
):
# Handle caret range expression.
if constraint.startswith("^"):
base_version = vrc(constraint.lstrip("^"))
prerelease = base_version.value.prerelease
base_version.value.prerelease = ()
if base_version.major:
high = base_version.next_major()
elif base_version.minor:
high = base_version.next_minor()
else:
high = base_version.next_patch()
base_version.value.prerelease = prerelease
lower = base_version
constraints.extend(
[
VersionConstraint(comparator=">=", version=lower),
VersionConstraint(comparator="<", version=high),
]
)
elif constraint.endswith(".x") or constraint.startswith("~"):
constraints.extend(
get_npm_version_constraints_from_semver_npm_spec(
string=constraint, cls=cls
Expand Down
2 changes: 1 addition & 1 deletion tests/data/npm_advisory.json
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@
"test_index": 308,
"scheme": "npm",
"npm_native": "^2.0.18 || ^3.0.16 || ^3.1.6 || ^4.0.8 || ^5.0.0-beta.5",
"expected_vers": "vers:npm/>=2.0.18|<3.0.0|>=3.0.16|>=3.1.6|<4.0.0|<4.0.0|>=4.0.8|>=5.0.0-beta.5|<5.0.0|>=5.0.0|<5.0.1|<6.0.0"
"expected_vers": "vers:npm/>=2.0.18|<3.0.0|>=3.0.16|>=3.1.6|<4.0.0|<4.0.0|>=4.0.8|>=5.0.0-beta.5|<5.0.0|<6.0.0"
},
{
"test_index": 309,
Expand Down
33 changes: 33 additions & 0 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,39 @@ def test_NpmVersionRange_from_native_with_compatible_with_version_operator(self)
version_range = NpmVersionRange.from_native(npm_range)
assert version_range == expected

def test_NpmVersionRange_from_native_with_prerelease_carate_range(self):
npm_range = "^1.2.3-beta.1"
expected = NpmVersionRange(
constraints=(
VersionConstraint(comparator=">=", version=SemverVersion(string="1.2.3-beta.1")),
VersionConstraint(comparator="<", version=SemverVersion(string="2.0.0")),
)
)
version_range = NpmVersionRange.from_native(npm_range)
assert version_range == expected

def test_NpmVersionRange_from_native_with_prerelease_carate_range_wihtout_major(self):
npm_range = "^0.2.1-beta"
expected = NpmVersionRange(
constraints=(
VersionConstraint(comparator=">=", version=SemverVersion(string="0.2.1-beta")),
VersionConstraint(comparator="<", version=SemverVersion(string="0.3.0")),
)
)
version_range = NpmVersionRange.from_native(npm_range)
assert version_range == expected

def test_NpmVersionRange_from_native_with_prerelease_carate_range_wihtout_major_and_minor(self):
npm_range = "^0.0.2-beta"
expected = NpmVersionRange(
constraints=(
VersionConstraint(comparator=">=", version=SemverVersion(string="0.0.2-beta")),
VersionConstraint(comparator="<", version=SemverVersion(string="0.0.3")),
)
)
version_range = NpmVersionRange.from_native(npm_range)
assert version_range == expected

def test_NpmVersionRange_from_native_with_approximately_equal_to_operator(self):
npm_range = "~3.8.2"
expected = NpmVersionRange(
Expand Down

0 comments on commit f28c4b0

Please sign in to comment.