Files
unshackle/tests/commands/test_device_commands.py
T

69 lines
2.3 KiB
Python

from __future__ import annotations
from pathlib import Path
from typing import Any
import click
import pytest
from click.testing import CliRunner
from pyplayready import main as prd_upstream
from pywidevine import main as wvd_upstream
from unshackle.commands import prd as prd_module
from unshackle.commands import wvd as wvd_module
from unshackle.core.config import config
def record(monkeypatch: pytest.MonkeyPatch, module: Any, name: str) -> list[dict[str, Any]]:
"""Swap an upstream command for one with its parameters whose callback records the call."""
real = getattr(module, name)
calls: list[dict[str, Any]] = []
fake = click.Command(real.name, params=real.params, callback=lambda **kwargs: calls.append(kwargs))
monkeypatch.setattr(module, name, fake)
return calls
def names(module: Any, name: str) -> set[str]:
return {param.name for param in getattr(module, name).params if param.name}
def files(folder: Path, *names: str) -> Path:
folder.mkdir(parents=True, exist_ok=True)
for name in names:
(folder / name).write_bytes(b"x")
return folder
@pytest.mark.parametrize(
("args", "message"),
[
(["dev"], "Folder must contain zgpriv.dat and bgroupcert.dat"),
(["dev", "dev", "dev"], "Provide either a folder path"),
(["dev", "-e", "missing.dat"], None),
(["dev/zgpriv.dat", "missing.dat"], None),
],
)
def test_prd_new_refuses_bad_paths_before_the_upstream_command(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, args: list[str], message: str | None
) -> None:
files(tmp_path / "dev", "zgpriv.dat")
monkeypatch.chdir(tmp_path)
calls = record(monkeypatch, prd_upstream, "create_device")
result = CliRunner().invoke(prd_module.prd, ["new", *args])
assert result.exit_code == 2
assert message is None or message in result.output
assert calls == []
def test_wvd_dump_names_a_missing_device_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(config.directories, "wvds", tmp_path / "WVDs")
calls = record(monkeypatch, wvd_upstream, "export_device")
result = CliRunner().invoke(wvd_module.wvd, ["dump", "missing", "out"], standalone_mode=False)
assert isinstance(result.exception, click.UsageError)
assert str(tmp_path / "WVDs" / "missing.wvd") in result.exception.message
assert calls == []