Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
Add tests for changelog generation
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaiR committed Nov 12, 2018
1 parent b0ee87d commit 0f8bb84
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ext {
kotlinVersion = '1.3.0'
vertxVersion = '3.5.4'
jsoupVersion = '1.11.3'
junitVersion = '5.3.1'
ktlintVersion = '0.29.0'
}

Expand All @@ -35,6 +36,9 @@ dependencies {
implementation "io.vertx:vertx-core:$vertxVersion"
implementation "org.jsoup:jsoup:$jsoupVersion"

testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"

ktlint "com.github.shyiko:ktlint:$ktlintVersion"
}

Expand Down Expand Up @@ -65,6 +69,10 @@ task cleanProject(type: Delete, group: 'build') {
delete "$projectDir/.vertx", "$projectDir/logs", "$projectDir/out"
}

test {
useJUnitPlatform()
}

task ktlint(type: JavaExec, group: 'verification') {
description = 'Check Kotlin code style.'
main = 'com.github.shyiko.ktlint.Main'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.github.spair.repoxbot.dto.ChangelogEntry
import io.github.spair.repoxbot.dto.PullRequest

private val clText = ":cl:((?:.|\\n|\\r)*+)|\uD83C\uDD91((?:.|\\n|\\r)*+)".toRegex()
private val clRowWithClass = "-[ ](\\w+)(\\[link])?:[ ](.+)".toRegex()
private val clRowWithClass = "[-*][ ](\\w+)(\\[link])?:[ ](.+)".toRegex()

fun generateChangelog(pullRequest: PullRequest): Changelog? {
return findChangelogText(pullRequest.body)?.let { parseChangelog(it, pullRequest) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@file:Suppress("ClassName")

package io.github.spair.repoxbot.logic

import io.github.spair.repoxbot.dto.Changelog
import io.github.spair.repoxbot.dto.ChangelogEntry
import io.github.spair.repoxbot.dto.PullRequest
import io.github.spair.repoxbot.dto.PullRequestAction
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

private const val AUTHOR_NAME = "Author Name"
private const val PR_NUM = 12
private const val PR_LINK = "https://pr-link.com"

internal class `Test changelog generation from pull request` {

private val entry = ChangelogEntry("rscadd", "Test entry.")

@Test
fun `When no changelog`() {
assertNull(generateChangelog(createPullRequest("empty body")))
}

@Test
fun `When no entries`() {
assertTrue(generateChangelog(createPullRequest("body\n:cl: author"))!!.isEmpty())
}

@Test
fun `When has author and 1 entry`() {
val expected = Changelog("custom author", PR_LINK, PR_NUM, listOf(entry))
val body = """
body
:cl: custom author
- rscadd: Test entry.
""".trimIndent()
assertEquals(expected, generateChangelog(createPullRequest(body)))
}

@Test
fun `When has no author and multiple entries`() {
val expected = Changelog(AUTHOR_NAME, PR_LINK, PR_NUM, listOf(entry, entry))
val body = """
body
:cl:
- rscadd: test entry
- rscadd: Test entry.
""".trimIndent()
assertEquals(expected, generateChangelog(createPullRequest(body)))
}

@Test
fun `When has non typical entry`() {
val expected = Changelog(AUTHOR_NAME, PR_LINK, PR_NUM, listOf(ChangelogEntry("rscadd", "Test entry!")))
val body = """
body
:cl:
* rscadd: test entry!
""".trimIndent()
assertEquals(expected, generateChangelog(createPullRequest(body)))
}

private fun createPullRequest(body: String): PullRequest = PullRequest(
PullRequestAction.MERGED, AUTHOR_NAME, PR_NUM, "Title",
PR_LINK, "Diff Link", body, "Sender", "Touched label"
)
}

0 comments on commit 0f8bb84

Please sign in to comment.