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
27 changes: 26 additions & 1 deletion cribro-core/src/main/java/io/github/cribrodev/PitestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -198,7 +199,11 @@ public List<String> arguments() throws IOException {
options.put("targetClasses", String.join(",", targetClasses));
options.put("targetTests", targetTests == null ? ALL_TESTS : targetTests);
// Only these entries are mutated, so a glob cannot reach a test class of the same name.
options.put("mutableCodePaths", paths(mutableCodePaths));
options.put("mutableCodePaths", codePathPatterns(mutableCodePaths));
// PIT splits --sourceDirs at commas, as it does --mutableCodePaths, and has no escape for them.
// It reads the source directories only for its HTML report and searches each fragment of a path
// that holds a comma as a directory of its own. That report then shows a mutated file without
// its source, or with the source of a file of the same name in another directory.
options.put("sourceDirs", paths(sourceDirs));
options.put("classPathFile", classPathFile.getAbsolutePath());
// PIT otherwise puts its own launch classpath (commons-lang3, commons-text, ASM, jopt-simple)
Expand Down Expand Up @@ -247,6 +252,26 @@ public Map<String, String> systemProperties() {
return properties;
}

/**
* Returns the {@code --mutableCodePaths} value that matches each of the files exactly.
*
* <p>PIT splits the value at commas and reads each entry as a glob, or as a regular expression
* when it starts with {@code ~}. In either form it writes every {@code +} as {@code \+}, which
* inside a {@link Pattern#quote} literal matches a backslash and then a plus. Each entry is the
* path as such a literal with its commas and pluses written as {@code \x2C} and {@code \x2B}, so
* a path matches itself and nothing else, whatever punctuation it holds.
*/
static String codePathPatterns(List<File> files) {
return files.stream()
.map(
file ->
"~"
+ Pattern.quote(file.getAbsolutePath())
.replace(",", "\\E\\x2C\\Q")
.replace("+", "\\E\\x2B\\Q"))
.collect(Collectors.joining(","));
}

private static String paths(List<File> files) {
return files.stream().map(File::getAbsolutePath).collect(Collectors.joining(","));
}
Expand Down
63 changes: 63 additions & 0 deletions cribro-core/src/test/java/io/github/cribrodev/PitestRunTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeFalse;

import java.io.File;
import java.io.IOException;
Expand All @@ -29,11 +30,17 @@
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.pitest.classinfo.ClassName;
import org.pitest.mutationtest.commandline.OptionsParser;
import org.pitest.mutationtest.config.ReportOptions;

/** The PIT command line both plugins start PIT with. */
class PitestRunTest {
Expand Down Expand Up @@ -194,6 +201,62 @@ void theTestsClasspathReachesPitThroughAFileInItsOrder() throws IOException {
Files.readAllLines(Path.of(option(arguments, "classPathFile"))));
}

/**
* The sibling's name differs from the directory's only in its punctuation, so a pattern that let a
* wildcard stand for a punctuation character would mutate the sibling's class too. On Windows a
* backslash in the name nests a directory, and the path still holds {@code \E}.
*/
@ParameterizedTest
@CsvSource(
delimiter = '=',
textBlock =
"""
a,b = a;b
a+b = a-b
a{b = a(b
a^b = a_b
a|b = a_b
a[1] = a_1_
x\\Ey,z+w = x_Ey;z-w
a\\E,b = a_E;b
""")
void aMutableClassDirectoryIsMatchedByItsExactPath(String name, String sibling)
throws IOException {
assumeFalse(
OS.WINDOWS.isCurrentOs() && name.contains("|"), "Windows allows no | in a file name");
File punctuated = classDirectory(name, "Calc");
File plain = classDirectory("plain", "Util");
File other = classDirectory(sibling, "Other");

List<String> arguments =
pit(List.of(punctuated, plain, other))
.targetClasses(List.of("demo.*"))
.mutableCodePaths(List.of(punctuated, plain))
.arguments();

ReportOptions options = parsed(arguments);
assertEquals(
List.of("demo.Calc", "demo.Util"),
options.getMutationClassPaths().code().stream()
.map(ClassName::asJavaName)
.sorted()
.collect(Collectors.toList()),
"classes PIT may mutate, given --mutableCodePaths " + option(arguments, "mutableCodePaths"));
}

/** Returns the options PIT's own command-line parser reads from {@code arguments}. */
private static ReportOptions parsed(List<String> arguments) {
return new OptionsParser(name -> true).parse(arguments.toArray(String[]::new)).getOptions();
}

/** Returns a class directory under {@code name} that holds the class {@code demo.<simpleName>}. */
private File classDirectory(String name, String simpleName) throws IOException {
Path classes = directory.resolve(name).resolve("classes");
Files.createDirectories(classes.resolve("demo"));
Files.write(classes.resolve("demo").resolve(simpleName + ".class"), new byte[0]);
return classes.toFile();
}

/** A path outside ASCII has to reach PIT as written, whatever the platform's charset is. */
@Test
void pitStartsInTheCharsetItsClasspathFileIsWrittenIn() throws IOException {
Expand Down
Loading