|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
6 | | -import subprocess |
7 | | -import sys |
8 | 6 |
|
9 | 7 | import pytest |
10 | 8 |
|
|
14 | 12 | class TestMainModule: |
15 | 13 | """Tests for __main__.py entry point (0% coverage).""" |
16 | 14 |
|
17 | | - def test_main_module_runs_help(self): |
| 15 | + @pytest.fixture |
| 16 | + def runner(self): |
| 17 | + from click.testing import CliRunner |
| 18 | + return CliRunner() |
| 19 | + |
| 20 | + def test_main_module_runs_help(self, runner): |
18 | 21 | """python -m deadcode --help works (covers __main__.py:2-5).""" |
19 | | - result = subprocess.run( |
20 | | - [sys.executable, "-m", "deadcode", "--help"], |
21 | | - capture_output=True, text=False, |
22 | | - ) |
23 | | - assert result.returncode == 0 |
24 | | - assert b"Usage" in result.stdout |
| 22 | + result = runner.invoke(cli, ["--help"]) |
| 23 | + assert result.exit_code == 0 |
| 24 | + assert "Usage" in result.output |
25 | 25 |
|
26 | 26 |
|
27 | 27 | class TestCliEdgeCases: |
28 | 28 | """Edge cases for CLI uncovered paths.""" |
29 | 29 |
|
30 | | - def test_non_existent_project_exits_1(self): |
| 30 | + @pytest.fixture |
| 31 | + def runner(self): |
| 32 | + from click.testing import CliRunner |
| 33 | + return CliRunner() |
| 34 | + |
| 35 | + def test_non_existent_project_exits_1(self, runner): |
31 | 36 | """Scan with non-existent project exits 1 (cli.py:88-90).""" |
32 | | - result = subprocess.run( |
33 | | - [sys.executable, "-m", "deadcode", "--project", "/nonexistent/path", "scan"], |
34 | | - capture_output=True, text=False, |
35 | | - ) |
36 | | - assert result.returncode == 1 |
| 37 | + result = runner.invoke(cli, ["--project", "/nonexistent/path", "scan"]) |
| 38 | + assert result.exit_code == 1 |
37 | 39 |
|
38 | | - def test_fail_threshold_exits_high(self, tmp_path): |
| 40 | + def test_fail_threshold_exits_high(self, runner, tmp_path): |
39 | 41 | """--fail=0 exits 1 when findings exist (covers fail threshold path).""" |
40 | 42 | (tmp_path / "src" / "unused.ts").parent.mkdir(parents=True, exist_ok=True) |
41 | 43 | (tmp_path / "src" / "unused.ts").write_text("export function unused() { return 1; }\n") |
42 | | - result = subprocess.run( |
43 | | - [sys.executable, "-m", "deadcode", "-p", str(tmp_path), "scan", |
44 | | - "--fail", "0"], |
45 | | - capture_output=True, text=True, |
46 | | - ) |
47 | | - assert result.returncode == 1 |
48 | | - assert "FAIL" in result.stdout |
| 44 | + result = runner.invoke(cli, ["-p", str(tmp_path), "scan", "--fail", "0"]) |
| 45 | + assert result.exit_code == 1 |
| 46 | + assert "FAIL" in result.output |
49 | 47 |
|
50 | | - def test_ignore_flag_before_subcommand(self, tmp_path): |
| 48 | + def test_ignore_flag_before_subcommand(self, runner, tmp_path): |
51 | 49 | """--ignore group option rejects submodule patterns (covers _merge_config_ignore).""" |
52 | 50 | (tmp_path / "src" / "used.ts").parent.mkdir(parents=True, exist_ok=True) |
53 | 51 | (tmp_path / "src" / "used.ts").write_text("export function used() { return 1; }\n") |
| 52 | + (tmp_path / "src" / "unused.ts").parent.mkdir(parents=True, exist_ok=True) |
54 | 53 | (tmp_path / "src" / "unused.ts").write_text("export function unused() { return 2; }\n") |
55 | | - result = subprocess.run( |
56 | | - [sys.executable, "-m", "deadcode", "-p", str(tmp_path), |
57 | | - "--ignore", "**/unused.ts", "scan"], |
58 | | - capture_output=True, text=True, |
59 | | - ) |
60 | | - assert result.returncode == 0 |
61 | | - assert "unused" not in result.stdout |
| 54 | + result = runner.invoke(cli, ["-p", str(tmp_path), "--ignore", "**/unused.ts", "scan"]) |
| 55 | + assert result.exit_code == 0 |
| 56 | + assert "unused" not in result.output |
62 | 57 |
|
63 | 58 |
|
64 | 59 | class TestCliFormatOutput: |
|
0 commit comments