Skip to content

Commit caf3982

Browse files
Merge pull request #116 from r1chardj0n3s/remove-logging-mock
Remove monkeypatch use from test_find_extra_reqs
2 parents 7578589 + 95f29ec commit caf3982

File tree

2 files changed

+22
-70
lines changed

2 files changed

+22
-70
lines changed

tests/test_find_extra_reqs.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
import logging
66
import textwrap
77
from pathlib import Path
8-
from typing import Any, Callable, Iterable, List, Union
8+
from typing import Any, Set
99

1010
import pretend
1111
import pytest
12-
from pip._internal.req.req_file import ParsedRequirement
13-
from pytest import MonkeyPatch
1412

1513
from pip_check_reqs import common, find_extra_reqs
1614

@@ -96,47 +94,28 @@ def test_main_no_spec(capsys: pytest.CaptureFixture[Any]) -> None:
9694

9795

9896
@pytest.mark.parametrize(
99-
["verbose_cfg", "debug_cfg", "result"],
97+
["verbose_cfg", "debug_cfg", "expected_log_levels"],
10098
[
101-
(False, False, ["warn"]),
102-
(True, False, ["info", "warn"]),
103-
(False, True, ["debug", "info", "warn"]),
104-
(True, True, ["debug", "info", "warn"]),
99+
(False, False, {logging.WARNING}),
100+
(True, False, {logging.INFO, logging.WARNING}),
101+
(False, True, {logging.DEBUG, logging.INFO, logging.WARNING}),
102+
(True, True, {logging.DEBUG, logging.INFO, logging.WARNING}),
105103
],
106104
)
107105
def test_logging_config(
108-
monkeypatch: MonkeyPatch,
109106
caplog: pytest.LogCaptureFixture,
110107
verbose_cfg: bool,
111108
debug_cfg: bool,
112-
result: List[str],
109+
expected_log_levels: Set[int],
113110
tmp_path: Path,
114111
) -> None:
115112
source_dir = tmp_path / "source"
116113
source_dir.mkdir()
117114

118-
def fake_find_extra_reqs(
119-
requirements_filename: str, # pylint: disable=unused-argument
120-
paths: Iterable[str], # pylint: disable=unused-argument
121-
ignore_files_function: Callable[ # pylint: disable=unused-argument
122-
[str], bool
123-
],
124-
ignore_modules_function: Callable[ # pylint: disable=unused-argument
125-
[str], bool
126-
],
127-
ignore_requirements_function: Callable[ # noqa: E501 pylint: disable=unused-argument
128-
[Union[str, ParsedRequirement]], bool
129-
],
130-
skip_incompatible: bool, # pylint: disable=unused-argument
131-
) -> List[str]:
132-
return []
133-
134-
monkeypatch.setattr(
135-
find_extra_reqs,
136-
"find_extra_reqs",
137-
fake_find_extra_reqs,
138-
)
139-
arguments = [str(source_dir)]
115+
requirements_file = tmp_path / "requirements.txt"
116+
requirements_file.touch()
117+
118+
arguments = [str(source_dir), "--requirements", str(requirements_file)]
140119
if verbose_cfg:
141120
arguments.append("--verbose")
142121
if debug_cfg:
@@ -151,12 +130,8 @@ def fake_find_extra_reqs(
151130
]:
152131
find_extra_reqs.log.log(*event)
153132

154-
messages = [r.message for r in caplog.records]
155-
# first message is always the usage message
156-
if verbose_cfg or debug_cfg:
157-
assert messages[1:] == result
158-
else:
159-
assert messages == result
133+
log_levels = {r.levelno for r in caplog.records}
134+
assert log_levels == expected_log_levels
160135

161136

162137
def test_main_version(capsys: pytest.CaptureFixture[Any]) -> None:

tests/test_find_missing_reqs.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
import os
77
import textwrap
88
from pathlib import Path
9-
from typing import Any, Callable, Iterable, List, Tuple
9+
from typing import Any, Set
1010

1111
import pretend
1212
import pytest
13-
from pytest import MonkeyPatch
1413

1514
from pip_check_reqs import common, find_missing_reqs
1615

@@ -115,42 +114,24 @@ def test_main_no_spec(capsys: pytest.CaptureFixture[Any]) -> None:
115114

116115

117116
@pytest.mark.parametrize(
118-
["verbose_cfg", "debug_cfg", "result"],
117+
["verbose_cfg", "debug_cfg", "expected_log_levels"],
119118
[
120-
(False, False, ["warn"]),
121-
(True, False, ["info", "warn"]),
122-
(False, True, ["debug", "info", "warn"]),
123-
(True, True, ["debug", "info", "warn"]),
119+
(False, False, {logging.WARNING}),
120+
(True, False, {logging.INFO, logging.WARNING}),
121+
(False, True, {logging.DEBUG, logging.INFO, logging.WARNING}),
122+
(True, True, {logging.DEBUG, logging.INFO, logging.WARNING}),
124123
],
125124
)
126125
def test_logging_config(
127-
monkeypatch: MonkeyPatch,
128126
caplog: pytest.LogCaptureFixture,
129127
verbose_cfg: bool,
130128
debug_cfg: bool,
131-
result: List[str],
129+
expected_log_levels: Set[int],
132130
tmp_path: Path,
133131
) -> None:
134132
source_dir = tmp_path / "source"
135133
source_dir.mkdir()
136134

137-
def fake_find_missing_reqs(
138-
requirements_filename: str, # pylint: disable=unused-argument
139-
paths: Iterable[str], # pylint: disable=unused-argument
140-
ignore_files_function: Callable[ # pylint: disable=unused-argument
141-
[str], bool
142-
],
143-
ignore_modules_function: Callable[ # pylint: disable=unused-argument
144-
[str], bool
145-
],
146-
) -> List[Tuple[str, List[common.FoundModule]]]:
147-
return []
148-
149-
monkeypatch.setattr(
150-
find_missing_reqs,
151-
"find_missing_reqs",
152-
fake_find_missing_reqs,
153-
)
154135
arguments = [str(source_dir)]
155136
if verbose_cfg:
156137
arguments.append("--verbose")
@@ -166,12 +147,8 @@ def fake_find_missing_reqs(
166147
]:
167148
find_missing_reqs.log.log(*event)
168149

169-
messages = [r.message for r in caplog.records]
170-
# first message is always the usage message
171-
if verbose_cfg or debug_cfg:
172-
assert messages[1:] == result
173-
else:
174-
assert messages == result
150+
log_levels = {r.levelno for r in caplog.records}
151+
assert log_levels == expected_log_levels
175152

176153

177154
def test_main_version(

0 commit comments

Comments
 (0)