Skip to content

Commit 1c87668

Browse files
authored
test: refactor and extract util for asserting on bytecode version (#2072)
Part of #1699.
1 parent 3c34f14 commit 1c87668

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

github-workflows-kt/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
// Needed to use the right version of the compiler for the libraries that depend on it.
3232
testImplementation(kotlin("compiler"))
3333
testImplementation(kotlin("reflect"))
34+
testImplementation(projects.testUtils)
3435

3536
// GitHub action bindings
3637
testImplementation("actions:checkout:v4")
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
package io.github.typesafegithub.workflows
22

33
import io.github.typesafegithub.workflows.domain.Workflow
4+
import io.github.typesafegithub.workflows.testutils.JdkVersionToBytecodeVersion.JDK_11
5+
import io.github.typesafegithub.workflows.testutils.shouldHaveBytecodeVersion
46
import io.kotest.core.spec.style.FunSpec
5-
import io.kotest.matchers.shouldBe
6-
import java.io.DataInputStream
77

88
class JavaBytecodeVersionTest :
99
FunSpec(
1010
{
1111
test("library is built with desired Java bytecode version") {
12-
Workflow::class.java.classLoader
13-
.getResourceAsStream(
14-
"io/github/typesafegithub/workflows/domain/Workflow.class",
15-
)!!
16-
.use { inputStream ->
17-
val dataInputStream = DataInputStream(inputStream)
18-
require(dataInputStream.readInt() == 0xCAFEBABE.toInt()) { "Invalid class header" }
19-
val minor = dataInputStream.readUnsignedShort()
20-
val major = dataInputStream.readUnsignedShort()
21-
22-
// Corresponds to JDK 11
23-
// See https://javaalmanac.io/bytecode/versions/
24-
major shouldBe 55
25-
minor shouldBe 0
26-
}
12+
Workflow::class shouldHaveBytecodeVersion JDK_11
2713
}
2814
},
2915
)

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ include(
1010
"jit-binding-server",
1111
"shared-internal",
1212
"code-generator",
13+
"test-utils",
1314
)
1415

1516
plugins {

test-utils/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins {
2+
buildsrc.convention.`kotlin-jvm`
3+
}
4+
5+
dependencies {
6+
implementation(platform("io.kotest:kotest-bom:5.9.1"))
7+
implementation("io.kotest:kotest-assertions-core")
8+
implementation("io.kotest:kotest-common")
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.typesafegithub.workflows.testutils
2+
3+
import io.kotest.matchers.shouldBe
4+
import io.kotest.mpp.qualifiedNameOrNull
5+
import java.io.DataInputStream
6+
import kotlin.reflect.KClass
7+
8+
infix fun KClass<*>.shouldHaveBytecodeVersion(expectedVersion: String) {
9+
this::class.java.classLoader
10+
.getResourceAsStream(
11+
this.qualifiedNameOrNull()!!.replace(".", "/") + ".class",
12+
)!!
13+
.use { inputStream ->
14+
val dataInputStream = DataInputStream(inputStream)
15+
require(dataInputStream.readInt() == 0xCAFEBABE.toInt()) { "Invalid class header" }
16+
val minor = dataInputStream.readUnsignedShort()
17+
val major = dataInputStream.readUnsignedShort()
18+
19+
val actualVersion = "$major.$minor"
20+
actualVersion shouldBe expectedVersion
21+
}
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.typesafegithub.workflows.testutils
2+
3+
/**
4+
* See https://javaalmanac.io/bytecode/versions/
5+
*/
6+
object JdkVersionToBytecodeVersion {
7+
const val JDK_11 = "55.0"
8+
}

0 commit comments

Comments
 (0)