Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 8660351

Browse files
authored
Run Python 'coverage' tool as a module if possible
This means coverage can still be called to convert its data to XML even if it's not on PATH. Closes #71.
1 parent 0b96aba commit 8660351

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

codecov/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ def try_to_run(cmd):
183183
except subprocess.CalledProcessError as e:
184184
write(' Error running `%s`: %s' % (cmd, str(getattr(e, 'output', str(e)))))
185185

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)
186200

187201
def remove_non_ascii(data):
188202
try:
@@ -670,12 +684,12 @@ def main(*argv, **kwargs):
670684
# The `-a` option is mandatory here. If we
671685
# have a `.coverage` in the current directory, calling
672686
# without the option would delete the previous data
673-
try_to_run('coverage combine -a')
687+
run_python_coverage(['combine', '-a'])
674688

675689
if os.path.exists(opj(os.getcwd(), '.coverage')) and not os.path.exists(opj(os.getcwd(), 'coverage.xml')):
676690
write(' Generating coverage xml reports for Python')
677691
# using `-i` to ignore "No source for code" error
678-
try_to_run('coverage xml -i')
692+
run_python_coverage(['xml', '-i'])
679693
reports.append(read(opj(os.getcwd(), 'coverage.xml')))
680694

681695
reports = list(filter(bool, reports))

0 commit comments

Comments
 (0)