Skip to content

Commit

Permalink
Better error message for invalid scope values
Browse files Browse the repository at this point in the history
This fixes #57.
  • Loading branch information
ajoberstar committed Feb 27, 2018
1 parent 4b8e11b commit 8c28231
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
16 changes: 16 additions & 0 deletions reckon-core/src/main/java/org/ajoberstar/reckon/core/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,24 @@
*/
package org.ajoberstar.reckon.core;

import java.util.Arrays;
import java.util.stream.Collectors;

public enum Scope {
MAJOR,
MINOR,
PATCH;

public static Scope from(String value) {
try {
return Scope.valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
String scopes = Arrays.stream(Scope.values())
.map(Scope::name)
.map(String::toLowerCase)
.collect(Collectors.joining(", "));
String message = String.format("Scope \"%s\" is not one of: %s", value, scopes);
throw new IllegalArgumentException(message, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ public ScopeNormalStrategy(Function<VcsInventory, Optional<String>> scopeCalc) {

@Override
public Version reckonNormal(VcsInventory inventory) {
Optional<Scope> providedScope =
scopeCalc
.apply(inventory)
.filter(value -> !value.isEmpty())
.map(String::toUpperCase)
.map(Scope::valueOf);
Optional<Scope> providedScope = scopeCalc.apply(inventory)
.filter(value -> !value.isEmpty())
.map(Scope::from);

Scope scope;
if (providedScope.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ class ScopeNormalStrategyTest extends Specification {
thrown(NullPointerException)
}

def 'if scope supplier returns invalid scope, throw'() {
given:
def inventory = new VcsInventory(
'abcdef',
null,
Version.valueOf('1.2.3-milestone.1'),
Version.valueOf('1.2.2'),
1,
[] as Set,
[] as Set
)
when:
new ScopeNormalStrategy({ Optional.of("general") }).reckonNormal(inventory)
then:
def e = thrown(IllegalArgumentException)
e.getMessage() == 'Scope "general" is not one of: major, minor, patch'
}

def 'if supplier returns empty, scope defaults to minor if base version is base normal'() {
given:
def inventory = new VcsInventory(
Expand Down

0 comments on commit 8c28231

Please sign in to comment.