• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3import os, sys
4import unittest_suite
5from autotest_lib.client.common_lib import utils
6
7
8root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
9
10
11invalid_dirs = ['client/tests/', 'client/site_tests/', 'tko/migrations/',
12                'server/tests/', 'server/site_tests/', 'server/self-test/',
13                'contrib/', 'utils/', 'ui/', 'frontend/migrations',
14                'frontend/afe/simplejson/', 'metrics/', 'old_cli/',
15                'client/common_lib/test_utils/', 'client/profilers/',
16                'site_packages']
17# append site specific invalid_dirs list, if any
18invalid_dirs.extend(utils.import_site_symbol(
19    __file__, 'autotest_lib.utils.site_coverage_suite', 'invalid_dirs', []))
20
21
22invalid_files = ['unittest_suite.py', 'coverage_suite.py', '__init__.py',
23                 'common.py']
24# append site specific invalid_files list, if any
25invalid_files.extend(utils.import_site_symbol(
26    __file__, 'autotest_lib.utils.site_coverage_suite', 'invalid_files', []))
27
28
29def is_valid_directory(dirpath):
30    dirpath += '/'
31    for invalid_dir in invalid_dirs:
32        if dirpath.startswith(os.path.join(root, invalid_dir)):
33            return False
34
35    return True
36
37
38def is_test_filename(filename):
39    return (filename.endswith('_unittest.py') or filename.endswith('_test.py'))
40
41
42def is_valid_filename(f):
43    # has to be a .py file
44    if not f.endswith('.py'):
45        return False
46
47    # but there are exceptions
48    if is_test_filename(f):
49        return False
50    elif f in invalid_files:
51        return False
52    else:
53        return True
54
55
56def run_unittests(prog, dirname, files):
57    for f in files:
58        if is_test_filename(f):
59            testfile = os.path.abspath(os.path.join(dirname, f))
60            cmd = "%s -x %s" % (prog, testfile)
61            utils.system_output(cmd, ignore_status=True, timeout=100)
62
63
64def main():
65    coverage = os.path.join(root, "contrib/coverage.py")
66
67    # remove preceeding coverage data
68    cmd = "%s -e" % (coverage)
69    os.system(cmd)
70
71    # I know this looks weird but is required for accurate results
72    cmd = "cd %s && find . -name '*.pyc' | xargs rm" % root
73    os.system(cmd)
74
75    # now walk through directory grabbing list of files
76    if len(sys.argv) == 2:
77        start = os.path.join(root, sys.argv[1])
78    else:
79        start = root
80
81    # run unittests through coverage analysis
82    os.path.walk(start, run_unittests, coverage)
83
84    module_strings = []
85    for dirpath, dirnames, files in os.walk(start):
86        if is_valid_directory(dirpath):
87            for f in files:
88                if is_valid_filename(f):
89                    temp = os.path.join(dirpath, f)
90                    module_strings.append(temp)
91
92    # analyze files
93    cmd = "%s -r -m %s" % (coverage, " ".join(module_strings))
94    os.system(cmd)
95
96
97if __name__ == "__main__":
98    main()
99