|
| 1 | +"""Unit tests for the CLI helper functions in __main__.py.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import io |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from language_tool_python.__main__ import ( |
| 11 | + CliArgs, |
| 12 | + _read_project_version, |
| 13 | + get_input_text, |
| 14 | + get_remote_server, |
| 15 | + get_rules, |
| 16 | + get_text, |
| 17 | + parse_args, |
| 18 | + print_exception, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class TestGetRules: |
| 23 | + """Tests for the get_rules() rule-string parser.""" |
| 24 | + |
| 25 | + def test_comma_separated(self) -> None: |
| 26 | + """Comma-separated rule IDs are returned as a set.""" |
| 27 | + assert get_rules("RULE_A,RULE_B") == {"RULE_A", "RULE_B"} |
| 28 | + |
| 29 | + def test_uppercases(self) -> None: |
| 30 | + """Rule IDs are uppercased.""" |
| 31 | + assert get_rules("rule_a") == {"RULE_A"} |
| 32 | + |
| 33 | + def test_hyphen_allowed(self) -> None: |
| 34 | + """Hyphens inside rule IDs are preserved.""" |
| 35 | + assert get_rules("MORFOLOGIK-RULE") == {"MORFOLOGIK-RULE"} |
| 36 | + |
| 37 | + def test_whitespace_separated(self) -> None: |
| 38 | + """Whitespace-separated rule IDs are each returned.""" |
| 39 | + assert get_rules("RULE_A RULE_B") == {"RULE_A", "RULE_B"} |
| 40 | + |
| 41 | + def test_empty_string(self) -> None: |
| 42 | + """Empty input returns an empty set.""" |
| 43 | + assert get_rules("") == set() |
| 44 | + |
| 45 | + |
| 46 | +class TestParseArgsEnabledOnly: |
| 47 | + """Tests for the --enabled-only CLI argument validation.""" |
| 48 | + |
| 49 | + def test_enabled_only_with_disable_raises(self) -> None: |
| 50 | + """--enabled-only combined with --disable causes SystemExit.""" |
| 51 | + with pytest.raises(SystemExit): |
| 52 | + parse_args( |
| 53 | + [ |
| 54 | + "-l", |
| 55 | + "en-US", |
| 56 | + "--enabled-only", |
| 57 | + "-e", |
| 58 | + "RULE", |
| 59 | + "-d", |
| 60 | + "OTHER", |
| 61 | + "file.txt", |
| 62 | + ] |
| 63 | + ) |
| 64 | + |
| 65 | + def test_enabled_only_with_enable_passes(self) -> None: |
| 66 | + """--enabled-only with --enable is accepted.""" |
| 67 | + args = parse_args(["-l", "en-US", "--enabled-only", "-e", "RULE", "file.txt"]) |
| 68 | + assert args.enabled_only is True |
| 69 | + assert "RULE" in args.enable |
| 70 | + |
| 71 | + |
| 72 | +class TestGetRemoteServer: |
| 73 | + """Tests for the get_remote_server() URL builder.""" |
| 74 | + |
| 75 | + def _args(self, host: str | None = None, port: str | None = None) -> CliArgs: |
| 76 | + """Build a minimal CliArgs with only remote_host/remote_port set.""" |
| 77 | + args = CliArgs() |
| 78 | + args.remote_host = host |
| 79 | + args.remote_port = port |
| 80 | + return args |
| 81 | + |
| 82 | + def test_no_host_returns_none(self) -> None: |
| 83 | + """Returns None when no remote host is set.""" |
| 84 | + assert get_remote_server(self._args()) is None |
| 85 | + |
| 86 | + def test_host_without_port(self) -> None: |
| 87 | + """Returns the host name alone when no port is given.""" |
| 88 | + assert get_remote_server(self._args(host="localhost")) == "localhost" |
| 89 | + |
| 90 | + def test_host_with_port(self) -> None: |
| 91 | + """Returns host:port when both are provided.""" |
| 92 | + result = get_remote_server(self._args(host="localhost", port="8081")) |
| 93 | + assert result == "localhost:8081" |
| 94 | + |
| 95 | + |
| 96 | +class TestPrintException: |
| 97 | + """Tests for the print_exception() stderr printer.""" |
| 98 | + |
| 99 | + def test_without_debug_prints_to_stderr( |
| 100 | + self, capsys: pytest.CaptureFixture[str] |
| 101 | + ) -> None: |
| 102 | + """Without debug=True, only the message is printed to stderr.""" |
| 103 | + print_exception(ValueError("test error"), debug=False) |
| 104 | + assert "test error" in capsys.readouterr().err |
| 105 | + |
| 106 | + def test_with_debug_prints_traceback( |
| 107 | + self, capsys: pytest.CaptureFixture[str] |
| 108 | + ) -> None: |
| 109 | + """With debug=True, the full traceback is printed to stderr.""" |
| 110 | + try: |
| 111 | + msg = "original error" |
| 112 | + raise ValueError(msg) |
| 113 | + except ValueError: |
| 114 | + print_exception(ValueError("current error"), debug=True) |
| 115 | + captured = capsys.readouterr() |
| 116 | + assert "ValueError" in captured.err |
| 117 | + |
| 118 | + |
| 119 | +class TestGetText: |
| 120 | + """Tests for the get_text() file reader.""" |
| 121 | + |
| 122 | + def test_reads_file(self, tmp_path: Path) -> None: |
| 123 | + """File content is returned as-is when no ignore pattern is given.""" |
| 124 | + f = tmp_path / "test.txt" |
| 125 | + f.write_text("hello world\n", encoding="utf-8") |
| 126 | + result = get_text(str(f), encoding="utf-8", ignore=None) |
| 127 | + assert result == "hello world\n" |
| 128 | + |
| 129 | + def test_ignore_replaces_matching_lines(self, tmp_path: Path) -> None: |
| 130 | + """Lines matching the ignore regex are replaced with a newline.""" |
| 131 | + f = tmp_path / "test.txt" |
| 132 | + f.write_text("keep this\n# skip this\nkeep too\n", encoding="utf-8") |
| 133 | + result = get_text(str(f), encoding="utf-8", ignore=r"#.*") |
| 134 | + assert "# skip this" not in result |
| 135 | + assert "keep this" in result |
| 136 | + assert "keep too" in result |
| 137 | + |
| 138 | + def test_no_ignore_keeps_all(self, tmp_path: Path) -> None: |
| 139 | + """All lines are kept when no ignore pattern is set.""" |
| 140 | + f = tmp_path / "test.txt" |
| 141 | + f.write_text("line1\nline2\n", encoding="utf-8") |
| 142 | + result = get_text(str(f), encoding=None, ignore=None) |
| 143 | + assert result == "line1\nline2\n" |
| 144 | + |
| 145 | + |
| 146 | +class TestGetInputText: |
| 147 | + """Tests for the get_input_text() stdin/file dispatcher.""" |
| 148 | + |
| 149 | + def _args( |
| 150 | + self, ignore_lines: str | None = None, encoding: str | None = None |
| 151 | + ) -> CliArgs: |
| 152 | + """Build a minimal CliArgs with only ignore_lines/encoding set.""" |
| 153 | + args = CliArgs() |
| 154 | + args.ignore_lines = ignore_lines |
| 155 | + args.encoding = encoding |
| 156 | + return args |
| 157 | + |
| 158 | + def test_reads_from_file(self, tmp_path: Path) -> None: |
| 159 | + """Regular filename is read from disk.""" |
| 160 | + f = tmp_path / "input.txt" |
| 161 | + f.write_text("test content", encoding="utf-8") |
| 162 | + result = get_input_text(str(f), self._args()) |
| 163 | + assert result == "test content" |
| 164 | + |
| 165 | + def test_reads_from_stdin(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 166 | + """Filename '-' reads from stdin.""" |
| 167 | + monkeypatch.setattr("sys.stdin", io.StringIO("stdin content")) |
| 168 | + result = get_input_text("-", self._args()) |
| 169 | + assert result == "stdin content" |
| 170 | + |
| 171 | + def test_stdin_with_ignore_lines(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 172 | + """Matching lines from stdin are suppressed when ignore_lines is set.""" |
| 173 | + monkeypatch.setattr("sys.stdin", io.StringIO("keep\n# skip\nkeep2\n")) |
| 174 | + result = get_input_text("-", self._args(ignore_lines=r"#.*")) |
| 175 | + assert "# skip" not in result |
| 176 | + assert "keep" in result |
| 177 | + |
| 178 | + def test_uses_encoding(self, tmp_path: Path) -> None: |
| 179 | + """Non-UTF-8 files are decoded with the specified encoding.""" |
| 180 | + f = tmp_path / "latin.txt" |
| 181 | + content = "caf\xe9" |
| 182 | + f.write_bytes(content.encode("latin-1")) |
| 183 | + result = get_input_text(str(f), self._args(encoding="latin-1")) |
| 184 | + assert "caf" in result |
| 185 | + |
| 186 | + |
| 187 | +class TestReadProjectVersion: |
| 188 | + """Tests for _read_project_version().""" |
| 189 | + |
| 190 | + def test_reads_version_from_pyproject(self) -> None: |
| 191 | + """Version string is read from the project's pyproject.toml.""" |
| 192 | + pyproject = Path(__file__).parent.parent.parent / "pyproject.toml" |
| 193 | + version = _read_project_version(pyproject) |
| 194 | + assert isinstance(version, str) |
| 195 | + assert version.count(".") >= 1 |
0 commit comments