Skip to content

Commit

Permalink
💥 Consolidated commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatXliner committed Nov 25, 2023
1 parent 87eba9b commit 2831b0b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
34 changes: 23 additions & 11 deletions idae/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import List, Optional

import typer
from click.exceptions import UsageError
from packaging.requirements import Requirement
from packaging.version import Version
from rich.console import Console
Expand All @@ -24,27 +25,21 @@
console = Console(stderr=True)


@cli.command()
def clean() -> None:
"""Clean the virtual environment caches."""
clean_venvs()


@cli.command()
def run(
script: Annotated[
Path,
Optional[Path], # noqa: FA100 # Typer can't handle unions
typer.Argument(
exists=True,
file_okay=True,
dir_okay=False,
readable=True,
resolve_path=True,
help="The path of the script to run (module only)",
help="The path of the script to run (modules only)",
),
],
] = None,
python_flags: Annotated[
Optional[List[str]], # noqa: FA100 # Typer is sped
Optional[List[str]], # noqa: FA100
typer.Option(help="Extra flags to pass to Python"),
] = None,
ignore_version: Annotated[
Expand All @@ -55,6 +50,12 @@ def run(
help="Ignore Python version requirements specified in the script",
),
] = False,
clean: Annotated[
bool,
typer.Option(
help="Clean the virtual environment caches",
),
] = False,
force_version: Annotated[
Optional[str], # noqa: FA100
typer.Option(
Expand All @@ -64,7 +65,18 @@ def run(
),
] = None,
) -> None:
"""Automatically install necessary dependencies to run a Python script."""
"""Automatically install necessary dependencies to run a Python script.
--clean can be used without 'SCRIPT'
"""
if not clean and script is None:
msg = "Missing argument 'SCRIPT'."
raise UsageError(msg)

Check warning on line 74 in idae/cli.py

View check run for this annotation

Codecov / codecov/patch

idae/cli.py#L73-L74

Added lines #L73 - L74 were not covered by tests
if clean:
clean_venvs()
console.print("[green bold]Cleaned all venvs[/]")
if script is None:
raise typer.Exit(code=0)
# Get script dependencies
pyproject = read(str(script.read_text()))
script_deps = []
Expand Down
19 changes: 8 additions & 11 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def empty_cache() -> None:
def test_main(capfd):
result = runner.invoke(
cli,
["run", "tests/examples/rich_requests.py"],
["tests/examples/rich_requests.py"],
)
out, _ = capfd.readouterr()
assert result.exit_code == 0
Expand All @@ -52,7 +52,7 @@ def test_main(capfd):
def test_clean_venvs(capfd):
result = runner.invoke(
cli,
["run", "tests/examples/rich_requests.py"],
["tests/examples/rich_requests.py"],
)
out, _ = capfd.readouterr()
assert result.exit_code == 0
Expand All @@ -62,7 +62,7 @@ def test_clean_venvs(capfd):

result = runner.invoke(
cli,
["clean"],
["--clean"],
)
assert result.exit_code == 0
assert not CACHE_DIR.exists()
Expand All @@ -72,7 +72,7 @@ def test_clean_venvs(capfd):
def test_ignore_version(capfd):
result = runner.invoke(
cli,
["run", "--ignore-version", "tests/examples/impossible_python.py"],
["--ignore-version", "tests/examples/impossible_python.py"],
)
out, _ = capfd.readouterr()
assert result.exit_code == 0
Expand All @@ -86,7 +86,7 @@ def test_caching(capfd):
start = time.time()
result = runner.invoke(
cli,
["run", "tests/examples/rich_requests.py"],
["tests/examples/rich_requests.py"],
)
out, _ = capfd.readouterr()
assert result.exit_code == 0
Expand All @@ -95,7 +95,7 @@ def test_caching(capfd):
start = time.time()
result = runner.invoke(
cli,
["run", "tests/examples/rich_requests.py"],
["tests/examples/rich_requests.py"],
)
out, _ = capfd.readouterr()
assert result.exit_code == 0
Expand All @@ -107,7 +107,7 @@ def test_caching(capfd):
def test_impossible_python():
result = runner.invoke(
cli,
["run", "tests/examples/impossible_python.py"],
["tests/examples/impossible_python.py"],
)
assert result.exit_code == 1
assert "not found" in result.stderr
Expand All @@ -118,7 +118,6 @@ def test_force_version_impossible_python():
result = runner.invoke(
cli,
[
"run",
"tests/examples/rich_requests.py",
"--force-version",
"~&%29",
Expand All @@ -134,7 +133,6 @@ def test_force_version_impossible_python(self):
result = runner.invoke(
cli,
[
"run",
"tests/examples/rich_requests.py",
"--force-version",
"69420",
Expand All @@ -148,7 +146,6 @@ def test_force_version_python(self, capfd):
result = runner.invoke(
cli,
[
"run",
"--force-version",
".".join(map(str, sys.version_info[:2])),
"tests/examples/impossible_python.py",
Expand All @@ -162,7 +159,7 @@ def test_force_version_python(self, capfd):
def test_force_short_impossible_python(self):
result = runner.invoke(
cli,
["run", "-f", "69420", "tests/examples/rich_requests.py"],
["-f", "69420", "tests/examples/rich_requests.py"],
)
assert result.exit_code == 1
assert "not found" in result.stderr

0 comments on commit 2831b0b

Please sign in to comment.