Skip to content

Commit 2013c93

Browse files
authored
test(library): add tests for reading contexts in Kotlin-based steps (#1947)
Part of #1946.
1 parent c96bd58 commit 2013c93

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.typesafegithub.workflows.yaml
2+
3+
import io.github.typesafegithub.workflows.domain.contexts.Contexts
4+
import io.github.typesafegithub.workflows.domain.contexts.GithubContext
5+
import kotlinx.serialization.json.Json
6+
7+
internal fun loadContextsFromEnvVars(getenv: (String) -> String?): Contexts {
8+
fun getEnvVarOrFail(varName: String): String = getenv(varName) ?: error("$varName should be set!")
9+
10+
val githubContextRaw = getEnvVarOrFail("GHWKT_GITHUB_CONTEXT_JSON")
11+
val githubContext = json.decodeFromString<GithubContext>(githubContextRaw)
12+
return Contexts(
13+
github = githubContext,
14+
)
15+
}
16+
17+
private val json = Json { ignoreUnknownKeys = true }

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ToYaml.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import io.github.typesafegithub.workflows.domain.KotlinLogicStep
55
import io.github.typesafegithub.workflows.domain.Mode
66
import io.github.typesafegithub.workflows.domain.Permission
77
import io.github.typesafegithub.workflows.domain.Workflow
8-
import io.github.typesafegithub.workflows.domain.contexts.Contexts
9-
import io.github.typesafegithub.workflows.domain.contexts.GithubContext
108
import io.github.typesafegithub.workflows.dsl.toBuilder
119
import io.github.typesafegithub.workflows.internal.relativeToAbsolute
1210
import io.github.typesafegithub.workflows.shared.internal.findGitRoot
1311
import io.github.typesafegithub.workflows.yaml.Preamble.Just
1412
import io.github.typesafegithub.workflows.yaml.Preamble.WithOriginalAfter
1513
import io.github.typesafegithub.workflows.yaml.Preamble.WithOriginalBefore
16-
import kotlinx.serialization.json.Json
1714
import java.nio.file.Path
1815
import kotlin.io.path.absolute
1916
import kotlin.io.path.invariantSeparatorsPathString
@@ -72,16 +69,6 @@ internal fun Workflow.writeToFile(
7269
}
7370
}
7471

75-
private fun loadContextsFromEnvVars(getenv: (String) -> String?): Contexts {
76-
fun getEnvVarOrFail(varName: String): String = getenv(varName) ?: error("$varName should be set!")
77-
78-
val githubContextRaw = getEnvVarOrFail("GHWKT_GITHUB_CONTEXT_JSON")
79-
val githubContext = json.decodeFromString<GithubContext>(githubContextRaw)
80-
return Contexts(
81-
github = githubContext,
82-
)
83-
}
84-
8572
private fun commentify(preamble: String): String {
8673
if (preamble.isEmpty()) return ""
8774

@@ -174,5 +161,3 @@ private fun Workflow.toYamlInternal(jobsWithConsistencyCheck: List<Job<*>>): Map
174161
*_customArguments.toList().toTypedArray(),
175162
"jobs" to jobsWithConsistencyCheck.jobsToYaml(),
176163
)
177-
178-
private val json = Json { ignoreUnknownKeys = true }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.typesafegithub.workflows.yaml
2+
3+
import io.github.typesafegithub.workflows.domain.contexts.Contexts
4+
import io.github.typesafegithub.workflows.domain.contexts.GithubContext
5+
import io.github.typesafegithub.workflows.domain.contexts.GithubContextEvent
6+
import io.kotest.core.spec.style.FunSpec
7+
import io.kotest.matchers.shouldBe
8+
9+
class ContextMappingTest :
10+
FunSpec({
11+
test("successfully load all supported contexts with all supported fields") {
12+
// Given
13+
val testGithubContextJson = javaClass.getResource("/contexts/github.json")!!.readText()
14+
15+
// When
16+
val contexts =
17+
loadContextsFromEnvVars(
18+
getenv = mapOf(
19+
"GHWKT_GITHUB_CONTEXT_JSON" to testGithubContextJson,
20+
)::get,
21+
)
22+
23+
// Then
24+
contexts shouldBe
25+
Contexts(
26+
github =
27+
GithubContext(
28+
repository = "some-owner/some-repo",
29+
sha = "db76dd0f1149901e1cdf60ec98d568b32fa7eb71",
30+
ref = "refs/heads/main",
31+
event =
32+
GithubContextEvent(
33+
after = "1383af4847629428f1675f5c2e81e67cc3a4efb0",
34+
),
35+
event_name = "push",
36+
),
37+
)
38+
}
39+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"repository": "some-owner/some-repo",
3+
"sha": "db76dd0f1149901e1cdf60ec98d568b32fa7eb71",
4+
"ref": "refs/heads/main",
5+
"event": {
6+
"after": "1383af4847629428f1675f5c2e81e67cc3a4efb0"
7+
},
8+
"event_name": "push"
9+
}

0 commit comments

Comments
 (0)