10000 test(library): add tests for reading contexts in Kotlin-based steps by krzema12 · Pull Request #1947 · typesafegithub/github-workflows-kt · GitHub
[go: up one dir, main page]

Skip to content

test(library): add tests for reading contexts in Kotlin-based steps #1947

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

Merged
merged 1 commit into from
May 6, 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
@@ -0,0 +1,17 @@
package io.github.typesafegithub.workflows.yaml

import io.github.typesafegithub.workflows.domain.contexts.Contexts
import io.github.typesafegithub.workflows.domain.contexts.GithubContext
import kotlinx.serialization.json.Json

internal fun loadContextsFromEnvVars(getenv: (String) -> String?): Contexts {
fun getEnvVarOrFail(varName: String): String = getenv(varName) ?: error("$varName should be set!")

val githubContextRaw = getEnvVarOrFail("GHWKT_GITHUB_CONTEXT_JSON")
val githubContext = json.decodeFromString<GithubContext>(githubContextRaw)
return Contexts(
github = githubContext,
)
}

private val json = Json { ignoreUnknownKeys = true }
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import io.github.typesafegithub.workflows.domain.KotlinLogicStep
import io.github.typesafegithub.workflows.domain.Mode
import io.github.typesafegithub.workflows.domain.Permission
import io.github.typesafegithub.workflows.domain.Workflow
import io.github.typesafegithub.workflows.domain.contexts.Contexts
import io.github.typesafegithub.workflows.domain.contexts.GithubContext
import io.github.typesafegithub.workflows.dsl.toBuilder
import io.github.typesafegithub.workflows.internal.relativeToAbsolute
import io.github.typesafegithub.workflows.shared.internal.findGitRoot
import io.github.typesafegithub.workflows.yaml.Preamble.Just
import io.github.typesafegithub.workflows.yaml.Preamble.WithOriginalAfter
import io.github.typesafegithub.workflows.yaml.Preamble.WithOriginalBefore
import kotlinx.serialization.json.Json
import java.nio.file.Path
import kotlin.io.path.absolute
import kotlin.io.path.invariantSeparatorsPathString
Expand Down Expand Up @@ -72,16 +69,6 @@ internal fun Workflow.writeToFile(
}
}

private fun loadContextsFromEnvVars(getenv: (String) -> String?): Contexts {
fun getEnvVarOrFail(varName: String): String = getenv(varName) ?: error("$varName should be set!")

val githubContextRaw = getEnvVarOrFail("GHWKT_GITHUB_CONTEXT_JSON")
val githubContext = json.decodeFromString<GithubContext>(githubContextRaw)
return Contexts(
github = githubContext,
)
}

private fun commentify(preamble: String): String {
if (preamble.isEmpty()) return ""

Expand Down Expand Up @@ -174,5 +161,3 @@ private fun Workflow.toYamlInternal(jobsWithConsistencyCheck: List<Job<*>>): Map
*_customArguments.toList().toTypedArray(),
"jobs" to jobsWithConsistencyCheck.jobsToYaml(),
)

private val json = Json { ignoreUnknownKeys = true }
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.github.typesafegithub.workflows.yaml

import io.github.typesafegithub.workflows.domain.contexts.Contexts
import io.github.typesafegithub.workflows.domain.contexts.GithubContext
import io.github.typesafegithub.workflows.domain.contexts.GithubContextEvent
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class ContextMappingTest :
FunSpec({
test("successfully load all supported contexts with all supported fields") {
// Given
val testGithubContextJson = javaClass.getResource("/contexts/github.json")!!.readText()

// When
val contexts =
loadContextsFromEnvVars(
getenv = mapOf(
"GHWKT_GITHUB_CONTEXT_JSON" to testGithubContextJson,
)::get,
)

// Then
contexts shouldBe
Contexts(
github =
GithubContext(
repository = "some-owner/some-repo",
sha = "db76dd0f1149901e1cdf60ec98d568b32fa7eb71",
ref = "refs/heads/main",
event =
GithubContextEvent(
after = "1383af4847629428f1675f5c2e81e67cc3a4efb0",
),
event_name = "push",
),
)
}
})
9 changes: 9 additions & 0 deletions github-workflows-kt/src/test/resources/contexts/github.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"repository": "some-owner/some-repo",
"sha": "db76dd0f1149901e1cdf60ec98d568b32fa7eb71",
"ref": "refs/heads/main",
"event": {
"after": "1383af4847629428f1675f5c2e81e67cc3a4efb0"
},
"event_name": "push"
}
Loading
0