]> www.average.org Git - loctrkd.git/blob - test/test_black.py
Update changelog for 2.00 release
[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 main, TestCase, skipUnless
7
8 from . import no_less_than
9
10 is_acceptable_verison = no_less_than("21.1")
11
12 black_version = "0.0"
13 try:
14     vermatch = match("[\.\d]*", get_distribution("black").version)
15     if vermatch is not None:
16         black_version = vermatch.group()
17 except DistributionNotFound:
18     pass
19
20
21 class BlackFormatter(TestCase):
22     @skipUnless(
23         is_acceptable_verison(black_version),
24         "Do not trust earlier black versions",
25     )
26     def test_black(self) -> None:
27         if not which("black"):
28             self.fail(f"black not installed.")
29         cmd = (
30             ["black", "--check", "--diff", "-l", "79"]
31             + glob("loctrkd/**/*.py", recursive=True)
32             + glob("test/**/*.py", recursive=True)
33         )
34         output = run(cmd, capture_output=True)
35         if output.returncode == 1:
36             self.fail(
37                 f"black found code that needs reformatting:\n{output.stdout.decode()}"
38             )
39         if output.returncode != 0:
40             self.fail(
41                 f"black exited with code {output.returncode}:\n{output.stderr.decode()}"
42             )
43
44
45 if __name__ == "__main__":
46     main()