Skip to content

Commit

Permalink
remove jacoco agent from classpath if present
Browse files Browse the repository at this point in the history
fixes #1070
  • Loading branch information
Henry Coles committed Aug 2, 2022
1 parent 49b5a13 commit 6d86d9a
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Read all about it at http://pitest.org

* #1063 - Improve filtering of equivalent return mutants
* #1066 - Expand static initializer filtering
* #1070 - Remove jacoco agent if present on argline

### 1.9.3

Expand Down
16 changes: 13 additions & 3 deletions pitest-entry/src/main/java/org/pitest/process/WrappingProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,23 @@ private ProcessBuilder createProcessBuilder(String javaProc,

// IBM jdk adds this, thereby breaking everything
removeClassPathProperties(cmd);

removeJacocoAgent(cmd);

return new ProcessBuilder(cmd);
}

private void removeJacocoAgent(List<String> cmd) {
removeFromClassPath(cmd, line -> line.startsWith("-javaagent") && line.contains("jacoco"));
}

private static void removeClassPathProperties(List<String> cmd) {
removeFromClassPath(cmd, s -> s.startsWith("-Djava.class.path"));
}

private static void removeFromClassPath(List<String> cmd, Predicate<String> match) {
for (int i = cmd.size() - 1; i >= 0; i--) {
if (cmd.get(i).startsWith("-Djava.class.path")) {
if (match.test(cmd.get(i))) {
cmd.remove(i);
}
}
Expand All @@ -106,10 +116,10 @@ private List<String> createLaunchArgs(String javaProcess,

createClasspathJar(classPath, cmd);

cmd.addAll(args);

addPITJavaAgent(agentJarLocator, cmd);

cmd.addAll(args);

addLaunchJavaAgents(cmd);

cmd.add(mainClass.getName());
Expand Down
10 changes: 10 additions & 0 deletions pitest-maven-verification/src/test/java/org/pitest/PitMojoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.InputStream;
import java.util.Properties;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeFalse;
Expand Down Expand Up @@ -445,6 +446,14 @@ public void shouldFailCleanlyWhenTestPluginMissing() throws IOException {
}
}

@Test
public void shouldDisableJacoco() throws IOException, VerificationException {
File testDir = prepare("/pit-jacoco");
verifier.executeGoals(asList("test-compile", "org.pitest:pitest-maven:mutationCoverage"));

String actual = readResults(testDir);
assertThat(actual).doesNotContain("RUN_ERROR");
}

private void skipIfJavaVersionNotSupportByThirdParty() {
String javaVersion = System.getProperty("java.version");
Expand Down Expand Up @@ -474,6 +483,7 @@ private File prepare(String testPath) throws IOException,
verifier = new Verifier(path);
verifier.setAutoclean(false);
verifier.setDebug(true);
verifier.getCliOptions().add("-Dverbose=true");
verifier.getCliOptions().add("-Dpit.version=" + VERSION);
verifier.getCliOptions().add(
"-Dthreads=" + (Runtime.getRuntime().availableProcessors()));
Expand Down
96 changes: 96 additions & 0 deletions pitest-maven-verification/src/test/resources/pit-jacoco/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>pitest-sample</artifactId>
<version>0.1-SNAPSHOT</version>
<name>pit maven findOccupiedTestPackages coverage</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pit.version}</version>
<configuration>
<verbose>true</verbose>
<outputFormats>
<value>XML</value>
</outputFormats>
<timestampedReports>false</timestampedReports>
<exportLineCoverage>true</exportLineCoverage>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!-- Add this checking -->
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.9</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sources;

public class DiscoveredClass {

public int add(int a, int b) {
return a + b;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sources;

public class MessageBuilder {

public String getMessage(String name) {

StringBuilder result = new StringBuilder();

if (name == null || name.trim().length() == 0) {
result.append("Stuff!");
} else {
result.append("Hello " + name);
}
return result.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tests;

import static junit.framework.Assert.assertEquals;

import org.junit.Test;

import sources.DiscoveredClass;

public class DiscoveredTest {

@Test
public void testAdd() {
assertEquals(2, new DiscoveredClass().add(1, 1));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tests;

import org.junit.Test;
import sources.MessageBuilder;

import static org.junit.Assert.assertEquals;

public class TestMessageBuilder {

@Test
public void testName() {
MessageBuilder obj = new MessageBuilder();
assertEquals("Hello there", obj.getMessage("there"));
}

}

0 comments on commit 6d86d9a

Please sign in to comment.