Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions codecov_cli/plugins/pycoverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def __init__(self, config: dict):

def run_preparation(self, collector) -> PreparationPluginReturn:
with sentry_sdk.start_span(name="pycoverage"):
if shutil.which("coverage") is None:
coverage_bin = shutil.which("coverage")
if coverage_bin is None:
logger.warning("coverage.py is not installed or can't be found.")
return

Expand All @@ -67,9 +68,9 @@ def run_preparation(self, collector) -> PreparationPluginReturn:
return
coverage_dir = pathlib.Path(path_to_coverage_data).parent
if self.config.report_type == "xml":
return self._generate_XML_report(coverage_dir)
return self._generate_XML_report(coverage_dir, coverage_bin)
if self.config.report_type == "json":
return self._generate_JSON_report(coverage_dir)
return self._generate_JSON_report(coverage_dir, coverage_bin)
return PreparationPluginReturn(
success=False,
messages=[f"report type {self.config.report_type} unknown"],
Expand All @@ -93,24 +94,26 @@ def _get_path_to_coverage(self) -> pathlib.Path:
None,
)

def _generate_XML_report(self, dir: pathlib.Path) -> PreparationPluginReturn:
def _generate_XML_report(
self, dir: pathlib.Path, coverage_bin: str
) -> PreparationPluginReturn:
"""Generates up-to-date XML report in the given directory"""
# the following if conditions avoid creating dummy .coverage file
if next(iglob(str(dir / ".coverage.*")), None) is not None:
logger.info(f"Running coverage combine -a in {dir}")
subprocess.run(["coverage", "combine", "-a"], cwd=dir)
subprocess.run([coverage_bin, "combine", "-a"], cwd=dir)

if (dir / ".coverage").exists():
logger.info(f"Generating coverage.xml report in {dir}")
completed_process = subprocess.run(
["coverage", "xml", "-i"], cwd=dir, capture_output=True
[coverage_bin, "xml", "-i"], cwd=dir, capture_output=True
)

output = completed_process.stdout.decode().strip()
logger.info(output)
return PreparationPluginReturn(success=True, messages=[])

def _generate_JSON_report(self, dir: pathlib.Path):
def _generate_JSON_report(self, dir: pathlib.Path, coverage_bin: str):
if (dir / ".coverage").exists():
logger.info(
f"Generating JSON report in {dir}",
Expand All @@ -120,7 +123,7 @@ def _generate_JSON_report(self, dir: pathlib.Path):
)
),
)
command = ["coverage", "json"]
command = [coverage_bin, "json"]
if self.config.include_contexts:
command.append("--show-contexts")
completed_process = subprocess.run(command, cwd=dir, capture_output=True)
Expand Down
Loading