Skip to content

Commit eb76106

Browse files
committed
Add test_check_cla.py
1 parent b1cd7ce commit eb76106

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

check-cla/check_cla.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010
if TYPE_CHECKING:
1111
from argparse import Namespace
12+
from collections.abc import Sequence
1213

1314

14-
def parse_args() -> Namespace:
15+
def parse_args(argv: Sequence[str] | None = None) -> Namespace:
1516
# parse CLI for inputs
1617
parser = ArgumentParser()
17-
parser.add_argument("cla_path", type=Path, help="Local path to the CLA file.")
18+
parser.add_argument("path", type=Path, help="Local path to the CLA file.")
1819
parser.add_argument(
1920
"--id",
2021
type=int,
@@ -27,21 +28,24 @@ def parse_args() -> Namespace:
2728
required=True,
2829
help="Contributor's GitHub login.",
2930
)
30-
return parser.parse_args()
31+
return parser.parse_args(argv)
3132

3233

33-
def main() -> None:
34-
args = parse_args()
35-
36-
path = args.cla_path
34+
def read_cla(path: Path) -> dict[int, str]:
3735
try:
38-
signees = json.loads(path.read_text())
36+
return json.loads(path.read_text())
3937
except FileNotFoundError:
40-
signees = {}
38+
return {}
39+
4140

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

4544

45+
def main() -> None:
46+
args = parse_args()
47+
write_cla(args.path, {**read_cla(args.path), args.id: args.login})
48+
49+
4650
if __name__ == "__main__":
4751
main()

check-cla/data/empty.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

check-cla/data/multiple.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"111": "foo",
3+
"123": "login",
4+
"222": "bar"
5+
}

check-cla/data/single.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"123": "login"
3+
}

check-cla/test_check_cla.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)