|
| 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 | + |
1 | 25 | import json |
2 | 26 | import os |
3 | 27 |
|
@@ -57,3 +81,70 @@ def test_build(description, input_dict, expected_output, expected_failure): |
57 | 81 | else: |
58 | 82 | purl = PackageURL(**kwargs) |
59 | 83 | 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