Skip to content
Open
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
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[![Gradle Plugin Portal](https://img.shields.io/gradle-plugin-portal/v/io.github.cribro-dev.cribro)](https://plugins.gradle.org/plugin/io.github.cribro-dev.cribro)

cribro reports which lines a branch changed that no test executes, and which changed lines no test would notice
breaking. It works in Gradle and Maven builds with Java or Kotlin sources: `test` ends with a diff coverage report from
JaCoCo, and a separate task runs PIT mutation testing on the changed lines only. The reports are plain text sized for
breaking. In a Git work tree, `test` in a Gradle or Maven build ends with a diff coverage report from JaCoCo, and a
separate task runs PIT mutation testing on changed Java or Kotlin lines only. The reports are plain text sized for
an AI agent, so an agent that changed the code reads them in the build output and writes the tests that are missing.
There is no HTML report of its own.

Expand Down Expand Up @@ -51,9 +51,9 @@ changes.

## Gradle

For a Gradle build, cribro is a settings plugin, `io.github.cribro-dev.cribro`. It attaches JaCoCo to the `test`
task of every Java project, which then ends with the diff coverage report, and adds a `pitestDiff` task that runs PIT
on the changed lines.
For a Gradle build, cribro is a settings plugin, `io.github.cribro-dev.cribro`. In a Git work tree, it attaches JaCoCo
to the `test` task of every Java project and prints a diff coverage report. The `pitestDiff` task runs PIT on the
changed lines.

### Gradle requirements

Expand Down Expand Up @@ -252,8 +252,8 @@ is `:postgresql`. The guide lists the paths. For the root project, the task path
The plugin registers `cribro` on the root project, and adds a `cribroProjectElements` configuration to every project;
`cribro` resolves that configuration of each project to list the projects that have the tasks. Applied as a settings
plugin or through the init script, cribro works with Isolated Projects, tried on Gradle 9.8.2. On Gradle 8, Isolated
Projects fails the `cribro` task where a subproject has the same group and name as the root project. For every project
with the `java` plugin, it:
Projects fails the `cribro` task where a subproject has the same group and name as the root project. In a Git work
tree, for every project with the `java` plugin, it:

- applies `jacoco`, enables it on the `test` task, and has `test` finalized by `jacocoTestReport` with the XML report,
then by `diffCoverage`. These override the build's own settings after its scripts run, so a build that disables
Expand All @@ -266,11 +266,14 @@ with the `java` plugin, it:
bring one;
- runs PIT in a separate JVM from the `test` task's launcher, with its JVM arguments and system properties.

Outside a Git work tree, cribro leaves project-owned JaCoCo coverage in place and skips diff coverage and mutation
testing. With automatic diff coverage enabled, a test build prints the skip notice once.

## Maven

For a Maven build, cribro is a Maven extension and plugin, `io.github.cribro-dev:cribro-maven-plugin`. Loaded as an
extension, it attaches JaCoCo to the tests of surefire's `default-test` execution and prints the diff coverage report
before Maven's build summary. Its goal `cribro:pitest-diff` runs PIT on the changed lines.
For a Maven build, cribro is a Maven extension and plugin, `io.github.cribro-dev:cribro-maven-plugin`. In a Git work
tree, the extension attaches JaCoCo to the tests of Surefire's `default-test` execution and prints the diff coverage
report before Maven's build summary. Its goal `cribro:pitest-diff` runs PIT on the changed lines.

### Maven requirements

Expand Down Expand Up @@ -390,13 +393,16 @@ the Gradle ones with `-D` in place of `-P`.

### What cribro changes in a Maven build

Loaded as an extension, cribro:
Loaded as an extension in a Git work tree, cribro:

- adds a JaCoCo agent to the `argLine` of surefire's `default-test` execution just before it runs, whatever the POM
sets there. Where the `argLine` already attaches a JaCoCo agent, as `jacoco-maven-plugin`'s `prepare-agent` does,
cribro reads that agent's file instead;
- writes `target/cribro/jacoco.xml` and `target/cribro/diff-coverage.txt` in each module the change reached;
- declares `cribro-maven-plugin` in every module that does not declare it, so that the `cribro:` prefix resolves.
- writes `target/cribro/jacoco.xml` and `target/cribro/diff-coverage.txt` in each module the change reached.

Outside a Git work tree, cribro honors the POM's JaCoCo configuration and skips diff coverage and mutation testing.
With automatic diff coverage enabled, a test build prints the skip notice once. The extension declares
`cribro-maven-plugin` in every module that does not declare it, so that the `cribro:` prefix resolves in any layout.

`cribro:pitest-diff` runs PIT in a separate JVM, with the classpath, JVM, `argLine`, system properties, environment
variables, working directory, groups, includes, and excludes of surefire's `default-test` execution.
Expand Down
23 changes: 13 additions & 10 deletions agent/cribro/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ description: Measure how well the tests of a Gradle or Maven Java or Kotlin proj

# Diff coverage and diff mutation testing

cribro adds diff coverage to `test` and diff mutation testing to every Java or Kotlin project of a Gradle or Maven build. A build
with a `pom.xml` follows the [Maven](#maven) section; the rest of this section is for Gradle builds. A changed file is
In a Git work tree, cribro measures diff coverage with `test` and runs diff mutation testing on Java or Kotlin projects.
A build with a `pom.xml` follows the [Maven](#maven) section. A changed file is
matched to its coverage counters and its mutants by the package it declares and its file name, not by its directory, so
a Kotlin file whose directory does not follow its package is measured all the same.

In a Gradle build, cribro adds a `pitestDiff` task. A build can apply it itself: its `settings.gradle` or
In a Gradle build, cribro adds a `pitestDiff` task. Pass `-Pcribro.pitestTargetTests=<test classes>` to that task.
A build can apply cribro itself: its `settings.gradle` or
`settings.gradle.kts` names `io.github.cribro-dev.cribro`. Otherwise the init script at
`~/.gradle/cribro/cribro.init.gradle` applies it without changing the build.

Expand All @@ -30,12 +31,14 @@ The init-script form also works in a build that applies cribro itself. The guide
tasks, gives every later command in the form this build needs, and says how to act on each report. It comes from the
plugin that will run, so it matches its version.

The usual sequence:
## Reporting sequence

1. After changing `src/main` and its tests, run the `test` command the guide prints. It ends with the changed lines no
test executed; write a test for each, or justify leaving it untested.
2. Once the changed lines are covered, run the `pitestDiff` command the guide prints, with
`-Pcribro.pitestTargetTests=<test classes>`. Each surviving mutant is a changed line whose behavior no test asserts.
1. After changing `src/main` and its tests, run the `test` command the guide prints. Use the report to find changed
lines no test executed, then write a test for each or justify leaving it untested. If the build prints
`Diff coverage: skipped, since <dir> is not in a git work tree.`, report that diff coverage was skipped and end
the sequence.
2. Once the changed lines are covered, run the mutation testing command the guide prints for the relevant test
classes. Each surviving mutant is a changed line whose behavior no test asserts.

Where a Gradle build does not apply cribro and `~/.gradle/cribro/cribro.init.gradle` does not exist, cribro is not
installed: tell the user, and point them at <https://github.com/cribro-dev/cribro#install>.
Expand All @@ -56,7 +59,7 @@ Otherwise:
mvn -Dmaven.ext.class.path="$HOME"/.m2/cribro/cribro-maven.jar -q cribro:guide
```

Then follow the same sequence with the commands the guide prints: `test` ends with the diff coverage report, and
`cribro:pitest-diff` with `-Dcribro.pitestTargetTests=<test classes>` reports surviving mutants. Where neither the build
Follow the [reporting sequence](#reporting-sequence) with the Maven commands from the guide. Pass
`-Dcribro.pitestTargetTests=<test classes>` to `cribro:pitest-diff`. Where neither the build
nor `~/.m2/cribro/cribro-maven.jar` provides cribro, it is not installed: tell the user, and point them at
<https://github.com/cribro-dev/cribro#maven>.
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ public List<String> unreadable() {
* explicit ref, the upstream branch is tried, then the default branches of {@code origin}.
*
* @param explicit the ref the caller named, or null
* @throws CribroException where the explicit ref does not resolve, because diffing against
* something else would report the wrong lines as changed
* @throws CribroException where Git cannot read its repository or the explicit ref does not
* resolve
*/
public static ChangedLines measure(Git git, String explicit) {
if (explicit != null && !resolves(git, explicit)) {
git.output("rev-parse", "--git-dir");
throw new CribroException(BASE_PROPERTY + " " + explicit + " does not resolve to a commit");
}
String upstream = git.outputOrNull(UPSTREAM_NAME);
Expand All @@ -122,6 +123,7 @@ public static ChangedLines measure(Git git, String explicit) {
candidate, mergeBase, changedLines(git, mergeBase, unreadable), unreadable);
}
}
git.output("rev-parse", "--git-dir");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public static String noBase(String flag) {
+ ChangedLines.noBaseHint(flag);
}

/**
* Returns the text printed in place of a report where the build lies in no git work tree, as in
* a source release, so that no line can count as changed.
*/
public static String notInWorkTree(File directory) {
return "Diff coverage: skipped, since " + directory + " is not in a git work tree.";
}

/**
* Returns whether any of the changed files lies under one of the source roots, so that a caller
* with many modules prepares a report only for those the change reached.
Expand Down
Loading
Loading