Skip to content

Commit

Permalink
Merge branch 'main' into windows
Browse files Browse the repository at this point in the history
  • Loading branch information
big-andy-coates authored Jan 31, 2024
2 parents c7304b0 + 7ce24e2 commit b707116
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ subprojects {
testImplementation("org.mockito:mockito-junit-jupiter:$mockitoVersion")
testImplementation("org.hamcrest:hamcrest-core:$hamcrestVersion")
testImplementation("com.google.guava:guava-testlib:$guavaVersion")
testImplementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
testRuntimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl:$log4jVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
}
Expand Down
20 changes: 20 additions & 0 deletions util/src/main/java/org/creekservice/api/test/util/TestPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
Expand Down Expand Up @@ -94,6 +95,25 @@ public static Stream<Path> listDirectory(final Path path) {
}
}

/**
* List directory content, recursive.
*
* <p>Strictly speaking, the returned stream should have {@link Stream#close()} called on it.
*
* @param path the directory to list
* @param options the walk options
* @return stream of entries in the directory
*/
@SuppressWarnings("resource")
public static Stream<Path> listDirectoryRecursive(
final Path path, final FileVisitOption... options) {
try {
return Files.walk(path, options).filter(p -> !path.equals(p));
} catch (final IOException e) {
throw new AssertionError("Failed to list directory: " + path, e);
}
}

/**
* Create a directory and all parent directories if they don't exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.creekservice.api.test.util.TestPaths.copy;
import static org.creekservice.api.test.util.TestPaths.ensureDirectories;
import static org.creekservice.api.test.util.TestPaths.listDirectory;
import static org.creekservice.api.test.util.TestPaths.listDirectoryRecursive;
import static org.creekservice.api.test.util.TestPaths.moduleRoot;
import static org.creekservice.api.test.util.TestPaths.projectRoot;
import static org.creekservice.api.test.util.TestPaths.readBytes;
Expand Down Expand Up @@ -121,6 +122,31 @@ void shouldThrowOnErrorListingDirectory() {
assertThrows(AssertionError.class, () -> listDirectory(tempDir.resolve("I do not exist")));
}

@Test
void shouldListDirectoryRecursively() {
// Given:
ensureDirectories(tempDir.resolve("child1/subChild1"));
write(tempDir.resolve("child2"), "some-content");
write(tempDir.resolve("child1/subChild2"), "some-content");

// When:
final Stream<Path> result = listDirectoryRecursive(tempDir);

// Then:
final List<String> children =
result.map(tempDir::relativize).map(Objects::toString).collect(Collectors.toList());
assertThat(
children,
containsInAnyOrder("child1", "child2", "child1/subChild1", "child1/subChild2"));
}

@Test
void shouldThrowOnErrorListingDirectoryRecursively() {
assertThrows(
AssertionError.class,
() -> listDirectoryRecursive(tempDir.resolve("I do not exist")));
}

@Test
void shouldEnsureDirectories() {
// Given:
Expand Down

0 comments on commit b707116

Please sign in to comment.