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.
20 is_python3
= bool(sys
.version_info
.major
== 3)
23 ALL_PRAGMAS
= ['no cover', 'no win32', 'python2', 'python3', 'untested',
25 DEFAULT_PRAGMAS
= ALL_PRAGMAS
[:]
28 DEFAULT_PRAGMAS
.remove('python3')
30 DEFAULT_PRAGMAS
.remove('python2')
32 if sys
.platform
== 'win32':
33 DEFAULT_PRAGMAS
.remove('win32')
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 '
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.
73 def argv_from_args(args
):
76 argv
.append('--no-pragmas')
78 argv
.extend(['--path', arg
])
80 argv
.extend(['--show', arg
])
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
])
91 parser
= argparse
.ArgumentParser()
93 args
, remaining_args
= parser
.parse_known_args(argv
)
95 for path
in args
.path
:
96 if path
not in sys
.path
:
101 from coverage
.execfile import run_python_module
, run_python_file
103 print("Error: coverage is not available.")
106 cov
= coverage
.coverage(source
=args
.source
)
113 args
.pragma
= args
.pragma
or DEFAULT_PRAGMAS
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
)
127 if remaining_args
[0] == '-m':
128 run_python_module(remaining_args
[1], remaining_args
[1:])
130 run_python_file(remaining_args
[0], remaining_args
)
131 except SystemExit as e
:
135 cov
.report(show_missing
=args
.show_missing
)
139 if __name__
== '__main__':