From a4b5867e2b2b8478b2ebb1bb21311df5988da43a Mon Sep 17 00:00:00 2001 From: Alexander Goussas Date: Fri, 24 Oct 2025 08:50:58 -0500 Subject: [PATCH] tests: add more tests for markdown changelog parser --- .../aloussase/changelog/ChangelogEntry.kt | 2 +- .../changelog/formatter/MarkdownFormatter.kt | 2 +- .../github/aloussase/changelog/git/Commit.kt | 2 + .../parser/MarkdownChangelogParser.kt | 27 +++++++---- .../parser/MarkdownChangelogParserTests.kt | 48 ++++++++++++++++--- 5 files changed, 63 insertions(+), 18 deletions(-) diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogEntry.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogEntry.kt index 4733a14..9d509f1 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogEntry.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogEntry.kt @@ -3,6 +3,6 @@ package io.github.aloussase.changelog import io.github.aloussase.changelog.git.Commit data class ChangelogEntry( - val branchName: String, + val release: String, val commits: List = emptyList(), ) diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatter.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatter.kt index da6cbb7..5f67ce6 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatter.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatter.kt @@ -4,6 +4,6 @@ import io.github.aloussase.changelog.Changelog class MarkdownFormatter : ChangelogFormatter { override fun format(changelog: Changelog): String { - return changelog.entries.first().branchName + return changelog.entries.first().release } } diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/Commit.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/Commit.kt index 817c74f..40c9fb2 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/Commit.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/Commit.kt @@ -1,5 +1,7 @@ package io.github.aloussase.changelog.git data class Commit( + val author: String, + val branchName: String, val message: String ) diff --git a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParser.kt b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParser.kt index 824dd04..b3e2a1b 100644 --- a/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParser.kt +++ b/changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParser.kt @@ -11,8 +11,8 @@ import org.gradle.api.GradleException class MarkdownChangelogParser : ChangelogParser { companion object { - private val BRANCH_NAME_REGEX = Regex("## [\\w-]+") - private val COMMIT_REGRX = Regex("- [\\w ]+") + private val RELEASE_VERSION_REGEX = Regex("## \\[(\\d+\\.\\d+\\.\\d+)]") + private val COMMIT_REGEX = Regex("- ([\\w-]+): ([\\w ]+) \\(([\\w.]+@[\\w.]+)\\)") private val DOC_TITLE_REGEX = Regex("# (Changelog|CHANGELOG)") } @@ -29,28 +29,37 @@ class MarkdownChangelogParser : ChangelogParser { val lines = blk.split("\n") if (lines.isEmpty()) continue - val branchName = lines[0] - if (!BRANCH_NAME_REGEX.matches(branchName)) { + val releaseMatch = RELEASE_VERSION_REGEX.matchEntire(lines[0]) + if (releaseMatch == null) { return Result.failure( - GradleException("Expected a valid branch name, but got $branchName") + GradleException("Expected a valid release version, but got ${lines[0]}") ) } + val releaseVersion = releaseMatch.groupValues[1] val commits = arrayListOf() for (line in lines.subList(1, lines.size)) { - if (COMMIT_REGRX.matches(line)) { - commits.add(Commit(line.dropWhile { it == '-' || it == ' ' })) + val commitMatch = COMMIT_REGEX.matchEntire(line) + if (commitMatch != null) { + val (branchName, message, author) = commitMatch.destructured + commits.add( + Commit( + author, + branchName, + message, + ) + ) } else { return Result.failure( - GradleException("Expected a valid commit message, but got $line") + GradleException("Expected a valid commit line, but got $line") ) } } entries.add( ChangelogEntry( - branchName.dropWhile { it == '#' || it == ' ' }, + releaseVersion, commits ) ) diff --git a/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParserTests.kt b/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParserTests.kt index 76f61a0..e9c6dc8 100644 --- a/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParserTests.kt +++ b/changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParserTests.kt @@ -1,8 +1,7 @@ package io.github.aloussase.changelog.parser import org.hamcrest.MatcherAssert.assertThat -import org.hamcrest.Matchers.emptyIterable -import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.* import org.hamcrest.collection.IsCollectionWithSize.hasSize import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest @@ -34,20 +33,21 @@ class MarkdownChangelogParserTests { @Test fun givenDocumentContainingSingleEntryWithNoCommitsWhenParseisInvokedThenReturnChangelogWithThatEntryAndNoCommits() { - val doc = "# Changelog\n\n## LOYMAR-123" + val doc = "# Changelog\n\n## [0.1.1]" val parser = MarkdownChangelogParser() val result = parser.parse(doc) assertThat(result.isSuccess, equalTo(true)) assertThat(result.getOrThrow().entries, hasSize(1)) - assertThat(result.getOrThrow().entries.first().branchName, equalTo("LOYMAR-123")) + assertThat(result.getOrThrow().entries.first().release, equalTo("0.1.1")) assertThat(result.getOrThrow().entries.first().commits, emptyIterable()) } @Test fun givenDocumentContainingSingleEntryWithCommitsWhenParseIsInvokedThenReturnChangelogWithSingleEntryAndCommits() { - val doc = "# Changelog\n\n## LOYMAR-123\n- first commit\n- second commit" + val doc = + "# Changelog\n\n## [0.1.1]\n- LOYMAR-123: first commit (johndoe@gmail.com)\n- LOYMAR-123: second commit (johndoe@gmail.com)" val parser = MarkdownChangelogParser() val result = parser.parse(doc) @@ -61,16 +61,50 @@ class MarkdownChangelogParserTests { @Test fun givenDocumentWithTwoEntriesWhenParseIsInvokedThenReturnChangelogWithTwoEntries() { - val doc = "# Changelog\n\n## LOYMAR-123\n- first commit\n- second commit\n\n## LOYMAR-456\n- third commit" + val doc = + "# Changelog\n\n## [0.1.1]\n- LOYMAR-123: first commit (johndoe@gmail.com)\n\n## [0.1.0]\n- LOYMAR-123: third commit (janedoe@gmail.com)" val parser = MarkdownChangelogParser() val result = parser.parse(doc) assertThat(result.isSuccess, equalTo(true)) assertThat(result.getOrThrow().entries, hasSize(2)) - assertThat(result.getOrThrow().entries[0].commits, hasSize(2)) + assertThat(result.getOrThrow().entries[0].commits, hasSize(1)) assertThat(result.getOrThrow().entries[1].commits, hasSize(1)) assertThat(result.getOrThrow().entries[1].commits[0].message, equalTo("third commit")) } + @ParameterizedTest + @ValueSource( + strings = [ + "LOYMAR-123", + "0.1.x", + "" + ] + ) + fun givenDocumentWithInvalidReleaseVersionWhenParseIsInvokedThenExceptionShouldBeThrown(releaseVersion: String) { + val doc = + "# Changelog\n\n## $releaseVersion\n- LOYMAR-123: first commit (johndoe@gmail.com)\n\n## [0.1.0]\n- LOYMAR-123: third commit (janedoe@gmail.com)" + val parser = MarkdownChangelogParser() + + val result = parser.parse(doc) + + assertThat(result.isFailure, equalTo(true)) + assertThat(result.exceptionOrNull()?.message, containsString("Expected a valid release version")) + assertThat(result.exceptionOrNull()?.message, containsString(releaseVersion)) + } + + @ParameterizedTest + @ValueSource(strings = ["LOYMAR 123", "", "@@@"]) + fun givenDocumentWithInvalidCommitBranchWhenParseIsInvokedThenFailureShouldBeReturned(branchName: String) { + val doc = "# Changelog\n\n## [1.1.1]\n- $branchName: first commit (johndoe@gmail.com)" + val parser = MarkdownChangelogParser() + + val result = parser.parse(doc) + + assertThat(result.isFailure, equalTo(true)) + assertThat(result.exceptionOrNull()?.message, containsString(branchName)) + assertThat(result.exceptionOrNull()?.message, containsString("Expected a valid commit")) + } + } -- 2.43.0