Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cribro-core/src/main/java/io/github/cribrodev/PitestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ public PitestRun targetTests(String targetTests) {
return this;
}

/** The class directories PIT may mutate; a glob cannot reach a class outside them. */
/**
* The classpath entries, class directories or jars, that hold the classes PIT may mutate; a glob
* cannot reach a class outside them.
*/
public PitestRun mutableCodePaths(List<File> mutableCodePaths) {
this.mutableCodePaths = List.copyOf(mutableCodePaths);
return this;
Expand Down Expand Up @@ -194,7 +197,7 @@ public List<String> arguments() throws IOException {
options.put("reportDir", reportDir.getAbsolutePath());
options.put("targetClasses", String.join(",", targetClasses));
options.put("targetTests", targetTests == null ? ALL_TESTS : targetTests);
// Only these directories are mutated, so a glob cannot reach a test class of the same name.
// Only these entries are mutated, so a glob cannot reach a test class of the same name.
options.put("mutableCodePaths", paths(mutableCodePaths));
options.put("sourceDirs", paths(sourceDirs));
options.put("classPathFile", classPathFile.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ResolvableDependencies;
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
import org.gradle.api.artifacts.result.ResolvedComponentResult;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.attributes.Category;
Expand Down Expand Up @@ -386,6 +388,7 @@ && compareVersions(jacoco.getToolVersion(), JACOCO_VERSION) < 0) {
project.files(
(Callable<Object>) () -> missingLauncher(project, testRuntime, t)));
task.getMainClassesDirs().from(main.getOutput().getClassesDirs());
task.getMainArtifacts().from(mainVariantArtifacts(testRuntime));
task.getMainSourceDirs().from((Callable<Set<File>>) () -> sourceDirs(main));
task.getReportDir()
.set(project.getLayout().getBuildDirectory().dir("reports/pitest-diff"));
Expand Down Expand Up @@ -421,6 +424,38 @@ && compareVersions(jacoco.getToolVersion(), JACOCO_VERSION) < 0) {
return overrides;
}

/**
* Returns the files of this project's main variant, the one with the capability {@code
* <group>:<name>}, that {@code configuration} resolves to, such as the jar {@code
* java-test-fixtures} puts on the test runtime classpath, whatever task builds it. The test
* fixtures' jar is left out: its variant carries the capability {@code
* <group>:<name>-test-fixtures}.
*/
private static Provider<List<File>> mainVariantArtifacts(Configuration configuration) {
ResolvableDependencies incoming = configuration.getIncoming();
return incoming
.getArtifacts()
.getResolvedArtifacts()
.zip(
incoming.getResolutionResult().getRootComponent(),
(artifacts, root) -> {
ModuleVersionIdentifier self = root.getModuleVersion();
List<File> files = new ArrayList<>();
for (ResolvedArtifactResult artifact : artifacts) {
if (artifact.getId().getComponentIdentifier().equals(root.getId())
&& self != null
&& artifact.getVariant().getCapabilities().stream()
.anyMatch(
c ->
c.getGroup().equals(self.getGroup())
&& c.getName().equals(self.getName()))) {
files.add(artifact.getFile());
}
}
return files;
});
}

/**
* Returns the JUnit Platform launcher matching the engine on the test runtime classpath, or
* nothing where the tests do not run on the JUnit Platform or bring a launcher of their own.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
Expand Down Expand Up @@ -78,10 +81,23 @@ public abstract class PitestDiffTask extends DefaultTask {
@Classpath
public abstract ConfigurableFileCollection getTestRuntimeClasspath();

/** The main classes, the only code PIT mutates; they are on the test runtime classpath too. */
/**
* The directories of the main classes, which PIT mutates where the test runtime classpath holds
* them.
*/
@Internal
public abstract ConfigurableFileCollection getMainClassesDirs();

/**
* The artifacts of this project's main variant, the one with the capability {@code
* <group>:<name>}, that the test runtime classpath resolves to, such as the jar that {@code
* java-test-fixtures} puts there in place of the main classes directories. PIT mutates
* those the test runtime classpath holds; where the collection is empty, it mutates only the
* {@link #getMainClassesDirs() directories}.
*/
@Internal
public abstract ConfigurableFileCollection getMainArtifacts();

/** The main source directories, where the report finds the text of a mutated line. */
@Internal
public abstract ConfigurableFileCollection getMainSourceDirs();
Expand Down Expand Up @@ -154,6 +170,32 @@ public boolean hasTargetClasses() throws IOException {
.isEmpty();
}

/**
* Returns the entries of the test runtime classpath that hold the main classes: their
* directories and the artifacts of the project's main variant, whichever the classpath holds. PIT
* mutates a class only where it finds it in one of these entries of the classpath the tests run
* with.
*/
private List<File> mutableCodePaths() {
Set<File> main = new LinkedHashSet<>(getMainClassesDirs().getFiles());
main.addAll(getMainArtifacts().getFiles());
return getTestRuntimeClasspath().getFiles().stream()
.filter(main::contains)
.collect(Collectors.toList());
}

private String noMainClassesMessage() {
return "Found none of the main classes on the test runtime classpath of "
+ getPath()
+ ". The classpath holds neither the directories "
+ getMainClassesDirs().getFiles()
+ " nor the project's own jar resolved through configurations.testRuntimeClasspath, such"
+ " as the one java-test-fixtures puts there, so PIT would have no class to mutate. Add the"
+ " directories to sourceSets.test.runtimeClasspath, the classpath pitestDiff runs the"
+ " tests with under PIT. PIT then mutates the main classes even where the tests load"
+ " them from another file, such as a jar added with files(...).";
}

@TaskAction
public void run() throws IOException {
Path changedLines = getChangedLinesFile().get().getAsFile().toPath();
Expand All @@ -163,12 +205,17 @@ public void run() throws IOException {
getFileSystemOperations().delete(spec -> spec.delete(reportDir));
Files.createDirectories(reportDir.toPath());
File log = new File(reportDir, PitestRun.LOG);
List<File> mutableCodePaths = mutableCodePaths();
// PIT given no code path falls back to a default that also admits the test classes.
if (mutableCodePaths.isEmpty()) {
throw new GradleException(noMainClassesMessage());
}
PitestRun pit =
new PitestRun()
.targetClasses(ChangedLinesFile.targetClasses(ChangedLinesFile.read(changedLines).keySet()))
.reportDir(reportDir)
.targetTests(getTargetTests().getOrNull())
.mutableCodePaths(List.copyOf(getMainClassesDirs().getFiles()))
.mutableCodePaths(mutableCodePaths)
.sourceDirs(List.copyOf(getMainSourceDirs().getFiles()))
.classPath(List.copyOf(getTestRuntimeClasspath().getFiles()))
.jvmArgs(getTestJvmArgs().get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,11 @@ it, and it needs an Arcmutate licence file, `arcmutate-licence.txt`, at the repo
under PIT and drop out.
- PIT runs JUnit Jupiter tests one at a time, whatever the build sets for parallel execution.
- PIT runs only the `test` task's tests, not those of other `Test` tasks such as integration tests.
- PIT mutates the main classes where `sourceSets.test.runtimeClasspath` holds them: in the main classes directories, or
in an artifact with the project's own capability `<group>:<name>` that `configurations.testRuntimeClasspath`
resolves to, such as the jar `java-test-fixtures` puts there, whatever task builds it. A file added with `files(...)`
is not resolved by the configuration and does not count. Where the classpath holds neither, the task fails with
`Found none of the main classes on the test runtime classpath of <task path>`. Add the directories to
`sourceSets.test.runtimeClasspath`. PIT then mutates the main classes even where the tests load them from another
file, such as the `jar` task's archive added with `files(...)`.
- Do not run two Gradle builds in the same project at once.
Loading
Loading