Skip to content

Commit

Permalink
fix: making semantic version match not greedy (refs #142)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Dec 12, 2022
1 parent a22a611 commit 14d0f08
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static SemanticVersion getHighestVersion(final List<String> tags) {

public static Optional<SemanticVersion> findSemanticVersion(final String tag) {
final Matcher semanticVersionMatcher =
Pattern.compile("[0-9]+\\.[0-9]+\\.?[0-9]+?").matcher(tag);
Pattern.compile("[0-9]+\\.[0-9]+\\.?[0-9]*").matcher(tag);
if (!semanticVersionMatcher.find()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import se.bjurr.gitchangelog.api.GitChangelogApi;
import se.bjurr.gitchangelog.internal.semantic.SemanticVersion;
import se.bjurr.gitchangelog.internal.semantic.SemanticVersioning;
Expand Down Expand Up @@ -179,6 +181,48 @@ public void testPatchStepWithPatternMatching() throws Throwable {
assertThat(this.sut.getNextVersion(highestVersion).getVersionStep()).isEqualTo(PATCH);
}

@Test
public void testLongPatchMatches() throws Throwable {
this.tags.add("1.2.33333");
this.tags.add("1.2.3");

final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags);
assertThat(highestVersion.toString()) //
.isEqualTo("1.2.33333");
}

@Test
public void testLongPatchMatches_reverse_order() throws Throwable {
this.tags.add("1.2.3");
this.tags.add("1.2.33333");

final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags);
assertThat(highestVersion.toString()) //
.isEqualTo("1.2.33333");
}

@Test
public void testLongMinorMatches() throws Throwable {
this.tags.add("1.22222");
this.tags.add("1.2");

final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags);
assertThat(highestVersion.toString()) //
.isEqualTo("1.22222.0");
}

@Test
public void testLongMinorMatches_reverse_order() throws Throwable {
this.tags.add("1.2");
this.tags.add("1.22222");

final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags);
assertThat(highestVersion.toString()) //
.isEqualTo("1.22222.0");
assertThat(highestVersion.findTag().orElse("")) //
.isEqualTo("1.22222");
}

@Test
public void testTagCleanup() throws Throwable {
assertThat(this.getTagNameFrom("whatever1.2.3.4")).isEqualTo("1.2.3");
Expand Down

0 comments on commit 14d0f08

Please sign in to comment.