Skip to content

Commit

Permalink
Add test_check_cla.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard committed Jan 9, 2025
1 parent b1cd7ce commit 23e1aa9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
24 changes: 14 additions & 10 deletions check-cla/check_cla.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

if TYPE_CHECKING:
from argparse import Namespace
from collections.abc import Sequence


def parse_args() -> Namespace:
def parse_args(argv: Sequence[str] | None = None) -> Namespace:
# parse CLI for inputs
parser = ArgumentParser()
parser.add_argument("cla_path", type=Path, help="Local path to the CLA file.")
parser.add_argument("path", type=Path, help="Local path to the CLA file.")
parser.add_argument(
"--id",
type=int,
Expand All @@ -27,21 +28,24 @@ def parse_args() -> Namespace:
required=True,
help="Contributor's GitHub login.",
)
return parser.parse_args()
return parser.parse_args(argv)


def main() -> None:
args = parse_args()

path = args.cla_path
def read_cla(path: Path) -> dict[int, str]:
try:
signees = json.loads(path.read_text())
return json.loads(path.read_text())
except FileNotFoundError:
signees = {}
return {}


signees[args.id] = args.login
def write_cla(path: Path, signees: dict[int, str]) -> None:
path.write_text(json.dumps(signees, indent=2, sort_keys=True) + "\n")


def main() -> None:
args = parse_args()
write_cla(args.path, {**read_cla(args.path), args.id: args.login})


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions check-cla/data/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions check-cla/data/multiple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"111": "foo",
"123": "login",
"222": "bar"
}
3 changes: 3 additions & 0 deletions check-cla/data/single.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"123": "login"
}
67 changes: 67 additions & 0 deletions check-cla/test_check_cla.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from __future__ import annotations

import filecmp
from argparse import Namespace
from pathlib import Path
from typing import TYPE_CHECKING

import pytest

from check_cla import parse_args, read_cla, write_cla

if TYPE_CHECKING:
from typing import Final

DATA: Final = Path(__file__).parent / "data"

EMPTY: Final[dict[int, str]] = {}
SINGLE: Final = {"123": "login"}
MULTIPLE: Final = {"111": "foo", "123": "login", "222": "bar"}


def test_parse_args() -> None:
with pytest.raises(SystemExit):
parse_args([])
with pytest.raises(SystemExit):
parse_args(["file"])
with pytest.raises(SystemExit):
parse_args(["--id=123"])
with pytest.raises(SystemExit):
parse_args(["--login=login"])
with pytest.raises(SystemExit):
parse_args(["file", "--id=123"])
with pytest.raises(SystemExit):
parse_args(["file", "--login=login"])
with pytest.raises(SystemExit):
parse_args(["--id=123", "--login=login"])
assert parse_args(["file", "--id=123", "--login=login"]) == Namespace(
path=Path("file"),
id=123,
login="login",
)


@pytest.mark.parametrize(
"path,signees",
[
("missing", EMPTY),
("empty.json", EMPTY),
("single.json", SINGLE),
("multiple.json", MULTIPLE),
],
)
def test_read_cla(path: str, signees: dict[int, str]) -> None:
assert read_cla(DATA / path) == signees


@pytest.mark.parametrize(
"path,signees",
[
("empty.json", EMPTY),
("single.json", SINGLE),
("multiple.json", MULTIPLE),
],
)
def test_write_cla(tmp_path: Path, path: str, signees: dict[int, str]) -> None:
write_cla(tmp := tmp_path / path, signees)
assert filecmp.cmp(tmp, DATA / path)

0 comments on commit 23e1aa9

Please sign in to comment.