Skip to content

Commit

Permalink
Fix blank line at the end of file
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Aug 30, 2024
1 parent c35f9ec commit a99a891
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Fix blank line at the end of file [#299](https://github.com/nbbrd/heylogs/issues/299)

## [0.9.1] - 2024-08-28

### Fixed
Expand Down
21 changes: 21 additions & 0 deletions heylogs-api/src/main/java/internal/heylogs/FlexmarkIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package internal.heylogs;

import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.parser.Parser;

public final class FlexmarkIO {

private FlexmarkIO() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

public static Parser newParser() {
return Parser.builder().build();
}

public static Formatter newFormatter() {
Formatter.Builder result = Formatter.builder();
result.set(Formatter.MAX_TRAILING_BLANK_LINES, 0);
return result.build();
}
}
21 changes: 11 additions & 10 deletions heylogs-api/src/test/java/nbbrd/heylogs/HeylogsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.ast.Node;
import internal.heylogs.FlexmarkIO;
import internal.heylogs.StylishFormat;
import lombok.NonNull;
import nbbrd.design.MightBePromoted;
Expand All @@ -11,7 +12,6 @@
import nbbrd.heylogs.spi.RuleSeverity;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.jupiter.api.Test;
import tests.heylogs.api.Sample;

import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -65,7 +65,7 @@ public void testExtractVersions() {
Function<Filter, String> usingMain = extractor -> {
Document doc = using("/Main.md");
x.extractVersions(doc, extractor);
return Sample.FORMATTER.render(doc);
return FlexmarkIO.newFormatter().render(doc);
};

assertThat(builder().ref("1.1.0").build())
Expand All @@ -84,16 +84,14 @@ public void testExtractVersions() {
"- Fixed typos in Italian translation from [@lorenzo-arena](https://github.com/lorenzo-arena).\n" +
"- Fixed typos in Indonesian translation from [@ekojs](https://github.com/ekojs).\n" +
"\n" +
"[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n" +
"\n");
"[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n");

assertThat(builder().ref("1.1.0").ignoreContent(true).build())
.extracting(usingMain, STRING)
.isEqualTo(
"## [1.1.0] - 2019-02-15\n" +
"\n" +
"[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n" +
"\n");
"[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n");

assertThat(builder().ref("zzz").build())
.extracting(usingMain, STRING)
Expand Down Expand Up @@ -127,23 +125,26 @@ public void testReleaseChanges() {
"## [1.2.3] - 2010-01-01",
"[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.2.3...HEAD",
"[1.2.3]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.1.0...v1.2.3")
.doesNotContain("[unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.1.0...HEAD");
.doesNotContain("[unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.1.0...HEAD")
.endsWith("[0.0.1]: https://github.com/olivierlacan/keep-a-changelog/releases/tag/v0.0.1\n");

assertThat(releaseChangesToString(x, using("/UnreleasedChanges.md"), v123))
.contains(
"## [Unreleased]",
"## [1.2.3] - 2010-01-01",
"[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.2.3...HEAD",
"[1.2.3]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.1.0...v1.2.3")
.doesNotContain("[unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.1.0...HEAD");
.doesNotContain("[unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.1.0...HEAD")
.endsWith("[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n");

assertThat(releaseChangesToString(x, using("/FirstRelease.md"), v123))
.contains(
"## [Unreleased]",
"## [1.2.3] - 2010-01-01",
"[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.2.3...HEAD",
"[1.2.3]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.2.3...v1.2.3")
.doesNotContain("[unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/HEAD...HEAD");
.doesNotContain("[unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/HEAD...HEAD")
.endsWith("[1.2.3]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.2.3...v1.2.3\n");
}

@Test
Expand Down Expand Up @@ -261,7 +262,7 @@ public void testFormatStatus() throws IOException {

private static String releaseChangesToString(Heylogs heylogs, Document doc, Version version) {
heylogs.releaseChanges(doc, version, "v");
return Sample.FORMATTER.render(doc);
return FlexmarkIO.newFormatter().render(doc);
}

private static final class MockedRule implements Rule {
Expand Down
12 changes: 4 additions & 8 deletions heylogs-api/src/test/java/tests/heylogs/api/Sample.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package tests.heylogs.api;

import com.vladsch.flexmark.ast.Heading;
import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import internal.heylogs.FlexmarkIO;
import nbbrd.heylogs.*;
import nbbrd.heylogs.spi.RuleIssue;
import nbbrd.io.function.IOConsumer;
Expand All @@ -19,30 +18,27 @@

public class Sample {

public static final Parser PARSER = Parser.builder().build();
public static final Formatter FORMATTER = Formatter.builder().build();

public static Document using(String name) {
try (InputStream stream = Sample.class.getResourceAsStream(name)) {
if (stream == null) {
throw new IllegalArgumentException("Missing resource '" + name + "'");
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
return PARSER.parseReader(reader);
return FlexmarkIO.newParser().parseReader(reader);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}

public static Heading asHeading(String text) {
return (Heading) PARSER.parse(text).getChildOfType(Heading.class);
return (Heading) FlexmarkIO.newParser().parse(text).getChildOfType(Heading.class);
}

public static String asText(Heading heading) {
Document doc = new Document(null, BasedSequence.NULL);
doc.appendChild(heading);
return FORMATTER.render(doc).trim();
return FlexmarkIO.newFormatter().render(doc).trim();
}

public static final Problem PROBLEM1 = Problem.builder().id("rule1").severity(ERROR).issue(RuleIssue.builder().message("boom").line(5).column(18).build()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import internal.heylogs.FlexmarkIO;
import lombok.AccessLevel;
import lombok.NonNull;
import nbbrd.console.picocli.CommandSupporter;
Expand All @@ -25,7 +26,7 @@ public class MarkdownInputSupport extends TextInputSupport implements DirectoryS
return CommandSupporter.create(MarkdownInputSupport::new, supporters);
}

private @NonNull Parser parser = Parser.builder().build();
private @NonNull Parser parser = FlexmarkIO.newParser();

public Document readDocument(Path file) throws IOException {
try (Reader reader = newBufferedReader(file)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.util.ast.Document;
import internal.heylogs.FlexmarkIO;
import lombok.AccessLevel;
import lombok.NonNull;
import nbbrd.console.picocli.CommandSupporter;
Expand All @@ -23,7 +24,7 @@ public class MarkdownOutputSupport extends TextOutputSupport {
return CommandSupporter.create(MarkdownOutputSupport::new, supporters);
}

private @NonNull Formatter formatter = Formatter.builder().build();
private @NonNull Formatter formatter = FlexmarkIO.newFormatter();

public void writeDocument(Path file, Document document) throws IOException {
try (Writer writer = newBufferedWriter(file)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import internal.heylogs.FlexmarkIO;
import nbbrd.design.MightBePromoted;
import nbbrd.heylogs.Heylogs;
import nbbrd.heylogs.ext.semver.SemVerRule;
Expand Down Expand Up @@ -43,7 +44,7 @@ protected void notifyMissingChangelog() {

protected Document readChangelog(File inputFile) throws MojoExecutionException {
getLog().info("Reading changelog " + inputFile);
Parser parser = Parser.builder().build();
Parser parser = FlexmarkIO.newParser();
try (Reader reader = Files.newBufferedReader(inputFile.toPath())) {
return parser.parseReader(reader);
} catch (IOException ex) {
Expand All @@ -53,7 +54,7 @@ protected Document readChangelog(File inputFile) throws MojoExecutionException {

protected void writeChangelog(Document document, File outputFile) throws MojoExecutionException {
getLog().info("Writing changelog " + outputFile);
Formatter formatter = Formatter.builder().build();
Formatter formatter = FlexmarkIO.newFormatter();
try {
Files.createDirectories(outputFile.getParentFile().toPath());
try (Writer writer = Files.newBufferedWriter(outputFile.toPath())) {
Expand Down

0 comments on commit a99a891

Please sign in to comment.