forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_subprocess.py
More file actions
553 lines (439 loc) · 18.6 KB
/
Copy pathtest_cli_subprocess.py
File metadata and controls
553 lines (439 loc) · 18.6 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
"""Subprocess CLI tests — real process execution to catch platform-specific bugs.
These tests run `soup` (or `python -m soup_cli`) as a real subprocess,
catching issues that in-process CliRunner misses:
- encoding bugs (cp1251/cp1252 on Windows)
- path separator issues
- pipe buffering / stdout encoding
- entry-point script resolution
- exit code propagation through the OS shell
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
import pytest
# Use `python -m soup_cli` for reliability across platforms (no need for
# the entry-point script to be installed or on PATH).
SOUP_CMD = [sys.executable, "-m", "soup_cli"]
# Timeout for all subprocess calls (seconds). Tests should be fast —
# these are smoke-level checks, not training runs.
TIMEOUT = 30
def run_soup(
*args: str, timeout: int = TIMEOUT, env: dict | None = None,
) -> subprocess.CompletedProcess:
"""Run soup CLI as a subprocess and return CompletedProcess."""
merged_env = {**os.environ, **(env or {})}
# Force UTF-8 in the child process (affects print / Rich Console output)
merged_env["PYTHONIOENCODING"] = "utf-8"
merged_env["PYTHONLEGACYWINDOWSSTDIO"] = "0"
return subprocess.run(
[*SOUP_CMD, *args],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=timeout,
env=merged_env,
# Decode as UTF-8 on the *parent* side too — avoids cp1251/cp1252
# failing on Rich box-drawing characters.
encoding="utf-8",
errors="replace",
)
# ---------------------------------------------------------------------------
# Basic entry-point tests
# ---------------------------------------------------------------------------
class TestEntryPoint:
"""Verify the CLI launches correctly as a real process."""
def test_no_args_shows_help(self):
result = run_soup()
# Typer with no_args_is_help=True returns exit code 0 or 2
assert result.returncode in (0, 2)
combined = (result.stdout or "") + (result.stderr or "")
assert "Fine-tune" in combined or "Usage" in combined
def test_help_flag(self):
result = run_soup("--help")
assert result.returncode == 0
assert "Fine-tune" in result.stdout
def test_version(self):
from soup_cli import __version__
result = run_soup("version")
assert result.returncode == 0
assert __version__ in result.stdout
def test_version_full(self):
result = run_soup("version", "--full")
assert result.returncode == 0
assert "Python" in result.stdout
def test_unknown_command_fails(self):
result = run_soup("nonexistent_command_xyz")
assert result.returncode != 0
# ---------------------------------------------------------------------------
# Command --help tests (every command must print help without crashing)
# ---------------------------------------------------------------------------
ALL_COMMANDS = [
"init",
"train",
"chat",
"push",
"export",
"merge",
"eval",
"serve",
"sweep",
"diff",
"infer",
"doctor",
"quickstart",
"ui",
"version",
]
ALL_DATA_SUBCOMMANDS = [
"inspect",
"validate",
"convert",
"merge",
"dedup",
"stats",
"generate",
]
ALL_RUNS_SUBCOMMANDS = [
"show",
"compare",
"delete",
]
class TestCommandHelp:
"""Every registered command must respond to --help without crashing."""
@pytest.mark.parametrize("cmd", ALL_COMMANDS)
def test_command_help(self, cmd):
result = run_soup(cmd, "--help")
assert result.returncode == 0, (
f"`soup {cmd} --help` failed (rc={result.returncode}):\n{result.stderr}"
)
# Should contain some usage info
assert len(result.stdout) > 20, f"`soup {cmd} --help` output too short"
@pytest.mark.parametrize("subcmd", ALL_DATA_SUBCOMMANDS)
def test_data_subcommand_help(self, subcmd):
result = run_soup("data", subcmd, "--help")
assert result.returncode == 0, (
f"`soup data {subcmd} --help` failed (rc={result.returncode}):\n{result.stderr}"
)
@pytest.mark.parametrize("subcmd", ALL_RUNS_SUBCOMMANDS)
def test_runs_subcommand_help(self, subcmd):
result = run_soup("runs", subcmd, "--help")
assert result.returncode == 0, (
f"`soup runs {subcmd} --help` failed (rc={result.returncode}):\n{result.stderr}"
)
def test_data_no_args_shows_help(self):
result = run_soup("data")
# Typer returns 0 or 2 depending on no_args_is_help vs missing args
assert result.returncode in (0, 2)
combined = (result.stdout or "") + (result.stderr or "")
assert "inspect" in combined.lower() or "usage" in combined.lower()
def test_runs_lists_or_shows_help(self):
result = run_soup("runs")
# runs without subcommand lists experiments (may be empty) — exit 0
assert result.returncode == 0
# ---------------------------------------------------------------------------
# Error handling / exit code tests
# ---------------------------------------------------------------------------
class TestErrorHandling:
"""Verify errors produce correct exit codes and readable messages."""
def test_train_missing_config(self):
result = run_soup("train", "--config", "nonexistent_file.yaml")
assert result.returncode == 1
def test_chat_missing_model(self):
result = run_soup("chat", "--model", "nonexistent_model_path")
assert result.returncode == 1
def test_push_missing_model(self):
result = run_soup(
"push", "--model", "nonexistent_model_path", "--repo", "user/model"
)
assert result.returncode == 1
def test_init_unknown_template(self):
result = run_soup("init", "--template", "nonexistent_template")
assert result.returncode == 1
def test_export_missing_model(self):
result = run_soup("export", "--model", "nonexistent_model_path")
assert result.returncode == 1
def test_merge_missing_adapter(self):
result = run_soup("merge", "--adapter", "nonexistent_adapter_path")
assert result.returncode == 1
def test_infer_missing_files(self):
result = run_soup(
"infer",
"--model", "nonexistent_model",
"--input", "nonexistent.jsonl",
"--output", "out.jsonl",
)
assert result.returncode == 1
# ---------------------------------------------------------------------------
# Encoding / Unicode safety on all platforms
# ---------------------------------------------------------------------------
class TestEncoding:
"""Verify output contains no problematic Unicode on any platform."""
PROBLEMATIC_CHARS = [
"\u2192", # → right arrow
"\u2014", # — em dash
"\u2018", # ' left single quote
"\u2019", # ' right single quote
"\u201c", # " left double quote
"\u201d", # " right double quote
]
def test_version_output_is_ascii_safe(self):
result = run_soup("version")
assert result.returncode == 0
for char in self.PROBLEMATIC_CHARS:
assert char not in result.stdout, (
f"Version output contains problematic Unicode char: {repr(char)}"
)
def test_help_output_is_ascii_safe(self):
result = run_soup("--help")
assert result.returncode == 0
for char in self.PROBLEMATIC_CHARS:
assert char not in result.stdout, (
f"Help output contains problematic Unicode char: {repr(char)}"
)
def test_error_output_is_ascii_safe(self):
"""Error messages must be safe for Windows terminals."""
result = run_soup("train", "--config", "nonexistent.yaml")
combined = result.stdout + result.stderr
for char in self.PROBLEMATIC_CHARS:
assert char not in combined, (
f"Error output contains problematic Unicode char: {repr(char)}"
)
# ---------------------------------------------------------------------------
# Data command — real file I/O
# ---------------------------------------------------------------------------
class TestDataCommands:
"""Test data subcommands with actual file operations."""
def test_data_inspect_jsonl(self, tmp_path):
"""Inspect a small JSONL file via subprocess."""
data_file = tmp_path / "test.jsonl"
rows = [
{"messages": [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"},
]}
for _ in range(5)
]
data_file.write_text(
"\n".join(json.dumps(r) for r in rows), encoding="utf-8"
)
result = run_soup("data", "inspect", str(data_file))
assert result.returncode == 0
assert "5" in result.stdout # row count
def test_data_inspect_nonexistent_file(self):
result = run_soup("data", "inspect", "nonexistent_file.jsonl")
assert result.returncode == 1
def test_data_validate_jsonl(self, tmp_path):
"""Validate a well-formed JSONL file."""
data_file = tmp_path / "valid.jsonl"
rows = [
{"messages": [
{"role": "user", "content": f"Q{i}"},
{"role": "assistant", "content": f"A{i}"},
]}
for i in range(3)
]
data_file.write_text(
"\n".join(json.dumps(r) for r in rows), encoding="utf-8"
)
result = run_soup("data", "validate", str(data_file))
assert result.returncode == 0
def test_data_stats_jsonl(self, tmp_path):
"""Stats command should work without encoding errors on any OS."""
data_file = tmp_path / "stats.jsonl"
rows = [
{"messages": [
{"role": "user", "content": f"Question {i}"},
{"role": "assistant", "content": f"Answer {i} " + "x" * (i * 10)},
]}
for i in range(10)
]
data_file.write_text(
"\n".join(json.dumps(r) for r in rows), encoding="utf-8"
)
result = run_soup("data", "stats", str(data_file))
assert result.returncode == 0, (
f"data stats failed (rc={result.returncode}):\n{result.stderr}"
)
# ---------------------------------------------------------------------------
# Init command — file creation
# ---------------------------------------------------------------------------
class TestInitCommand:
"""Test init command creates files correctly."""
TEMPLATES = ["chat", "code", "medical", "reasoning", "vision"]
@pytest.mark.parametrize("template", TEMPLATES)
def test_init_creates_config_file(self, tmp_path, template):
"""soup init --template X should create a valid YAML config."""
result = run_soup(
"init",
"--template", template,
"--output", str(tmp_path / f"{template}.yaml"),
)
assert result.returncode == 0, (
f"init --template {template} failed:\n{result.stderr}"
)
config_file = tmp_path / f"{template}.yaml"
assert config_file.exists(), f"Config file not created for template {template}"
content = config_file.read_text(encoding="utf-8")
assert "base:" in content
def test_init_default_output(self, tmp_path, monkeypatch):
"""soup init --template chat writes soup.yaml in cwd."""
monkeypatch.chdir(tmp_path)
result = run_soup("init", "--template", "chat")
assert result.returncode == 0
# ---------------------------------------------------------------------------
# Doctor command — system check
# ---------------------------------------------------------------------------
class TestDoctorCommand:
"""Doctor should always succeed (even without GPU)."""
def test_doctor_runs(self):
# doctor imports torch and checks GPU — allow extra time
result = run_soup("doctor", timeout=120)
assert result.returncode == 0
# Should mention Python at minimum
assert "python" in result.stdout.lower() or "Python" in result.stdout
# ---------------------------------------------------------------------------
# Verbose flag
# ---------------------------------------------------------------------------
class TestVerboseFlag:
"""--verbose / -V flag should not crash."""
def test_verbose_with_version(self):
result = run_soup("--verbose", "version")
assert result.returncode == 0
def test_verbose_short_flag(self):
result = run_soup("-V", "version")
assert result.returncode == 0
def test_verbose_error_shows_traceback(self):
result = run_soup("--verbose", "train", "--config", "nonexistent.yaml")
assert result.returncode == 1
# Verbose mode should show traceback details
combined = result.stdout + result.stderr
assert len(combined) > 0
# ---------------------------------------------------------------------------
# Platform-specific regression tests
# ---------------------------------------------------------------------------
class TestPlatformRegression:
"""Regressions that appeared on specific platforms."""
def test_stdout_encoding_no_crash(self):
"""Verify stdout doesn't crash with encoding issues (Windows cp1252 bug)."""
result = run_soup("version", "--full")
assert result.returncode == 0
# Output should be decodable text, no mojibake
assert result.stdout.isprintable() or "\n" in result.stdout
def test_path_with_spaces(self, tmp_path):
"""Paths with spaces must work on all OSes."""
spaced_dir = tmp_path / "path with spaces"
spaced_dir.mkdir()
data_file = spaced_dir / "test.jsonl"
data_file.write_text(
json.dumps({"messages": [
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hello"},
]}),
encoding="utf-8",
)
result = run_soup("data", "inspect", str(data_file))
assert result.returncode == 0
@pytest.mark.skipif(sys.platform != "win32", reason="Windows-only test")
def test_windows_long_path(self, tmp_path):
"""Long paths should work on Windows (>100 chars)."""
long_dir = tmp_path
for i in range(5):
long_dir = long_dir / f"subdirectory_level_{i}_name"
long_dir.mkdir()
data_file = long_dir / "data.jsonl"
data_file.write_text(
json.dumps({"messages": [
{"role": "user", "content": "Test"},
{"role": "assistant", "content": "OK"},
]}),
encoding="utf-8",
)
assert len(str(data_file)) > 100
result = run_soup("data", "inspect", str(data_file))
assert result.returncode == 0
def test_unicode_in_data_file(self, tmp_path):
"""Data files with Unicode content must load on all platforms."""
data_file = tmp_path / "unicode.jsonl"
rows = [
{"messages": [
{"role": "user", "content": "Wie geht es dir?"},
{"role": "assistant", "content": "Mir geht es gut, danke!"},
]},
{"messages": [
{"role": "user", "content": "Как дела?"},
{"role": "assistant", "content": "Хорошо, спасибо!"},
]},
{"messages": [
{"role": "user", "content": "お元気ですか?"},
{"role": "assistant", "content": "元気です、ありがとう!"},
]},
]
data_file.write_text(
"\n".join(json.dumps(r, ensure_ascii=False) for r in rows),
encoding="utf-8",
)
result = run_soup("data", "inspect", str(data_file))
assert result.returncode == 0
def test_empty_jsonl_file(self, tmp_path):
"""Empty data file should not crash (may show 0 rows)."""
data_file = tmp_path / "empty.jsonl"
data_file.write_text("", encoding="utf-8")
result = run_soup("data", "inspect", str(data_file))
# Should complete without crashing — exit 0 (0 rows) or 1 (error)
assert result.returncode in (0, 1)
# ---------------------------------------------------------------------------
# Quickstart --dry-run
# ---------------------------------------------------------------------------
class TestQuickstart:
"""Quickstart command basic checks."""
def test_quickstart_help(self):
result = run_soup("quickstart", "--help")
assert result.returncode == 0
assert "dry" in result.stdout.lower() or "demo" in result.stdout.lower()
# ---------------------------------------------------------------------------
# Sweep command
# ---------------------------------------------------------------------------
class TestSweepCommand:
"""Sweep command validation."""
def test_sweep_missing_config(self):
result = run_soup(
"sweep", "--config", "nonexistent.yaml",
"--param", "lr=1e-4,2e-4",
)
assert result.returncode == 1
def test_sweep_help(self):
result = run_soup("sweep", "--help")
assert result.returncode == 0
# ---------------------------------------------------------------------------
# Diff command
# ---------------------------------------------------------------------------
class TestDiffCommand:
"""Diff command validation."""
def test_diff_help(self):
result = run_soup("diff", "--help")
assert result.returncode == 0
assert "model" in result.stdout.lower() or "compare" in result.stdout.lower()
# ---------------------------------------------------------------------------
# Eval command
# ---------------------------------------------------------------------------
class TestEvalCommand:
"""Eval command validation."""
def test_eval_help(self):
result = run_soup("eval", "--help")
assert result.returncode == 0
def test_eval_missing_model(self):
result = run_soup("eval", "benchmark", "--model", "nonexistent_model")
assert result.returncode == 1
# ---------------------------------------------------------------------------
# Serve / UI commands
# ---------------------------------------------------------------------------
class TestServeUI:
"""Serve and UI commands help checks (don't actually start servers)."""
def test_serve_help(self):
result = run_soup("serve", "--help")
assert result.returncode == 0
assert "backend" in result.stdout.lower() or "model" in result.stdout.lower()
def test_ui_help(self):
result = run_soup("ui", "--help")
assert result.returncode == 0