Skip to content

Commit 61e4bd5

Browse files
committed
Add tests for purl types
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent b3ca8b3 commit 61e4bd5

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

src/packageurl/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def normalize_qualifiers(
178178
Raise ValueError on errors.
179179
"""
180180
if not qualifiers:
181-
return None if encode else {}
181+
return None
182182

183183
if isinstance(qualifiers, basestring):
184184
qualifiers_str = qualifiers if isinstance(qualifiers, str) else qualifiers.decode("utf-8")

tests/test_purl_spec.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
# Copyright (c) the purl authors
2+
# SPDX-License-Identifier: MIT
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in all
12+
# copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
# SOFTWARE.
21+
22+
# Visit https://github.com/package-url/packageurl-python for support and
23+
# download.
24+
125
import json
226
import os
327

@@ -57,3 +81,70 @@ def test_build(description, input_dict, expected_output, expected_failure):
5781
else:
5882
purl = PackageURL(**kwargs)
5983
assert purl.to_string() == expected_output
84+
85+
86+
def load_spec_files(spec_dir):
87+
"""
88+
Load all JSON files from the given directory into a dictionary.
89+
Key = filename, Value = parsed JSON content
90+
"""
91+
spec_data = {}
92+
for filename in os.listdir(spec_dir):
93+
if filename.endswith("-test.json"):
94+
filepath = os.path.join(spec_dir, filename)
95+
with open(filepath, 'r', encoding='utf-8') as f:
96+
try:
97+
data = json.load(f)
98+
spec_data[filename] = data["tests"]
99+
except json.JSONDecodeError as e:
100+
print(f"Error parsing {filename}: {e}")
101+
return spec_data
102+
103+
104+
SPEC_DIR = os.path.join(os.path.dirname(__file__), '..', 'spec', 'tests', 'types')
105+
spec_dict = load_spec_files(SPEC_DIR)
106+
107+
flattened_cases = []
108+
for filename, cases in spec_dict.items():
109+
for case in cases:
110+
flattened_cases.append((filename, case["description"], case))
111+
112+
113+
@pytest.mark.parametrize("filename,description,test_case", flattened_cases)
114+
def test_package_type_case(filename, description, test_case):
115+
test_type = test_case["test_type"]
116+
expected_failure = test_case.get("expected_failure", False)
117+
118+
if expected_failure:
119+
with pytest.raises(Exception):
120+
run_test_case(test_case, test_type, description)
121+
else:
122+
run_test_case(test_case, test_type, description)
123+
124+
125+
def run_test_case(case, test_type, desc):
126+
if test_type == "parse":
127+
purl = PackageURL.from_string(case["input"])
128+
expected = case["expected_output"]
129+
assert purl.type == expected["type"]
130+
assert purl.namespace == expected["namespace"]
131+
assert purl.name == expected["name"]
132+
assert purl.version == expected["version"]
133+
assert purl.qualifiers == expected["qualifiers"]
134+
assert purl.subpath == expected["subpath"]
135+
136+
elif test_type == "roundtrip":
137+
purl = PackageURL.from_string(case["input"])
138+
assert purl.to_string() == case["expected_output"]
139+
140+
elif test_type == "build":
141+
input_data = case["input"]
142+
purl = PackageURL(
143+
type=input_data["type"],
144+
namespace=input_data["namespace"],
145+
name=input_data["name"],
146+
version=input_data["version"],
147+
qualifiers=input_data.get("qualifiers"),
148+
subpath=input_data.get("subpath"),
149+
)
150+
assert purl.to_string() == case["expected_output"]

0 commit comments

Comments
 (0)