Skip to content

Commit

Permalink
Add tests for scope/stage calculators
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoberstar committed Feb 12, 2022
1 parent 8b3138c commit ec7e26b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public Builder git(Repository repo) {
*/
public Builder git(Repository repo, VersionTagParser tagParser) {
if (repo == null) {
this.inventorySupplier = () -> new VcsInventory(null, false, null, null, null, 0, Collections.emptySet(), Collections.emptySet(), Collections.emptyList());
this.inventorySupplier = () -> VcsInventory.empty(false);
} else {
var realParser = Optional.ofNullable(tagParser).orElse(VersionTagParser.getDefault());
this.inventorySupplier = new GitInventorySupplier(repo, realParser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,8 @@ public int hashCode() {
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}

public static VcsInventory empty(boolean clean) {
return new VcsInventory(null, clean, null, null, null, 0, Collections.emptySet(), Collections.emptySet(), Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -174,7 +170,7 @@ public void noCommitsEmpty() {
});

var emptySupplier = new GitInventorySupplier(emptyGrgit.getRepository().getJgit().getRepository(), VersionTagParser.getDefault());
assertEquals(new VcsInventory(null, true, null, null, null, 0, null, null, null), emptySupplier.getInventory());
assertEquals(VcsInventory.empty(true), emptySupplier.getInventory());
}

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.ajoberstar.reckon.core;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.ajoberstar.reckon.core;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.*;

public class ScopeCalculatorTest {
@Test
@DisplayName("ofUserString filters out empty scope strings")
public void ofUserStringFiltersEmptyStrings() {
var calc = ScopeCalculator.ofUserString(i -> Optional.of(""));
assertEquals(Optional.empty(), calc.calculate(VcsInventory.empty(false)));
}

@Test
@DisplayName("ofUserString handles mixed case")
public void ofUserStringMixedCase() {
var calc = ScopeCalculator.ofUserString(i -> Optional.of("MiNor"));
assertEquals(Optional.of(Scope.MINOR), calc.calculate(VcsInventory.empty(false)));
}

@Test
@DisplayName("ofCommitMessage behaves as expected")
public void ofCommitMessageNoMatch() {
var pattern = Pattern.compile("^(major|minor|patch): .+");
var calc = ScopeCalculator.ofCommitMessage(msg -> {
var matcher = pattern.matcher(msg);
if (matcher.find()) {
return Optional.of(Scope.from(matcher.group(1)));
} else {
return Optional.empty();
}
});
assertEquals(Optional.empty(), calc.calculate(VcsInventory.empty(false)), "Should not find a scope in an empty inventory");

var inventoryNoMatches = getInventoryWithMessages("some message", "other message\n\nminor: something", "major");
assertEquals(Optional.empty(), calc.calculate(inventoryNoMatches), "Should not find a scope when no messages match");

var inventoryOneMatch = getInventoryWithMessages("some message", "patch: some fix");
assertEquals(Optional.of(Scope.PATCH), calc.calculate(inventoryOneMatch), "Should find the one matching scope");

var inventoryMultiMatch = getInventoryWithMessages("some message", "patch: some fix", "major: breaking change");
assertEquals(Optional.of(Scope.MAJOR), calc.calculate(inventoryMultiMatch), "Should find the more significant matching scope");
}

private VcsInventory getInventoryWithMessages(String... messages) {
return new VcsInventory(
null,
false,
null,
null,
null,
0,
null,
null,
Arrays.asList(messages)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.ajoberstar.reckon.core;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

public class StageCalculatorTest {
@Test
@DisplayName("ofUserString filters out empty scope strings")
public void ofUserStringFiltersEmptyStrings() {
var calc = StageCalculator.ofUserString((i, v) -> Optional.of(""));
assertEquals(Optional.empty(), calc.calculate(VcsInventory.empty(false), Version.IDENTITY));
}

@Test
@DisplayName("ofUserString handles mixed case")
public void ofUserStringMixedCase() {
var calc = StageCalculator.ofUserString((i, v) -> Optional.of("BeTa"));
assertEquals(Optional.of("beta"), calc.calculate(VcsInventory.empty(false), Version.IDENTITY));
}
}

0 comments on commit ec7e26b

Please sign in to comment.