-
Notifications
You must be signed in to change notification settings - Fork 54
Add tests for latest purl-spec #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4c237ce
Add purl-spec as submodule
TG1999 f1fecb3
Add submodule support in CI
TG1999 10b4df9
Ignore spec in black
TG1999 5f0924c
Update CI
TG1999 265754c
Update CI
TG1999 b8a6d84
Update CI
TG1999 83c1a6a
Add tests for spec parsing
TG1999 b3ca8b3
Fix tests
TG1999 61e4bd5
Add tests for purl types
TG1999 47dd757
Fix linting issues
TG1999 d563792
Update tests
TG1999 eafd33a
Change tests
TG1999 3b9c6df
Revert changes
TG1999 48dee0d
Add support for mlflow
TG1999 dc77d35
Fix colon parsing in purl parsing
TG1999 2efa591
Update spec commit
TG1999 c127000
Fix linting
TG1999 3a58ba5
Fix linting issues
TG1999 100b947
Address review comments
TG1999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [submodule "spec"] | ||
| path = spec | ||
| url = https://github.com/package-url/purl-spec.git | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| # Copyright (c) the purl authors | ||
| # SPDX-License-Identifier: MIT | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in all | ||
| # copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| # Visit https://github.com/package-url/packageurl-python for support and | ||
| # download. | ||
|
|
||
| import json | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from packageurl import PackageURL | ||
|
|
||
| current_dir = os.path.dirname(__file__) | ||
| root_dir = os.path.abspath(os.path.join(current_dir, "..")) | ||
| spec_file_path = os.path.join(root_dir, "spec", "tests", "spec", "specification-test.json") | ||
|
|
||
| with open(spec_file_path, "r", encoding="utf-8") as f: | ||
| test_cases = json.load(f) | ||
|
|
||
| tests = test_cases["tests"] | ||
|
|
||
| parse_tests = [t for t in tests if t["test_type"] == "parse"] | ||
| build_tests = [t for t in tests if t["test_type"] == "build"] | ||
|
|
||
|
|
||
| def load_spec_files(spec_dir): | ||
| """ | ||
| Load all JSON files from the given directory into a dictionary. | ||
| Key = filename, Value = parsed JSON content | ||
| """ | ||
| spec_data = {} | ||
| for filename in os.listdir(spec_dir): | ||
| if filename.endswith("-test.json"): | ||
| filepath = os.path.join(spec_dir, filename) | ||
| with open(filepath, "r", encoding="utf-8") as f: | ||
| try: | ||
| data = json.load(f) | ||
| spec_data[filename] = data["tests"] | ||
| except json.JSONDecodeError as e: | ||
| print(f"Error parsing {filename}: {e}") | ||
| return spec_data | ||
|
|
||
|
|
||
| SPEC_DIR = os.path.join(os.path.dirname(__file__), "..", "spec", "tests", "types") | ||
| spec_dict = load_spec_files(SPEC_DIR) | ||
|
|
||
| flattened_cases = [] | ||
| for filename, cases in spec_dict.items(): | ||
| for case in cases: | ||
| flattened_cases.append((filename, case["description"], case)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "description, input_str, expected_output, expected_failure", | ||
| [ | ||
| (t["description"], t["input"], t["expected_output"], t["expected_failure"]) | ||
| for t in parse_tests | ||
| ], | ||
| ) | ||
| def test_parse(description, input_str, expected_output, expected_failure): | ||
| if expected_failure: | ||
| with pytest.raises(Exception): | ||
| PackageURL.from_string(input_str) | ||
| else: | ||
| result = PackageURL.from_string(input_str) | ||
| assert result.to_string() == expected_output | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "description, input_dict, expected_output, expected_failure", | ||
| [ | ||
| (t["description"], t["input"], t["expected_output"], t["expected_failure"]) | ||
| for t in build_tests | ||
| ], | ||
| ) | ||
| def test_build(description, input_dict, expected_output, expected_failure): | ||
| kwargs = { | ||
| "type": input_dict.get("type"), | ||
| "namespace": input_dict.get("namespace"), | ||
| "name": input_dict.get("name"), | ||
| "version": input_dict.get("version"), | ||
| "qualifiers": input_dict.get("qualifiers"), | ||
| "subpath": input_dict.get("subpath"), | ||
| } | ||
|
|
||
| if expected_failure: | ||
| with pytest.raises(Exception): | ||
| PackageURL(**kwargs).to_string() | ||
| else: | ||
| purl = PackageURL(**kwargs) | ||
| assert purl.to_string() == expected_output | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("filename,description,test_case", flattened_cases) | ||
| def test_package_type_case(filename, description, test_case): | ||
| test_type = test_case["test_type"] | ||
| expected_failure = test_case.get("expected_failure", False) | ||
|
|
||
| if expected_failure: | ||
| with pytest.raises(Exception): | ||
| run_test_case(test_case, test_type, description) | ||
| else: | ||
| run_test_case(test_case, test_type, description) | ||
|
|
||
|
|
||
| def run_test_case(case, test_type, desc): | ||
| if test_type == "parse": | ||
| purl = PackageURL.from_string(case["input"]) | ||
| expected = case["expected_output"] | ||
| assert purl.type == expected["type"] | ||
| assert purl.namespace == expected["namespace"] | ||
| assert purl.name == expected["name"] | ||
| assert purl.version == expected["version"] | ||
| if expected["qualifiers"]: | ||
| assert purl.qualifiers == expected["qualifiers"] | ||
| else: | ||
| assert not purl.qualifiers | ||
| assert purl.subpath == expected["subpath"] | ||
|
|
||
| elif test_type == "roundtrip": | ||
| purl = PackageURL.from_string(case["input"]) | ||
| assert purl.to_string() == case["expected_output"] | ||
|
|
||
| elif test_type == "build": | ||
| input_data = case["input"] | ||
| purl = PackageURL( | ||
| type=input_data["type"], | ||
| namespace=input_data["namespace"], | ||
| name=input_data["name"], | ||
| version=input_data["version"], | ||
| qualifiers=input_data.get("qualifiers"), | ||
| subpath=input_data.get("subpath"), | ||
| ) | ||
| assert purl.to_string() == case["expected_output"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.