Skip to content

Commit

Permalink
Fix consistent-separator rule when unreleased keyword is present
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Nov 9, 2023
1 parent 28647d4 commit 2e8f568
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Output all separators in error message by [@koppor](https://github.com/koppor) [#164](https://github.com/nbbrd/heylogs/pull/164)

### Fixed

- Fix consistent-separator rule when unreleased keyword is present [#163](https://github.com/nbbrd/heylogs/issues/163)

## [0.7.1] - 2023-10-19

### Fixed
Expand Down
10 changes: 3 additions & 7 deletions heylogs-api/src/main/java/internal/heylogs/ExtendedRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import nbbrd.design.VisibleForTesting;
import nbbrd.heylogs.Failure;
import nbbrd.heylogs.Nodes;
import nbbrd.heylogs.Util;
import nbbrd.heylogs.Version;
import nbbrd.heylogs.spi.Rule;
import nbbrd.heylogs.spi.RuleBatch;
Expand All @@ -20,7 +21,6 @@
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -120,7 +120,7 @@ static Failure validateConsistentSeparator(Document doc) {
.descendants(doc)
.filter(Version::isVersionLevel)
.map(illegalArgumentToNull(Version::parse))
.filter(Objects::nonNull)
.filter(version -> version != null && !version.isUnreleased())
.map(Version::getSeparator)
.distinct()
.collect(toList());
Expand All @@ -129,16 +129,12 @@ static Failure validateConsistentSeparator(Document doc) {
? Failure
.builder()
.rule(CONSISTENT_SEPARATOR)
.message("Expecting consistent version-date separator " + toUnicode(separators.get(0)) + ", found " + separators.stream().map(ExtendedRules::toUnicode).collect(joining(", ", "[", "]")))
.message("Expecting consistent version-date separator " + Util.toUnicode(separators.get(0)) + ", found " + separators.stream().map(Util::toUnicode).collect(joining(", ", "[", "]")))
.location(doc)
.build()
: NO_PROBLEM;
}

private static String toUnicode(Character c) {
return String.format(Locale.ROOT, "\\u%04x", (int) c);
}

@MightBeGenerated
@ServiceProvider
public static final class Batch implements RuleBatch {
Expand Down
5 changes: 5 additions & 0 deletions heylogs-api/src/main/java/nbbrd/heylogs/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.NonNull;

import java.util.Locale;
import java.util.function.Function;

public final class Util {
Expand All @@ -19,4 +20,8 @@ private Util() {
}
};
}

public static @NonNull String toUnicode(@NonNull Character c) {
return String.format(Locale.ROOT, "\\u%04x", (int) c);
}
}
18 changes: 14 additions & 4 deletions heylogs-api/src/main/java/nbbrd/heylogs/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
@RepresentableAs(Heading.class)
public class Version implements BaseSection {

private static final String UNRELEASED_KEYWORD = "unreleased";
private static final int HEADING_LEVEL = 2;

@VisibleForTesting
static final char HYPHEN = '-';

Expand All @@ -31,6 +28,14 @@ public class Version implements BaseSection {
@VisibleForTesting
static final char EM_DASH = '—';

private static final int HEADING_LEVEL = 2;

private static final String UNRELEASED_KEYWORD = "unreleased";

private static final char UNRELEASED_SEPARATOR = HYPHEN;

private static final LocalDate UNRELEASED_DATE = LocalDate.MAX;

// The unicode en dash ("–") and em dash ("—") are also accepted as separators
private static final CharPredicate VALID_SEPARATOR = CharPredicate.anyOf(HYPHEN, EN_DASH, EM_DASH);

Expand All @@ -46,6 +51,11 @@ public boolean isUnreleased() {
return UNRELEASED_KEYWORD.equalsIgnoreCase(ref);
}

@Override
public String toString() {
return "Version(ref=" + ref + ", separator=" + Util.toUnicode(separator) + ", date=" + date + ")";
}

@Override
public @NonNull Heading toHeading() {
Heading result = new Heading();
Expand Down Expand Up @@ -87,7 +97,7 @@ public boolean isUnreleased() {
throw new IllegalArgumentException("Unexpected additional part: '" + parts.next().getChars() + "'");
}

return new Version(ref, HYPHEN, LocalDate.MAX);
return new Version(ref, UNRELEASED_SEPARATOR, UNRELEASED_DATE);
}

if (!parts.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ public void testValidateGitHubIssueRef() {
public void testValidateConsistentSeparator() {
assertThat(validateConsistentSeparator(using("/ErraticSeparator.md")))
.isEqualTo(Failure.builder().rule(CONSISTENT_SEPARATOR).message("Expecting consistent version-date separator \\u002d, found [\\u002d, \\u2013, \\u2014]").line(1).column(1).build());

assertThat(validateConsistentSeparator(using("/NonDefaultSeparator.md")))
.isEqualTo(NO_PROBLEM);
}
}
9 changes: 9 additions & 0 deletions heylogs-api/src/test/java/nbbrd/heylogs/VersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,14 @@ public void testToHeading() {
.isEqualTo("## [1.1.0] - 2019-02-15");
}

@Test
public void testToString() {
assertThat(Version.of("Unreleased", HYPHEN, LocalDate.MAX))
.hasToString("Version(ref=Unreleased, separator=\\u002d, date=+999999999-12-31)");

assertThat(Version.of("1.1.0", HYPHEN, d20190215))
.hasToString("Version(ref=1.1.0, separator=\\u002d, date=2019-02-15)");
}

private final LocalDate d20190215 = LocalDate.parse("2019-02-15");
}
17 changes: 17 additions & 0 deletions heylogs-api/src/test/resources/NonDefaultSeparator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

## [Unreleased]

## [1.1.0] — 2019-02-15

## [1.0.0] — 2017-06-20

## [0.3.0] — 2015-12-03

## [0.2.0] — 2015-10-06

[unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.1.0...HEAD
[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.3.0...v1.0.0
[0.3.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.1.0...v0.2.0

0 comments on commit 2e8f568

Please sign in to comment.