|
| 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 | + |
| 25 | +import json |
| 26 | +import os |
| 27 | + |
| 28 | +import pytest |
| 29 | + |
| 30 | +from packageurl import PackageURL |
| 31 | + |
| 32 | +current_dir = os.path.dirname(__file__) |
| 33 | +root_dir = os.path.abspath(os.path.join(current_dir, "..")) |
| 34 | +spec_file_path = os.path.join(root_dir, "spec", "tests", "spec", "specification-test.json") |
| 35 | + |
| 36 | +with open(spec_file_path, "r", encoding="utf-8") as f: |
| 37 | + test_cases = json.load(f) |
| 38 | + |
| 39 | +tests = test_cases["tests"] |
| 40 | + |
| 41 | +parse_tests = [t for t in tests if t["test_type"] == "parse"] |
| 42 | +build_tests = [t for t in tests if t["test_type"] == "build"] |
| 43 | + |
| 44 | + |
| 45 | +def load_spec_files(spec_dir): |
| 46 | + """ |
| 47 | + Load all JSON files from the given directory into a dictionary. |
| 48 | + Key = filename, Value = parsed JSON content |
| 49 | + """ |
| 50 | + spec_data = {} |
| 51 | + for filename in os.listdir(spec_dir): |
| 52 | + if filename.endswith("-test.json"): |
| 53 | + filepath = os.path.join(spec_dir, filename) |
| 54 | + with open(filepath, "r", encoding="utf-8") as f: |
| 55 | + try: |
| 56 | + data = json.load(f) |
| 57 | + spec_data[filename] = data["tests"] |
| 58 | + except json.JSONDecodeError as e: |
| 59 | + print(f"Error parsing {filename}: {e}") |
| 60 | + return spec_data |
| 61 | + |
| 62 | + |
| 63 | +SPEC_DIR = os.path.join(os.path.dirname(__file__), "..", "spec", "tests", "types") |
| 64 | +spec_dict = load_spec_files(SPEC_DIR) |
| 65 | + |
| 66 | +flattened_cases = [] |
| 67 | +for filename, cases in spec_dict.items(): |
| 68 | + for case in cases: |
| 69 | + flattened_cases.append((filename, case["description"], case)) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + "description, input_str, expected_output, expected_failure", |
| 74 | + [ |
| 75 | + (t["description"], t["input"], t["expected_output"], t["expected_failure"]) |
| 76 | + for t in parse_tests |
| 77 | + ], |
| 78 | +) |
| 79 | +def test_parse(description, input_str, expected_output, expected_failure): |
| 80 | + if expected_failure: |
| 81 | + with pytest.raises(Exception): |
| 82 | + PackageURL.from_string(input_str) |
| 83 | + else: |
| 84 | + result = PackageURL.from_string(input_str) |
| 85 | + assert result.to_string() == expected_output |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize( |
| 89 | + "description, input_dict, expected_output, expected_failure", |
| 90 | + [ |
| 91 | + (t["description"], t["input"], t["expected_output"], t["expected_failure"]) |
| 92 | + for t in build_tests |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_build(description, input_dict, expected_output, expected_failure): |
| 96 | + kwargs = { |
| 97 | + "type": input_dict.get("type"), |
| 98 | + "namespace": input_dict.get("namespace"), |
| 99 | + "name": input_dict.get("name"), |
| 100 | + "version": input_dict.get("version"), |
| 101 | + "qualifiers": input_dict.get("qualifiers"), |
| 102 | + "subpath": input_dict.get("subpath"), |
| 103 | + } |
| 104 | + |
| 105 | + if expected_failure: |
| 106 | + with pytest.raises(Exception): |
| 107 | + PackageURL(**kwargs).to_string() |
| 108 | + else: |
| 109 | + purl = PackageURL(**kwargs) |
| 110 | + assert purl.to_string() == expected_output |
| 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 | + if expected["qualifiers"]: |
| 134 | + assert purl.qualifiers == expected["qualifiers"] |
| 135 | + else: |
| 136 | + assert not purl.qualifiers |
| 137 | + assert purl.subpath == expected["subpath"] |
| 138 | + |
| 139 | + elif test_type == "roundtrip": |
| 140 | + purl = PackageURL.from_string(case["input"]) |
| 141 | + assert purl.to_string() == case["expected_output"] |
| 142 | + |
| 143 | + elif test_type == "build": |
| 144 | + input_data = case["input"] |
| 145 | + purl = PackageURL( |
| 146 | + type=input_data["type"], |
| 147 | + namespace=input_data["namespace"], |
| 148 | + name=input_data["name"], |
| 149 | + version=input_data["version"], |
| 150 | + qualifiers=input_data.get("qualifiers"), |
| 151 | + subpath=input_data.get("subpath"), |
| 152 | + ) |
| 153 | + assert purl.to_string() == case["expected_output"] |
0 commit comments