@@ -183,6 +183,20 @@ def try_to_run(cmd):
183
183
except subprocess .CalledProcessError as e :
184
184
write (' Error running `%s`: %s' % (cmd , str (getattr (e , 'output' , str (e )))))
185
185
186
+ def run_python_coverage (args ):
187
+ """Run the Python coverage tool
188
+
189
+ If it's importable in this Python, launch it using 'python -m'.
190
+ Otherwise, look it up on PATH like any other command.
191
+ """
192
+ try :
193
+ import coverage
194
+ except ImportError :
195
+ # Coverage is not installed on this Python. Hope it's on PATH.
196
+ try_to_run (['coverage' ] + args )
197
+ else :
198
+ # Coverage is installed on this Python. Run it as a module.
199
+ try_to_run ([sys .executable , '-m' , 'coverage' ] + args )
186
200
187
201
def remove_non_ascii (data ):
188
202
try :
@@ -670,12 +684,12 @@ def main(*argv, **kwargs):
670
684
# The `-a` option is mandatory here. If we
671
685
# have a `.coverage` in the current directory, calling
672
686
# without the option would delete the previous data
673
- try_to_run ( 'coverage combine -a' )
687
+ run_python_coverage ([ ' combine' , ' -a'] )
674
688
675
689
if os .path .exists (opj (os .getcwd (), '.coverage' )) and not os .path .exists (opj (os .getcwd (), 'coverage.xml' )):
676
690
write (' Generating coverage xml reports for Python' )
677
691
# using `-i` to ignore "No source for code" error
678
- try_to_run ( 'coverage xml -i' )
692
+ run_python_coverage ([ ' xml' , ' -i'] )
679
693
reports .append (read (opj (os .getcwd (), 'coverage.xml' )))
680
694
681
695
reports = list (filter (bool , reports ))
0 commit comments