forked from hyphen-2025/cyber-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_check_language.py
More file actions
398 lines (321 loc) · 14.4 KB
/
test_check_language.py
File metadata and controls
398 lines (321 loc) · 14.4 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""Tests for the check-language CLI command.
Covers:
- cmd_check_language() — argument parsing, exit codes
- _human_result() — human formatter
- _count_md_files() — file counting helper
- _default_roots() — fallback root resolution
- _read_config_languages() — config-driven language loading
"""
import sys
import io
import unittest
from contextlib import redirect_stdout, redirect_stderr
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import MagicMock, patch
sys.path.insert(0, str(Path(__file__).parent.parent / "skills" / "cypilot" / "scripts"))
from cypilot.commands.check_language import (
cmd_check_language,
_count_md_files,
_default_roots,
_human_result,
_read_config_languages,
_read_config_ignore_patterns,
)
def _run(argv, capture=True):
"""Execute cmd_check_language, suppressing all output by default."""
if capture:
out = io.StringIO()
err = io.StringIO()
with redirect_stdout(out), redirect_stderr(err):
code = cmd_check_language(argv)
return code, out.getvalue(), err.getvalue()
return cmd_check_language(argv), "", ""
class TestCmdCheckLanguagePassCases(unittest.TestCase):
"""cmd_check_language() returns 0 for clean English files."""
def setUp(self):
self._tmpdir = TemporaryDirectory()
self.root = Path(self._tmpdir.name)
def tearDown(self):
self._tmpdir.cleanup()
def _md(self, name: str, content: str):
p = self.root / name
p.write_text(content, encoding="utf-8")
return p
def test_english_file_passes(self):
p = self._md("doc.md", "# Title\n\nHello world.\n")
code, _, _ = _run(["--languages", "en", str(p)])
self.assertEqual(code, 0)
def test_empty_directory_passes(self):
empty = self.root / "empty"
empty.mkdir()
code, _, _ = _run(["--languages", "en", str(empty)])
self.assertEqual(code, 0)
def test_bilingual_file_passes_with_both_langs(self):
p = self._md("bi.md", "Hello\nПривет\n")
code, _, _ = _run(["--languages", "en,ru", str(p)])
self.assertEqual(code, 0)
def test_quiet_flag_does_not_change_exit_code(self):
p = self._md("clean.md", "Clean English.\n")
code, _, _ = _run(["--languages", "en", "--quiet", str(p)])
self.assertEqual(code, 0)
class TestCmdCheckLanguageViolations(unittest.TestCase):
"""cmd_check_language() returns 2 when violations are found."""
def setUp(self):
self._tmpdir = TemporaryDirectory()
self.root = Path(self._tmpdir.name)
def tearDown(self):
self._tmpdir.cleanup()
def _md(self, name: str, content: str):
p = self.root / name
p.write_text(content, encoding="utf-8")
return p
def test_cyrillic_in_english_doc_returns_2(self):
p = self._md("doc.md", "# Title\n\nПривет мир\n")
code, _, _ = _run(["--languages", "en", str(p)])
self.assertEqual(code, 2)
def test_multiple_violations_still_return_2(self):
p = self._md("doc.md", "Строка 1\nСтрока 2\n")
code, _, _ = _run(["--languages", "en", str(p)])
self.assertEqual(code, 2)
def test_quiet_flag_with_violations_returns_2(self):
p = self._md("doc.md", "Привет\n")
code, _, _ = _run(["--languages", "en", "--quiet", str(p)])
self.assertEqual(code, 2)
class TestCmdCheckLanguageErrors(unittest.TestCase):
"""cmd_check_language() returns 1 on configuration / path errors."""
def setUp(self):
self._tmpdir = TemporaryDirectory()
self.root = Path(self._tmpdir.name)
def tearDown(self):
self._tmpdir.cleanup()
def test_unknown_language_returns_1(self):
code, _, _ = _run(["--languages", "xx_INVALID", str(self.root)])
self.assertEqual(code, 1)
def test_nonexistent_path_returns_1(self):
missing = str(self.root / "no_such_path")
code, _, _ = _run(["--languages", "en", missing])
self.assertEqual(code, 1)
def test_mixed_known_unknown_langs_returns_1(self):
code, _, _ = _run(["--languages", "en,xx,yy", str(self.root)])
self.assertEqual(code, 1)
class TestCountMdFiles(unittest.TestCase):
"""_count_md_files() correctly counts .md files."""
def setUp(self):
self._tmpdir = TemporaryDirectory()
self.root = Path(self._tmpdir.name)
def tearDown(self):
self._tmpdir.cleanup()
def test_count_single_md_file(self):
p = self.root / "a.md"
p.write_text("x")
self.assertEqual(_count_md_files([p]), 1)
def test_count_non_md_file_is_zero(self):
p = self.root / "a.py"
p.write_text("x")
self.assertEqual(_count_md_files([p]), 0)
def test_count_directory_recursive(self):
sub = self.root / "sub"
sub.mkdir()
(sub / "a.md").write_text("x")
(sub / "b.md").write_text("x")
(sub / "c.py").write_text("x")
self.assertEqual(_count_md_files([self.root]), 2)
def test_empty_directory(self):
self.assertEqual(_count_md_files([self.root]), 0)
def test_multiple_roots(self):
p1 = self.root / "a.md"
p2 = self.root / "b.md"
p1.write_text("x")
p2.write_text("x")
self.assertEqual(_count_md_files([p1, p2]), 2)
class TestDefaultRoots(unittest.TestCase):
"""_default_roots() returns fallback path when no context is set."""
def test_no_context_falls_back_to_cwd(self):
with patch("cypilot.utils.context.get_context", return_value=None):
roots = _default_roots()
self.assertIsInstance(roots, list)
self.assertGreater(len(roots), 0)
def test_with_context_returns_architecture_dir(self):
mock_ctx = MagicMock()
mock_ctx.project_root = Path("/fake/project")
with patch("cypilot.utils.context.get_context", return_value=mock_ctx):
roots = _default_roots()
self.assertEqual(roots, [Path("/fake/project") / "architecture"])
class TestReadConfigLanguages(unittest.TestCase):
"""_read_config_languages() reads from workspace config or returns default."""
def test_no_context_returns_default_english(self):
with patch("cypilot.utils.context.get_context", return_value=None):
langs = _read_config_languages()
self.assertEqual(langs, ["en"])
def test_workspace_config_error_raises_value_error(self):
mock_ctx = MagicMock()
mock_ctx.project_root = Path("/fake/project")
with patch("cypilot.utils.context.get_context", return_value=mock_ctx):
with patch("cypilot.utils.workspace.find_workspace_config", return_value=(None, "bad config")):
with self.assertRaises(ValueError):
_read_config_languages()
def test_no_workspace_config_returns_english(self):
mock_ctx = MagicMock()
mock_ctx.project_root = Path("/fake/project")
with patch("cypilot.utils.context.get_context", return_value=mock_ctx):
with patch("cypilot.utils.workspace.find_workspace_config", return_value=(None, None)):
langs = _read_config_languages()
self.assertEqual(langs, ["en"])
def test_workspace_config_with_languages_returns_them(self):
mock_ctx = MagicMock()
mock_ctx.project_root = Path("/fake/project")
mock_cfg = MagicMock()
mock_cfg.validation = MagicMock()
mock_cfg.validation.allowed_content_languages = ["en", "ru"]
with patch("cypilot.utils.context.get_context", return_value=mock_ctx):
with patch("cypilot.utils.workspace.find_workspace_config", return_value=(mock_cfg, None)):
langs = _read_config_languages()
self.assertEqual(langs, ["en", "ru"])
def test_workspace_config_no_validation_returns_english(self):
mock_ctx = MagicMock()
mock_ctx.project_root = Path("/fake/project")
mock_cfg = MagicMock()
mock_cfg.validation = None
with patch("cypilot.utils.context.get_context", return_value=mock_ctx):
with patch("cypilot.utils.workspace.find_workspace_config", return_value=(mock_cfg, None)):
langs = _read_config_languages()
self.assertEqual(langs, ["en"])
class TestHumanResult(unittest.TestCase):
"""_human_result() renders output without raising exceptions."""
def _capture(self, data, quiet=False):
out = io.StringIO()
err = io.StringIO()
with redirect_stdout(out), redirect_stderr(err):
_human_result(data, quiet=quiet)
return out.getvalue() + err.getvalue()
def test_pass_status_renders(self):
data = {"status": "PASS", "allowed_languages": ["en"], "files_scanned": 3}
output = self._capture(data)
self.assertIsInstance(output, str)
def test_fail_status_renders_violations(self):
data = {
"status": "FAIL",
"allowed_languages": ["en"],
"files_scanned": 1,
"violation_count": 1,
"file_count": 1,
"violations": [
{"path": "/tmp/doc.md", "line": 5, "chars": "Привет", "preview": "Привет мир"}
],
}
output = self._capture(data)
self.assertIsInstance(output, str)
def test_error_status_renders(self):
data = {"status": "ERROR", "message": "Something went wrong"}
output = self._capture(data)
self.assertIsInstance(output, str)
def test_quiet_mode_suppresses_header(self):
data = {"status": "PASS", "allowed_languages": ["en"], "files_scanned": 0}
output_normal = self._capture(data, quiet=False)
output_quiet = self._capture(data, quiet=True)
# Quiet mode should produce less or equal output
self.assertLessEqual(len(output_quiet), len(output_normal))
class TestReadConfigIgnorePatterns(unittest.TestCase):
"""_read_config_ignore_patterns() reads ignore patterns from workspace config."""
def test_no_context_returns_empty_list(self):
with patch("cypilot.utils.context.get_context", return_value=None):
patterns = _read_config_ignore_patterns()
self.assertEqual(patterns, [])
def test_workspace_config_error_raises_value_error(self):
mock_ctx = MagicMock()
mock_ctx.project_root = Path("/fake/project")
with patch("cypilot.utils.context.get_context", return_value=mock_ctx):
with patch(
"cypilot.utils.workspace.find_workspace_config",
return_value=(None, "bad config"),
):
with self.assertRaises(ValueError):
_read_config_ignore_patterns()
def test_workspace_config_with_ignore_paths_returns_them(self):
mock_ctx = MagicMock()
mock_ctx.project_root = Path("/fake/project")
mock_cfg = MagicMock()
mock_cfg.validation = MagicMock()
mock_cfg.validation.ignore_paths = ["docs/translations/**", "*.i18n.md"]
with patch("cypilot.utils.context.get_context", return_value=mock_ctx):
with patch(
"cypilot.utils.workspace.find_workspace_config",
return_value=(mock_cfg, None),
):
patterns = _read_config_ignore_patterns()
self.assertEqual(patterns, ["docs/translations/**", "*.i18n.md"])
def test_no_workspace_config_returns_empty_list(self):
mock_ctx = MagicMock()
mock_ctx.project_root = Path("/fake/project")
with patch("cypilot.utils.context.get_context", return_value=mock_ctx):
with patch(
"cypilot.utils.workspace.find_workspace_config",
return_value=(None, None),
):
patterns = _read_config_ignore_patterns()
self.assertEqual(patterns, [])
class TestDefaultRootsExceptionBranch(unittest.TestCase):
"""_default_roots() falls back to cwd when get_context raises."""
def test_import_error_falls_back_to_cwd(self):
with patch("cypilot.utils.context.get_context", side_effect=ImportError("no module")):
roots = _default_roots()
self.assertIsInstance(roots, list)
self.assertGreater(len(roots), 0)
def test_attribute_error_falls_back_to_cwd(self):
with patch("cypilot.utils.context.get_context", side_effect=AttributeError("no attr")):
roots = _default_roots()
self.assertIsInstance(roots, list)
self.assertGreater(len(roots), 0)
class TestCmdCheckLanguageConfigErrors(unittest.TestCase):
"""cmd_check_language() returns 1 when config loading fails."""
def setUp(self):
self._tmpdir = TemporaryDirectory()
self.root = Path(self._tmpdir.name)
def tearDown(self):
self._tmpdir.cleanup()
def test_config_language_error_returns_1(self):
with patch(
"cypilot.commands.check_language._read_config_languages",
side_effect=ValueError("bad config"),
):
code, _, _ = _run([str(self.root)])
self.assertEqual(code, 1)
def test_config_ignore_patterns_error_returns_1(self):
with patch(
"cypilot.commands.check_language._read_config_languages",
return_value=["en"],
):
with patch(
"cypilot.commands.check_language._read_config_ignore_patterns",
side_effect=ValueError("bad ignore config"),
):
code, _, _ = _run([str(self.root)])
self.assertEqual(code, 1)
def test_lang_scan_error_returns_1(self):
from cypilot.utils.content_language import LangScanError
with patch(
"cypilot.utils.content_language.scan_paths",
side_effect=LangScanError(Path("/fake/file.md"), OSError("perm denied")),
):
code, _, _ = _run(["--languages", "en", str(self.root)])
self.assertEqual(code, 1)
def test_no_paths_uses_default_roots(self):
arch = self.root / "architecture"
arch.mkdir()
with patch(
"cypilot.commands.check_language._default_roots",
return_value=[arch],
):
with patch(
"cypilot.commands.check_language._read_config_languages",
return_value=["en"],
):
with patch(
"cypilot.commands.check_language._read_config_ignore_patterns",
return_value=[],
):
code, _, _ = _run([])
self.assertEqual(code, 0)
if __name__ == "__main__":
unittest.main()