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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.jspecify.annotations.NullMarked;
Expand Down Expand Up @@ -67,18 +68,29 @@ public static Path findTestSourcePath(Method testMethod) {
.replace("-classes", "");
String packagePath = declaringClass.getPackageName().replace(".", "/");
String pathRegex =
"(?!build|target).*%s.*/%s/%s\\.(java|kt|groovy|scala)$"
"(?!(?:build|target|bin|out)/).*%s.*/%s/%s\\.(java|kt|groovy|scala)$"
.formatted(sourceSetName, packagePath, declaringClass.getSimpleName());
try (Stream<Path> pathStream =
Files.find(
Path.of(""),
packageDepth + 10,
(path, attributes) ->
attributes.isRegularFile() && path.normalize().toString().matches(pathRegex))) {
return pathStream
.findFirst()
.map(Path::normalize)
.orElseThrow(() -> new FileApproverError("Could not locate test source file"));
List<Path> matches = pathStream.map(Path::normalize).toList();
return switch (matches.size()) {
case 0 -> throw new FileApproverError("Could not locate test source file");
case 1 -> matches.getFirst();
default -> {
List<Path> srcMatches =
matches.stream().filter(path -> path.toString().contains("src")).toList();
if (srcMatches.size() == 1) {
yield srcMatches.getFirst();
}
throw new FileApproverError(
"Found multiple test source files (%d contain 'src'): %s"
.formatted(srcMatches.size(), matches));
Comment on lines +90 to +91
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message indicates how many paths contain 'src' but then shows all matches. This could be confusing if srcMatches.size() differs from matches.size(). Consider either showing only srcMatches in the error message, or clarifying that all matches are being shown. For example: "Found multiple test source files (%d total, %d contain 'src'): %s".formatted(matches.size(), srcMatches.size(), matches)

Suggested change
"Found multiple test source files (%d contain 'src'): %s"
.formatted(srcMatches.size(), matches));
"Found multiple test source files (%d total, %d contain 'src'): %s"
.formatted(matches.size(), srcMatches.size(), matches));

Copilot uses AI. Check for mistakes.
}
};
} catch (IOException e) {
throw new FileApproverError("Could not traverse code directory", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.nio.file.Files.copy;
import static java.nio.file.Files.createDirectories;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -61,11 +62,17 @@ void findTestSourcePath() {
assertThat(testSourcePath).isEqualTo(thisTestSourcePath.normalize());
}

@Test
void findTestSourcePath_file_in_build() throws IOException {
Path wrongTestSourcePath =
Path.of(
"build/spotless-clean/spotlessJava/java/test/org/approvej/approve/StackTraceTestFinderUtilTest.java");
@ParameterizedTest
@ValueSource(
strings = {
"build/spotless-clean/spotlessJava/java/test/org/approvej/approve/StackTraceTestFinderUtilTest.java",
"target/spotless-clean/spotlessJava/java/test/org/approvej/approve/StackTraceTestFinderUtilTest.java",
"bin/test/org/approvej/approve/StackTraceTestFinderUtilTest.java",
"out/test/org/approvej/approve/StackTraceTestFinderUtilTest.java",
"other/test/java/org/approvej/approve/StackTraceTestFinderUtilTest.java",
})
void findTestSourcePath_duplicate_file(String wrongPath) throws IOException {
Path wrongTestSourcePath = Path.of(wrongPath);
wrongTestSourcePathsToCleanup.add(wrongTestSourcePath);
createDirectories(wrongTestSourcePath.getParent());
copy(thisTestSourcePath, wrongTestSourcePath);
Expand All @@ -78,18 +85,27 @@ void findTestSourcePath_file_in_build() throws IOException {
}

@Test
void findTestSourcePath_file_in_target() throws IOException {
void findTestSourcePath_no_source_file() throws NoSuchMethodException {
Method method =
org.assertj.core.api.Assertions.class.getDeclaredMethod("assertThat", boolean.class);

assertThatThrownBy(() -> StackTraceTestFinderUtil.findTestSourcePath(method))
.isInstanceOf(FileApproverError.class)
.hasMessage("Could not locate test source file");
}

@Test
void findTestSourcePath_ambiguous() throws IOException {
Path wrongTestSourcePath =
Path.of(
"target/spotless-clean/spotlessJava/java/test/org/approvej/approve/StackTraceTestFinderUtilTest.java");
Path.of("other/src/test/java/org/approvej/approve/StackTraceTestFinderUtilTest.java");
wrongTestSourcePathsToCleanup.add(wrongTestSourcePath);
createDirectories(wrongTestSourcePath.getParent());
copy(thisTestSourcePath, wrongTestSourcePath);

Path testSourcePath =
StackTraceTestFinderUtil.findTestSourcePath(
StackTraceTestFinderUtil.currentTestMethod().method());
Method method = StackTraceTestFinderUtil.currentTestMethod().method();

assertThat(testSourcePath).isEqualTo(thisTestSourcePath.normalize());
assertThatThrownBy(() -> StackTraceTestFinderUtil.findTestSourcePath(method))
.isInstanceOf(FileApproverError.class)
.hasMessageStartingWith("Found multiple test source files (");
}
}