Skip to content

Commit 66f2cf1

Browse files
author
Aleksey Petryankin
committed
Increase test coverage for local configs
- and minor changes for github spellchecker
1 parent 400e238 commit 66f2cf1

File tree

2 files changed

+91
-23
lines changed

2 files changed

+91
-23
lines changed

.pyenchant_pylint_custom_dict.txt

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ codecs
6161
col's
6262
conf
6363
config
64+
configs
6465
const
6566
Const
6667
contextlib
@@ -309,11 +310,13 @@ str
309310
stringified
310311
subclasses
311312
subcommands
313+
subconfigs
312314
subdicts
313315
subgraphs
314316
sublists
315317
submodule
316318
submodules
319+
subpackage
317320
subparsers
318321
subparts
319322
subprocess

tests/config/test_per_directory_config.py

+88-23
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,47 @@ def test_fall_back_on_base_config(tmp_path: Path) -> None:
3232

3333

3434
@pytest.fixture
35-
def _create_subconfig_test_fs(tmp_path: Path) -> tuple[Path, Path, Path, Path]:
35+
def _create_subconfig_test_fs(tmp_path: Path) -> tuple[Path, ...]:
3636
level1_dir = tmp_path / "level1_dir"
3737
level1_init = level1_dir / "__init__.py"
3838
conf_file1 = level1_dir / "pylintrc"
3939
test_file1 = level1_dir / "a.py"
4040
test_file3 = level1_dir / "z.py"
41+
level1_dir_without_config = tmp_path / "level1_dir_without_config"
42+
level1_init2 = level1_dir_without_config / "__init__.py"
43+
test_file4 = level1_dir_without_config / "aa.py"
4144
subdir = level1_dir / "sub"
4245
level2_init = subdir / "__init__.py"
4346
conf_file2 = subdir / "pylintrc"
4447
test_file2 = subdir / "b.py"
48+
os.makedirs(level1_dir_without_config)
4549
os.makedirs(subdir)
4650
level1_init.touch()
51+
level1_init2.touch()
4752
level2_init.touch()
4853
test_file_text = "#LEVEL1\n#LEVEL2\n#ALL_LEVELS\n#TODO\n"
4954
test_file1.write_text(test_file_text)
5055
test_file2.write_text(test_file_text)
5156
test_file3.write_text(test_file_text)
57+
test_file4.write_text(test_file_text)
5258
conf1 = "[MISCELLANEOUS]\nnotes=LEVEL1,ALL_LEVELS"
5359
conf2 = "[MISCELLANEOUS]\nnotes=LEVEL2,ALL_LEVELS"
5460
conf_file1.write_text(conf1)
5561
conf_file2.write_text(conf2)
56-
return level1_dir, test_file1, test_file2, test_file3
62+
return level1_dir, test_file1, test_file2, test_file3, test_file4
5763

5864

5965
# check that use-parent-configs doesn't break anything
6066
@pytest.mark.parametrize(
6167
"local_config_args",
6268
[["--use-local-configs=y"], ["--use-local-configs=y", "--use-parent-configs=y"]],
6369
)
64-
# check files and configs from top-level package or subpackage
70+
# check modules and use of configuration files from top-level package or subpackage
6571
@pytest.mark.parametrize("test_file_index", [0, 1, 2])
6672
# check cases when cwd contains pylintrc or not
67-
@pytest.mark.parametrize("start_dir_modificator", [".", ".."])
73+
@pytest.mark.parametrize(
74+
"start_dir_modificator", [".", "..", "../level1_dir_without_config"]
75+
)
6876
def test_subconfig_vs_root_config(
6977
_create_subconfig_test_fs: tuple[Path, ...],
7078
capsys: CaptureFixture,
@@ -90,28 +98,70 @@ def test_subconfig_vs_root_config(
9098
test_file = test_file.parent
9199
os.chdir(orig_cwd)
92100

93-
expected_note = f"LEVEL{(test_file_index%2)+1}"
94-
assert (
95-
expected_note in output[1]
96-
), f"readable debug output:\n{output[0]}\n{output[1]}"
97-
assert (
98-
expected_note in output[2]
99-
), f"readable debug output:\n{output[0]}\n{output[2]}"
101+
expected_note = "LEVEL1"
102+
if test_file_index == 1:
103+
expected_note = "LEVEL2"
104+
assert_message = f"Wrong note after checking FILE. Readable debug output:\n{output[0]}\n{output[1]}"
105+
assert expected_note in output[1], assert_message
106+
assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n{output[0]}\n{output[2]}"
107+
assert expected_note in output[2], assert_message
100108

101109
if test_file_index == 0:
102110
# 'pylint level1_dir/' should use config from subpackage when checking level1_dir/sub/b.py
103-
assert (
104-
"LEVEL2" in output[2]
105-
), f"readable debug output:\n{output[0]}\n{output[2]}"
111+
assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n{output[0]}\n{output[2]}"
112+
assert "LEVEL2" in output[2], assert_message
106113
if test_file_index == 1:
107114
# 'pylint level1_dir/sub/b.py' and 'pylint level1_dir/sub/' should use
108115
# level1_dir/sub/pylintrc, not level1_dir/pylintrc
109-
assert (
110-
"LEVEL1" not in output[1]
111-
), f"readable debug output:\n{output[0]}\n{output[1]}"
112-
assert (
113-
"LEVEL1" not in output[2]
114-
), f"readable debug output:\n{output[0]}\n{output[2]}"
116+
assert_message = f"Wrong note after checking FILE. Readable debug output:\n{output[0]}\n{output[1]}"
117+
assert "LEVEL1" not in output[1], assert_message
118+
assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n{output[0]}\n{output[2]}"
119+
assert "LEVEL1" not in output[2], assert_message
120+
121+
122+
# check that use-parent-configs doesn't break anything
123+
@pytest.mark.parametrize(
124+
"local_config_args",
125+
[["--use-local-configs=y"], ["--use-local-configs=y", "--use-parent-configs=y"]],
126+
)
127+
# check cases when test_file without local config belongs to cwd subtree or not
128+
@pytest.mark.parametrize(
129+
"start_dir_modificator", [".", "..", "../level1_dir_without_config"]
130+
)
131+
def test_missing_local_config(
132+
_create_subconfig_test_fs: tuple[Path, ...],
133+
capsys: CaptureFixture,
134+
local_config_args: list[str],
135+
start_dir_modificator: str,
136+
) -> None:
137+
"""Test that when checked file or module doesn't have config
138+
in its own directory, it uses default config or config from cwd.
139+
"""
140+
level1_dir, *tmp_files = _create_subconfig_test_fs
141+
# file from level1_dir_without_config
142+
test_file = tmp_files[3]
143+
start_dir = (level1_dir / start_dir_modificator).resolve()
144+
145+
orig_cwd = os.getcwd()
146+
output = [f"{start_dir = }, {test_file = }"]
147+
os.chdir(start_dir)
148+
for _ in range(2):
149+
# _Run adds --rcfile, which overrides config from cwd, so we need original Run here
150+
LintRun([*local_config_args, str(test_file)], exit=False)
151+
output.append(capsys.readouterr().out.replace("\\n", "\n"))
152+
153+
test_file = test_file.parent
154+
os.chdir(orig_cwd)
155+
156+
# from default config
157+
expected_note = "TODO"
158+
if start_dir_modificator == ".":
159+
# from config in level1_dir
160+
expected_note = "LEVEL1"
161+
assert_message = f"Wrong note after checking FILE. Readable debug output:\n{output[0]}\n{output[1]}"
162+
assert expected_note in output[1], assert_message
163+
assert_message = f"Wrong note after checking DIRECTORY. Readable debug output:\n{output[0]}\n{output[2]}"
164+
assert expected_note in output[2], assert_message
115165

116166

117167
@pytest.mark.parametrize("test_file_index", [0, 1])
@@ -120,7 +170,7 @@ def test_subconfig_vs_cli_arg(
120170
capsys: CaptureFixture,
121171
test_file_index: int,
122172
) -> None:
123-
"""Test that cli args have priority over subconfigs."""
173+
"""Test that CLI arguments have priority over subconfigs."""
124174
test_root, *tmp_files = _create_subconfig_test_fs
125175
test_file = tmp_files[test_file_index]
126176
orig_cwd = os.getcwd()
@@ -129,9 +179,9 @@ def test_subconfig_vs_cli_arg(
129179
output = capsys.readouterr().out.replace("\\n", "\n")
130180
os.chdir(orig_cwd)
131181

132-
# check that cli arg overrides default value
182+
# check that CLI argument overrides default value
133183
assert "TODO" not in output
134-
# notes=FIXME in cli should override all pylintrc configs
184+
# notes=FIXME in arguments should override all pylintrc configs
135185
assert "ALL_LEVELS" not in output
136186

137187

@@ -162,3 +212,18 @@ def test_subconfig_in_parent(tmp_path: Path, capsys: CaptureFixture) -> None:
162212
# check that file is linted with config from ../, which is not a cwd
163213
assert "TODO" not in output1
164214
assert "LEVEL1" in output1
215+
216+
217+
def test_register_local_config_accepts_directory(
218+
_create_subconfig_test_fs: tuple[Path, ...]
219+
) -> None:
220+
"""Test that register_local_config can handle directory as argument."""
221+
level1_dir, *tmp_files = _create_subconfig_test_fs
222+
# init linter without local configs
223+
linter = LintRun([str(tmp_files[0])], exit=False).linter
224+
assert level1_dir not in linter._directory_namespaces
225+
226+
# call register_local_config with directory as argument
227+
assert level1_dir.is_dir()
228+
linter.register_local_config(str(level1_dir))
229+
assert level1_dir in linter._directory_namespaces

0 commit comments

Comments
 (0)