Skip to content

Commit 101beb4

Browse files
committed
[#48] feat: improve message for input/output mismatch
1 parent 4709a21 commit 101beb4

File tree

3 files changed

+112
-6
lines changed

3 files changed

+112
-6
lines changed

src/main/kotlin/it/krzeminski/githubactionstyping/ManifestsToReport.kt

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import it.krzeminski.githubactionstyping.parsing.parseManifest
55
import it.krzeminski.githubactionstyping.parsing.parseTypesManifest
66
import it.krzeminski.githubactionstyping.reporting.toPlaintextReport
77
import it.krzeminski.githubactionstyping.validation.ItemValidationResult
8+
import it.krzeminski.githubactionstyping.validation.buildInputOutputMismatchValidationResult
89
import it.krzeminski.githubactionstyping.validation.validate
910

1011
fun manifestsToReport(manifest: String, typesManifest: String): Pair<Boolean, String> {
@@ -18,15 +19,17 @@ fun manifestsToReport(manifest: String, typesManifest: String): Pair<Boolean, St
1819
val inputsInTypesManifest = parsedTypesManifest.inputs.keys
1920
val inputsInManifest = parsedManifest.inputs.keys
2021

21-
if (inputsInManifest != inputsInTypesManifest) {
22-
throw IllegalStateException("The same set of inputs should exist in action manifest and types manifest!")
23-
}
24-
2522
val outputsInTypesManifest = parsedTypesManifest.outputs.keys
2623
val outputsInManifest = parsedManifest.outputs.keys
2724

28-
if (outputsInManifest != outputsInTypesManifest) {
29-
throw IllegalStateException("The same set of outputs should exist in action manifest and types manifest!")
25+
if (inputsInManifest != inputsInTypesManifest || outputsInManifest != outputsInTypesManifest) {
26+
val inputOutputMismatchValidationResult = buildInputOutputMismatchValidationResult(
27+
inputsInManifest = inputsInManifest,
28+
inputsInTypesManifest = inputsInTypesManifest,
29+
outputsInManifest = outputsInManifest,
30+
outputsInTypesManifest = outputsInTypesManifest,
31+
)
32+
return Pair(false, inputOutputMismatchValidationResult.toPlaintextReport())
3033
}
3134

3235
printlnDebug("Action's manifest:")

src/main/kotlin/it/krzeminski/githubactionstyping/validation/ManifestValidation.kt

+37
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ fun TypesManifest.validate(): ActionValidationResult {
2222
)
2323
}
2424

25+
fun buildInputOutputMismatchValidationResult(
26+
inputsInManifest: Set<String>,
27+
inputsInTypesManifest: Set<String>,
28+
outputsInManifest: Set<String>,
29+
outputsInTypesManifest: Set<String>,
30+
): ActionValidationResult {
31+
return ActionValidationResult(
32+
overallResult = ItemValidationResult.Invalid(
33+
"Input/output mismatch detected. Please fix it first, then rerun to see other possible violations.",
34+
),
35+
inputs = (inputsInManifest + inputsInTypesManifest)
36+
.associateWith {
37+
if (it in inputsInManifest && it in inputsInTypesManifest) {
38+
ItemValidationResult.Valid
39+
} else {
40+
if (it !in inputsInManifest) {
41+
ItemValidationResult.Invalid("This input doesn't exist in the action manifest.")
42+
} else {
43+
ItemValidationResult.Invalid("This input doesn't exist in the types manifest.")
44+
}
45+
}
46+
},
47+
outputs = (outputsInManifest + outputsInTypesManifest)
48+
.associateWith {
49+
if (it in outputsInManifest && it in outputsInTypesManifest) {
50+
ItemValidationResult.Valid
51+
} else {
52+
if (it !in outputsInManifest) {
53+
ItemValidationResult.Invalid("This output doesn't exist in the action manifest.")
54+
} else {
55+
ItemValidationResult.Invalid("This output doesn't exist in the types manifest.")
56+
}
57+
}
58+
},
59+
)
60+
}
61+
2562
private fun ApiItem.validate(): ItemValidationResult {
2663
if (this.type == null) {
2764
return ItemValidationResult.Invalid("Type must be specified. Use 'type' attribute.")

src/test/kotlin/it/krzeminski/githubactionstyping/validation/ManifestsToReportTest.kt

+66
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,72 @@ class ManifestsToReportTest : FunSpec({
199199
None.
200200
201201
202+
""".trimIndent()
203+
}
204+
}
205+
206+
test("inputs mismatch between action manifest and types manifest") {
207+
// when
208+
val manifest = """
209+
name: GitHub Actions Typing
210+
description: Bring type-safety to your GitHub actions' API!
211+
author: Piotr Krzemiński
212+
inputs:
213+
foo:
214+
description: 'Set to true to display debug information helpful when troubleshooting issues with this action.'
215+
required: false
216+
bar:
217+
description: 'Testing enum'
218+
required: false
219+
outputs:
220+
goo:
221+
description: 'Set to true to display debug information helpful when troubleshooting issues with this action.'
222+
zoo:
223+
description: 'Testing enum'
224+
""".trimIndent()
225+
val typesManifest = """
226+
inputs:
227+
foo:
228+
type: boolean
229+
baz:
230+
type: enum
231+
allowed-values:
232+
- foo
233+
- bar
234+
outputs:
235+
goo:
236+
type: boolean
237+
boo:
238+
type: enum
239+
""".trimIndent()
240+
241+
// when
242+
val (isValid, report) = manifestsToReport(manifest, typesManifest)
243+
244+
// then
245+
assertSoftly {
246+
isValid shouldBe false
247+
report shouldBe """
248+
Overall result:
249+
${'\u001b'}[31m❌ INVALID: Input/output mismatch detected. Please fix it first, then rerun to see other possible violations.${'\u001b'}[0m
250+
251+
Inputs:
252+
• foo:
253+
${'\u001b'}[32m✔ VALID${'\u001b'}[0m
254+
• bar:
255+
${'\u001b'}[31m❌ INVALID: This input doesn't exist in the types manifest.${'\u001b'}[0m
256+
• baz:
257+
${'\u001b'}[31m❌ INVALID: This input doesn't exist in the action manifest.${'\u001b'}[0m
258+
259+
Outputs:
260+
• goo:
261+
${'\u001b'}[32m✔ VALID${'\u001b'}[0m
262+
• zoo:
263+
${'\u001b'}[31m❌ INVALID: This output doesn't exist in the types manifest.${'\u001b'}[0m
264+
• boo:
265+
${'\u001b'}[31m❌ INVALID: This output doesn't exist in the action manifest.${'\u001b'}[0m
266+
267+
202268
""".trimIndent()
203269
}
204270
}

0 commit comments

Comments
 (0)