]> www.average.org Git - loctrkd.git/blob - test/test_black.py
do not run black older than 21.1
[loctrkd.git] / test / test_black.py
1 from glob import glob
2 from pkg_resources import get_distribution, DistributionNotFound
3 from re import match
4 from subprocess import run
5 from shutil import which
6 from unittest import TestCase, skipUnless
7
8 black_version = 0.0
9 try:
10     vermatch = match("[\.\d]*", get_distribution("black").version)
11     if vermatch is not None:
12         black_version = float(vermatch.group())
13 except DistributionNotFound:
14     pass
15
16
17 class BlackFormatter(TestCase):
18     @skipUnless(black_version >= 21.1, "Do not trust earlier black versions")
19     def test_black(self) -> None:
20         if not which("black"):
21             self.fail(f"black not installed.")
22         cmd = (
23             ["black", "--check", "--diff", "-l", "79"]
24             + glob("gps303/**/*.py", recursive=True)
25             + glob("test/**/*.py", recursive=True)
26         )
27         output = run(cmd, capture_output=True)
28         if output.returncode == 1:
29             self.fail(
30                 f"black found code that needs reformatting:\n{output.stdout.decode()}"
31             )
32         if output.returncode != 0:
33             self.fail(
34                 f"black exited with code {output.returncode}:\n{output.stderr.decode()}"
35             )