]> git.madduck.net Git - etc/vim.git/blob - tests/util.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Don't run Docker workflow on push to forks (#2324)
[etc/vim.git] / tests / util.py
1 import os
2 import unittest
3 from pathlib import Path
4 from typing import List, Tuple, Any
5 from functools import partial
6
7 import black
8 from black.output import out, err
9 from black.debug import DebugVisitor
10
11 THIS_DIR = Path(__file__).parent
12 PROJECT_ROOT = THIS_DIR.parent
13 EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)"
14 DETERMINISTIC_HEADER = "[Deterministic header]"
15
16
17 DEFAULT_MODE = black.Mode()
18 ff = partial(black.format_file_in_place, mode=DEFAULT_MODE, fast=True)
19 fs = partial(black.format_str, mode=DEFAULT_MODE)
20
21
22 def dump_to_stderr(*output: str) -> str:
23     return "\n" + "\n".join(output) + "\n"
24
25
26 class BlackBaseTestCase(unittest.TestCase):
27     maxDiff = None
28     _diffThreshold = 2 ** 20
29
30     def assertFormatEqual(self, expected: str, actual: str) -> None:
31         if actual != expected and not os.environ.get("SKIP_AST_PRINT"):
32             bdv: DebugVisitor[Any]
33             out("Expected tree:", fg="green")
34             try:
35                 exp_node = black.lib2to3_parse(expected)
36                 bdv = DebugVisitor()
37                 list(bdv.visit(exp_node))
38             except Exception as ve:
39                 err(str(ve))
40             out("Actual tree:", fg="red")
41             try:
42                 exp_node = black.lib2to3_parse(actual)
43                 bdv = DebugVisitor()
44                 list(bdv.visit(exp_node))
45             except Exception as ve:
46                 err(str(ve))
47         self.assertMultiLineEqual(expected, actual)
48
49
50 def read_data(name: str, data: bool = True) -> Tuple[str, str]:
51     """read_data('test_name') -> 'input', 'output'"""
52     if not name.endswith((".py", ".pyi", ".out", ".diff")):
53         name += ".py"
54     base_dir = THIS_DIR / "data" if data else PROJECT_ROOT
55     return read_data_from_file(base_dir / name)
56
57
58 def read_data_from_file(file_name: Path) -> Tuple[str, str]:
59     with open(file_name, "r", encoding="utf8") as test:
60         lines = test.readlines()
61     _input: List[str] = []
62     _output: List[str] = []
63     result = _input
64     for line in lines:
65         line = line.replace(EMPTY_LINE, "")
66         if line.rstrip() == "# output":
67             result = _output
68             continue
69
70         result.append(line)
71     if _input and not _output:
72         # If there's no output marker, treat the entire file as already pre-formatted.
73         _output = _input[:]
74     return "".join(_input).strip() + "\n", "".join(_output).strip() + "\n"