-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
301 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,73 @@ | ||
""" | ||
List all scenarios. | ||
""" | ||
import logging | ||
from pathlib import Path | ||
|
||
from packaging.requirements import Requirement | ||
|
||
from packse.error import InvalidScenario, FileNotFound | ||
from packse.scenario import ( | ||
Package, | ||
Scenario, | ||
load_scenarios, | ||
scenario_prefix, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def list( | ||
targets: list[Path], | ||
no_versions: bool = False, | ||
skip_invalid: bool = False, | ||
no_sources: bool = False, | ||
): | ||
scenarios: dict[Path, list[Scenario]] = {} | ||
|
||
# Validate and collect all targets first | ||
for target in sorted(targets): | ||
if not target.exists(): | ||
raise FileNotFound(target) | ||
|
||
try: | ||
logger.debug("Loading %s", target) | ||
scenarios[target] = load_scenarios(target) | ||
except Exception as exc: | ||
if not skip_invalid: | ||
raise InvalidScenario(target, reason=str(exc)) from exc | ||
|
||
# Then list each one | ||
for source, scenarios in scenarios.items(): | ||
prefix = "" if no_sources else " " * 4 | ||
if not no_sources: | ||
print(to_display_path(source)) | ||
|
||
for scenario in scenarios: | ||
if no_versions: | ||
name = scenario.name | ||
else: | ||
name = scenario_prefix(scenario) | ||
|
||
print(prefix + name) | ||
|
||
|
||
def to_display_path(path: Path | str, relative_to: Path | str | None = None) -> str: | ||
""" | ||
Convert a path to a displayable path. The absolute path or relative path to the | ||
current (or given) directory will be returned, whichever is shorter. | ||
""" | ||
path, relative_to = ( | ||
Path(path).resolve(), | ||
Path(relative_to or ".").resolve(), | ||
) | ||
|
||
absolute_path = str(path) | ||
|
||
try: | ||
relative_path = str(path.relative_to(relative_to)) | ||
except ValueError: | ||
# Not a child path, use the other one | ||
return absolute_path | ||
|
||
return relative_path if len(relative_path) < len(absolute_path) else absolute_path |
This file contains 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 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 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,113 @@ | ||
# serializer version: 1 | ||
# name: test_list_example | ||
dict({ | ||
'exit_code': 0, | ||
'stderr': '', | ||
'stdout': ''' | ||
scenarios/example.json | ||
example-5803604b | ||
|
||
''', | ||
}) | ||
# --- | ||
# name: test_list_invalid_target | ||
dict({ | ||
'exit_code': 1, | ||
'stderr': ''' | ||
File at '[PWD]/test.json' is not a valid scenario: Input data was truncated. | ||
|
||
''', | ||
'stdout': '', | ||
}) | ||
# --- | ||
# name: test_list_invalid_target_skip_invalid | ||
dict({ | ||
'exit_code': 0, | ||
'stderr': '', | ||
'stdout': ''' | ||
[PROJECT_ROOT]/scenarios/example.json | ||
example-5803604b | ||
|
||
''', | ||
}) | ||
# --- | ||
# name: test_list_no_sources | ||
dict({ | ||
'exit_code': 0, | ||
'stderr': '', | ||
'stdout': ''' | ||
example-5803604b | ||
requires-package-does-not-exist-e4666012 | ||
requires-exact-version-does-not-exist-c640da4b | ||
requires-greater-version-does-not-exist-ceb05782 | ||
requires-less-version-does-not-exist-14de847d | ||
transitive-requires-package-does-not-exist-15763ba4 | ||
requires-direct-incompatible-versions-3a64108d | ||
requires-transitive-incompatible-with-root-version-8af9847a | ||
requires-transitive-incompatible-with-transitive-cb77ed7e | ||
|
||
''', | ||
}) | ||
# --- | ||
# name: test_list_no_target | ||
dict({ | ||
'exit_code': 0, | ||
'stderr': '', | ||
'stdout': ''' | ||
scenarios/example.json | ||
example-5803604b | ||
scenarios/requires-does-not-exist.json | ||
requires-package-does-not-exist-e4666012 | ||
requires-exact-version-does-not-exist-c640da4b | ||
requires-greater-version-does-not-exist-ceb05782 | ||
requires-less-version-does-not-exist-14de847d | ||
transitive-requires-package-does-not-exist-15763ba4 | ||
scenarios/requires-incompatible-versions.json | ||
requires-direct-incompatible-versions-3a64108d | ||
requires-transitive-incompatible-with-root-version-8af9847a | ||
requires-transitive-incompatible-with-transitive-cb77ed7e | ||
|
||
''', | ||
}) | ||
# --- | ||
# name: test_list_no_version | ||
dict({ | ||
'exit_code': 0, | ||
'stderr': '', | ||
'stdout': ''' | ||
scenarios/example.json | ||
example | ||
scenarios/requires-does-not-exist.json | ||
requires-package-does-not-exist | ||
requires-exact-version-does-not-exist | ||
requires-greater-version-does-not-exist | ||
requires-less-version-does-not-exist | ||
transitive-requires-package-does-not-exist | ||
scenarios/requires-incompatible-versions.json | ||
requires-direct-incompatible-versions | ||
requires-transitive-incompatible-with-root-version | ||
requires-transitive-incompatible-with-transitive | ||
|
||
''', | ||
}) | ||
# --- | ||
# name: test_list_one_target_does_not_exist | ||
dict({ | ||
'exit_code': 1, | ||
'stderr': ''' | ||
File 'foo' not found. | ||
|
||
''', | ||
'stdout': '', | ||
}) | ||
# --- | ||
# name: test_list_target_does_not_exist | ||
dict({ | ||
'exit_code': 1, | ||
'stderr': ''' | ||
File 'foo' not found. | ||
|
||
''', | ||
'stdout': '', | ||
}) | ||
# --- |
This file contains 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
Oops, something went wrong.