File: filename_matching.py

package info (click to toggle)
mitmproxy 8.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,952 kB
  • sloc: python: 53,389; javascript: 1,603; xml: 186; sh: 105; ansic: 68; makefile: 13
file content (79 lines) | stat: -rwxr-xr-x 2,174 bytes parent folder | download | duplicates (2)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3

import os
import re
import glob
import sys


def check_src_files_have_test():
    missing_test_files = []

    excluded = [
        "mitmproxy/contrib/",
        "mitmproxy/io/proto/",
        "mitmproxy/proxy/layers/http",
        "mitmproxy/test/",
        "mitmproxy/tools/",
        "mitmproxy/platform/",
        "mitmproxy/utils/pyinstaller/",
    ]
    src_files = glob.glob("mitmproxy/**/*.py", recursive=True)
    src_files = [f for f in src_files if os.path.basename(f) != "__init__.py"]
    src_files = [
        f for f in src_files if not any(os.path.normpath(p) in f for p in excluded)
    ]
    for f in src_files:
        p = os.path.join("test", os.path.dirname(f), "test_" + os.path.basename(f))
        if not os.path.isfile(p):
            missing_test_files.append((f, p))

    return missing_test_files


def check_test_files_have_src():
    unknown_test_files = []

    excluded = [
        "test/mitmproxy/data/",
        "test/mitmproxy/net/data/",
        "/tservers.py",
        "/conftest.py",
    ]
    test_files = glob.glob("test/mitmproxy/**/*.py", recursive=True)
    test_files = [f for f in test_files if os.path.basename(f) != "__init__.py"]
    test_files = [
        f for f in test_files if not any(os.path.normpath(p) in f for p in excluded)
    ]
    for f in test_files:
        p = os.path.join(
            re.sub("^test/", "", os.path.dirname(f)),
            re.sub("^test_", "", os.path.basename(f)),
        )
        if not os.path.isfile(p):
            unknown_test_files.append((f, p))

    return unknown_test_files


def main():
    exitcode = 0

    missing_test_files = check_src_files_have_test()
    if missing_test_files:
        exitcode += 1
        for f, p in sorted(missing_test_files):
            print(f"{f} MUST have a matching test file: {p}")

    unknown_test_files = check_test_files_have_src()
    if unknown_test_files:
        # TODO: enable this in the future
        # exitcode += 1
        for f, p in sorted(unknown_test_files):
            print(f"{f} DOES NOT MATCH a source file! Expected to find: {p}")

    sys.exit(exitcode)


if __name__ == "__main__":
    main()