Skip to content

Commit

Permalink
♻️ 🐛 :white_checkmark:
Browse files Browse the repository at this point in the history
Fix tests, catch bugs, refactor
  • Loading branch information
ThatXliner committed Nov 25, 2023
1 parent 5a3cdc7 commit 6095162
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
16 changes: 2 additions & 14 deletions idae/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ def run(
executable=sys.executable,
)
if force_version is not None:
python = get_python(force_version) # type: ignore[assignment]
# I should probably shove this into the `get_python`
# function to avoid code duplication
if not python:
console.print(f"[red]error: Python version {force_version} not found[/red]")
raise typer.Exit(code=1)
python = get_python(force_version, console) # type: ignore[assignment]
if pyproject is not None and "run" in pyproject:
script_deps = (
[]
Expand All @@ -94,15 +89,8 @@ def run(
and "requires-python" in pyproject["run"]
):
python = get_python(
pyproject["run"]["requires-python"], # type: ignore[assignment]
pyproject["run"]["requires-python"], console # type: ignore[assignment]
)
if not python:
console.print(
"[red]error: Python version "
+ pyproject["run"]["requires-python"]
+ " not found[/red]",
)
raise typer.Exit(code=1)

venv_path = get_venv(script_deps, python)

Expand Down
27 changes: 22 additions & 5 deletions idae/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@

from __future__ import annotations

from typing import TYPE_CHECKING

import findpython # type: ignore[import]
from packaging.specifiers import SpecifierSet
import typer
from packaging.specifiers import InvalidSpecifier, SpecifierSet

if TYPE_CHECKING:
from rich.console import Console


def get_python(version: str) -> findpython.PythonVersion | None:
"""Resolve the version string."""
def get_python(version: str, console: Console) -> findpython.PythonVersion:
"""Resolve the version string or raise Exit."""
# Order from latest version to earliest
pythons = {python.version: python for python in findpython.find_all()}
target = SpecifierSet(version)
try:
float(version)
except ValueError:
pass
else:
version = f"=={version}"
try:
target = SpecifierSet(version)
except InvalidSpecifier as err:
console.print(f"[red]error: Python version {version} could not be parsed[/red]")
raise typer.Exit(code=1) from err
for python_version, python in pythons.items():
if python_version in target:
return python
return None
console.print(f"[red]error: Python version {version} not found[/red]")
raise typer.Exit(code=1)
19 changes: 16 additions & 3 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,18 @@ class TestForceFlags:
def test_force_version_impossible_python(self):
result = runner.invoke(
cli,
["run", "--force-version", "69420", "tests/examples/rich_requests.py"],
[
"run",
"tests/examples/rich_requests.py",
"--force-version",
"69420",
],
)
assert result.exit_code == 1
assert "not found" in result.stderr

@pytest.mark.usefixtures("empty_cache")
def test_force_version_python(self):
def test_force_version_python(self, capfd):
result = runner.invoke(
cli,
[
Expand All @@ -134,7 +139,15 @@ def test_force_version_python(self):
"tests/examples/impossible_python.py",
],
)
assert result.exit_code == 1
assert result.exit_code == 0
out, err = capfd.readouterr()
assert out == EXAMPLE_OUTPUT, (
out,
err,
result.stdout,
result.stderr,
".".join(map(str, sys.version_info[:2])),
)

@pytest.mark.usefixtures("empty_cache")
def test_force_short_impossible_python(self):
Expand Down

0 comments on commit 6095162

Please sign in to comment.