|
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | import language_tool_python |
10 | | -from language_tool_python.__main__ import main |
| 10 | +from language_tool_python.__main__ import main, parse_args |
11 | 11 |
|
12 | 12 |
|
13 | 13 | @pytest.mark.parametrize( |
|
38 | 38 | True, |
39 | 39 | ), |
40 | 40 | (["-l", "en-US", "--ignore-lines=^#", "-"], '# These are "dumb".\n', True), |
| 41 | + ( |
| 42 | + ["-l", "en-US", "-D", "TYPOS", "-"], |
| 43 | + "This is noot okay.\n", |
| 44 | + True, |
| 45 | + ), |
| 46 | + ( |
| 47 | + ["-l", "en-US", "--disable-categories=TYPOS", "-"], |
| 48 | + "This is noot okay.\n", |
| 49 | + True, |
| 50 | + ), |
| 51 | + ( |
| 52 | + ["-l", "en-US", "-E", "TYPOS", "-"], |
| 53 | + "This is okay.\n", |
| 54 | + True, |
| 55 | + ), |
| 56 | + ( |
| 57 | + ["-l", "en-US", "--enabled-only", "-E", "TYPOS", "-"], |
| 58 | + "This is noot okay.\n", |
| 59 | + False, |
| 60 | + ), |
| 61 | + ( |
| 62 | + ["-l", "en-US", "--enabled-only", "-E", "TYPOS", "-"], |
| 63 | + "This is okay.\n", |
| 64 | + True, |
| 65 | + ), |
41 | 66 | ], |
42 | 67 | ) |
43 | 68 | def test_cli_exit_codes( |
@@ -132,6 +157,61 @@ def test_cli_remote_error(remote_server: tuple[str, int]) -> None: |
132 | 157 | assert code != 0 |
133 | 158 |
|
134 | 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 | + |
135 | 215 | def main_with_stdin(argv: list[str], stdin: str) -> int: |
136 | 216 | """Execute the main CLI with simulated stdin input. |
137 | 217 |
|
|
0 commit comments