File: run_tests.py

package info (click to toggle)
pep8-naming 0.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 148 kB
  • ctags: 163
  • sloc: python: 520; makefile: 10; sh: 4
file content (81 lines) | stat: -rw-r--r-- 1,995 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
80
81
import sys
import os
import pep8ext_naming
import re

PyCF_ONLY_AST = 1024

IS_PY3 = sys.version_info[0] == 3
IS_PY3_TEST = re.compile("^#\s*python3\s*only")
IS_PY2_TEST = re.compile("^#\s*python2\s*only")


def main():
    print('Running pep8-naming tests')
    test_count = 0
    errors = 0
    for filename in os.listdir('testsuite'):
        with open(os.path.join('testsuite', filename)) as fd:
            lines = list(fd)
            if not is_test_allowed(lines):
                continue

            for testcase, codes in load_tests(lines):
                test_count += 1
                errors += test_file(filename, testcase, codes)

    if errors == 0:
        print("%s tests run successful" % test_count)
        sys.exit(0)
    else:
        print("%i of %i tests failed" % (errors, test_count))
        sys.exit(1)


def is_test_allowed(lines):
    if IS_PY3 and any(IS_PY2_TEST.search(line) for line in lines[:3]):
        return False

    if not IS_PY3 and any(IS_PY3_TEST.search(line) for line in lines[:3]):
        return False

    return True


def load_tests(lines):
    testcase = []
    codes = []
    for line in lines:
        if line.startswith("#:"):
            if testcase:
                yield testcase, codes
                del testcase[:]
            codes = line.split()[1:]
        else:
            testcase.append(line)

    if testcase and codes:
        yield testcase, codes


def test_file(filename, lines, codes):
    tree = compile(''.join(lines), '', 'exec', PyCF_ONLY_AST)
    checker = pep8ext_naming.NamingChecker(tree, filename)
    found_errors = []
    for lineno, col_offset, msg, instance in checker.run():
        found_errors.append(msg.split()[0])

    if not found_errors and codes == ['Okay']:
        return 0

    errors = 0
    for code in codes:
        if code not in found_errors:
            errors += 1
            print("ERROR: %s not in %s" % (code, filename))

    return errors


if __name__ == '__main__':
    main()