|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import filecmp |
| 4 | +from argparse import Namespace |
| 5 | +from pathlib import Path |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from check_cla import parse_args, read_cla, write_cla |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from typing import Any, Final |
| 14 | + |
| 15 | +DATA: Final = Path(__file__).parent / "data" |
| 16 | + |
| 17 | +EMPTY: Final[dict[int, str]] = {} |
| 18 | +SINGLE: Final = {"123": "login"} |
| 19 | +MULTIPLE: Final = {"111": "foo", "123": "login", "222": "bar"} |
| 20 | + |
| 21 | + |
| 22 | +def test_parse_args() -> None: |
| 23 | + with pytest.raises(SystemExit): |
| 24 | + parse_args([]) |
| 25 | + with pytest.raises(SystemExit): |
| 26 | + parse_args(["file"]) |
| 27 | + with pytest.raises(SystemExit): |
| 28 | + parse_args(["--id=123"]) |
| 29 | + with pytest.raises(SystemExit): |
| 30 | + parse_args(["--login=login"]) |
| 31 | + with pytest.raises(SystemExit): |
| 32 | + parse_args(["file", "--id=123"]) |
| 33 | + with pytest.raises(SystemExit): |
| 34 | + parse_args(["file", "--login=login"]) |
| 35 | + with pytest.raises(SystemExit): |
| 36 | + parse_args(["--id=123", "--login=login"]) |
| 37 | + assert parse_args(["file", "--id=123", "--login=login"]) == Namespace( |
| 38 | + path=Path("file"), |
| 39 | + id=123, |
| 40 | + login="login", |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def ids_basename(value: Any) -> Any: |
| 45 | + if isinstance(value, Path): |
| 46 | + return value.name |
| 47 | + return value |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "path,signees", |
| 52 | + [ |
| 53 | + (DATA / "missing", EMPTY), |
| 54 | + (DATA / "empty.json", EMPTY), |
| 55 | + (DATA / "single.json", SINGLE), |
| 56 | + (DATA / "multiple.json", MULTIPLE), |
| 57 | + ], |
| 58 | + ids=ids_basename, |
| 59 | +) |
| 60 | +def test_read_cla(path: Path, signees: dict[int, str]) -> None: |
| 61 | + assert read_cla(path) == signees |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "path,signees", |
| 66 | + [ |
| 67 | + (DATA / "empty.json", EMPTY), |
| 68 | + (DATA / "single.json", SINGLE), |
| 69 | + (DATA / "multiple.json", MULTIPLE), |
| 70 | + ], |
| 71 | + ids=ids_basename, |
| 72 | +) |
| 73 | +def test_write_cla(tmp_path: Path, path: Path, signees: dict[int, str]) -> None: |
| 74 | + write_cla(tmp := tmp_path / path.name, signees) |
| 75 | + assert filecmp.cmp(tmp, path) |
0 commit comments