Skip to content

Commit

Permalink
Fix wrong markdown render of changelog that use CRLF or CR line separ…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
danygold authored and hsz committed Jul 6, 2023
1 parent af15130 commit 29b4738
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/jetbrains/changelog/ChangelogPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ChangelogPlugin : Plugin<Project> {
if (!exists()) {
createNewFile()
}
readText()
readText().normalizeToLineFeed()
}
}.get(),
defaultPreTitle = preTitle.orNull,
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/org/jetbrains/changelog/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/test/kotlin/org/jetbrains/changelog/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open class BaseTest {
field = value
changelogFile.run {
createNewFile()
writeText(value.trimIndent().trim())
writeText(value)
}
}
get() = changelogFile.readText()
Expand Down
41 changes: 41 additions & 0 deletions src/test/kotlin/org/jetbrains/changelog/ExtensionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1181,4 +1181,88 @@ class PatchChangelogTaskTest : BaseTest() {

assertTrue(result.output.contains("Reusing configuration cache."))
}

@Test
fun `patch with CRLF line separator`() {
changelog =
"""
<!-- Foo bar -->
# 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 =
"""
<!-- Foo bar -->
# 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))
)
}
}

0 comments on commit 29b4738

Please sign in to comment.