|
1 | 1 | # stdlib |
2 | 2 | import re |
3 | | -from typing import Union, no_type_check |
| 3 | +from typing import List, Mapping, Union, no_type_check |
4 | 4 |
|
5 | 5 | # 3rd party |
6 | 6 | import pytest |
|
10 | 10 | from consolekit.terminal_colours import strip_ansi |
11 | 11 | from consolekit.testing import CliRunner, Result, click_version |
12 | 12 | from domdf_python_tools.paths import PathPlus, in_directory |
| 13 | +from domdf_python_tools.typing import PathLike |
13 | 14 |
|
14 | 15 | # this package |
| 16 | +import formate |
15 | 17 | from formate import Reformatter, reformat_file |
16 | 18 | from formate.__main__ import main |
17 | | -from formate.config import load_toml |
| 19 | +from formate.classes import EntryPoint, Hook |
| 20 | +from formate.config import formats_filetypes, load_toml, wants_filename |
18 | 21 |
|
19 | 22 | path_sub = re.compile(r" .*/pytest-of-.*/pytest-\d+") |
20 | 23 |
|
@@ -169,6 +172,32 @@ def test_reformatter_class( |
169 | 172 | advanced_file_regression.check_file(tmp_pathplus / "code.py") |
170 | 173 |
|
171 | 174 |
|
| 175 | +@pytest.mark.usefixtures("demo_environment") |
| 176 | +def test_reformatter_class_non_python_hook( |
| 177 | + tmp_pathplus: PathPlus, |
| 178 | + monkeypatch, |
| 179 | + ): |
| 180 | + |
| 181 | + config = load_toml(tmp_pathplus / "formate.toml") |
| 182 | + config["hooks"]["format-foo"] = {"priority": 10} # type: ignore[index] |
| 183 | + |
| 184 | + (tmp_pathplus / "code.foo").touch() |
| 185 | + |
| 186 | + @formats_filetypes(".foo") |
| 187 | + @wants_filename |
| 188 | + def format_foo(source: str, formate_filename: PathLike) -> str: |
| 189 | + return "Result of format-foo" |
| 190 | + |
| 191 | + def parse_hooks(config: Mapping) -> List[Hook]: |
| 192 | + return [Hook(name="format-foo", entry_point=EntryPoint("format-foo", format_foo))] |
| 193 | + |
| 194 | + monkeypatch.setattr(formate, "parse_hooks", parse_hooks) |
| 195 | + r = Reformatter(tmp_pathplus / "code.foo", config) |
| 196 | + |
| 197 | + assert r.run() |
| 198 | + assert r.to_string() == "Result of format-foo\n" |
| 199 | + |
| 200 | + |
172 | 201 | @pytest.mark.usefixtures("demo_environment") |
173 | 202 | def test_cli( |
174 | 203 | tmp_pathplus: PathPlus, |
@@ -243,6 +272,41 @@ def test_cli_verbose_verbose( |
243 | 272 | check_out(result, advanced_data_regression) |
244 | 273 |
|
245 | 274 |
|
| 275 | +@pytest.mark.usefixtures("demo_environment") |
| 276 | +def test_cli_verbose_verbose_no_supported_hooks( |
| 277 | + tmp_pathplus: PathPlus, |
| 278 | + advanced_file_regression: AdvancedFileRegressionFixture, |
| 279 | + advanced_data_regression: AdvancedDataRegressionFixture, |
| 280 | + ): |
| 281 | + |
| 282 | + result: Result |
| 283 | + (tmp_pathplus / "code.c").touch() |
| 284 | + (tmp_pathplus / "a_dir").mkdir() |
| 285 | + |
| 286 | + with in_directory(tmp_pathplus): |
| 287 | + runner = CliRunner(mix_stderr=False) |
| 288 | + result = runner.invoke( |
| 289 | + main, |
| 290 | + args=["code.py", "a_dir", "--no-colour", "--diff", "--verbose", "-v"], |
| 291 | + ) |
| 292 | + |
| 293 | + assert result.exit_code == 1 |
| 294 | + |
| 295 | + advanced_file_regression.check_file(tmp_pathplus / "code.py") |
| 296 | + |
| 297 | + # Calling a second time shouldn't change anything |
| 298 | + with in_directory(tmp_pathplus): |
| 299 | + runner = CliRunner(mix_stderr=False) |
| 300 | + result = runner.invoke( |
| 301 | + main, |
| 302 | + args=["code.py", "code.c", "--no-colour", "--diff", "--verbose", "-v"], |
| 303 | + ) |
| 304 | + |
| 305 | + assert result.exit_code == 0 |
| 306 | + |
| 307 | + check_out(result, advanced_data_regression) |
| 308 | + |
| 309 | + |
246 | 310 | @pytest.mark.usefixtures("demo_environment") |
247 | 311 | @max_version("3.9.9", reason="Output differs on Python 3.10+") |
248 | 312 | @not_pypy("Output differs on PyPy") |
|
0 commit comments