140 lines
3.9 KiB
Python
140 lines
3.9 KiB
Python
"""Tests for the Typer CLI."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Sequence
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from typer.testing import CliRunner
|
|
|
|
from genshin_tcg import __version__
|
|
from genshin_tcg.cli import app
|
|
from genshin_tcg.data.catalog import Catalog, save_catalog
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
def test_version_flag() -> None:
|
|
result = runner.invoke(app, ["--version"])
|
|
assert result.exit_code == 0
|
|
assert __version__ in result.output
|
|
|
|
|
|
def test_list_without_catalogue_errors(tmp_path: Path) -> None:
|
|
result = runner.invoke(
|
|
app,
|
|
["--build-dir", str(tmp_path / "build"), "--assets-dir", str(tmp_path / "assets"), "list"],
|
|
)
|
|
assert result.exit_code == 1
|
|
assert "Error" in result.output
|
|
|
|
|
|
def test_list_prints_catalogue(tmp_path: Path, sample_catalog: Catalog) -> None:
|
|
build_dir = tmp_path / "build"
|
|
build_dir.mkdir(parents=True)
|
|
save_catalog(sample_catalog, build_dir / "catalog.json")
|
|
|
|
result = runner.invoke(
|
|
app,
|
|
["--build-dir", str(build_dir), "--assets-dir", str(tmp_path / "assets"), "list"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "test_pyro" in result.output
|
|
assert "Test Pyro" in result.output
|
|
|
|
|
|
class _FakeWiki:
|
|
"""A stand-in for :class:`genshin_tcg.data.fandom.FandomSource`."""
|
|
|
|
def __init__(self, *args: object, **kwargs: object) -> None:
|
|
pass
|
|
|
|
def __enter__(self) -> _FakeWiki:
|
|
return self
|
|
|
|
def __exit__(self, *exc: object) -> None:
|
|
return None
|
|
|
|
def character_regions(self) -> dict[str, str]:
|
|
return {"test_pyro": "Mondstadt"}
|
|
|
|
def enemy_families(self) -> dict[str, str]:
|
|
return {}
|
|
|
|
def region_emblems(self) -> dict[str, str]:
|
|
return {}
|
|
|
|
def file_urls(self, names: Sequence[str]) -> dict[str, str]:
|
|
return {}
|
|
|
|
def download(self, url: str, destination: Path, *, force: bool = False) -> bool:
|
|
return False
|
|
|
|
|
|
def test_factions_command_writes_overrides(
|
|
tmp_path: Path, sample_catalog: Catalog, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
build_dir = tmp_path / "build"
|
|
build_dir.mkdir(parents=True)
|
|
save_catalog(sample_catalog, build_dir / "catalog.json")
|
|
monkeypatch.setattr("genshin_tcg.pipeline.FandomSource", _FakeWiki)
|
|
|
|
result = runner.invoke(
|
|
app,
|
|
["--build-dir", str(build_dir), "--assets-dir", str(tmp_path / "assets"), "factions"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "Factions" in result.output
|
|
overrides = tmp_path / "assets" / "overrides" / "factions.json"
|
|
assert overrides.is_file()
|
|
assert "mondstadt" in overrides.read_text(encoding="utf-8")
|
|
|
|
|
|
def test_factions_command_no_icons(
|
|
tmp_path: Path, sample_catalog: Catalog, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
build_dir = tmp_path / "build"
|
|
build_dir.mkdir(parents=True)
|
|
save_catalog(sample_catalog, build_dir / "catalog.json")
|
|
monkeypatch.setattr("genshin_tcg.pipeline.FandomSource", _FakeWiki)
|
|
|
|
result = runner.invoke(
|
|
app,
|
|
[
|
|
"--build-dir",
|
|
str(build_dir),
|
|
"--assets-dir",
|
|
str(tmp_path / "assets"),
|
|
"factions",
|
|
"--no-icons",
|
|
],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "Icons" not in result.output
|
|
|
|
|
|
def test_factions_command_dry_run(
|
|
tmp_path: Path, sample_catalog: Catalog, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
build_dir = tmp_path / "build"
|
|
build_dir.mkdir(parents=True)
|
|
save_catalog(sample_catalog, build_dir / "catalog.json")
|
|
monkeypatch.setattr("genshin_tcg.pipeline.FandomSource", _FakeWiki)
|
|
|
|
result = runner.invoke(
|
|
app,
|
|
[
|
|
"--build-dir",
|
|
str(build_dir),
|
|
"--assets-dir",
|
|
str(tmp_path / "assets"),
|
|
"factions",
|
|
"--dry-run",
|
|
],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "test_pyro: mondstadt" in result.output
|
|
assert not (tmp_path / "assets" / "overrides" / "factions.json").exists()
|
|
|