-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_cli_integration.py
More file actions
839 lines (666 loc) · 27.5 KB
/
test_cli_integration.py
File metadata and controls
839 lines (666 loc) · 27.5 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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
"""
CLI command integration tests.
Tests covering:
- CLI argument parsing
- Output formatters (JSON, Text, CSV)
- Input/output file handling
- Command invocations (check, build, completion, config)
- Error handling and exit codes
"""
import io
import json
import sys
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from myspellchecker.cli import (
CSVFormatter,
Formatter,
JSONFormatter,
TextFormatter,
confidence_type,
generate_completion_script,
get_checker,
get_config_from_preset,
open_input_file,
open_output_file,
process_stream,
validate_build_inputs,
)
from myspellchecker.core.constants import ValidationLevel
class TestArgumentParsing:
"""Test CLI argument parsing and validation."""
def test_confidence_type_valid_values(self):
"""Test confidence_type with valid float values."""
assert confidence_type("0.0") == 0.0
assert confidence_type("1.0") == 1.0
assert confidence_type("0.5") == 0.5
assert confidence_type("0.75") == 0.75
def test_confidence_type_invalid_string(self):
"""Test confidence_type with non-numeric string."""
import argparse
with pytest.raises(argparse.ArgumentTypeError, match="Invalid float value"):
confidence_type("not-a-number")
def test_confidence_type_out_of_range(self):
"""Test confidence_type with values outside 0-1 range."""
import argparse
with pytest.raises(argparse.ArgumentTypeError, match="must be between"):
confidence_type("1.5")
with pytest.raises(argparse.ArgumentTypeError, match="must be between"):
confidence_type("-0.1")
class TestFileHandling:
"""Test input/output file handling functions."""
def test_open_input_file_none_returns_stdin(self):
"""Test open_input_file returns stdin when path is None."""
result = open_input_file(None)
assert result is sys.stdin
def test_open_input_file_valid_file(self):
"""Test open_input_file opens valid file."""
with tempfile.NamedTemporaryFile(
mode="w", suffix=".txt", delete=False, encoding="utf-8"
) as f:
f.write("test content")
path = f.name
try:
result = open_input_file(path)
assert result is not None
content = result.read()
assert content == "test content"
result.close()
finally:
Path(path).unlink()
def test_open_input_file_nonexistent(self):
"""Test open_input_file with nonexistent file exits."""
with pytest.raises(SystemExit) as exc_info:
open_input_file("/nonexistent/path/file.txt")
assert exc_info.value.code == 2
def test_open_output_file_none_returns_stdout(self):
"""Test open_output_file returns stdout when path is None."""
result = open_output_file(None)
assert result is sys.stdout
def test_open_output_file_valid_path(self):
"""Test open_output_file creates file."""
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / "output.txt"
result = open_output_file(str(path))
assert result is not None
result.write("test")
result.close()
assert path.exists()
assert path.read_text() == "test"
class TestOutputFormatters:
"""Test output formatter implementations."""
def test_formatter_base_class(self):
"""Test Formatter base class methods."""
formatter = Formatter()
output = io.StringIO()
# Base class methods should not raise
formatter.begin(output)
formatter.write_result(output, None, 1, "test.txt")
formatter.end(output, {})
def test_json_formatter_empty_results(self):
"""Test JSONFormatter with no results."""
formatter = JSONFormatter()
output = io.StringIO()
formatter.end(output, {"total_lines": 0, "total_errors": 0})
result = json.loads(output.getvalue())
assert result["summary"]["total_lines"] == 0
assert result["summary"]["total_errors"] == 0
assert result["results"] == []
def test_json_formatter_with_results(self):
"""Test JSONFormatter with results."""
formatter = JSONFormatter()
output = io.StringIO()
# Mock result object
mock_result = MagicMock()
mock_result.text = "test text"
mock_result.has_errors = True
mock_error = MagicMock()
mock_error.to_dict.return_value = {"type": "syllable", "text": "test"}
mock_result.errors = [mock_error]
formatter.write_result(output, mock_result, 1, "test.txt")
formatter.end(output, {"total_lines": 1, "total_errors": 1})
result = json.loads(output.getvalue())
assert len(result["results"]) == 1
assert result["results"][0]["file"] == "test.txt"
assert result["results"][0]["line"] == 1
assert result["results"][0]["has_errors"] is True
def test_text_formatter_begin(self):
"""Test TextFormatter begin method outputs warning."""
formatter = TextFormatter()
output = io.StringIO()
formatter.begin(output)
content = output.getvalue()
assert "WARNING" in content
assert "Myanmar" in content
def test_text_formatter_no_errors(self):
"""Test TextFormatter writes nothing for results without errors."""
formatter = TextFormatter()
output = io.StringIO()
mock_result = MagicMock()
mock_result.has_errors = False
formatter.write_result(output, mock_result, 1, "test.txt")
assert output.getvalue() == ""
def test_text_formatter_with_errors(self):
"""Test TextFormatter writes errors in grep-like format."""
formatter = TextFormatter()
output = io.StringIO()
mock_result = MagicMock()
mock_result.has_errors = True
mock_error = MagicMock()
mock_error.position = 5
mock_error.error_type = "syllable"
mock_error.text = "test"
mock_error.suggestions = ["suggestion1", "suggestion2"]
mock_result.errors = [mock_error]
formatter.write_result(output, mock_result, 1, "test.txt")
content = output.getvalue()
assert "test.txt:1:5" in content
assert "syllable" in content
assert "suggestion1" in content
def test_text_formatter_end_summary(self):
"""Test TextFormatter end method outputs summary."""
formatter = TextFormatter()
output = io.StringIO()
formatter.end(output, {"total_errors": 5, "total_lines": 10})
content = output.getvalue()
assert "Summary" in content
assert "5 errors" in content
assert "10 lines" in content
def test_csv_formatter_begin(self):
"""Test CSVFormatter begin method outputs header row."""
formatter = CSVFormatter()
output = io.StringIO()
formatter.begin(output)
content = output.getvalue()
assert "file,line,position,error_type,text,suggestions" in content
def test_csv_formatter_sanitize_field(self):
"""Test CSVFormatter sanitizes fields to prevent formula injection."""
formatter = CSVFormatter()
# Fields starting with formula characters should be prefixed
assert formatter._sanitize_csv_field("=SUM(A1)") == "'=SUM(A1)"
assert formatter._sanitize_csv_field("+1234") == "'+1234"
assert formatter._sanitize_csv_field("-test") == "'-test"
assert formatter._sanitize_csv_field("@mention") == "'@mention"
# Normal fields should be unchanged
assert formatter._sanitize_csv_field("normal text") == "normal text"
assert formatter._sanitize_csv_field("") == ""
def test_csv_formatter_with_errors(self):
"""Test CSVFormatter writes CSV rows for errors."""
formatter = CSVFormatter()
output = io.StringIO()
formatter.begin(output)
mock_result = MagicMock()
mock_result.has_errors = True
mock_error = MagicMock()
mock_error.position = 5
mock_error.error_type = "syllable"
mock_error.text = "test"
mock_error.suggestions = ["s1", "s2"]
mock_result.errors = [mock_error]
formatter.write_result(output, mock_result, 1, "test.txt")
content = output.getvalue()
lines = content.strip().split("\n")
assert len(lines) == 2 # header + data row
assert "test.txt" in lines[1]
class TestConfigPresets:
"""Test configuration preset loading."""
def test_get_config_from_preset_default(self):
"""Test loading default preset."""
config = get_config_from_preset("default")
assert config is not None
def test_get_config_from_preset_fast(self):
"""Test loading fast preset."""
config = get_config_from_preset("fast")
assert config is not None
# Fast preset disables phonetic and context
assert config.use_phonetic is False
assert config.use_context_checker is False
def test_get_config_from_preset_accurate(self):
"""Test loading accurate preset."""
config = get_config_from_preset("accurate")
assert config is not None
def test_get_config_from_preset_minimal(self):
"""Test loading minimal preset."""
config = get_config_from_preset("minimal")
assert config is not None
def test_get_config_from_preset_strict(self):
"""Test loading strict preset."""
config = get_config_from_preset("strict")
assert config is not None
def test_get_config_from_preset_invalid(self):
"""Test loading invalid preset raises error."""
with pytest.raises(ValueError, match="Unknown preset"):
get_config_from_preset("invalid_preset")
class TestCompletionScripts:
"""Test shell completion script generation."""
def test_generate_bash_completion(self):
"""Test generating bash completion script."""
script = generate_completion_script("bash")
assert "complete -F _myspellchecker_completion myspellchecker" in script
assert "COMPREPLY" in script
assert "check" in script
assert "build" in script
def test_generate_zsh_completion(self):
"""Test generating zsh completion script."""
script = generate_completion_script("zsh")
assert "#compdef myspellchecker" in script
assert "_myspellchecker" in script
def test_generate_fish_completion(self):
"""Test generating fish completion script."""
script = generate_completion_script("fish")
assert "complete -c myspellchecker" in script
assert "__fish_use_subcommand" in script
def test_generate_completion_invalid_shell(self):
"""Test generating completion for unsupported shell."""
with pytest.raises(ValueError, match="Unsupported shell"):
generate_completion_script("powershell")
class TestBuildValidation:
"""Test build input validation."""
def test_validate_build_inputs_no_files(self):
"""Test validation with no input files."""
result = validate_build_inputs([], "output.db")
assert result["valid"] is True # Just warns, doesn't fail
assert len(result["warnings"]) > 0
def test_validate_build_inputs_missing_file(self):
"""Test validation with missing input file."""
result = validate_build_inputs(
["/nonexistent/file.txt"],
"output.db",
)
assert result["valid"] is False
assert any("not found" in err for err in result["errors"])
def test_validate_build_inputs_valid_file(self):
"""Test validation with valid input file."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
f.write("test content")
path = f.name
try:
result = validate_build_inputs([path], "output.db")
assert result["valid"] is True
assert result["stats"]["total_files"] == 1
finally:
Path(path).unlink()
def test_validate_build_inputs_empty_file_warning(self):
"""Test validation warns about empty files."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
# Write nothing - empty file
path = f.name
try:
result = validate_build_inputs([path], "output.db")
assert any("Empty file" in warn for warn in result["warnings"])
finally:
Path(path).unlink()
def test_validate_build_inputs_output_exists_warning(self):
"""Test validation warns when output file exists."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".db", delete=False) as f:
output_path = f.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
f.write("content")
input_path = f.name
try:
result = validate_build_inputs([input_path], output_path)
assert any("will be overwritten" in warn for warn in result["warnings"])
finally:
Path(output_path).unlink()
Path(input_path).unlink()
def test_validate_build_inputs_invalid_output_dir(self):
"""Test validation fails for invalid output directory."""
result = validate_build_inputs(
[],
"/nonexistent/directory/output.db",
)
assert result["valid"] is False
assert any("directory" in err.lower() for err in result["errors"])
class TestGetChecker:
"""Test SpellChecker initialization through CLI."""
def test_get_checker_default(self):
"""Test get_checker with default settings."""
# May use MemoryProvider if no default database exists
checker = get_checker()
assert checker is not None
checker.close()
def test_get_checker_with_flags(self):
"""Test get_checker with feature flags disabled."""
checker = get_checker(no_phonetic=True, no_context=True)
assert checker is not None
checker.close()
def test_get_checker_with_preset(self):
"""Test get_checker with preset."""
checker = get_checker(preset="fast")
assert checker is not None
checker.close()
def test_get_checker_invalid_db_exits(self):
"""Test get_checker with invalid database path exits."""
with pytest.raises(SystemExit) as exc_info:
get_checker(database_path="/nonexistent/path/db.sqlite")
assert exc_info.value.code == 1
class TestProcessStream:
"""Test stream processing function."""
def test_process_stream_empty_input(self):
"""Test processing empty input stream."""
input_stream = io.StringIO("")
output_stream = io.StringIO()
checker = get_checker(preset="fast")
formatter = JSONFormatter()
try:
stats = process_stream(
input_stream,
"test.txt",
checker,
formatter,
output_stream,
ValidationLevel.SYLLABLE,
)
assert stats["total_lines"] == 0
assert stats["total_errors"] == 0
finally:
checker.close()
def test_process_stream_blank_lines_skipped(self):
"""Test that blank lines are skipped."""
input_stream = io.StringIO("\n\n\n")
output_stream = io.StringIO()
checker = get_checker(preset="fast")
formatter = JSONFormatter()
try:
stats = process_stream(
input_stream,
"test.txt",
checker,
formatter,
output_stream,
ValidationLevel.SYLLABLE,
)
assert stats["total_lines"] == 0
finally:
checker.close()
def test_process_stream_with_content(self):
"""Test processing stream with content."""
input_stream = io.StringIO("test line 1\ntest line 2")
output_stream = io.StringIO()
checker = get_checker(preset="fast")
formatter = JSONFormatter()
try:
stats = process_stream(
input_stream,
"test.txt",
checker,
formatter,
output_stream,
ValidationLevel.SYLLABLE,
)
assert stats["total_lines"] == 2
finally:
checker.close()
class TestCLIMain:
"""Test CLI main function invocation."""
def test_main_help(self):
"""Test main with --help doesn't crash."""
from myspellchecker.cli import main
with patch.object(sys, "argv", ["myspellchecker", "--help"]):
with pytest.raises(SystemExit) as exc_info:
main()
# Help exits with code 0
assert exc_info.value.code == 0
def test_main_check_help(self):
"""Test main check --help doesn't crash."""
from myspellchecker.cli import main
with patch.object(sys, "argv", ["myspellchecker", "check", "--help"]):
with pytest.raises(SystemExit) as exc_info:
main()
assert exc_info.value.code == 0
def test_main_build_help(self):
"""Test main build --help doesn't crash."""
from myspellchecker.cli import main
with patch.object(sys, "argv", ["myspellchecker", "build", "--help"]):
with pytest.raises(SystemExit) as exc_info:
main()
assert exc_info.value.code == 0
def test_main_completion_bash(self):
"""Test main completion --shell bash."""
from myspellchecker.cli import main
with patch.object(sys, "argv", ["myspellchecker", "completion", "--shell", "bash"]):
with patch("builtins.print") as mock_print:
main()
# Should print bash completion script
output = mock_print.call_args[0][0]
assert "complete" in output
def test_main_completion_zsh(self):
"""Test main completion --shell zsh."""
from myspellchecker.cli import main
with patch.object(sys, "argv", ["myspellchecker", "completion", "--shell", "zsh"]):
with patch("builtins.print") as mock_print:
main()
output = mock_print.call_args[0][0]
assert "#compdef" in output
def test_main_completion_fish(self):
"""Test main completion --shell fish."""
from myspellchecker.cli import main
with patch.object(sys, "argv", ["myspellchecker", "completion", "--shell", "fish"]):
with patch("builtins.print") as mock_print:
main()
output = mock_print.call_args[0][0]
assert "complete -c myspellchecker" in output
def test_main_config_show(self):
"""Test main config show command."""
from myspellchecker.cli import main
with patch.object(sys, "argv", ["myspellchecker", "config", "show"]):
with patch("builtins.print") as mock_print:
main()
# Should print configuration search paths
calls = [str(call) for call in mock_print.call_args_list]
call_text = " ".join(calls)
assert "Configuration" in call_text or "Search" in call_text
class TestCLICheckCommand:
"""Test CLI check command with various options."""
def test_check_with_file_json_output(self):
"""Test check command with file input and JSON output."""
from myspellchecker.cli import main
with tempfile.NamedTemporaryFile(
mode="w", suffix=".txt", delete=False, encoding="utf-8"
) as f:
f.write("test text")
input_path = f.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
output_path = f.name
try:
with patch.object(
sys,
"argv",
["myspellchecker", "check", input_path, "-o", output_path, "-f", "json"],
):
main()
# Verify output file was created and contains valid JSON
with open(output_path) as f:
result = json.load(f)
assert "summary" in result
assert "results" in result
finally:
Path(input_path).unlink()
Path(output_path).unlink()
def test_check_with_preset_fast(self):
"""Test check command with fast preset."""
from myspellchecker.cli import main
with tempfile.NamedTemporaryFile(
mode="w", suffix=".txt", delete=False, encoding="utf-8"
) as f:
f.write("test")
input_path = f.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
output_path = f.name
try:
with patch.object(
sys,
"argv",
[
"myspellchecker",
"check",
input_path,
"-o",
output_path,
"-f",
"json",
"--preset",
"fast",
],
):
main()
assert Path(output_path).exists()
finally:
Path(input_path).unlink()
Path(output_path).unlink()
def test_check_with_text_format(self):
"""Test check command with text output format."""
from myspellchecker.cli import main
with tempfile.NamedTemporaryFile(
mode="w", suffix=".txt", delete=False, encoding="utf-8"
) as f:
f.write("test")
input_path = f.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
output_path = f.name
try:
with patch.object(
sys,
"argv",
["myspellchecker", "check", input_path, "-o", output_path, "-f", "text"],
):
main()
content = Path(output_path).read_text()
assert "WARNING" in content # TextFormatter outputs warning
finally:
Path(input_path).unlink()
Path(output_path).unlink()
def test_check_with_csv_format(self):
"""Test check command with CSV output format."""
from myspellchecker.cli import main
with tempfile.NamedTemporaryFile(
mode="w", suffix=".txt", delete=False, encoding="utf-8"
) as f:
f.write("test")
input_path = f.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f:
output_path = f.name
try:
with patch.object(
sys,
"argv",
["myspellchecker", "check", input_path, "-o", output_path, "-f", "csv"],
):
main()
content = Path(output_path).read_text()
assert "file,line,position" in content # CSV header
finally:
Path(input_path).unlink()
Path(output_path).unlink()
class TestCLIBuildCommand:
"""Test CLI build command."""
def test_build_validate_only(self):
"""Test build command with --validate flag."""
from myspellchecker.cli import main
with tempfile.NamedTemporaryFile(
mode="w", suffix=".txt", delete=False, encoding="utf-8"
) as f:
f.write("test content")
input_path = f.name
try:
with patch.object(
sys,
"argv",
["myspellchecker", "build", "-i", input_path, "--validate"],
):
with pytest.raises(SystemExit) as exc_info:
main()
# Validation should pass and exit with 0
assert exc_info.value.code == 0
finally:
Path(input_path).unlink()
def test_build_validate_missing_file(self):
"""Test build validate with missing input file."""
from myspellchecker.cli import main
with patch.object(
sys,
"argv",
["myspellchecker", "build", "-i", "/nonexistent/file.txt", "--validate"],
):
with pytest.raises(SystemExit) as exc_info:
main()
# Validation should fail
assert exc_info.value.code == 1
class TestCLISegmentCommand:
"""Test CLI segment command."""
def test_segment_help(self):
"""Test segment --help."""
from myspellchecker.cli import main
with patch.object(sys, "argv", ["myspellchecker", "segment", "--help"]):
with pytest.raises(SystemExit) as exc_info:
main()
assert exc_info.value.code == 0
class TestCLIConfigCommand:
"""Test CLI config command."""
def test_config_init(self):
"""Test config init creates configuration file."""
from myspellchecker.cli import main
with tempfile.TemporaryDirectory() as tmpdir:
config_path = Path(tmpdir) / "test_config.yml"
with patch.object(
sys,
"argv",
["myspellchecker", "config", "init", "--path", str(config_path)],
):
main()
assert config_path.exists()
def test_config_init_existing_no_force(self):
"""Test config init doesn't overwrite without --force."""
from myspellchecker.cli import main
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as f:
f.write("existing content")
config_path = f.name
try:
with patch.object(
sys,
"argv",
["myspellchecker", "config", "init", "--path", config_path],
):
# init_config_file raises FileExistsError which isn't caught
# as ConfigurationError in the CLI, so it propagates up
with pytest.raises((SystemExit, FileExistsError)):
main()
# File should still have original content
content = Path(config_path).read_text()
assert "existing content" in content
finally:
Path(config_path).unlink()
def test_config_init_existing_with_force(self):
"""Test config init overwrites with --force."""
from myspellchecker.cli import main
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as f:
f.write("existing content")
config_path = f.name
try:
with patch.object(
sys,
"argv",
["myspellchecker", "config", "init", "--path", config_path, "--force"],
):
main()
# File should be overwritten
content = Path(config_path).read_text()
assert "existing content" not in content
finally:
Path(config_path).unlink()
class TestCLIErrorHandling:
"""Test CLI error handling and exit codes."""
def test_invalid_input_file_exits_with_code_2(self):
"""Test that invalid input file exits with code 2."""
with pytest.raises(SystemExit) as exc_info:
open_input_file("/nonexistent/file.txt")
assert exc_info.value.code == 2
def test_invalid_db_exits_with_code_1(self):
"""Test that invalid database exits with code 1."""
with pytest.raises(SystemExit) as exc_info:
get_checker(database_path="/nonexistent/db.sqlite")
assert exc_info.value.code == 1
if __name__ == "__main__":
pytest.main([__file__, "-v"])