Add per-user preferences support.
[chromium-blink-merge.git] / third_party / typ / tools / cov.py
blob8e78fc4c924e79da739e83aecb9b47beef894914
1 #!/usr/bin/python
2 # Copyright 2014 Google Inc. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 import argparse
17 import sys
18 import textwrap
20 is_python3 = bool(sys.version_info.major == 3)
23 ALL_PRAGMAS = ['no cover', 'no win32', 'python2', 'python3', 'untested',
24 'win32']
25 DEFAULT_PRAGMAS = ALL_PRAGMAS[:]
27 if is_python3:
28 DEFAULT_PRAGMAS.remove('python3')
29 else:
30 DEFAULT_PRAGMAS.remove('python2')
32 if sys.platform == 'win32':
33 DEFAULT_PRAGMAS.remove('win32')
34 else:
35 DEFAULT_PRAGMAS.remove('no win32')
38 def add_arguments(parser):
39 parser.add_argument('--no-pragmas', action='store_true', default=False,
40 help='Show all uncovered lines (no pragmas).')
41 parser.add_argument('--path', action='append', default=[],
42 help='Prepend given directories to sys.path.')
43 parser.add_argument('--pragma', action='append', default=[],
44 help=('The coverage pragmas to honor '
45 '(defaults to %s).' % DEFAULT_PRAGMAS))
46 parser.add_argument('--show', action='append', default=[],
47 help='Show code protected by the specified pragmas '
48 '(uses all pragmas *except* for the ones '
49 'specified).')
50 parser.add_argument('--show-missing', action='store_true',
51 default=False, help='Show missing lines.')
52 parser.add_argument('--source', action='append', default=[],
53 help='Limit coverage data to the given directories.')
55 parser.formatter_class = argparse.RawTextHelpFormatter
56 parser.epilog = textwrap.dedent("""
57 Valid pragma values are:
58 'no cover': The default coverage pragma, this now means we
59 truly cannot cover it.
60 'no win32': Code that only executes when not on Windows.
61 'python2': Code that only executes under Python2.
62 'python3': Code that only executees under Python3.
63 'untested': Code that does not yet have tests.
64 'win32': Code that only executes on Windows.
66 In typ, we aim for 'no cover' to only apply to code that executes only
67 when coverage is not available (and hence can never be counted). Most
68 code, if annotated at all, should be 'untested', and we should strive
69 for 'untested' to not be used, either.
70 """)
73 def argv_from_args(args):
74 argv = []
75 if args.no_pragmas:
76 argv.append('--no-pragmas')
77 for arg in args.path:
78 argv.extend(['--path', arg])
79 for arg in args.show:
80 argv.extend(['--show', arg])
81 if args.show_missing:
82 argv.append('--show-missing')
83 for arg in args.source:
84 argv.extend(['--source', arg])
85 for arg in args.pragma:
86 argv.extend(['--pragma', arg])
87 return argv
90 def main(argv=None):
91 parser = argparse.ArgumentParser()
92 add_arguments(parser)
93 args, remaining_args = parser.parse_known_args(argv)
95 for path in args.path:
96 if path not in sys.path:
97 sys.path.append(path)
99 try:
100 import coverage
101 from coverage.execfile import run_python_module, run_python_file
102 except ImportError:
103 print("Error: coverage is not available.")
104 sys.exit(1)
106 cov = coverage.coverage(source=args.source)
107 cov.erase()
108 cov.clear_exclude()
110 if args.no_pragmas:
111 args.pragma = []
113 args.pragma = args.pragma or DEFAULT_PRAGMAS
115 if args.show:
116 args.show_missing = True
117 for pragma in args.show:
118 if pragma in args.pragma:
119 args.pragma.remove(pragma)
121 for pragma in args.pragma:
122 cov.exclude('pragma: %s' % pragma)
124 ret = 0
125 cov.start()
126 try:
127 if remaining_args[0] == '-m':
128 run_python_module(remaining_args[1], remaining_args[1:])
129 else:
130 run_python_file(remaining_args[0], remaining_args)
131 except SystemExit as e:
132 ret = e.code
133 cov.stop()
134 cov.save()
135 cov.report(show_missing=args.show_missing)
136 return ret
139 if __name__ == '__main__':
140 sys.exit(main())