Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: third_party/typ/tools/cov.py

Issue 627763002: Add new 'typ' python testing framework to third_party/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: upload to typ v0.8.1, update README.chromium Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright 2014 Google Inc. All rights reserved.
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 import argparse
17 import sys
18 import textwrap
19
20 is_python3 = bool(sys.version_info.major == 3)
21
22
23 ALL_PRAGMAS = ['no cover', 'no win32', 'python2', 'python3', 'untested',
24 'win32']
25 DEFAULT_PRAGMAS = ALL_PRAGMAS[:]
26
27 if is_python3:
28 DEFAULT_PRAGMAS.remove('python3')
29 else:
30 DEFAULT_PRAGMAS.remove('python2')
31
32 if sys.platform == 'win32':
33 DEFAULT_PRAGMAS.remove('win32')
34 else:
35 DEFAULT_PRAGMAS.remove('no win32')
36
37
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.')
54
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.
65
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 """)
71
72
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
88
89
90 def main(argv=None):
91 parser = argparse.ArgumentParser()
92 add_arguments(parser)
93 args, remaining_args = parser.parse_known_args(argv)
94
95 for path in args.path:
96 if path not in sys.path:
97 sys.path.append(path)
98
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)
105
106 cov = coverage.coverage(source=args.source)
107 cov.erase()
108 cov.clear_exclude()
109
110 if args.no_pragmas:
111 args.pragma = []
112
113 args.pragma = args.pragma or DEFAULT_PRAGMAS
114
115 for pragma in args.show:
116 if pragma in args.pragma:
117 args.pragma.remove(pragma)
118
119 for pragma in args.pragma:
120 cov.exclude('pragma: %s' % pragma)
121
122 ret = 0
123 cov.start()
124 try:
125 if remaining_args[0] == '-m':
126 run_python_module(remaining_args[1], remaining_args)
127 else:
128 run_python_file(remaining_args[0], remaining_args)
129 except SystemExit as e:
130 ret = e.code
131 cov.stop()
132 cov.report(show_missing=args.show_missing)
133 return ret
134
135
136 if __name__ == '__main__':
137 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698