]> git.frustrated-labs.net Git - gradle-changelog-plugin.git/commitdiff
tests: add more tests for markdown changelog parser
authorAlexander Goussas <[email protected]>
Fri, 24 Oct 2025 13:50:58 +0000 (08:50 -0500)
committerAlexander Goussas <[email protected]>
Fri, 24 Oct 2025 13:50:58 +0000 (08:50 -0500)
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/ChangelogEntry.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/formatter/MarkdownFormatter.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/git/Commit.kt
changelog-plugin/src/main/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParser.kt
changelog-plugin/src/test/kotlin/io/github/aloussase/changelog/parser/MarkdownChangelogParserTests.kt

index 4733a14dc49a8c225ea4e7c37be9e2b8fb6d24a0..9d509f10784088c111e0fbd694db4e1752e68dbd 100644 (file)
@@ -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<Commit> = emptyList(),
 )
index da6cbb754416aa25e362d1f1b187df86ee3bacc0..5f67ce6cadd6f45a185fa8686527b51225bc51d7 100644 (file)
@@ -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
     }
 }
index 817c74f1e37a1aaa7e453743e3c7f0bf3a7f4886..40c9fb20db9993575b681dbb2619e128f1533524 100644 (file)
@@ -1,5 +1,7 @@
 package io.github.aloussase.changelog.git
 
 data class Commit(
+    val author: String,
+    val branchName: String,
     val message: String
 )
index 824dd04f0de1dcddba196d7512eee1d34afb3ef1..b3e2a1b3f463072409775226cb5fe41c29ffe3b4 100644 (file)
@@ -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<Commit>()
 
             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
                 )
             )
index 76f61a09311d3b5a29c9fe5f40ae3b732cec822a..e9c6dc82af7950bd5e40a0afd4747d8d7375f132 100644 (file)
@@ -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 ([email protected])\n- LOYMAR-123: second commit ([email protected])"
         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 ([email protected])\n\n## [0.1.0]\n- LOYMAR-123: third commit ([email protected])"
         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 ([email protected])\n\n## [0.1.0]\n- LOYMAR-123: third commit ([email protected])"
+        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 ([email protected])"
+        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"))
+    }
+
 }