-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_lint.py
More file actions
230 lines (196 loc) · 8.14 KB
/
Copy pathtest_cli_lint.py
File metadata and controls
230 lines (196 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch, MagicMock
ROOT = Path(__file__).resolve().parents[1]
SCRIPT = ROOT / "PythonBox_v8.py"
sys.path.insert(0, str(ROOT))
from PythonBox_v8 import parse_cli_args, LinterRunner
def run_lint(target: str, timeout: int = 30) -> subprocess.CompletedProcess:
env = {**os.environ, "PYTHONIOENCODING": "utf-8"}
return subprocess.run(
[sys.executable, str(SCRIPT), "--lint", target],
capture_output=True, text=True, timeout=timeout, env=env,
)
def run_headless(arguments, timeout: int = 30) -> subprocess.CompletedProcess:
env = {**os.environ, "PYTHONIOENCODING": "utf-8"}
return subprocess.run(
[sys.executable, str(SCRIPT), *arguments],
capture_output=True, text=True, timeout=timeout, env=env,
)
class TestCLILint(unittest.TestCase):
def test_lint_clean_file(self):
with tempfile.NamedTemporaryFile(suffix=".py", mode="w",
encoding="utf-8", delete=False) as f:
f.write("x = 1\n")
f.flush()
path = f.name
try:
result = run_lint(path)
self.assertIn("Keine Findings", result.stdout)
self.assertEqual(result.returncode, 0)
finally:
os.unlink(path)
def test_lint_syntax_error(self):
with tempfile.NamedTemporaryFile(suffix=".py", mode="w",
encoding="utf-8", delete=False) as f:
f.write("def foo(\n")
f.flush()
path = f.name
try:
result = run_lint(path)
self.assertEqual(result.returncode, 1)
self.assertTrue(len(result.stdout.strip()) > 0)
finally:
os.unlink(path)
def test_lint_nonexistent_file(self):
result = run_lint("nonexistent_file_xyz_12345.py")
self.assertEqual(result.returncode, 2)
self.assertIn("nicht gefunden", result.stderr)
def test_lint_output_format(self):
with tempfile.NamedTemporaryFile(suffix=".py", mode="w",
encoding="utf-8", delete=False) as f:
f.write("import os\nimport os\n")
f.flush()
path = f.name
try:
result = run_lint(path)
if result.returncode == 1:
lines = result.stdout.strip().split("\n")
finding_lines = [l for l in lines if path in l]
for line in finding_lines:
self.assertRegex(line, r".+:\d+:\d+: \S+ .+")
finally:
os.unlink(path)
def test_no_gui_started(self):
with tempfile.NamedTemporaryFile(suffix=".py", mode="w",
encoding="utf-8", delete=False) as f:
f.write("x = 1\n")
f.flush()
path = f.name
try:
result = run_lint(path)
self.assertNotIn("QApplication", result.stderr)
self.assertNotIn("Traceback", result.stderr)
finally:
os.unlink(path)
class TestParseCLIArgs(unittest.TestCase):
def test_open_flag(self):
args = parse_cli_args(["--open", "test.py"])
self.assertEqual(args.open, "test.py")
self.assertIsNone(args.lint)
def test_lint_flag(self):
args = parse_cli_args(["--lint", "test.py"])
self.assertEqual(args.lint, "test.py")
self.assertIsNone(args.open)
def test_positional_file(self):
args = parse_cli_args(["test.py"])
self.assertEqual(args.open, "test.py")
self.assertIsNone(args.lint)
def test_no_args(self):
args = parse_cli_args([])
self.assertIsNone(args.open)
self.assertIsNone(args.lint)
self.assertIsNone(args.run)
self.assertIsNone(args.theme)
def test_unknown_args_preserved(self):
args = parse_cli_args(["--open", "test.py", "-style", "fusion"])
self.assertEqual(args.open, "test.py")
self.assertIn("-style", args._remaining)
def test_run_flag(self):
args = parse_cli_args(["--run", "tool.py"])
self.assertEqual(args.run, "tool.py")
self.assertIsNone(args.open)
self.assertIsNone(args.lint)
def test_theme_flag_is_normalized(self):
args = parse_cli_args(["--theme", "light"])
self.assertEqual(args.theme, "Light")
def test_theme_flag_preserves_qt_args(self):
args = parse_cli_args(["--theme", "dracula", "-style", "fusion"])
self.assertEqual(args.theme, "Dracula")
self.assertIn("-style", args._remaining)
def test_lint_with_real_flake8(self):
with tempfile.NamedTemporaryFile(suffix=".py", mode="w",
encoding="utf-8", delete=False) as f:
f.write("import os\nimport os\n")
f.flush()
path = f.name
try:
result = run_lint(path)
if result.returncode == 1:
lines = result.stdout.strip().split("\n")
finding_lines = [l for l in lines if path in l]
self.assertTrue(len(finding_lines) > 0,
"flake8 should find duplicate import")
for line in finding_lines:
self.assertRegex(line, r".+:\d+:\d+: \S+ .+")
finally:
os.unlink(path)
class TestLinterDetection(unittest.TestCase):
@patch("shutil.which", return_value=None)
@patch("subprocess.run")
def test_missing_flake8_not_detected(self, mock_run, mock_which):
mock_result = MagicMock()
mock_result.returncode = 1
mock_run.return_value = mock_result
runner = LinterRunner.__new__(LinterRunner)
runner.has_flake8 = False
runner.has_pylint = False
runner._check_available_linters()
self.assertFalse(runner.has_flake8)
self.assertFalse(getattr(runner, '_flake8_via_module', False))
@patch("shutil.which", return_value=None)
@patch("subprocess.run")
def test_missing_pylint_not_detected(self, mock_run, mock_which):
mock_result = MagicMock()
mock_result.returncode = 1
mock_run.return_value = mock_result
runner = LinterRunner.__new__(LinterRunner)
runner.has_flake8 = False
runner.has_pylint = False
runner._check_available_linters()
self.assertFalse(runner.has_pylint)
self.assertFalse(getattr(runner, '_pylint_via_module', False))
@patch("shutil.which", return_value=None)
@patch("subprocess.run")
def test_module_flake8_detected_on_success(self, mock_run, mock_which):
mock_result = MagicMock()
mock_result.returncode = 0
mock_run.return_value = mock_result
runner = LinterRunner.__new__(LinterRunner)
runner.has_flake8 = False
runner.has_pylint = False
runner._check_available_linters()
self.assertTrue(runner.has_flake8)
self.assertTrue(runner._flake8_via_module)
class TestHeadlessRun(unittest.TestCase):
def test_run_executes_script_and_returns_exit_code(self):
with tempfile.NamedTemporaryFile(suffix=".py", mode="w",
encoding="utf-8", delete=False) as f:
f.write("import sys\nprint('run-ok')\nsys.exit(3)\n")
f.flush()
path = f.name
try:
result = run_headless(["--run", path])
self.assertEqual(result.returncode, 3)
self.assertIn("run-ok", result.stdout)
self.assertNotIn("QApplication", result.stderr)
finally:
os.unlink(path)
def test_run_forwards_extra_arguments(self):
with tempfile.NamedTemporaryFile(suffix=".py", mode="w",
encoding="utf-8", delete=False) as f:
f.write("import sys\nprint('|'.join(sys.argv[1:]))\n")
f.flush()
path = f.name
try:
result = run_headless(["--run", path, "eins", "zwei"])
self.assertEqual(result.returncode, 0)
self.assertIn("eins|zwei", result.stdout)
finally:
os.unlink(path)
if __name__ == "__main__":
unittest.main()