2
2
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
3
3
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
4
4
5
+ from __future__ import annotations
6
+
7
+ import os
8
+ import os .path
5
9
from pathlib import Path
6
10
11
+ import pytest
12
+ from pytest import CaptureFixture
13
+
14
+ from pylint .lint import Run as LintRun
7
15
from pylint .testutils ._run import _Run as Run
8
16
9
17
@@ -21,3 +29,96 @@ def test_fall_back_on_base_config(tmp_path: Path) -> None:
21
29
f .write ("1" )
22
30
Run ([str (test_file )], exit = False )
23
31
assert id (runner .linter .config ) == id (runner .linter ._base_config )
32
+
33
+
34
+ def _create_subconfig_test_fs (tmp_path : Path ) -> tuple [Path , Path , Path ]:
35
+ level1_dir = tmp_path / "level1_dir"
36
+ conf_file1 = level1_dir / "pylintrc"
37
+ test_file1 = level1_dir / "a.py"
38
+ subdir = level1_dir / "sub"
39
+ conf_file2 = subdir / "pylintrc"
40
+ test_file2 = subdir / "b.py"
41
+ os .makedirs (subdir )
42
+ test_file_text = "#NOTE_A\n #NOTE_B\n #TODO\n "
43
+ test_file1 .write_text (test_file_text )
44
+ test_file2 .write_text (test_file_text )
45
+ conf1 = "[MISCELLANEOUS]\n notes=NOTE_A,NOTE_B"
46
+ conf2 = "[MISCELLANEOUS]\n notes=NOTE_B"
47
+ conf_file1 .write_text (conf1 )
48
+ conf_file2 .write_text (conf2 )
49
+ return (level1_dir , test_file1 , test_file2 )
50
+
51
+
52
+ def _create_parent_subconfig_fs (tmp_path : Path ) -> Path :
53
+ level1_dir = tmp_path / "package"
54
+ conf_file = level1_dir / "pylintrc"
55
+ subdir = level1_dir / "sub"
56
+ test_file = subdir / "b.py"
57
+ os .makedirs (subdir )
58
+ test_file_text = "#NOTE_A\n #NOTE_B\n #TODO\n "
59
+ test_file .write_text (test_file_text )
60
+ conf = "[MISCELLANEOUS]\n notes=NOTE_A,NOTE_B"
61
+ conf_file .write_text (conf )
62
+ return test_file
63
+
64
+
65
+ @pytest .mark .parametrize (
66
+ "local_config_args" ,
67
+ [["--use-local-configs=y" ], ["--use-local-configs=y" , "--use-parent-configs=y" ]],
68
+ )
69
+ @pytest .mark .parametrize ("start_dir_modificator" , ["." , ".." ])
70
+ def test_subconfig_vs_root_config (
71
+ tmp_path : Path ,
72
+ capsys : CaptureFixture ,
73
+ local_config_args : list [str ],
74
+ start_dir_modificator : str ,
75
+ ) -> None :
76
+ """Test that each checked file uses config from its own
77
+ or closest parent directory.
78
+ """
79
+ level1_dir , test_file1 , test_file2 = _create_subconfig_test_fs (tmp_path )
80
+ start_dir = (level1_dir / start_dir_modificator ).resolve ()
81
+ orig_cwd = os .getcwd ()
82
+ os .chdir (start_dir )
83
+ # _Run adds --rcfile, which overrides config from cwd
84
+ LintRun ([* local_config_args , str (test_file1 )], exit = False )
85
+ output1 = capsys .readouterr ().out .replace ("\\ n" , "\n " )
86
+ LintRun ([* local_config_args , str (test_file2 )], exit = False )
87
+ output2 = capsys .readouterr ().out .replace ("\\ n" , "\n " )
88
+ os .chdir (orig_cwd )
89
+
90
+ # different results for identical files because of different configs
91
+ assert "NOTE_A" in output1
92
+ assert "NOTE_A" not in output2
93
+
94
+
95
+ def test_subconfig_vs_cli_arg (tmp_path : Path , capsys : CaptureFixture ) -> None :
96
+ """Test that cli args have priority over subconfigs."""
97
+ test_root , test_file1 , test_file2 = _create_subconfig_test_fs (tmp_path )
98
+ orig_cwd = os .getcwd ()
99
+ os .chdir (test_root )
100
+ Run (["--notes=FIXME" , "--use-local-configs=y" , str (test_file1 )], exit = False )
101
+ output1 = capsys .readouterr ().out .replace ("\\ n" , "\n " )
102
+ Run (["--notes=FIXME" , "--use-local-configs=y" , str (test_file2 )], exit = False )
103
+ output2 = capsys .readouterr ().out .replace ("\\ n" , "\n " )
104
+ os .chdir (orig_cwd )
105
+
106
+ # check that cli arg overrides default value
107
+ assert "TODO" not in output1
108
+ # notes=FIXME in cli should override all pylintrc configs
109
+ assert "NOTE_B" not in output1
110
+ assert "NOTE_B" not in output2
111
+
112
+
113
+ def test_subconfig_in_parent (tmp_path : Path , capsys : CaptureFixture ) -> None :
114
+ """Test that searching local configs in parent directories works."""
115
+ test_file = _create_parent_subconfig_fs (tmp_path )
116
+ orig_cwd = os .getcwd ()
117
+ os .chdir (tmp_path )
118
+ Run (["--use-parent-configs=y" , "--use-local-configs=y" , str (test_file )], exit = False )
119
+ output1 = capsys .readouterr ().out .replace ("\\ n" , "\n " )
120
+ os .chdir (orig_cwd )
121
+
122
+ # check that file is linted with config from ../, which is not a cwd
123
+ assert "TODO" not in output1
124
+ assert "NOTE_A" in output1
0 commit comments