Skip to content

Commit 9cc903d

Browse files
committed
more tests
1 parent e60c24d commit 9cc903d

4 files changed

Lines changed: 221 additions & 2 deletions

File tree

licensecheck/io/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def cli() -> None: # pragma: no cover
133133
config += Config.from_path(file, optional=True)
134134

135135
scopedData: ConfigNode = config.get("tool", {}).get("licensecheck", ConfigNode())
136-
licensecheckConf: LC_Config = LC_Config.from_mapping(**scopedData.data, **args)
136+
licensecheckConf: LC_Config = LC_Config.from_mapping(**{**scopedData.data, **args})
137137

138138
ec = main(licensecheckConf)
139139
stdin_path.unlink(missing_ok=True)

licensecheck/models/packageinfo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def get_filtered_dict(self, hide_output_parameters: set[str]) -> dict:
3030
:param set[str] hide_output_parameters: set of parameters to ignore
3131
:return dict: filtered dictionary
3232
"""
33+
hide_output_parameters_upper = {x.upper for x in hide_output_parameters}
3334
return {
3435
k: (v if v is not None else UNKNOWN)
3536
for k, v in self.__dict__.items()
36-
if k.upper() not in hide_output_parameters
37+
if k.upper() not in hide_output_parameters_upper
3738
}

tests/io/data/fmt/advanced_ignore_params.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"name": "example0",
1111
"version": "1.0.0",
1212
"size": 10,
13+
"homePage": "https://example.com",
14+
"author": "example_author",
1315
"license": "mit",
1416
"licenseCompat": true,
1517
"errorCode": 0,
@@ -19,6 +21,8 @@
1921
"name": "example1",
2022
"version": "UNKNOWN",
2123
"size": 10,
24+
"homePage": "https://example.com",
25+
"author": "example_author",
2226
"license": "gpl3",
2327
"licenseCompat": false,
2428
"errorCode": 1,

tests/io/test_cli_main.py

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
import pytest
2+
3+
from licensecheck.models.config import LC_Config
4+
from licensecheck.io.cli import main
5+
6+
7+
@pytest.fixture
8+
def config() -> LC_Config:
9+
return LC_Config(
10+
requirements_paths={"requirements.txt"},
11+
file=None,
12+
license="MIT",
13+
pypi_api=None,
14+
groups=set(),
15+
extras=set(),
16+
ignore_packages=set(),
17+
fail_packages=set(),
18+
ignore_licenses=set(),
19+
fail_licenses=set(),
20+
only_licenses=set(),
21+
skip_dependencies=set(),
22+
hide_output_parameters=set(),
23+
format="simple",
24+
show_only_failing=False,
25+
zero=False,
26+
)
27+
28+
29+
def test_main_success(
30+
config: LC_Config,
31+
monkeypatch,
32+
) -> None:
33+
monkeypatch.setattr(
34+
"licensecheck.io.cli.checker.check",
35+
lambda **_: (False, []),
36+
)
37+
38+
monkeypatch.setattr(
39+
"licensecheck.io.cli.fmt.formatMap",
40+
{"simple": object()},
41+
)
42+
43+
monkeypatch.setattr(
44+
"licensecheck.io.cli.fmt.fmt",
45+
lambda *args, **kwargs: "output",
46+
)
47+
48+
assert main(config) == 0
49+
50+
51+
def test_main_invalid_format(
52+
config: LC_Config,
53+
monkeypatch,
54+
):
55+
config.format = "does-not-exist"
56+
57+
monkeypatch.setattr(
58+
"licensecheck.io.cli.checker.check",
59+
lambda **_: (False, []),
60+
)
61+
62+
monkeypatch.setattr(
63+
"licensecheck.io.cli.fmt.formatMap",
64+
{},
65+
)
66+
67+
assert main(config) == 2
68+
69+
70+
@pytest.mark.parametrize(
71+
("zero", "incompatible", "expected"),
72+
[
73+
(False, False, 0),
74+
(False, True, 0),
75+
(True, False, 0),
76+
(True, True, 1),
77+
],
78+
)
79+
def test_main_exit_code_zero_mode(
80+
config: LC_Config,
81+
monkeypatch,
82+
zero: bool,
83+
incompatible: bool,
84+
expected: int,
85+
) -> None:
86+
config.zero = zero
87+
88+
monkeypatch.setattr(
89+
"licensecheck.io.cli.checker.check",
90+
lambda **_: (incompatible, []),
91+
)
92+
93+
monkeypatch.setattr(
94+
"licensecheck.io.cli.fmt.formatMap",
95+
{"simple": object()},
96+
)
97+
98+
monkeypatch.setattr(
99+
"licensecheck.io.cli.fmt.fmt",
100+
lambda *args, **kwargs: "",
101+
)
102+
103+
assert main(config) == expected
104+
105+
106+
def test_main_invalid_hidden_parameter(
107+
config: LC_Config,
108+
monkeypatch,
109+
):
110+
config.hide_output_parameters = {"NOT_A_FIELD"}
111+
112+
monkeypatch.setattr(
113+
"licensecheck.io.cli.checker.check",
114+
lambda **_: (False, []),
115+
)
116+
117+
with pytest.raises(
118+
ValueError,
119+
match="Invalid parameter",
120+
):
121+
main(config)
122+
123+
124+
@pytest.mark.parametrize(
125+
"hidden",
126+
[
127+
[],
128+
["LICENSE"],
129+
["LICENSE", "NAME"],
130+
],
131+
)
132+
def test_main_valid_hidden_parameters(
133+
config: LC_Config,
134+
monkeypatch,
135+
hidden: list[str],
136+
):
137+
config.hide_output_parameters = hidden
138+
139+
monkeypatch.setattr(
140+
"licensecheck.io.cli.checker.check",
141+
lambda **_: (False, []),
142+
)
143+
144+
monkeypatch.setattr(
145+
"licensecheck.io.cli.fmt.formatMap",
146+
{"simple": object()},
147+
)
148+
149+
monkeypatch.setattr(
150+
"licensecheck.io.cli.fmt.fmt",
151+
lambda *args, **kwargs: "",
152+
)
153+
154+
assert main(config) == 0
155+
156+
157+
def test_main_passes_args_to_checker(
158+
config: LC_Config,
159+
monkeypatch,
160+
):
161+
called = {}
162+
163+
def fake_check(**kwargs):
164+
called.update(kwargs)
165+
return False, []
166+
167+
monkeypatch.setattr(
168+
"licensecheck.io.cli.checker.check",
169+
fake_check,
170+
)
171+
172+
monkeypatch.setattr(
173+
"licensecheck.io.cli.fmt.formatMap",
174+
{"simple": object()},
175+
)
176+
177+
monkeypatch.setattr(
178+
"licensecheck.io.cli.fmt.fmt",
179+
lambda *args, **kwargs: "",
180+
)
181+
182+
main(config)
183+
184+
assert called["groups"] == config.groups
185+
assert called["extras"] == config.extras
186+
assert called["skip_dependencies"] == config.skip_dependencies
187+
188+
189+
def test_main_closes_output_file(
190+
config: LC_Config,
191+
monkeypatch,
192+
tmp_path,
193+
):
194+
output = tmp_path / "out.txt"
195+
196+
config.file = str(output)
197+
198+
monkeypatch.setattr(
199+
"licensecheck.io.cli.checker.check",
200+
lambda **_: (False, []),
201+
)
202+
203+
monkeypatch.setattr(
204+
"licensecheck.io.cli.fmt.formatMap",
205+
{"simple": object()},
206+
)
207+
208+
monkeypatch.setattr(
209+
"licensecheck.io.cli.fmt.fmt",
210+
lambda *args, **kwargs: "hello",
211+
)
212+
213+
assert main(config) == 0
214+
assert output.read_text() == "hello\n"

0 commit comments

Comments
 (0)