diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b26ac2..4f7cc5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] +### Fixed +- Wrong markdown render of changelog that use CRLF or CR line separator [#176](../../issues/176) + ## [2.1.0] - 2023-06-02 ### Added diff --git a/src/main/kotlin/org/jetbrains/changelog/ChangelogPlugin.kt b/src/main/kotlin/org/jetbrains/changelog/ChangelogPlugin.kt index 2ada3e0..ce069df 100644 --- a/src/main/kotlin/org/jetbrains/changelog/ChangelogPlugin.kt +++ b/src/main/kotlin/org/jetbrains/changelog/ChangelogPlugin.kt @@ -101,7 +101,7 @@ class ChangelogPlugin : Plugin { if (!exists()) { createNewFile() } - readText() + readText().normalizeToLineFeed() } }.get(), defaultPreTitle = preTitle.orNull, diff --git a/src/main/kotlin/org/jetbrains/changelog/extensions.kt b/src/main/kotlin/org/jetbrains/changelog/extensions.kt index 20ae322..6932b74 100644 --- a/src/main/kotlin/org/jetbrains/changelog/extensions.kt +++ b/src/main/kotlin/org/jetbrains/changelog/extensions.kt @@ -39,6 +39,17 @@ internal fun String.reformat(lineSeparator: String): String { } } +internal fun String.normalizeToLineFeed(): String { + val result = listOf( + "\r\n" to "\n", + "\r" to "\n", + ).fold(this) { acc, (pattern, replacement) -> + acc.replace(pattern, replacement) + } + + return result +} + fun interface ChangelogSectionUrlBuilder { fun build(repositoryUrl: String, currentVersion: String?, previousVersion: String?, isUnreleased: Boolean): String } diff --git a/src/test/kotlin/org/jetbrains/changelog/BaseTest.kt b/src/test/kotlin/org/jetbrains/changelog/BaseTest.kt index bf69dcb..a86d5e0 100644 --- a/src/test/kotlin/org/jetbrains/changelog/BaseTest.kt +++ b/src/test/kotlin/org/jetbrains/changelog/BaseTest.kt @@ -36,7 +36,7 @@ open class BaseTest { field = value changelogFile.run { createNewFile() - writeText(value.trimIndent().trim()) + writeText(value) } } get() = changelogFile.readText() diff --git a/src/test/kotlin/org/jetbrains/changelog/ExtensionsTest.kt b/src/test/kotlin/org/jetbrains/changelog/ExtensionsTest.kt index f148563..c02f6d4 100644 --- a/src/test/kotlin/org/jetbrains/changelog/ExtensionsTest.kt +++ b/src/test/kotlin/org/jetbrains/changelog/ExtensionsTest.kt @@ -76,4 +76,45 @@ class ExtensionsTest { """.trimIndent().reformat(lineSeparator) ) } + + @Test + fun `normalize string to line feed`() { + val text = """ + Pre title content. + + # Title + Summary + + ## [Unreleased] + + ## [1.0.0] + - asd + + ## [0.1.0] + + ### Added + - Buz + + """.trimIndent() + + assertEquals( + text, + text.replace("\n", "\r\n").normalizeToLineFeed() + ) + + assertEquals( + text, + text.replace("\n", "\r").normalizeToLineFeed() + ) + + assertEquals( + text, + text.normalizeToLineFeed() + ) + + assertEquals( + "text\ntext2\ntext3\ntext4", + "text\ntext2\rtext3\r\ntext4".normalizeToLineFeed() + ) + } } diff --git a/src/test/kotlin/org/jetbrains/changelog/tasks/GetChangelogTaskTest.kt b/src/test/kotlin/org/jetbrains/changelog/tasks/GetChangelogTaskTest.kt index 57f3364..0aea9a6 100644 --- a/src/test/kotlin/org/jetbrains/changelog/tasks/GetChangelogTaskTest.kt +++ b/src/test/kotlin/org/jetbrains/changelog/tasks/GetChangelogTaskTest.kt @@ -95,6 +95,7 @@ class GetChangelogTaskTest : BaseTest() { ) } + @Test fun `returns the Unreleased change notes without empty sections`() { val result = runTask(GET_CHANGELOG_TASK_NAME, "--quiet", "--unreleased", "--no-empty-sections") @@ -261,4 +262,100 @@ class GetChangelogTaskTest : BaseTest() { assertTrue(result.output.contains("Reusing configuration cache.")) } + + @Test + fun `get changelog with CRLF line separator`() { + changelog = + """ + # Changelog + ## [Unreleased] + Some unreleased changes. + + - bar + + ### Added + + ### Fixed + - I fixed myself a beverage. + + ## [1.0.1] - 2022-10-17 + Release with bugfix. + + ### Fixed + - bar + + ## [1.0.0] - 2022-10-10 + That was a great release. + + ### Added + - foo + + [Unreleased]: https://jetbrians.com/Unreleased + [1.0.1]: https://jetbrians.com/1.0.1 + [1.0.0]: https://jetbrians.com/1.0.0 + """.trimIndent().replace("\n", "\r\n") + + val result = runTask(GET_CHANGELOG_TASK_NAME, "--quiet") + + assertMarkdown( + """ + ## [1.0.1] - 2022-10-17 + Release with bugfix. + + ### Fixed + - bar + + [1.0.1]: https://jetbrians.com/1.0.1 + """.trimIndent().replace("\n", "\r\n"), + result.output + ) + } + + @Test + fun `get changelog with CR line separator`() { + changelog = + """ + # Changelog + ## [Unreleased] + Some unreleased changes. + + - bar + + ### Added + + ### Fixed + - I fixed myself a beverage. + + ## [1.0.1] - 2022-10-17 + Release with bugfix. + + ### Fixed + - bar + + ## [1.0.0] - 2022-10-10 + That was a great release. + + ### Added + - foo + + [Unreleased]: https://jetbrians.com/Unreleased + [1.0.1]: https://jetbrians.com/1.0.1 + [1.0.0]: https://jetbrians.com/1.0.0 + """.trimIndent().replace("\n", "\r") + + val result = runTask(GET_CHANGELOG_TASK_NAME, "--quiet") + + assertMarkdown( + """ + ## [1.0.1] - 2022-10-17 + Release with bugfix. + + ### Fixed + - bar + + [1.0.1]: https://jetbrians.com/1.0.1 + """.trimIndent().replace("\n", "\r"), + result.output + ) + } } diff --git a/src/test/kotlin/org/jetbrains/changelog/tasks/InitializeChangelogTaskTest.kt b/src/test/kotlin/org/jetbrains/changelog/tasks/InitializeChangelogTaskTest.kt index 06608b2..6c24802 100644 --- a/src/test/kotlin/org/jetbrains/changelog/tasks/InitializeChangelogTaskTest.kt +++ b/src/test/kotlin/org/jetbrains/changelog/tasks/InitializeChangelogTaskTest.kt @@ -181,4 +181,102 @@ class InitializeChangelogTaskTest : BaseTest() { assertTrue(result.output.contains("Reusing configuration cache.")) } + + @Test + fun `creates new changelog file with CRLF line separator`() { + extension.lineSeparator.set("\r\n") + + runTask(INITIALIZE_CHANGELOG_TASK_NAME) + + assertMarkdown( + """ + # Changelog + + ## Unreleased + + ### Added + + ### Changed + + ### Deprecated + + ### Removed + + ### Fixed + + ### Security + + """.trimIndent().replace("\n", "\r\n"), + extension.render() + ) + + assertMarkdown( + """ + ## Unreleased + + ### Added + + ### Changed + + ### Deprecated + + ### Removed + + ### Fixed + + ### Security + + """.trimIndent().replace("\n", "\r\n"), + extension.renderItem(extension.getUnreleased()) + ) + } + + @Test + fun `creates new changelog file with CR line separator`() { + extension.lineSeparator.set("\r") + + runTask(INITIALIZE_CHANGELOG_TASK_NAME) + + assertMarkdown( + """ + # Changelog + + ## Unreleased + + ### Added + + ### Changed + + ### Deprecated + + ### Removed + + ### Fixed + + ### Security + + """.trimIndent().replace("\n", "\r"), + extension.render() + ) + + assertMarkdown( + """ + ## Unreleased + + ### Added + + ### Changed + + ### Deprecated + + ### Removed + + ### Fixed + + ### Security + + """.trimIndent().replace("\n", "\r"), + extension.renderItem(extension.getUnreleased()) + ) + } } diff --git a/src/test/kotlin/org/jetbrains/changelog/tasks/PatchChangelogTaskTest.kt b/src/test/kotlin/org/jetbrains/changelog/tasks/PatchChangelogTaskTest.kt index e8b011c..2c03c9b 100644 --- a/src/test/kotlin/org/jetbrains/changelog/tasks/PatchChangelogTaskTest.kt +++ b/src/test/kotlin/org/jetbrains/changelog/tasks/PatchChangelogTaskTest.kt @@ -1181,4 +1181,88 @@ class PatchChangelogTaskTest : BaseTest() { assertTrue(result.output.contains("Reusing configuration cache.")) } + + @Test + fun `patch with CRLF line separator`() { + changelog = + """ + + + # Changelog + My project changelog. + + ## [Unreleased] + Fancy release. + + ### Added + - foo + + ### Changed + - changed + - changed 2 + """.trimIndent().replace("\n", "\r\n") + + project.evaluate() + runTask(PATCH_CHANGELOG_TASK_NAME) + + assertMarkdown( + """ + ## [1.0.0] - $date + Fancy release. + + ### Added + - foo + + ### Changed + - changed + - changed 2 + + [1.0.0]: https://github.com/JetBrains/gradle-changelog-plugin/commits/v1.0.0 + + """.trimIndent().replace("\n", "\r\n"), + extension.renderItem(extension.get(version)) + ) + } + + @Test + fun `patch with CR line separator`() { + changelog = + """ + + + # Changelog + My project changelog. + + ## [Unreleased] + Fancy release. + + ### Added + - foo + + ### Changed + - changed + - changed 2 + """.trimIndent().replace("\n", "\r") + + project.evaluate() + runTask(PATCH_CHANGELOG_TASK_NAME) + + assertMarkdown( + """ + ## [1.0.0] - $date + Fancy release. + + ### Added + - foo + + ### Changed + - changed + - changed 2 + + [1.0.0]: https://github.com/JetBrains/gradle-changelog-plugin/commits/v1.0.0 + + """.trimIndent().replace("\n", "\r"), + extension.renderItem(extension.get(version)) + ) + } }