Skip to content

Commit

Permalink
Implement Cobertura output option for #9275 (#9325)
Browse files Browse the repository at this point in the history
### Problem

Implement Cobertura output option for #9275

### Solution

Add new option (`--scoverage-report-output-as-cobertura`) for triggering `CoberturaXmlWriter(...)` method call in `scoverage-report-generator_2.12-0.0.3.jar` to generate Cobertura output

### Result

Generate Cobertura output (`.pants.d/test/junit/_runs/*/*/all/scoverage/reports/xml/cobertura.xml`) 
when `--scoverage-enable-scoverage --scoverage-report-output-as-cobertura` options are provided in pants command.
  • Loading branch information
patliu85 authored and stuhood committed Mar 30, 2020
1 parent 1bf1eab commit dc9327e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/python/pants/backend/jvm/tasks/coverage/scoverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def scoverage_report_jar(**kwargs):
return JarDependency(
org="org.pantsbuild",
name="scoverage-report-generator_2.12",
rev="0.0.2",
rev="0.0.3",
**kwargs,
)

Expand Down Expand Up @@ -69,6 +69,14 @@ def scoverage_report_jar(**kwargs):
"filter.",
)

register(
"--output-as-cobertura",
type=bool,
default=False,
fingerprint=False,
help="Export cobertura formats which would allow users to merge with cobertura coverage for java targets.",
)

def create(self, settings, targets, execute_java_for_targets):
"""
:param settings: Generic code coverage settings.
Expand All @@ -87,6 +95,7 @@ def create(self, settings, targets, execute_java_for_targets):

opts = Scoverage.Factory.global_instance().get_options()
target_filters = opts.target_filters
output_as_cobertura = opts.output_as_cobertura
coverage_output_dir = settings.context.options.for_global_scope().pants_distdir

return Scoverage(
Expand All @@ -95,6 +104,7 @@ def create(self, settings, targets, execute_java_for_targets):
settings,
targets,
execute_java_for_targets,
output_as_cobertura,
coverage_output_dir=coverage_output_dir,
)

Expand All @@ -105,6 +115,7 @@ def __init__(
settings,
targets,
execute_java_for_targets,
output_as_cobertura,
coverage_output_dir=None,
):
"""
Expand All @@ -125,6 +136,7 @@ def __init__(
self._execute_java = functools.partial(execute_java_for_targets, targets)
self._coverage_force = settings.options.coverage_force
self._report_path = report_path
self._output_as_cobertura = output_as_cobertura
self._coverage_output_dir = coverage_output_dir

#
Expand Down Expand Up @@ -179,6 +191,7 @@ def report(self, output_dir, execution_failed_exception=None):

main = "org.pantsbuild.scoverage.report.ScoverageReport"
scoverage_cp = self._report_path
output_as_cobertura = self._output_as_cobertura
html_report_path = os.path.join(output_dir, "scoverage", "reports", "html")
xml_report_path = os.path.join(output_dir, "scoverage", "reports", "xml")
safe_mkdir(html_report_path, clean=True)
Expand All @@ -199,6 +212,9 @@ def report(self, output_dir, execution_failed_exception=None):
f"{','.join(final_target_dirs)}",
]

if output_as_cobertura:
args.append("--outputAsCobertura")

result = self._execute_java(
classpath=scoverage_cp,
main=main,
Expand Down
3 changes: 3 additions & 0 deletions src/python/pants/java/jar/jar_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class JarDependency:
intransitive: bool
excludes: Tuple[Exclude, ...]
base_path: str
output_as_cobertura: bool

def __init__(
self,
Expand All @@ -136,6 +137,7 @@ def __init__(
intransitive: bool = False,
excludes: Optional[Sequence[Exclude]] = None,
base_path: Optional[str] = None,
output_as_cobertura: bool = False,
) -> None:
self.org = org
self.base_name = name
Expand All @@ -152,6 +154,7 @@ def __init__(
if os.path.isabs(base_path):
base_path = os.path.relpath(base_path, get_buildroot())
self.base_path = base_path
self.output_as_cobertura = output_as_cobertura

def __str__(self):
return "JarDependency({})".format(self.coordinate)
Expand Down

0 comments on commit dc9327e

Please sign in to comment.