Skip to content

Commit 951c137

Browse files
authored
Remove unsupported characters in Pypi from_native implementation (#57)
* Reference: #48 Remove unsupported characters in Pypi from_native implementation Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent e400e23 commit 951c137

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

src/univers/version_range.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,9 @@ def from_native(cls, string):
540540
if ";" in string:
541541
raise InvalidVersionRange(f"Unsupported PyPI environment marker: {string!r}")
542542

543-
unsupported_chars = ";<>!=\\/|{}()`?'\"\t\n "
543+
unsupported_chars = ";\\/|{}()`?'\"\t\n "
544+
string = "".join(string.split(" "))
545+
544546
if any(c in string for c in unsupported_chars):
545547
raise InvalidVersionRange(
546548
f"Unsupported character: {unsupported_chars!r} " f"in PyPI version: {string!r}"

tests/test_version_range.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,6 @@ def test_VersionRange_contains_works_for_star_range(self):
197197

198198
assert SemverVersion("1.0.0") in VersionRange.from_string("vers:nginx/*")
199199

200-
def test_PypiVersionRange_raises_ivr_for_unsupported_ranges(self):
201-
try:
202-
PypiVersionRange.from_native(
203-
"~= 0.9, >= 1.0, != 1.3.4.*, < 2.0, ~= 1.3.4.*, ===1.0, ==1.*"
204-
)
205-
raise Exception("Exception not raised")
206-
except InvalidVersionRange as ivre:
207-
assert str(ivre).startswith("Unsupported character")
208-
209-
def test_PypiVersionRange_raises_ivr_for_invalid_ranges(self):
210-
try:
211-
PypiVersionRange.from_native("~= 1.3, ===1.0, ==1.*")
212-
raise Exception("Exception not raised")
213-
except InvalidVersionRange as ivre:
214-
assert str(ivre).startswith("Unsupported character")
215-
216200
def test_NpmVersionRange_from_native_with_compatible_with_version_operator(self):
217201
npm_range = "^1.2.9"
218202
expected = NpmVersionRange(
@@ -283,6 +267,7 @@ def test_OpensslVersionRange_from_versions(self):
283267
"nginx": ["0.8.40+", "0.7.52-0.8.39", "0.9.10", "1.5.0+, 1.4.1+"],
284268
"npm": ["^1.2.9", "~3.8.2", "5.0.0 - 7.2.3", "2.1 || 2.6", "1.1.2 1.2.2", "<=2.1 >=1.1"],
285269
"openssl": ["1.1.1ak", "1.1.0", "3.0.2", "3.0.1, 0.9.7a", "1.0.2ck, 3.1.2"],
270+
"pypi": [">= 1.0", "<2.1.0", "!=5"],
286271
}
287272

288273

@@ -306,3 +291,28 @@ def test_from_native_and_from_string_round_trip(scheme, native_ranges):
306291
from_native = range_class.from_native(rng)
307292
from_string = range_class.from_string(from_native.to_string())
308293
assert from_native == from_string
294+
295+
296+
@pytest.mark.parametrize(
297+
"range, will_pass, expected",
298+
[
299+
(" ~= 0.9", False, "Unsupported PyPI version constraint operator"),
300+
("~= 1.3", False, "Unsupported PyPI version constraint operator"),
301+
(" >= 1.0", True, "vers:pypi/>=1.0"),
302+
(" != 1.3.4.*", False, "Unsupported PyPI version"),
303+
("< 2.0", True, "vers:pypi/<2.0"),
304+
("~= 1.3.4.*", False, ""),
305+
("==1. *", False, "Unsupported PyPI version"),
306+
("==1.3.4 ) (", False, "Unsupported character"),
307+
("===1.0", False, "Unsupported PyPI version"),
308+
],
309+
)
310+
def test_PypiVersionRange_raises_ivr_for_unsupported_and_invalid_ranges(range, will_pass, expected):
311+
if not will_pass:
312+
try:
313+
PypiVersionRange.from_native(range)
314+
raise Exception("Exception not raised")
315+
except InvalidVersionRange as ivre:
316+
assert expected in str(ivre)
317+
else:
318+
assert expected == str(PypiVersionRange.from_native(range))

0 commit comments

Comments
 (0)