summaryrefslogtreecommitdiffstats
path: root/test/generate
blob: ef0967db231f55becea46dd5966d810c6d2bb7c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3

# Generate skeleton files for completion of specified command

import fileinput
import re
import sys


def main():
    if len(sys.argv) < 2:
        print("Usage: %s command [args...]" % sys.argv[0], file=sys.stderr)
        sys.exit(1)

    cmd = testfile = sys.argv[1]
    args = " ".join(sys.argv[2:]) if len(sys.argv) > 2 else ""
    marker = ""
    if re.search("[.+-]", cmd):
        testfile = re.sub("[.-]", "_", cmd).replace("+", "plus")
        marker = '\n@pytest.mark.bashcomp(\n    cmd="%s",\n)' % cmd
    testfile = "test_%s.py" % testfile
    name = re.sub("(^|[_-]+)(.)", lambda m: m.group(2).upper(), cmd)
    name = name.replace("+", "Plus")

    with open("t/%s" % testfile, "w") as f:
        print(
            """\
import pytest

%s
class Test%s:
    @pytest.mark.complete("%s %s")
    def test_basic(self, completion):
        assert completion"""
            % (marker, name, cmd, args),
            file=f,
        )

    in_extra_dist = False
    extra_dist_lines = set()
    with fileinput.input(files=("t/Makefile.am"), inplace=True) as f:
        for line in f:
            if line.startswith("EXTRA_DIST "):
                in_extra_dist = True
            elif in_extra_dist:
                if line.startswith("\t"):
                    line = line.strip()
                    if not line.endswith("\\"):
                        line += " \\"
                    extra_dist_lines.add(line)
                    continue
                extra_dist_lines.add("%s \\" % testfile)
                sys.stdout.write("\t")
                print("\n\t".join(sorted(extra_dist_lines))[:-2])
                in_extra_dist = False
            sys.stdout.write(line)


if __name__ == "__main__":
    main()