|
| 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