From 24f58058b48853aafa4d4599bebfeefe463fa15b Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Fri, 14 Aug 2026 21:27:04 +0100 Subject: [PATCH] check directories populated when checking for empty projects --- .../pitest/maven/NonEmptyProjectCheck.java | 26 +++++++--- .../pitest/maven/NonEmptyProjectCheckIT.java | 50 +++++++++++++------ 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/pitest-maven/src/main/java/org/pitest/maven/NonEmptyProjectCheck.java b/pitest-maven/src/main/java/org/pitest/maven/NonEmptyProjectCheck.java index 9ea77001e..68982534c 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/NonEmptyProjectCheck.java +++ b/pitest-maven/src/main/java/org/pitest/maven/NonEmptyProjectCheck.java @@ -1,9 +1,10 @@ package org.pitest.maven; -import java.io.File; - import org.apache.maven.project.MavenProject; -import org.pitest.functional.FCollection; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.function.Predicate; public class NonEmptyProjectCheck implements Predicate { @@ -11,12 +12,23 @@ public class NonEmptyProjectCheck implements Predicate { @SuppressWarnings("unchecked") @Override public boolean test(MavenProject project) { - return FCollection.contains(project.getTestCompileSourceRoots(), exists()) - && FCollection.contains(project.getCompileSourceRoots(), exists()); + return project.getTestCompileSourceRoots().stream().anyMatch(this::exists) + && project.getCompileSourceRoots().stream().anyMatch(this::exists); } - private Predicate exists() { - return root -> new File(root).exists(); + private boolean exists(String root) { + var p = Path.of(root); + try { + return Files.isDirectory(p) && isPopulated(p); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static boolean isPopulated(Path p) throws IOException { + try (var list = Files.list(p)) { + return list.findAny().isPresent(); + } } } diff --git a/pitest-maven/src/test/java/org/pitest/maven/NonEmptyProjectCheckIT.java b/pitest-maven/src/test/java/org/pitest/maven/NonEmptyProjectCheckIT.java index 64d464a39..9f923b3e9 100644 --- a/pitest-maven/src/test/java/org/pitest/maven/NonEmptyProjectCheckIT.java +++ b/pitest-maven/src/test/java/org/pitest/maven/NonEmptyProjectCheckIT.java @@ -4,39 +4,47 @@ import static org.mockito.Mockito.when; import java.io.File; +import java.io.IOException; import java.util.Collections; import org.apache.maven.project.MavenProject; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.Mockito; + @Category(SystemTest.class) -@RunWith(MockitoJUnitRunner.class) public class NonEmptyProjectCheckIT { @Rule public TemporaryFolder realDir = new TemporaryFolder(); + + @Rule + public TemporaryFolder emptyDir = new TemporaryFolder(); NonEmptyProjectCheck testee = new NonEmptyProjectCheck(); - - @Mock - private MavenProject project; - + + private MavenProject project = Mockito.mock(MavenProject.class); + + @Before + public void populateDirectory() throws IOException { + realDir.newFile("temp"); + } + @Test public void shouldTreatProjectWithCodeAndTestsAsNonEmpty() { - when(project.getTestCompileSourceRoots()).thenReturn(Collections.singletonList(dirThatExists())); - when(project.getCompileSourceRoots()).thenReturn(Collections.singletonList(dirThatExists())); + when(project.getTestCompileSourceRoots()).thenReturn(Collections.singletonList(dirWithContents())); + when(project.getCompileSourceRoots()).thenReturn(Collections.singletonList(dirWithContents())); assertThat(testee.test(project)).isTrue(); } - + + @Test public void shouldTreatProjectWithNoTestsAsEmpty() { - when(project.getTestCompileSourceRoots()).thenReturn(Collections.singletonList(dirThatExists())); + when(project.getTestCompileSourceRoots()).thenReturn(Collections.singletonList(dirWithContents())); when(project.getCompileSourceRoots()).thenReturn(Collections.singletonList(nonExistentDir())); assertThat(testee.test(project)).isFalse(); } @@ -44,13 +52,25 @@ public void shouldTreatProjectWithNoTestsAsEmpty() { @Test public void shouldTreatProjectWithNoCodeAsEmpty() { when(project.getTestCompileSourceRoots()).thenReturn(Collections.singletonList(nonExistentDir())); - when(project.getCompileSourceRoots()).thenReturn(Collections.singletonList(dirThatExists())); + when(project.getCompileSourceRoots()).thenReturn(Collections.singletonList(dirWithContents())); assertThat(testee.test(project)).isFalse(); } - - private String dirThatExists() { + + + @Test + public void emptyDirectoriesAreTreatedAsEmpty() { + when(project.getTestCompileSourceRoots()).thenReturn(Collections.singletonList(emptyDir())); + when(project.getCompileSourceRoots()).thenReturn(Collections.singletonList(dirWithContents())); + assertThat(testee.test(project)).isFalse(); + } + + private String dirWithContents() { return realDir.getRoot().getAbsolutePath(); } + + private String emptyDir() { + return emptyDir.getRoot().getAbsolutePath(); + } private String nonExistentDir() { return new File("ifthisfileexistsbybizarrechancethetestwillfail").getAbsolutePath();