|
1 | 1 | package io.github.typesafegithub.workflows.actionbindinggenerator |
2 | 2 |
|
3 | 3 | import io.github.typesafegithub.workflows.actionbindinggenerator.generation.ActionBinding |
| 4 | +import io.github.typesafegithub.workflows.actionbindinggenerator.versioning.BindingVersion |
| 5 | +import io.github.typesafegithub.workflows.domain.actions.Action |
| 6 | +import io.kotest.common.mapError |
| 7 | +import io.kotest.core.spec.style.scopes.ContainerScope |
| 8 | +import io.kotest.datatest.withData |
4 | 9 | import io.kotest.matchers.Matcher.Companion.failure |
5 | 10 | import io.kotest.matchers.shouldBe |
| 11 | +import java.lang.reflect.InvocationTargetException |
6 | 12 | import java.nio.file.Paths |
7 | 13 |
|
8 | 14 | fun List<ActionBinding>.shouldContainAndMatchFile(path: String) { |
@@ -36,3 +42,68 @@ fun List<ActionBinding>.shouldContainAndMatchFile(path: String) { |
36 | 42 | } |
37 | 43 |
|
38 | 44 | private fun String.removeWindowsNewLines(): String = replace("\r\n", "\n") |
| 45 | + |
| 46 | +fun constructAction( |
| 47 | + owner: String, |
| 48 | + classBaseName: String, |
| 49 | + bindingVersion: BindingVersion, |
| 50 | + arguments: Map<String, Any?> = emptyMap(), |
| 51 | +): Action<*> { |
| 52 | + val constructor = |
| 53 | + Class |
| 54 | + .forName("io.github.typesafegithub.workflows.actions.$owner.${classBaseName}Binding${bindingVersion.name}") |
| 55 | + .let { |
| 56 | + @Suppress("UNCHECKED_CAST") |
| 57 | + it as Class<Action<*>> |
| 58 | + }.kotlin |
| 59 | + .constructors |
| 60 | + .first() |
| 61 | + return runCatching { |
| 62 | + constructor.callBy( |
| 63 | + arguments.mapKeys { (key, _) -> |
| 64 | + constructor.parameters.first { it.name == key } |
| 65 | + }, |
| 66 | + ) |
| 67 | + }.mapError { |
| 68 | + if (it is InvocationTargetException) it.targetException else it |
| 69 | + }.getOrThrow() |
| 70 | +} |
| 71 | + |
| 72 | +suspend fun ContainerScope.withBindingVersions( |
| 73 | + bindingVersions: Iterable<BindingVersion>, |
| 74 | + test: suspend ContainerScope.(BindingVersion) -> Unit, |
| 75 | +) = withData( |
| 76 | + nameFn = { "binding version $it" }, |
| 77 | + ts = bindingVersions, |
| 78 | + test = test, |
| 79 | +) |
| 80 | + |
| 81 | +suspend fun ContainerScope.withAllBindingVersions(test: suspend ContainerScope.(BindingVersion) -> Unit) = |
| 82 | + withBindingVersions( |
| 83 | + bindingVersions = BindingVersion.entries, |
| 84 | + test = test, |
| 85 | + ) |
| 86 | + |
| 87 | +suspend fun ContainerScope.withBindingVersions( |
| 88 | + bindingVersions: OpenEndRange<BindingVersion>, |
| 89 | + test: suspend ContainerScope.(BindingVersion) -> Unit, |
| 90 | +) = withBindingVersions( |
| 91 | + bindingVersions = BindingVersion.entries.filter { it in bindingVersions }, |
| 92 | + test = test, |
| 93 | +) |
| 94 | + |
| 95 | +suspend fun ContainerScope.withBindingVersions( |
| 96 | + bindingVersions: ClosedRange<BindingVersion>, |
| 97 | + test: suspend ContainerScope.(BindingVersion) -> Unit, |
| 98 | +) = withBindingVersions( |
| 99 | + bindingVersions = BindingVersion.entries.filter { it in bindingVersions }, |
| 100 | + test = test, |
| 101 | +) |
| 102 | + |
| 103 | +suspend fun ContainerScope.withBindingVersionsFrom( |
| 104 | + bindingVersion: BindingVersion, |
| 105 | + test: suspend ContainerScope.(BindingVersion) -> Unit, |
| 106 | +) = withBindingVersions( |
| 107 | + bindingVersions = bindingVersion..BindingVersion.entries.last(), |
| 108 | + test = test, |
| 109 | +) |
0 commit comments