blob: 7ca66e9b78d23dde3d698654395fef7b5564d6a8 [file] [log] [blame]
mbligh7c8ea992009-06-22 19:03:08 +00001#!/usr/bin/python
mbligh9cd69d32008-06-26 23:29:33 +00002
3import os, sys
4import unittest_suite
mbligh9cd69d32008-06-26 23:29:33 +00005from autotest_lib.client.common_lib import utils
6
showard39fef9c2009-12-10 21:43:07 +00007
mblighbd1eb202008-07-29 22:09:29 +00008root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
mbligh9cd69d32008-06-26 23:29:33 +00009
10
showard39fef9c2009-12-10 21:43:07 +000011invalid_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
mbligh9cd69d32008-06-26 23:29:33 +000029def is_valid_directory(dirpath):
showard39fef9c2009-12-10 21:43:07 +000030 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'))
mbligh9cd69d32008-06-26 23:29:33 +000040
41
42def is_valid_filename(f):
mbligh71397272009-02-03 21:58:10 +000043 # has to be a .py file
44 if not f.endswith('.py'):
45 return False
mbligh9cd69d32008-06-26 23:29:33 +000046
showard39fef9c2009-12-10 21:43:07 +000047 # but there are exceptions
48 if is_test_filename(f):
mbligh71397272009-02-03 21:58:10 +000049 return False
showard39fef9c2009-12-10 21:43:07 +000050 elif f in invalid_files:
mbligh71397272009-02-03 21:58:10 +000051 return False
52 else:
53 return True
mbligh9cd69d32008-06-26 23:29:33 +000054
55
showard39fef9c2009-12-10 21:43:07 +000056def 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
mbligh9cd69d32008-06-26 23:29:33 +000064def main():
mbligh71397272009-02-03 21:58:10 +000065 coverage = os.path.join(root, "contrib/coverage.py")
mbligh35b3ae12008-07-01 01:49:22 +000066
mbligh71397272009-02-03 21:58:10 +000067 # remove preceeding coverage data
68 cmd = "%s -e" % (coverage)
showard39fef9c2009-12-10 21:43:07 +000069 os.system(cmd)
mbligh35b3ae12008-07-01 01:49:22 +000070
showard39fef9c2009-12-10 21:43:07 +000071 # 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)
mbligh35b3ae12008-07-01 01:49:22 +000074
showard39fef9c2009-12-10 21:43:07 +000075 # 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
mbligh71397272009-02-03 21:58:10 +000084 module_strings = []
showard39fef9c2009-12-10 21:43:07 +000085 for dirpath, dirnames, files in os.walk(start):
mbligh71397272009-02-03 21:58:10 +000086 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)
mbligh35b3ae12008-07-01 01:49:22 +000091
mbligh71397272009-02-03 21:58:10 +000092 # analyze files
93 cmd = "%s -r -m %s" % (coverage, " ".join(module_strings))
showard39fef9c2009-12-10 21:43:07 +000094 os.system(cmd)
mbligh9cd69d32008-06-26 23:29:33 +000095
96
97if __name__ == "__main__":
mbligh71397272009-02-03 21:58:10 +000098 main()