Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(server): add tests for Maven metadata building #1837

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package io.github.typesafegithub.workflows.mavenbinding

import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions
import io.github.typesafegithub.workflows.shared.internal.model.Version
import java.time.format.DateTimeFormatter

internal suspend fun ActionCoords.buildMavenMetadataFile(githubToken: String): String? {
internal suspend fun ActionCoords.buildMavenMetadataFile(
githubToken: String,
fetchAvailableVersions: suspend (owner: String, name: String, githubToken: String?) -> List<Version> = ::fetchAvailableVersions,
): String? {
val availableMajorVersions =
fetchAvailableVersions(owner = owner, name = name, githubToken = githubToken)
fetchAvailableVersions(owner, name, githubToken)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping we could keep the named arguments for this call when adding a way to mock this function, but without a bit bigger refactoring (wrapping the function in a class or making the function accept ActionCoords) it seems to be not possible.

.filter { it.isMajorVersion() }
val newest = availableMajorVersions.maxOrNull() ?: return null
val lastUpdated =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.github.typesafegithub.workflows.mavenbinding

import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
import io.github.typesafegithub.workflows.shared.internal.model.Version
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.shouldBe
import java.time.ZonedDateTime

class MavenMetadataBuildingTest :
FunSpec({
val actionCoords =
ActionCoords(
owner = "owner",
name = "name",
version = "irrelevant",
)

test("various kinds of versions available") {
// Given
val fetchAvailableVersions: suspend (String, String, String?) -> List<Version> = { _, _, _ ->
listOf(
Version(version = "v3-beta", dateProvider = { ZonedDateTime.parse("2024-07-01T00:00:00Z") }),
Version(version = "v2", dateProvider = { ZonedDateTime.parse("2024-05-01T00:00:00Z") }),
Version(version = "v1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
Version(version = "v1.1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
Version(version = "v1.1.0", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
Version(version = "v1.0.1", dateProvider = { ZonedDateTime.parse("2024-03-05T00:00:00Z") }),
Version(version = "v1.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }),
Version(version = "v1.0.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }),
)
}

val xml =
actionCoords.buildMavenMetadataFile(
githubToken = "SOME_TOKEN",
fetchAvailableVersions = fetchAvailableVersions,
)

xml shouldBe
"""
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>owner</groupId>
<artifactId>name</artifactId>
<versioning>
<latest>v2</latest>
<release>v2</release>
<versions>
<version>v2</version>
<version>v1</version>
</versions>
<lastUpdated>20240501000000</lastUpdated>
</versioning>
</metadata>
""".trimIndent()
}

test("no major versions") {
// Given
val fetchAvailableVersions: suspend (String, String, String?) -> List<Version> = { _, _, _ ->
listOf(
Version(version = "v1.1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
Version(version = "v1.1.0", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
Version(version = "v1.0.1", dateProvider = { ZonedDateTime.parse("2024-03-05T00:00:00Z") }),
Version(version = "v1.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }),
Version(version = "v1.0.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }),
)
}

val xml =
actionCoords.buildMavenMetadataFile(
githubToken = "SOME_TOKEN",
fetchAvailableVersions = fetchAvailableVersions,
)

xml.shouldBeNull()
}

test("no versions available") {
// Given
val fetchAvailableVersions: suspend (String, String, String?) -> List<Version> = { _, _, _ ->
emptyList()
}

val xml =
actionCoords.buildMavenMetadataFile(
githubToken = "SOME_TOKEN",
fetchAvailableVersions = fetchAvailableVersions,
)

xml.shouldBeNull()
}
})
Loading