|
1 |
| -from pathlib import Path |
2 |
| -import epycs.subprocess |
3 |
| -from epycs.subprocess import cmd, ShellProgramFilters |
4 |
| - |
5 |
| -epycs.subprocess.exit_on_error = False |
6 |
| - |
7 |
| - |
8 |
| -TEST_DIR = Path(__file__).parent |
9 |
| -TEST_DATA_DIR = TEST_DIR / "adacut" |
10 |
| -CONTRIB = TEST_DIR.parent |
11 |
| -ADACUT_PY = CONTRIB / "adacut.py" |
12 |
| - |
13 |
| - |
14 |
| -adacut = cmd.python.arg(ADACUT_PY) |
15 |
| - |
16 |
| - |
17 |
| -class TestAdaCut: |
18 |
| - @classmethod |
19 |
| - def init_tests(cls): |
20 |
| - for d in ( |
21 |
| - d_tpl.parent |
22 |
| - for d_tpl in TEST_DATA_DIR.glob("**/template") |
23 |
| - if d_tpl.is_dir() |
24 |
| - ): |
25 |
| - tests = {} |
26 |
| - |
27 |
| - for dd in (d_src for d_src in d.glob("src_*") if d_src.is_dir()): |
28 |
| - cls.add_src_tests(tests, d, dd) |
29 |
| - |
30 |
| - for name, test in tests.items(): |
31 |
| - setattr(cls, f"test_{name}", test) |
32 |
| - |
33 |
| - def maybe_update_baseline(self, pytestconfig, expected_file, actual): |
34 |
| - if pytestconfig.getoption("--update-baseline"): |
35 |
| - with open(expected_file, "wt") as f: |
36 |
| - f.write(actual) |
37 |
| - |
38 |
| - def assert_file_content_equal(self, pytestconfig, expected_file, actual): |
39 |
| - with open(expected_file) as f: |
40 |
| - expected = f.read() |
41 |
| - |
42 |
| - try: |
43 |
| - assert actual == expected |
44 |
| - except AssertionError: |
45 |
| - self.maybe_update_baseline(pytestconfig, expected_file, actual) |
46 |
| - raise |
47 |
| - |
48 |
| - @classmethod |
49 |
| - def parse_options(cls, name): |
50 |
| - return name.split("_") |
51 |
| - |
52 |
| - @classmethod |
53 |
| - def add_src_tests(cls, tests, d, d_src): |
54 |
| - options = cls.parse_options(d_src.name[len("src_") :]) |
55 |
| - for f in (d / "template").glob("*.ad?"): |
56 |
| - |
57 |
| - def test_file_content_is_expected(self, pytestconfig): |
58 |
| - actual = adacut( |
59 |
| - *options, "--", f, check=True, out_filter=ShellProgramFilters.text |
60 |
| - ) |
61 |
| - self.assert_file_content_equal(pytestconfig, d_src / f.name, actual) |
62 |
| - |
63 |
| - tests[f"{d_src.parent.name}_{d_src.name}_{f.name}"] = ( |
64 |
| - test_file_content_is_expected |
65 |
| - ) |
66 |
| - |
67 |
| - |
68 |
| -TestAdaCut.init_tests() |
| 1 | +from pathlib import Path |
| 2 | +import epycs.subprocess |
| 3 | +from epycs.subprocess import cmd, ShellProgramFilters |
| 4 | + |
| 5 | +epycs.subprocess.exit_on_error = False |
| 6 | + |
| 7 | + |
| 8 | +TEST_DIR = Path(__file__).parent |
| 9 | +TEST_DATA_DIR = TEST_DIR / "adacut" |
| 10 | +CONTRIB = TEST_DIR.parent |
| 11 | +ADACUT_PY = CONTRIB / "adacut.py" |
| 12 | + |
| 13 | + |
| 14 | +adacut = cmd.python.arg(ADACUT_PY) |
| 15 | + |
| 16 | + |
| 17 | +class TestAdaCut: |
| 18 | + @classmethod |
| 19 | + def init_tests(cls): |
| 20 | + for d in ( |
| 21 | + d_tpl.parent |
| 22 | + for d_tpl in TEST_DATA_DIR.glob("**/template") |
| 23 | + if d_tpl.is_dir() |
| 24 | + ): |
| 25 | + tests = {} |
| 26 | + |
| 27 | + for dd in (d_src for d_src in d.glob("src_*") if d_src.is_dir()): |
| 28 | + cls.add_src_tests(tests, d, dd) |
| 29 | + |
| 30 | + for name, test in tests.items(): |
| 31 | + setattr(cls, f"test_{name}", test) |
| 32 | + |
| 33 | + def maybe_update_baseline(self, pytestconfig, expected_file, actual): |
| 34 | + if pytestconfig.getoption("--update-baseline"): |
| 35 | + with open(expected_file, "wt") as f: |
| 36 | + f.write(actual) |
| 37 | + |
| 38 | + def assert_file_content_equal(self, pytestconfig, expected_file, actual): |
| 39 | + with open(expected_file) as f: |
| 40 | + expected = f.read() |
| 41 | + |
| 42 | + try: |
| 43 | + assert actual == expected |
| 44 | + except AssertionError: |
| 45 | + self.maybe_update_baseline(pytestconfig, expected_file, actual) |
| 46 | + raise |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def parse_options(cls, name): |
| 50 | + return name.split("_") |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def add_src_tests(cls, tests, d, d_src): |
| 54 | + options = cls.parse_options(d_src.name[len("src_") :]) |
| 55 | + for f in (d / "template").glob("*.ad?"): |
| 56 | + |
| 57 | + def test_file_content_is_expected(self, pytestconfig): |
| 58 | + actual = adacut( |
| 59 | + *options, "--", f, check=True, out_filter=ShellProgramFilters.text |
| 60 | + ) |
| 61 | + self.assert_file_content_equal(pytestconfig, d_src / f.name, actual) |
| 62 | + |
| 63 | + tests[ |
| 64 | + f"{d_src.parent.name}_{d_src.name}_{f.name}" |
| 65 | + ] = test_file_content_is_expected |
| 66 | + |
| 67 | + |
| 68 | +TestAdaCut.init_tests() |
0 commit comments