Skip to content

Commit 007744d

Browse files
authored
test(abg): add unit tests for metadata reading (#2162)
Part of #2160. It's a preliminary change before making some changes to the module under test.
1 parent e3880bf commit 007744d

File tree

1 file changed

+275
-0
lines changed
  • action-binding-generator/src/test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/metadata

1 file changed

+275
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
package io.github.typesafegithub.workflows.actionbindinggenerator.metadata
2+
3+
import com.charleskorn.kaml.MissingRequiredPropertyException
4+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
5+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.CommitHash
6+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestForVersion
7+
import io.kotest.assertions.throwables.shouldThrow
8+
import io.kotest.core.spec.style.FunSpec
9+
import io.kotest.matchers.nulls.shouldBeNull
10+
import io.kotest.matchers.shouldBe
11+
import kotlinx.io.IOException
12+
import java.net.URI
13+
14+
class MetadataReadingTest :
15+
FunSpec({
16+
val coords = ActionCoords(owner = "test-owner", name = "test-name", version = "v1")
17+
val metadataYaml =
18+
"""
19+
name: Some cool action
20+
description: Really cool action!
21+
inputs:
22+
input-1:
23+
description: Some input
24+
default: false
25+
required: true
26+
deprecationMessage: This input has been deprecated
27+
outputs:
28+
output-1:
29+
description: Some output
30+
""".trimIndent()
31+
val expectedMetadata =
32+
Metadata(
33+
name = "Some cool action",
34+
description = "Really cool action!",
35+
inputs =
36+
mapOf(
37+
"input-1" to
38+
Input(
39+
description = "Some input",
40+
default = "false",
41+
required = true,
42+
deprecationMessage = "This input has been deprecated",
43+
),
44+
),
45+
outputs =
46+
mapOf(
47+
"output-1" to
48+
Output(
49+
description = "Some output",
50+
),
51+
),
52+
)
53+
54+
test("success, commit hash, .yml exists") {
55+
// Given
56+
var requestedUris = mutableListOf<URI>()
57+
58+
// When
59+
val metadata =
60+
coords.fetchMetadata(
61+
metadataRevision = CommitHash("commit-hash"),
62+
fetchUri = {
63+
requestedUris.add(it)
64+
65+
if (it.toURL().toString().endsWith(".yml")) {
66+
metadataYaml
67+
} else {
68+
throw IOException("Failed to fetch the file")
69+
}
70+
},
71+
)
72+
73+
// Then
74+
metadata shouldBe expectedMetadata
75+
requestedUris shouldBe
76+
listOf(
77+
URI("https://raw.githubusercontent.com/test-owner/test-name/commit-hash/action.yml"),
78+
)
79+
}
80+
81+
test("success, commit hash, .yaml exists") {
82+
// Given
83+
var requestedUris = mutableListOf<URI>()
84+
85+
// When
86+
val metadata =
87+
coords.fetchMetadata(
88+
metadataRevision = CommitHash("commit-hash"),
89+
fetchUri = {
90+
requestedUris.add(it)
91+
92+
if (it.toURL().toString().endsWith(".yaml")) {
93+
metadataYaml
94+
} else {
95+
throw IOException("Failed to fetch the file")
96+
}
97+
},
98+
)
99+
100+
// Then
101+
metadata shouldBe expectedMetadata
102+
requestedUris shouldBe
103+
listOf(
104+
URI("https://raw.githubusercontent.com/test-owner/test-name/commit-hash/action.yml"),
105+
URI("https://raw.githubusercontent.com/test-owner/test-name/commit-hash/action.yaml"),
106+
)
107+
}
108+
109+
test("success, commit hash, both .yml and .yaml exist") {
110+
// Given
111+
var requestedUris = mutableListOf<URI>()
112+
113+
// When
114+
val metadata =
115+
coords.fetchMetadata(
116+
metadataRevision = CommitHash("commit-hash"),
117+
fetchUri = {
118+
requestedUris.add(it)
119+
metadataYaml
120+
},
121+
)
122+
123+
// Then
124+
metadata shouldBe expectedMetadata
125+
requestedUris shouldBe
126+
listOf(
127+
URI("https://raw.githubusercontent.com/test-owner/test-name/commit-hash/action.yml"),
128+
)
129+
}
130+
131+
test("success, newest for version, .yml exists") {
132+
// Given
133+
var requestedUris = mutableListOf<URI>()
134+
135+
// When
136+
val metadata =
137+
coords.fetchMetadata(
138+
metadataRevision = NewestForVersion,
139+
fetchUri = {
140+
requestedUris.add(it)
141+
142+
if (it.toURL().toString().endsWith(".yml")) {
143+
metadataYaml
144+
} else {
145+
throw IOException("Failed to fetch the file")
146+
}
147+
},
148+
)
149+
150+
// Then
151+
metadata shouldBe expectedMetadata
152+
requestedUris shouldBe
153+
listOf(
154+
URI("https://raw.githubusercontent.com/test-owner/test-name/v1/action.yml"),
155+
)
156+
}
157+
158+
test("success, newest for version, .yaml exists") {
159+
// Given
160+
var requestedUris = mutableListOf<URI>()
161+
162+
// When
163+
val metadata =
164+
coords.fetchMetadata(
165+
metadataRevision = NewestForVersion,
166+
fetchUri = {
167+
requestedUris.add(it)
168+
169+
if (it.toURL().toString().endsWith(".yaml")) {
170+
metadataYaml
171+
} else {
172+
throw IOException("Failed to fetch the file")
173+
}
174+
},
175+
)
176+
177+
// Then
178+
metadata shouldBe expectedMetadata
179+
requestedUris shouldBe
180+
listOf(
181+
URI("https://raw.githubusercontent.com/test-owner/test-name/v1/action.yml"),
182+
URI("https://raw.githubusercontent.com/test-owner/test-name/v1/action.yaml"),
183+
)
184+
}
185+
186+
test("success, newest for version, both .yml and .yaml exist") {
187+
// Given
188+
var requestedUris = mutableListOf<URI>()
189+
190+
// When
191+
val metadata =
192+
coords.fetchMetadata(
193+
metadataRevision = NewestForVersion,
194+
fetchUri = {
195+
requestedUris.add(it)
196+
metadataYaml
197+
},
198+
)
199+
200+
// Then
201+
metadata shouldBe expectedMetadata
202+
requestedUris shouldBe
203+
listOf(
204+
URI("https://raw.githubusercontent.com/test-owner/test-name/v1/action.yml"),
205+
)
206+
}
207+
208+
test("unknown field") {
209+
// Given
210+
val metadataYaml =
211+
"""
212+
name: Some cool action
213+
description: Really cool action!
214+
unknown: field
215+
inputs:
216+
input-1:
217+
description: Some input
218+
default: false
219+
required: true
220+
deprecationMessage: This input has been deprecated
221+
outputs:
222+
output-1:
223+
description: Some output
224+
""".trimIndent()
225+
226+
// When
227+
val metadata =
228+
coords.fetchMetadata(
229+
metadataRevision = CommitHash("commit-hash"),
230+
fetchUri = { metadataYaml },
231+
)
232+
233+
// Then
234+
metadata shouldBe expectedMetadata
235+
}
236+
237+
test("missing required field") {
238+
// Given
239+
val metadataYaml =
240+
"""
241+
description: Really cool action!
242+
unknown: field
243+
inputs:
244+
input-1:
245+
description: Some input
246+
default: false
247+
required: true
248+
deprecationMessage: This input has been deprecated
249+
outputs:
250+
output-1:
251+
description: Some output
252+
""".trimIndent()
253+
254+
// Then
255+
shouldThrow<MissingRequiredPropertyException> {
256+
// When
257+
coords.fetchMetadata(
258+
metadataRevision = CommitHash("commit-hash"),
259+
fetchUri = { metadataYaml },
260+
)
261+
}
262+
}
263+
264+
test("all requests fail") {
265+
// When
266+
val metadata =
267+
coords.fetchMetadata(
268+
metadataRevision = CommitHash("commit-hash"),
269+
fetchUri = { throw IOException("Failed to fetch the file") },
270+
)
271+
272+
// Then
273+
metadata.shouldBeNull()
274+
}
275+
})

0 commit comments

Comments
 (0)