Skip to content

Commit

Permalink
Adds a flag to enable coverage for generated source files (#18664)
Browse files Browse the repository at this point in the history
Closes #11350.

RELNOTES: Add flag --experimental_collect_code_coverage_for_generated_files.
PiperOrigin-RevId: 539648731
Change-Id: I352de7a74c522db6fbe5e10a21268914d1e39d58

Co-authored-by: Rasrack <[email protected]>
  • Loading branch information
iancha1992 and Rasrack authored Jun 20, 2023
1 parent eafdf2e commit 092ef1c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,11 @@ public boolean shouldInstrumentTestTargets() {
return options.instrumentTestTargets;
}

/** Returns a boolean of whether to collect code coverage for generated files or not. */
public boolean shouldCollectCodeCoverageForGeneratedFiles() {
return options.collectCodeCoverageForGeneratedFiles;
}

/**
* Returns a new, unordered mapping of names to values of "Make" variables defined by this
* configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ public ExecConfigurationDistinguisherSchemeConverter() {
+ " not be specified directly - 'bazel coverage' command should be used instead.")
public boolean collectCodeCoverage;

@Option(
name = "experimental_collect_code_coverage_for_generated_files",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
help =
"If specified, Bazel will also generate collect coverage information for generated"
+ " files.")
public boolean collectCodeCoverageForGeneratedFiles;

@Option(
name = "build_runfile_manifests",
defaultValue = "true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ public static InstrumentedFilesInfo collect(
for (TransitiveInfoCollection dep :
getPrerequisitesForAttributes(ruleContext, spec.sourceAttributes)) {
for (Artifact artifact : dep.getProvider(FileProvider.class).getFilesToBuild().toList()) {
if (artifact.isSourceArtifact() &&
spec.instrumentedFileTypes.matches(artifact.getFilename())) {
if (shouldIncludeArtifact(ruleContext.getConfiguration(), artifact)
&& spec.instrumentedFileTypes.matches(artifact.getFilename())) {
localSourcesBuilder.add(artifact);
}
}
Expand Down Expand Up @@ -262,6 +262,14 @@ public static boolean shouldIncludeLocalSources(
&& config.getInstrumentationFilter().isIncluded(label.toString()));
}

/**
* Return whether the artifact should be collected based on the origin of the artifact and the
* --experimental_collect_code_coverage_for_generated_files config setting.
*/
public static boolean shouldIncludeArtifact(BuildConfigurationValue config, Artifact artifact) {
return artifact.isSourceArtifact() || config.shouldCollectCodeCoverageForGeneratedFiles();
}

/**
* The set of file types and attributes to visit to collect instrumented files for a certain rule
* type. The class is intentionally immutable, so that a single instance is sufficient for all
Expand Down

0 comments on commit 092ef1c

Please sign in to comment.