Skip to content

Commit 83c1a6a

Browse files
committed
Add tests for spec parsing
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent b8a6d84 commit 83c1a6a

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/packageurl/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ def from_string(cls, purl: str) -> Self:
463463
type_, sep, remainder = remainder.partition("/")
464464
if not type_ or not sep:
465465
raise ValueError(f"purl is missing the required type component: {purl!r}.")
466+
467+
# check if type starts with a number
468+
if type_[0] in string.digits:
469+
raise ValueError(f"purl type cannot start with a number: {type_!r}.")
466470

467471
type_ = type_.lower()
468472

tests/test_purl_spec.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import json
2+
import os
3+
4+
import pytest
5+
6+
from packageurl import PackageURL
7+
8+
current_dir = os.path.dirname(__file__)
9+
root_dir = os.path.abspath(os.path.join(current_dir, ".."))
10+
spec_file_path = os.path.join(root_dir, "spec", "tests", "spec", "specification-test.json")
11+
12+
valid_purl_types_file = os.path.join(root_dir, "spec", "purl-types-index.json")
13+
14+
15+
with open(spec_file_path, "r", encoding="utf-8") as f:
16+
test_cases = json.load(f)
17+
18+
with open(valid_purl_types_file, "r", encoding="utf-8") as f:
19+
valid_purl_types = json.load(f)
20+
21+
tests = test_cases["tests"]
22+
23+
parse_tests = [t for t in tests if t["test_type"] == "parse"]
24+
build_tests = [t for t in tests if t["test_type"] == "build"]
25+
26+
@pytest.mark.parametrize("description, input_str, expected_output, expected_failure", [
27+
(t["description"], t["input"], t["expected_output"], t["expected_failure"])
28+
for t in parse_tests
29+
])
30+
def test_parse(description, input_str, expected_output, expected_failure):
31+
if expected_failure:
32+
with pytest.raises(Exception):
33+
PackageURL.from_string(input_str)
34+
else:
35+
result = PackageURL.from_string(input_str)
36+
assert result.to_string() == expected_output
37+
38+
39+
@pytest.mark.parametrize("description, input_dict, expected_output, expected_failure", [
40+
(t["description"], t["input"], t["expected_output"], t["expected_failure"])
41+
for t in build_tests
42+
])
43+
def test_build(description, input_dict, expected_output, expected_failure):
44+
kwargs = {
45+
"type": input_dict.get("type"),
46+
"namespace": input_dict.get("namespace"),
47+
"name": input_dict.get("name"),
48+
"version": input_dict.get("version"),
49+
"qualifiers": input_dict.get("qualifiers"),
50+
"subpath": input_dict.get("subpath"),
51+
}
52+
53+
if expected_failure:
54+
with pytest.raises(Exception):
55+
PackageURL(**kwargs).to_string()
56+
else:
57+
purl = PackageURL(**kwargs)
58+
assert purl.to_string() == expected_output

0 commit comments

Comments
 (0)