Skip to content

Commit 6087824

Browse files
authored
test: add test for bytecode version of bindings (#2073)
Part of #1699.
1 parent 1c87668 commit 6087824

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed

maven-binding-builder/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ dependencies {
1010
implementation("io.github.oshai:kotlin-logging:7.0.13")
1111

1212
runtimeOnly(projects.githubWorkflowsKt)
13+
14+
testImplementation(projects.testUtils)
1315
}

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/JarBuilding.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
1515
import org.jetbrains.kotlin.config.Services
1616
import java.io.ByteArrayOutputStream
1717
import java.io.PrintStream
18+
import java.nio.file.Files
1819
import java.nio.file.Path
1920
import kotlin.io.path.Path
2021
import kotlin.io.path.createParentDirectories
2122
import kotlin.io.path.createTempDirectory
2223
import kotlin.io.path.div
2324
import kotlin.io.path.writeText
25+
import kotlin.streams.asSequence
2426

2527
internal data class Jars(
2628
val mainJar: () -> ByteArray,
2729
val sourcesJar: () -> ByteArray,
30+
val randomClassFile: () -> ByteArray,
2831
val typingActualSource: TypingActualSource?,
2932
)
3033

@@ -34,9 +37,14 @@ internal fun ActionCoords.buildJars(): Jars? {
3437
if (it.isEmpty()) return null
3538
}
3639

37-
val mainJar by lazy {
40+
fun prepareJarContents(): Pair<Path, Path> {
3841
val (sourceFilePaths, compilationInputDir) = binding.prepareDirectoryWithSources()
3942
val pathWithJarContents = compileBinding(sourceFilePaths = sourceFilePaths)
43+
return Pair(compilationInputDir, pathWithJarContents)
44+
}
45+
46+
val mainJar by lazy {
47+
val (compilationInputDir, pathWithJarContents) = prepareJarContents()
4048
val mainJarByteArrayOutputStream = ByteArrayOutputStream()
4149
mainJarByteArrayOutputStream.createZipFile(pathWithJarContents)
4250
pathWithJarContents.toFile().deleteRecursively()
@@ -52,9 +60,22 @@ internal fun ActionCoords.buildJars(): Jars? {
5260
sourcesJarByteArrayOutputStream.toByteArray()
5361
}
5462

63+
// Used for testing
64+
val randomClassFile by lazy {
65+
val (_, pathWithJarContents) = prepareJarContents()
66+
val firstClassFile =
67+
Files
68+
.walk(pathWithJarContents)
69+
.asSequence()
70+
.filter { Files.isRegularFile(it) && it.fileName.toString().endsWith(".class") }
71+
.first()
72+
firstClassFile.toFile().readBytes()
73+
}
74+
5575
return Jars(
5676
mainJar = { mainJar },
5777
sourcesJar = { sourcesJar },
78+
randomClassFile = { randomClassFile },
5879
typingActualSource = binding.firstNotNullOfOrNull { it.typingActualSource },
5980
)
6081
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.typesafegithub.workflows.mavenbinding
2+
3+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
4+
import io.github.typesafegithub.workflows.testutils.JdkVersionToBytecodeVersion.JDK_8
5+
import io.github.typesafegithub.workflows.testutils.shouldHaveBytecodeVersion
6+
import io.kotest.core.spec.style.FunSpec
7+
8+
class JavaBytecodeVersionTest :
9+
FunSpec(
10+
{
11+
test("bindings are built with desired Java bytecode version") {
12+
val actionCoords = ActionCoords("actions", "setup-java", "v3")
13+
val jars = actionCoords.buildJars()!!
14+
jars.randomClassFile() shouldHaveBytecodeVersion JDK_8
15+
}
16+
},
17+
)

test-utils/src/main/kotlin/io/github/typesafegithub/workflows/testutils/JavaBytecodeUtils.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package io.github.typesafegithub.workflows.testutils
22

33
import io.kotest.matchers.shouldBe
44
import io.kotest.mpp.qualifiedNameOrNull
5+
import java.io.ByteArrayInputStream
56
import java.io.DataInputStream
7+
import java.io.InputStream
68
import kotlin.reflect.KClass
79

810
infix fun KClass<*>.shouldHaveBytecodeVersion(expectedVersion: String) {
@@ -11,12 +13,19 @@ infix fun KClass<*>.shouldHaveBytecodeVersion(expectedVersion: String) {
1113
this.qualifiedNameOrNull()!!.replace(".", "/") + ".class",
1214
)!!
1315
.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
16+
inputStream shouldHaveBytecodeVersion expectedVersion
2117
}
2218
}
19+
20+
infix fun ByteArray.shouldHaveBytecodeVersion(expectedVersion: String) =
21+
ByteArrayInputStream(this) shouldHaveBytecodeVersion expectedVersion
22+
23+
private infix fun InputStream.shouldHaveBytecodeVersion(expectedVersion: String) {
24+
val dataInputStream = DataInputStream(this)
25+
require(dataInputStream.readInt() == 0xCAFEBABE.toInt()) { "Invalid class header" }
26+
val minor = dataInputStream.readUnsignedShort()
27+
val major = dataInputStream.readUnsignedShort()
28+
29+
val actualVersion = "$major.$minor"
30+
actualVersion shouldBe expectedVersion
31+
}

test-utils/src/main/kotlin/io/github/typesafegithub/workflows/testutils/JdkVersionToBytecodeVersion.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ package io.github.typesafegithub.workflows.testutils
44
* See https://javaalmanac.io/bytecode/versions/
55
*/
66
object JdkVersionToBytecodeVersion {
7+
const val JDK_8 = "52.0"
78
const val JDK_11 = "55.0"
89
}

0 commit comments

Comments
 (0)