Skip to content

Commit 739e0b8

Browse files
Vampirekrzema12
andauthored
test: verify action verification logic consistency with schema (#261)
Part of #253. --------- Co-authored-by: Piotr Krzemiński <3110813+krzema12@users.noreply.github.com>
1 parent 93aba7b commit 739e0b8

File tree

139 files changed

+2019
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+2019
-52
lines changed

src/jvmTest/kotlin/it/krzeminski/githubactionstyping/JsonSchemaValidatorSchemaValidationTest.kt

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package it.krzeminski.githubactionstyping
22

33
import io.github.optimumcode.json.schema.JsonSchema
4-
import io.kotest.core.spec.style.FunSpec
5-
import io.kotest.datatest.withData
64
import io.kotest.matchers.Matcher
75
import io.kotest.matchers.MatcherResult
8-
import io.kotest.matchers.file.shouldBeADirectory
9-
import io.kotest.matchers.file.shouldNotBeEmptyDirectory
106
import io.kotest.matchers.should
117
import io.kotest.matchers.shouldNot
128
import io.kotest.mpp.sysprop
@@ -19,66 +15,31 @@ import kotlinx.serialization.json.JsonPrimitive
1915
import java.io.File
2016

2117
private val schemaFile = File(sysprop("schemaFile")!!)
22-
private val catalogDir = File(sysprop("catalogDir")!!)
23-
private val externalDir = File(sysprop("externalDir")!!)
24-
private val goodDir = File(sysprop("goodDir")!!)
25-
private val badDir = File(sysprop("badDir")!!)
2618

2719
private lateinit var schema: JsonSchema
2820

2921
/**
3022
* Validate good and bad typings against the Json Schema Validator schema validator.
3123
*/
32-
class JsonSchemaValidatorSchemaValidationTest : FunSpec({
33-
beforeSpec {
34-
catalogDir.shouldBeADirectory()
35-
catalogDir.shouldNotBeEmptyDirectory()
36-
37-
schema = schemaFile
38-
.inputStream()
39-
.use {
40-
JsonSchema.fromDefinition(it.reader().readText())
41-
}
42-
}
43-
44-
context("catalog typings") {
45-
withData(
46-
nameFn = { it.name },
47-
catalogDir.listFiles()!!.asSequence()
48-
) {
49-
it.shouldBeValid()
50-
}
51-
}
52-
53-
if (externalDir.isDirectory) {
54-
context("external typings") {
55-
withData(
56-
nameFn = { it.name },
57-
externalDir.listFiles()!!.asSequence()
58-
) {
59-
it.shouldBeValid()
60-
}
24+
class JsonSchemaValidatorSchemaValidationTest : UseCaseTest() {
25+
init {
26+
beforeSpec {
27+
schema = schemaFile
28+
.inputStream()
29+
.use {
30+
JsonSchema.fromDefinition(it.reader().readText())
31+
}
6132
}
6233
}
6334

64-
context("valid typings") {
65-
withData(
66-
nameFn = { it.name },
67-
goodDir.listFiles()!!.asSequence()
68-
) {
69-
it.shouldBeValid()
70-
}
35+
override suspend fun testValid(typing: File) {
36+
typing.shouldBeValid()
7137
}
7238

73-
context("invalid typings") {
74-
withData(
75-
nameFn = { it.name },
76-
badDir.listFiles()!!.asSequence()
77-
) {
78-
it.shouldNotBeValid()
79-
}
39+
override suspend fun testInvalid(typing: File) {
40+
typing.shouldNotBeValid()
8041
}
81-
})
42+
}
8243

8344
private fun beValid(): Matcher<File> {
8445
return Matcher { dataFile ->
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package it.krzeminski.githubactionstyping
2+
3+
import io.kotest.assertions.assertSoftly
4+
import io.kotest.assertions.withClue
5+
import io.kotest.matchers.should
6+
import io.kotest.matchers.shouldBe
7+
import io.kotest.matchers.types.beOfType
8+
import it.krzeminski.githubactionstyping.parsing.parseTypesManifest
9+
import it.krzeminski.githubactionstyping.reporting.toPlaintextReport
10+
import it.krzeminski.githubactionstyping.validation.ItemValidationResult
11+
import it.krzeminski.githubactionstyping.validation.validate
12+
import java.io.File
13+
14+
/**
15+
* Validate that the action validation logic agrees with the Json Schema.
16+
*/
17+
class LogicConsistencyTest : UseCaseTest() {
18+
override suspend fun testValid(typing: File) {
19+
val validationResult = parseTypesManifest(typing.readText()).validate(typing.toPath().fileName)
20+
withClue(validationResult.toPlaintextReport()) {
21+
validationResult.overallResult shouldBe ItemValidationResult.Valid
22+
}
23+
}
24+
25+
override suspend fun testInvalid(typing: File) {
26+
val typesManifest = typing.readText()
27+
val expectedValidationError = typesManifest
28+
.split("\n# Expected validation error\n")[1]
29+
.trimMargin("#")
30+
.trim()
31+
32+
val validationResult = parseTypesManifest(typesManifest).validate(typing.toPath().fileName)
33+
assertSoftly {
34+
validationResult.overallResult should beOfType<ItemValidationResult.Invalid>()
35+
validationResult
36+
.toPlaintextReport()
37+
.trim()
38+
.replace("\u001B", "\\x1B")
39+
.shouldBe(expectedValidationError)
40+
}
41+
}
42+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package it.krzeminski.githubactionstyping
2+
3+
import io.kotest.core.spec.style.FunSpec
4+
import io.kotest.datatest.withData
5+
import io.kotest.matchers.file.shouldBeADirectory
6+
import io.kotest.matchers.file.shouldNotBeEmptyDirectory
7+
import io.kotest.mpp.sysprop
8+
import java.io.File
9+
10+
private val catalogDir = File(sysprop("catalogDir")!!)
11+
private val externalDir = File(sysprop("externalDir")!!)
12+
private val goodDir = File(sysprop("goodDir")!!)
13+
private val badDir = File(sysprop("badDir")!!)
14+
15+
abstract class UseCaseTest : FunSpec({
16+
beforeSpec {
17+
catalogDir.shouldBeADirectory()
18+
catalogDir.shouldNotBeEmptyDirectory()
19+
}
20+
}) {
21+
init {
22+
context("catalog typings") {
23+
withData(
24+
nameFn = { it.name },
25+
ts = catalogDir.listFiles()!!.asSequence(),
26+
test = { testValid(it) },
27+
)
28+
}
29+
30+
if (externalDir.isDirectory) {
31+
context("external typings") {
32+
withData(
33+
nameFn = { it.name },
34+
ts = externalDir.listFiles()!!.asSequence(),
35+
test = { testValid(it) },
36+
)
37+
}
38+
}
39+
40+
context("valid typings") {
41+
withData(
42+
nameFn = { it.name },
43+
ts = goodDir.listFiles()!!.asSequence(),
44+
test = { testValid(it) },
45+
)
46+
}
47+
48+
context("invalid typings") {
49+
withData(
50+
nameFn = { it.name },
51+
ts = badDir.listFiles()!!.asSequence(),
52+
test = { testInvalid(it) },
53+
)
54+
}
55+
}
56+
57+
abstract suspend fun testValid(typing: File)
58+
59+
abstract suspend fun testInvalid(typing: File)
60+
}

src/test/resources/bad-typings/inputs_boolean_list_item_with_allowed_values.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,17 @@ inputs:
99
allowed-values:
1010
- read
1111
- write
12+
13+
# Expected validation error
14+
#
15+
#For action with manifest at 'inputs_boolean_list_item_with_allowed_values.yml':
16+
#Result:
17+
#\x1B[31m❌ INVALID: Some typing is invalid.\x1B[0m
18+
#
19+
#Inputs:
20+
#• list-of-boolean:
21+
# \x1B[31m❌ INVALID: List item type: 'allowed-values' is not allowed for this type.\x1B[0m
22+
#
23+
#Outputs:
24+
#None.
25+
#

src/test/resources/bad-typings/inputs_boolean_list_item_with_list_item.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ inputs:
88
type: boolean
99
list-item:
1010
type: integer
11+
12+
# Expected validation error
13+
#
14+
#For action with manifest at 'inputs_boolean_list_item_with_list_item.yml':
15+
#Result:
16+
#\x1B[31m❌ INVALID: Some typing is invalid.\x1B[0m
17+
#
18+
#Inputs:
19+
#• list-of-boolean:
20+
# \x1B[31m❌ INVALID: List item type: 'list-item' is not allowed for this type.\x1B[0m
21+
#
22+
#Outputs:
23+
#None.
24+
#

src/test/resources/bad-typings/inputs_boolean_list_item_with_name.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ inputs:
77
list-item:
88
type: boolean
99
name: AllowedValues
10+
11+
# Expected validation error
12+
#
13+
#For action with manifest at 'inputs_boolean_list_item_with_name.yml':
14+
#Result:
15+
#\x1B[31m❌ INVALID: Some typing is invalid.\x1B[0m
16+
#
17+
#Inputs:
18+
#• list-of-boolean:
19+
# \x1B[31m❌ INVALID: List item type: 'name' is not allowed for this type.\x1B[0m
20+
#
21+
#Outputs:
22+
#None.
23+
#

src/test/resources/bad-typings/inputs_boolean_list_item_with_named_values.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ inputs:
88
type: boolean
99
named-values:
1010
infinite: 0
11+
12+
# Expected validation error
13+
#
14+
#For action with manifest at 'inputs_boolean_list_item_with_named_values.yml':
15+
#Result:
16+
#\x1B[31m❌ INVALID: Some typing is invalid.\x1B[0m
17+
#
18+
#Inputs:
19+
#• list-of-boolean:
20+
# \x1B[31m❌ INVALID: List item type: 'named-values' are currently supported only for integers.\x1B[0m
21+
#
22+
#Outputs:
23+
#None.
24+
#

src/test/resources/bad-typings/inputs_boolean_list_item_with_separator.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ inputs:
77
list-item:
88
type: boolean
99
separator: ','
10+
11+
# Expected validation error
12+
#
13+
#For action with manifest at 'inputs_boolean_list_item_with_separator.yml':
14+
#Result:
15+
#\x1B[31m❌ INVALID: Some typing is invalid.\x1B[0m
16+
#
17+
#Inputs:
18+
#• list-of-boolean:
19+
# \x1B[31m❌ INVALID: List item type: 'separator' is not allowed for this type.\x1B[0m
20+
#
21+
#Outputs:
22+
#None.
23+
#

src/test/resources/bad-typings/inputs_boolean_with_allowed_values.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,17 @@ inputs:
66
allowed-values:
77
- read
88
- write
9+
10+
# Expected validation error
11+
#
12+
#For action with manifest at 'inputs_boolean_with_allowed_values.yml':
13+
#Result:
14+
#\x1B[31m❌ INVALID: Some typing is invalid.\x1B[0m
15+
#
16+
#Inputs:
17+
#• verbose:
18+
# \x1B[31m❌ INVALID: 'allowed-values' is not allowed for this type.\x1B[0m
19+
#
20+
#Outputs:
21+
#None.
22+
#

src/test/resources/bad-typings/inputs_boolean_with_list_item.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ inputs:
55
type: boolean
66
list-item:
77
type: boolean
8+
9+
# Expected validation error
10+
#
11+
#For action with manifest at 'inputs_boolean_with_list_item.yml':
12+
#Result:
13+
#\x1B[31m❌ INVALID: Some typing is invalid.\x1B[0m
14+
#
15+
#Inputs:
16+
#• verbose:
17+
# \x1B[31m❌ INVALID: 'list-item' is not allowed for this type.\x1B[0m
18+
#
19+
#Outputs:
20+
#None.
21+
#

0 commit comments

Comments
 (0)