From e84e75057f60ca00bdf8089d474227d337a34577 Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Tue, 1 Dec 2026 11:36:12 +0300 Subject: [PATCH] Mutate the classes of a project whose path holds a comma In a project whose directory path held a comma, pitestDiff and cribro:pitest-diff mutated nothing and reported no mutant: no changed line has bytecode PIT may mutate PIT splits --mutableCodePaths at commas, so .../a,b/build/classes/java/main reached it as two entries that match no classpath entry. A path with a { made PIT fail with a PatternSyntaxException, and one with a ^ or a | matched nothing, since PIT turns each entry into a regular expression without escaping them. PIT reads an entry that starts with ~ as a regular expression, so each path now goes to PIT as ~ followed by Pattern.quote of the path, with every comma written as \x2C and every plus as \x2B: PIT escapes a + even in that form. The entry matches the path and nothing else. Writing the comma as the glob ? was rejected: it also matches a sibling directory whose name has another character there. Leaving a source directory with a comma out of --sourceDirs was rejected too: with every directory left out the value is empty, which PIT reads as its working directory, and without the option PIT fails with a NullPointerException. User-visible: In a project whose directory path holds a comma, a brace, a caret, or a vertical bar, pitestDiff and cribro:pitest-diff mutate the changed lines. Not changed: PIT splits --sourceDirs at commas too and has no escape for them, so in a project whose path holds a comma, PIT's HTML report shows a mutated file without its source, or with the source of a file of the same name in another directory. Fixes #30 Co-Authored-By: Claude Opus 5.5 --- .../java/io/github/cribrodev/PitestRun.java | 27 +++++++- .../io/github/cribrodev/PitestRunTest.java | 63 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/cribro-core/src/main/java/io/github/cribrodev/PitestRun.java b/cribro-core/src/main/java/io/github/cribrodev/PitestRun.java index d112974..c0dfaf1 100644 --- a/cribro-core/src/main/java/io/github/cribrodev/PitestRun.java +++ b/cribro-core/src/main/java/io/github/cribrodev/PitestRun.java @@ -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; /** @@ -198,7 +199,11 @@ public List 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) @@ -247,6 +252,26 @@ public Map systemProperties() { return properties; } + /** + * Returns the {@code --mutableCodePaths} value that matches each of the files exactly. + * + *

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 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 files) { return files.stream().map(File::getAbsolutePath).collect(Collectors.joining(",")); } diff --git a/cribro-core/src/test/java/io/github/cribrodev/PitestRunTest.java b/cribro-core/src/test/java/io/github/cribrodev/PitestRunTest.java index 302b1e7..c7ce450 100644 --- a/cribro-core/src/test/java/io/github/cribrodev/PitestRunTest.java +++ b/cribro-core/src/test/java/io/github/cribrodev/PitestRunTest.java @@ -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; @@ -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 { @@ -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 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 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.}. */ + 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 {