Skip to content

Commit

Permalink
GEODE-10075 Fix jacoco code coverage reports
Browse files Browse the repository at this point in the history
We haven't been maintaining this gradle build support, but it is useful to be
able to get code coverage from distributed test runs, eg.

./gradlew geode-core:distributedTest -PcodeCoverage --tests XXX geode-core:jacocoDistributedTestReport

Changes:
* Upgraded the jacoco report tasks to use the newer syntax for locations
* Changed our WorkingDirectoryIsolator to fix the relative paths to the jacoco
  agent and execution trace files.

It's hard for me to tell if gradle is actually using windows or unix paths when
running on Windows. Just to be sure, adding support for both types of paths for
this change and adding a unit test of WorkingDirectoryIsolator.
  • Loading branch information
upthewaterspout authored and mhansonp committed Mar 11, 2022
1 parent c997cea commit 7904e87
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.apache.geode.gradle.testing.isolation;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -29,11 +30,18 @@
import java.util.stream.IntStream;

import org.gradle.api.UncheckedIOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WorkingDirectoryIsolator implements Consumer<ProcessBuilder> {
private final Logger log = LoggerFactory.getLogger(this.getClass());

private static final AtomicInteger WORKER_ID = new AtomicInteger();
private static final Pattern GRADLE_WORKER_CLASSPATH_FILE_PATTERN =
Pattern.compile("^@.*gradle-worker-classpath.*txt$");

private static final Pattern RELATIVE_PARENT_DIRECTORY_PATTERN =
Pattern.compile("(?<![/\\\\])(\\.\\.[/\\\\])");
private static final String PROPERTIES_FILE_NAME = "gemfire.properties";

/**
Expand Down Expand Up @@ -65,6 +73,27 @@ public void accept(ProcessBuilder processBuilder) {
List<String> command = processBuilder.command();
findGradleWorkerClasspathArg(command)
.ifPresent(i -> updateGradleWorkerClasspathFile(command, i, newWorkingDirectory));

upgradeRelativePaths(command);

log.debug("WorkingDirectoryIsolator updated command. New Working directory: {}, Command: {}", newWorkingDirectory, command);
}

/**
* Replace all occurrences of "../" that are not proceeded by a / in the command with "../../"
* to match the new working directory of the process. This fixes issues with java agent commands
* like jacoco that pass relative paths, eg "-agentlib:../tmp/jacocoXXX=../jacoco/test.txt
* @param command the command line to upgrade. It will be modified in place.
*/
private void upgradeRelativePaths(List<String> command) {
for(int i =0 ; i < command.size(); i++) {
String element = command.get(i);
final Matcher matcher = RELATIVE_PARENT_DIRECTORY_PATTERN.matcher(element);
//TO be platform independent, we will capture the syntax used (../ or ..\) and
//simply duplicate that in the command line string to become (../../ or ..\..\)
element = RELATIVE_PARENT_DIRECTORY_PATTERN.matcher(element).replaceAll("$1$1");
command.set(i, element);
}
}

private void updateGradleWorkerClasspathFile(List<String> command, int argIndex, Path directory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
package org.apache.geode.gradle.testing.isolation;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.Arrays;

import org.junit.Test;

public class WorkingDirectoryIsolatorTest {

@Test
public void updatesRelativeUnixPaths() {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(new File(""));
processBuilder.command("/bin/java", "-javaagent:../some/jacocoagent.jar=destfile=../jacoco/integrationTest.exec", "GradleWorkerMain");
new WorkingDirectoryIsolator().accept(processBuilder);

assertEquals(Arrays.asList("/bin/java", "-javaagent:../../some/jacocoagent.jar=destfile=../../jacoco/integrationTest.exec", "GradleWorkerMain"), processBuilder.command());
}

@Test
public void updatesRelativeWindowsPaths() {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(new File(""));
processBuilder.command("/bin/java", "-javaagent:..\\some\\jacocoagent.jar=destfile=..\\jacoco\\integrationTest.exec", "GradleWorkerMain");
new WorkingDirectoryIsolator().accept(processBuilder);

assertEquals(Arrays.asList("/bin/java", "-javaagent:..\\..\\some\\jacocoagent.jar=destfile=..\\..\\jacoco\\integrationTest.exec", "GradleWorkerMain"), processBuilder.command());
}

}
26 changes: 6 additions & 20 deletions gradle/code-analysis.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ if (project.hasProperty("staticAnalysis")) {
if (project.hasProperty("codeCoverage")) {
apply plugin: 'jacoco'

configurations.jacocoAnt {
dependencies.all { dep ->
dep.transitive = true
}
}

task mergeIntegrationTestCoverage(type: JacocoMerge) {
description 'Merges Distributed and Integration test coverage results'

Expand All @@ -75,38 +69,30 @@ if (project.hasProperty("codeCoverage")) {

}

jacocoTestReport {
reports {
csv.enabled false
sourceSets project.sourceSets.main
html.destination "${buildDir}/jacocoTestHtml"
}
}

task jacocoIntegrationTestReport(type: JacocoReport) {
reports {
csv.enabled false
sourceSets project.sourceSets.main
html.destination "${buildDir}/jacocoIntegrationTestHtml"
executionData = fileTree(dir: 'build/jacoco', include: '**/integrationTest.exec')
html.outputLocation = project.file("${buildDir}/reports/jacoco/integrationTest")
executionData fileTree(dir: 'build/jacoco', include: '**/integrationTest.exec')
}
}

task jacocoDistributedTestReport(type: JacocoReport) {
reports {
csv.enabled false
sourceSets project.sourceSets.main
html.destination "${buildDir}/jacocoDistributedTestHtml"
executionData = fileTree(dir: 'build/jacoco', include: '**/distributedTest.exec')
html.outputLocation = project.file("${buildDir}/reports/jacoco/distributedTest")
executionData fileTree(dir: 'build/jacoco', include: '**/distributedTest.exec')
}
}

task jacocoOverallTestReport(type: JacocoReport) {
reports {
csv.enabled false
sourceSets project.sourceSets.main
html.destination "${buildDir}/jacocoOverallTestHtml"
executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')
html.outputLocation = project.file("${buildDir}/reports/jacoco/all")
executionData fileTree(dir: 'build/jacoco', include: '**/*.exec')
}
}
}

0 comments on commit 7904e87

Please sign in to comment.