Skip to content

Commit

Permalink
Improved calculation.
Browse files Browse the repository at this point in the history
 * integrated excludes list.
 * integrated unit test.
  • Loading branch information
khmarbaise committed Jul 28, 2023
1 parent b267210 commit 34ab5f5
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.3</version>
<version>5.10.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/soebes/module/calculator/FileSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ private FileSelector() {
// intentionally empty.
}

/**
* TODO: Need to find a good way to define the exclusions via parameter?
*/
static List<Path> selectAllFiles(Path start) throws IOException {
static List<Path> selectAllFiles(Path start, List<String> excludes) throws IOException {
try (Stream<Path> pathStream = Files.walk(start)) {
return pathStream
.filter(IS_VALID_FILE)
.filter(s -> !start.relativize(s).startsWith("target"))
.filter(s -> !start.relativize(s).startsWith(".git"))
.filter(s -> !start.relativize(s).startsWith(".github"))
.filter(s -> !start.relativize(s).startsWith(".idea"))
.filter(file -> excludes.stream().noneMatch(exclude -> start.relativize(file).startsWith(exclude)))
.collect(Collectors.toList());
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/soebes/module/calculator/ModuleCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -18,15 +19,15 @@ public ModuleCalculator() {
// intentionally empty.
}

public boolean hashChanged(Path path, Path hashFile) {
public boolean hashChanged(Path path, Path hashFile, List<String> excludes) {
LOGGER.debug("Starting: {} hashFile: {}", path, hashFile);
try {
ChecksumForFileResult hashFromFile = ChecksumForFileResult.NON_EXISTENT;
if (Files.exists(hashFile) && Files.isRegularFile(hashFile) && Files.isReadable(hashFile)) {
hashFromFile = new ChecksumForFileResult(Files.readAllBytes(hashFile));
}

ChecksumForFileResult calculatedHash = calculateHashForDirectoryTree(path);
ChecksumForFileResult calculatedHash = calculateHashForDirectoryTree(path,excludes);

//TODO: Create file only if not existing yet!
Files.createDirectories(hashFile.getParent());
Expand All @@ -41,13 +42,9 @@ public boolean hashChanged(Path path, Path hashFile) {
}
}

/**
* TODO: Find a way to handle those exclusions; also get things from .gitignore?
* var defaultExcludes = List.of("target", ".git", ".github", ".idea");
*/
public ChecksumForFileResult calculateHashForDirectoryTree(Path path) throws IOException {
public ChecksumForFileResult calculateHashForDirectoryTree(Path path, List<String> excludes) throws IOException {
LOGGER.debug("Starting: {}", path);
List<Path> paths = selectAllFiles(path);
List<Path> paths = selectAllFiles(path, excludes);
return paths
.parallelStream()
.peek(f -> LOGGER.debug("File:{}", f))
Expand Down
138 changes: 138 additions & 0 deletions src/test/java/com/soebes/module/calculator/FileSelectorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.soebes.module.calculator;

import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;


class FileSelectorTest {

@Test
void excludesNothing() throws IOException {
try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
Path root = fileSystem.getPath("/");
Files.createDirectories(root);
Files.createDirectories(root.resolve("/root-project/.git"));
Files.createDirectories(root.resolve("/root-project/.github"));
Files.createDirectories(root.resolve("/root-project/target"));
Files.createDirectories(root.resolve("/root-project/src/main/java/com/example"));
Files.createDirectories(root.resolve("/root-project/src/test/java/com/example"));
Files.createDirectories(root.resolve("/root-project/target/maven-status"));

Files.write(root.resolve("/root-project/.git/example-in-git"),"example in .git directory".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/.github/example-in-github"),"example in .github directory".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/target/first.class"),"Example in target".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/src/main/java/com/example/FirstJava.java"),"Das ist ein Test".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/pom.xml"), "pom of the project.".getBytes(StandardCharsets.UTF_8));
List<Path> result = FileSelector.selectAllFiles(root.resolve("/root-project"), Collections.emptyList());

List<Path> expectedResult = Arrays.asList(
root.resolve("/root-project/.git/example-in-git"),
root.resolve("/root-project/.github/example-in-github"),
root.resolve("/root-project/pom.xml"),
root.resolve("/root-project/target/first.class"),
root.resolve("/root-project/src/main/java/com/example/FirstJava.java")
);
assertThat(result).containsExactlyInAnyOrderElementsOf(expectedResult);
}
}

@Test
void excludesSingleTarget() throws IOException {
try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
Path root = fileSystem.getPath("/");
Files.createDirectories(root);
Files.createDirectories(root.resolve("/root-project/.git"));
Files.createDirectories(root.resolve("/root-project/.github"));
Files.createDirectories(root.resolve("/root-project/target"));
Files.createDirectories(root.resolve("/root-project/src/main/java/com/example"));
Files.createDirectories(root.resolve("/root-project/src/test/java/com/example"));
Files.createDirectories(root.resolve("/root-project/target/maven-status"));

Files.write(root.resolve("/root-project/.git/example-in-git"),"example in .git directory".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/.github/example-in-github"),"example in .github directory".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/target/first.class"),"Example in target".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/src/main/java/com/example/FirstJava.java"),"Das ist ein Test".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/pom.xml"), "pom of the project.".getBytes(StandardCharsets.UTF_8));

List<Path> result = FileSelector.selectAllFiles(root.resolve("/root-project"), Collections.singletonList("target"));

List<Path> expectedResult = Arrays.asList(
root.resolve("/root-project/.git/example-in-git"),
root.resolve("/root-project/.github/example-in-github"),
root.resolve("/root-project/pom.xml"),
root.resolve("/root-project/src/main/java/com/example/FirstJava.java")
);
assertThat(result).containsExactlyInAnyOrderElementsOf(expectedResult);
}
}

@Test
void excludesTargetAndGit() throws IOException {
try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
Path root = fileSystem.getPath("/");
Files.createDirectories(root);
Files.createDirectories(root.resolve("/root-project/.git"));
Files.createDirectories(root.resolve("/root-project/.github"));
Files.createDirectories(root.resolve("/root-project/target"));
Files.createDirectories(root.resolve("/root-project/src/main/java/com/example"));
Files.createDirectories(root.resolve("/root-project/src/test/java/com/example"));
Files.createDirectories(root.resolve("/root-project/target/maven-status"));

Files.write(root.resolve("/root-project/.git/example-in-git"),"example in .git directory".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/.github/example-in-github"),"example in .github directory".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/target/first.class"),"Example in target".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/src/main/java/com/example/FirstJava.java"),"Das ist ein Test".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/pom.xml"), "pom of the project.".getBytes(StandardCharsets.UTF_8));

List<Path> result = FileSelector.selectAllFiles(root.resolve("/root-project"), Arrays.asList("target", ".git"));

List<Path> expectedResult = Arrays.asList(
root.resolve("/root-project/.github/example-in-github"),
root.resolve("/root-project/pom.xml"),
root.resolve("/root-project/src/main/java/com/example/FirstJava.java")
);
assertThat(result).containsExactlyInAnyOrderElementsOf(expectedResult);
}
}

@Test
void excludesTargetGitGitHub() throws IOException {
try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
Path root = fileSystem.getPath("/");
Files.createDirectories(root);
Files.createDirectories(root.resolve("/root-project/.git"));
Files.createDirectories(root.resolve("/root-project/.github"));
Files.createDirectories(root.resolve("/root-project/target"));
Files.createDirectories(root.resolve("/root-project/src/main/java/com/example"));
Files.createDirectories(root.resolve("/root-project/src/test/java/com/example"));
Files.createDirectories(root.resolve("/root-project/target/maven-status"));

Files.write(root.resolve("/root-project/.git/example-in-git"),"example in .git directory".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/.github/example-in-github"),"example in .github directory".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/target/first.class"),"Example in target".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/src/main/java/com/example/FirstJava.java"),"Das ist ein Test".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/pom.xml"), "pom of the project.".getBytes(StandardCharsets.UTF_8));

List<Path> result = FileSelector.selectAllFiles(root.resolve("/root-project"), Arrays.asList("target", ".git", ".github"));

List<Path> expectedResult = Arrays.asList(
root.resolve("/root-project/pom.xml"),
root.resolve("/root-project/src/main/java/com/example/FirstJava.java")
);
assertThat(result).containsExactlyInAnyOrderElementsOf(expectedResult);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class ModuleCalculatorTest {

private static final List<String> DEFAULT_EXCLUDES = Arrays.asList("target", ".git", ".github", ".idea");

private static byte[] convert() {
int[] given = new int[]{
0x6e, 0x55, 0x00, 0xcf, 0x09, 0x33, 0x8d,
Expand Down Expand Up @@ -48,7 +52,7 @@ void name() throws IOException {
Files.write(root.resolve("/root-project/pom.xml"), "pom of the project.".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/target/maven-status/module.hash"), "hashfile".getBytes(StandardCharsets.UTF_8)); // Will be ignored!
ModuleCalculator moduleCalculator = new ModuleCalculator();
ChecksumForFileResult x = moduleCalculator.calculateHashForDirectoryTree(root.resolve("/root-project"));
ChecksumForFileResult x = moduleCalculator.calculateHashForDirectoryTree(root.resolve("/root-project"), DEFAULT_EXCLUDES);

assertThat(x).isEqualTo(DEFAULT);
}
Expand All @@ -67,7 +71,7 @@ void hasChangedShouldResultInFalse() throws IOException {
Files.write(root.resolve("/root-project/pom.xml"), "pom of the project.".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("/root-project/target/maven-status/module.hash"), convert());
ModuleCalculator moduleCalculator = new ModuleCalculator();
boolean hashChanged = moduleCalculator.hashChanged(root.resolve("/root-project"), root.resolve("/root-project/target/maven-status/module.hash"));
boolean hashChanged = moduleCalculator.hashChanged(root.resolve("/root-project"), root.resolve("/root-project/target/maven-status/module.hash"), DEFAULT_EXCLUDES);

assertThat(hashChanged).isFalse();
}
Expand All @@ -78,7 +82,7 @@ void hasChanged() throws IOException {
ModuleCalculator moduleCalculator = new ModuleCalculator();
Path hashFile = Paths.get("target", "hasChanged", "src", "test", "resources", "hash.file");
Files.deleteIfExists(hashFile);
boolean x = moduleCalculator.hashChanged(Paths.get("src", "test", "resources"), hashFile);
boolean x = moduleCalculator.hashChanged(Paths.get("src", "test", "resources"), hashFile, DEFAULT_EXCLUDES);
assertThat(x).isTrue();
}

Expand All @@ -88,14 +92,14 @@ void hasXXXhasChanged() throws IOException {
ModuleCalculator moduleCalculator = new ModuleCalculator();
Path hashFile = Paths.get("target", "hasXXXHasChanged", "src", "test", "resources", "hash.file");
Files.deleteIfExists(hashFile);
boolean x = moduleCalculator.hashChanged(Paths.get("src", "test", "resources"), hashFile);
boolean x = moduleCalculator.hashChanged(Paths.get("src", "test", "resources"), hashFile, DEFAULT_EXCLUDES);
assertThat(x).isTrue();
}

@Test
void calculateHashForDirectoryTree() throws IOException {
ModuleCalculator moduleCalculator = new ModuleCalculator();
ChecksumForFileResult x = moduleCalculator.calculateHashForDirectoryTree(Paths.get("."));
ChecksumForFileResult x = moduleCalculator.calculateHashForDirectoryTree(Paths.get("."), DEFAULT_EXCLUDES);

assertThat(x.getDigest().byteArray()).hasSize(32);
}
Expand Down

0 comments on commit 34ab5f5

Please sign in to comment.