X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/2082a325fdd14f0aabd88f7f12a20f9fb085c538..11f130b7deb74f424ebf57ef551953c9bc08393f:/tests/test_black.py?ds=sidebyside diff --git a/tests/test_black.py b/tests/test_black.py index 1b574c0..3ed5daa 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -10,10 +10,11 @@ from pathlib import Path import regex as re import sys from tempfile import TemporaryDirectory -from typing import Any, BinaryIO, Generator, List, Tuple, Iterator, TypeVar +from typing import Any, BinaryIO, Dict, Generator, List, Tuple, Iterator, TypeVar import unittest from unittest.mock import patch, MagicMock +import click from click import unstyle from click.testing import CliRunner @@ -31,6 +32,10 @@ else: from pathspec import PathSpec +# Import other test classes +from .test_primer import PrimerCLITests # noqa: F401 + + ff = partial(black.format_file_in_place, mode=black.FileMode(), fast=True) fs = partial(black.format_str, mode=black.FileMode()) THIS_FILE = Path(__file__) @@ -252,8 +257,8 @@ class BlackTestCase(unittest.TestCase): def test_piping_diff(self) -> None: diff_header = re.compile( - rf"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d " - rf"\+\d\d\d\d" + r"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d " + r"\+\d\d\d\d" ) source, _ = read_data("expression.py") expected, _ = read_data("expression.diff") @@ -1758,6 +1763,66 @@ class BlackTestCase(unittest.TestCase): finally: tmp_file.unlink() + def test_parse_pyproject_toml(self) -> None: + test_toml_file = THIS_DIR / "test.toml" + config = black.parse_pyproject_toml(str(test_toml_file)) + self.assertEqual(config["verbose"], 1) + self.assertEqual(config["check"], "no") + self.assertEqual(config["diff"], "y") + self.assertEqual(config["color"], True) + self.assertEqual(config["line_length"], 79) + self.assertEqual(config["target_version"], ["py36", "py37", "py38"]) + self.assertEqual(config["exclude"], r"\.pyi?$") + self.assertEqual(config["include"], r"\.py?$") + + def test_read_pyproject_toml(self) -> None: + test_toml_file = THIS_DIR / "test.toml" + + # Fake a click context and parameter so mypy stays happy + class FakeContext(click.Context): + def __init__(self) -> None: + self.default_map: Dict[str, Any] = {} + + class FakeParameter(click.Parameter): + def __init__(self) -> None: + pass + + fake_ctx = FakeContext() + black.read_pyproject_toml( + fake_ctx, FakeParameter(), str(test_toml_file), + ) + config = fake_ctx.default_map + self.assertEqual(config["verbose"], "1") + self.assertEqual(config["check"], "no") + self.assertEqual(config["diff"], "y") + self.assertEqual(config["color"], "True") + self.assertEqual(config["line_length"], "79") + self.assertEqual(config["target_version"], ["py36", "py37", "py38"]) + self.assertEqual(config["exclude"], r"\.pyi?$") + self.assertEqual(config["include"], r"\.py?$") + + def test_find_project_root(self) -> None: + with TemporaryDirectory() as workspace: + root = Path(workspace) + test_dir = root / "test" + test_dir.mkdir() + + src_dir = root / "src" + src_dir.mkdir() + + root_pyproject = root / "pyproject.toml" + root_pyproject.touch() + src_pyproject = src_dir / "pyproject.toml" + src_pyproject.touch() + src_python = src_dir / "foo.py" + src_python.touch() + + self.assertEqual( + black.find_project_root((src_dir, test_dir)), root.resolve() + ) + self.assertEqual(black.find_project_root((src_dir,)), src_dir.resolve()) + self.assertEqual(black.find_project_root((src_python,)), src_dir.resolve()) + class BlackDTestCase(AioHTTPTestCase): async def get_application(self) -> web.Application: @@ -1848,7 +1913,7 @@ class BlackDTestCase(AioHTTPTestCase): @unittest_run_loop async def test_blackd_diff(self) -> None: diff_header = re.compile( - rf"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" + r"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" ) source, _ = read_data("blackd_diff.py")