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
@@ -1,22 +1,34 @@
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<MavenProject> {

@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<String> 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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,73 @@
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();
}

@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();
Expand Down
Loading