Skip to content

Commit 23e1aa9

Browse files
committed
Add test_check_cla.py
1 parent b1cd7ce commit 23e1aa9

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 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+
@pytest.mark.parametrize(
45+
"path,signees",
46+
[
47+
("missing", EMPTY),
48+
("empty.json", EMPTY),
49+
("single.json", SINGLE),
50+
("multiple.json", MULTIPLE),
51+
],
52+
)
53+
def test_read_cla(path: str, signees: dict[int, str]) -> None:
54+
assert read_cla(DATA / path) == signees
55+
56+
57+
@pytest.mark.parametrize(
58+
"path,signees",
59+
[
60+
("empty.json", EMPTY),
61+
("single.json", SINGLE),
62+
("multiple.json", MULTIPLE),
63+
],
64+
)
65+
def test_write_cla(tmp_path: Path, path: str, signees: dict[int, str]) -> None:
66+
write_cla(tmp := tmp_path / path, signees)
67+
assert filecmp.cmp(tmp, DATA / path)

0 commit comments

Comments
 (0)