Skip to content

Commit

Permalink
Adds a flag to enable coverage for generated source files
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
  • Loading branch information
Rasrack authored and copybara-github committed Jun 12, 2023
1 parent c56a1a6 commit 9c83049
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 @@ -566,6 +566,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 @@ -434,6 +434,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 @@ -208,8 +208,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 @@ -257,6 +257,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 9c83049

Please sign in to comment.