|
1 | | -"""Tests for the command-line interface (CLI) functionality.""" |
| 1 | +"""Integration tests for the CLI using real LanguageTool server instances.""" |
2 | 2 |
|
3 | 3 | import io |
4 | 4 | import sys |
|
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | import language_tool_python |
10 | | -from language_tool_python.__main__ import main, parse_args |
| 10 | +from language_tool_python.__main__ import main |
| 11 | + |
| 12 | + |
| 13 | +def main_with_stdin(argv: list[str], stdin: str) -> int: |
| 14 | + """Execute the main CLI with simulated stdin input. |
| 15 | +
|
| 16 | + :param argv: Command-line arguments to pass to the main function. |
| 17 | + :param stdin: Input text to simulate as stdin. |
| 18 | + :return: Exit code returned by the main function. |
| 19 | + :rtype: int |
| 20 | + """ |
| 21 | + old_stdin = sys.stdin |
| 22 | + sys.stdin = io.StringIO(stdin) |
| 23 | + try: |
| 24 | + return main(argv) |
| 25 | + finally: |
| 26 | + sys.stdin = old_stdin |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="module") |
| 30 | +def remote_server() -> Generator[tuple[str, int], None, None]: |
| 31 | + """Fixture that provides a remote LanguageTool server for testing. |
| 32 | +
|
| 33 | + This fixture initializes a LanguageTool instance and yields its host and port, |
| 34 | + ensuring proper cleanup after all tests in the module complete. |
| 35 | +
|
| 36 | + :return: A tuple containing the server host and port (host, port). |
| 37 | + :rtype: Generator[Tuple[str, int], None, None] |
| 38 | + """ |
| 39 | + with language_tool_python.LanguageTool("en-US") as tool: |
| 40 | + host = tool._host |
| 41 | + port = tool._port |
| 42 | + yield host, port |
11 | 43 |
|
12 | 44 |
|
13 | 45 | @pytest.mark.parametrize( |
@@ -89,22 +121,6 @@ def test_cli_exit_codes( |
89 | 121 | assert code != 0 |
90 | 122 |
|
91 | 123 |
|
92 | | -@pytest.fixture(scope="module") |
93 | | -def remote_server() -> Generator[tuple[str, int], None, None]: |
94 | | - """Fixture that provides a remote LanguageTool server for testing. |
95 | | -
|
96 | | - This fixture initializes a LanguageTool instance and yields its host and port, |
97 | | - ensuring proper cleanup after all tests in the module complete. |
98 | | -
|
99 | | - :return: A tuple containing the server host and port (host, port). |
100 | | - :rtype: Generator[Tuple[str, int], None, None] |
101 | | - """ |
102 | | - with language_tool_python.LanguageTool("en-US") as tool: |
103 | | - host = tool._host |
104 | | - port = tool._port |
105 | | - yield host, port |
106 | | - |
107 | | - |
108 | 124 | def test_cli_remote_ok(remote_server: tuple[str, int]) -> None: |
109 | 125 | """Test the CLI with a remote server using valid input text. |
110 | 126 |
|
@@ -155,78 +171,3 @@ def test_cli_remote_error(remote_server: tuple[str, int]) -> None: |
155 | 171 | "This is noot okay.\n", |
156 | 172 | ) |
157 | 173 | assert code != 0 |
158 | | - |
159 | | - |
160 | | -def test_parse_args_enabled_only_with_enable_categories() -> None: |
161 | | - """Test that --enabled-only is accepted when only --enable-categories is provided. |
162 | | -
|
163 | | - :raises AssertionError: If parse_args raises an error for this valid combination. |
164 | | - """ |
165 | | - args = parse_args(["-l", "en-US", "--enabled-only", "-E", "TYPOS", "file.txt"]) |
166 | | - assert args.enabled_only is True |
167 | | - assert args.enable_categories == {"TYPOS"} |
168 | | - |
169 | | - |
170 | | -def test_parse_args_enabled_only_rejects_disable_categories() -> None: |
171 | | - """Test that --enabled-only cannot be combined with --disable-categories. |
172 | | -
|
173 | | - :raises SystemExit: Expected, as argparse calls sys.exit on error. |
174 | | - """ |
175 | | - with pytest.raises(SystemExit): |
176 | | - parse_args( |
177 | | - ["-l", "en-US", "--enabled-only", "-e", "RULE", "-D", "TYPOS", "file.txt"] |
178 | | - ) |
179 | | - |
180 | | - |
181 | | -def test_parse_args_enabled_only_requires_enable_or_enable_categories() -> None: |
182 | | - """Test that --enabled-only requires at least --enable or --enable-categories. |
183 | | -
|
184 | | - :raises SystemExit: Expected, as argparse calls sys.exit on error. |
185 | | - """ |
186 | | - with pytest.raises(SystemExit): |
187 | | - parse_args(["-l", "en-US", "--enabled-only", "file.txt"]) |
188 | | - |
189 | | - |
190 | | -def test_parse_args_categories() -> None: |
191 | | - """Test that --disable-categories and --enable-categories are parsed correctly. |
192 | | -
|
193 | | - :raises AssertionError: If the parsed category sets do not match the expected |
194 | | - values. |
195 | | - """ |
196 | | - args = parse_args( |
197 | | - ["-l", "en-US", "-D", "TYPOS,GRAMMAR", "-E", "PUNCTUATION", "file.txt"] |
198 | | - ) |
199 | | - assert args.disable_categories == {"TYPOS", "GRAMMAR"} |
200 | | - assert args.enable_categories == {"PUNCTUATION"} |
201 | | - |
202 | | - |
203 | | -def test_parse_args_categories_multiple_flags() -> None: |
204 | | - """Test that repeated -D/-E flags accumulate into the same set. |
205 | | -
|
206 | | - :raises AssertionError: If the category sets do not accumulate correctly. |
207 | | - """ |
208 | | - args = parse_args( |
209 | | - ["-l", "en-US", "-D", "TYPOS", "-D", "GRAMMAR", "-E", "PUNCTUATION", "file.txt"] |
210 | | - ) |
211 | | - assert args.disable_categories == {"TYPOS", "GRAMMAR"} |
212 | | - assert args.enable_categories == {"PUNCTUATION"} |
213 | | - |
214 | | - |
215 | | -def main_with_stdin(argv: list[str], stdin: str) -> int: |
216 | | - """Execute the main CLI with simulated stdin input. |
217 | | -
|
218 | | - This utility function temporarily replaces sys.stdin with a StringIO object |
219 | | - containing the provided input, executes the main CLI function, and then restores the |
220 | | - original stdin. |
221 | | -
|
222 | | - :param argv: Command-line arguments to pass to the main function. |
223 | | - :param stdin: Input text to simulate as stdin. |
224 | | - :return: Exit code returned by the main function. |
225 | | - :rtype: int |
226 | | - """ |
227 | | - old_stdin = sys.stdin |
228 | | - sys.stdin = io.StringIO(stdin) |
229 | | - try: |
230 | | - return main(argv) |
231 | | - finally: |
232 | | - sys.stdin = old_stdin |
0 commit comments