Update to Gradle 8.6 (#245)

- Fix and clean up the pkl-commons-test build script.
- Change tests to read test packages/certs directly from
  the file system instead of packaging and reading them
  from the class path.
- Update expected checksums of some test packages.
- Fix a conflict between Pkl's and Gradle's
  Kotlin libraries in the pkl-gradle project.
- Fix build deprecation warnings.
- Ensure Gradle distribution integrity with `distributionSha256Sum`.
- Manually verify integrity of Gradle wrapper added by this commit.
This commit is contained in:
translatenix
2024-03-15 17:00:23 -07:00
committed by GitHub
parent faa7ac69bb
commit 496e064caf
26 changed files with 220 additions and 189 deletions

View File

@@ -16,23 +16,108 @@ dependencies {
runtimeOnly(projects.pklCerts)
}
/**
* Creates test packages from the `src/test/files/packages` directory.
*
* These packages are used by PackageServer to serve assets when running
* LanguageSnippetTests and PackageResolversTest.
*/
val createTestPackages = tasks.create("createTestPackages")
val createTestPackages by tasks.registering
tasks.test {
dependsOn(createTestPackages)
dependsOn(exportCerts)
}
for (packageDir in file("src/main/files/packages").listFiles()!!) {
if (!packageDir.isDirectory) continue
val destinationDir = file("build/test-packages/${packageDir.name}")
val metadataJson = file("$packageDir/${packageDir.name}.json")
val packageContents = packageDir.resolve("package")
val zipFileName = "${packageDir.name}.zip"
val archiveFile = destinationDir.resolve(zipFileName)
val zipTask = tasks.register("zip-${packageDir.name}", Zip::class) {
destinationDirectory.set(destinationDir)
archiveFileName.set(zipFileName)
from(packageContents)
// required so that checksums are reproducible
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
}
val copyTask = tasks.register("copy-${packageDir.name}", Copy::class) {
dependsOn(zipTask)
from(metadataJson)
into(destinationDir)
val shasumFile = file("$destinationDir/${packageDir.name}.json.sha256")
outputs.file(shasumFile)
filter { line ->
line.replaceFirst("\$computedChecksum", archiveFile.computeChecksum())
}
doLast {
val outputFile = destinationDir.resolve("${packageDir.name}.json")
shasumFile.writeText(outputFile.computeChecksum())
}
}
createTestPackages.configure {
dependsOn(copyTask)
}
}
val keystoreDir = file("build/keystore")
val keystoreName = "localhost.p12"
val certsFileName = "localhost.pem"
val generateKeys by tasks.registering(JavaExec::class) {
val outputFile = file("$keystoreDir/$keystoreName")
outputs.file(outputFile)
mainClass.set("sun.security.tools.keytool.Main")
args = listOf(
"-genkeypair",
"-keyalg", "RSA",
"-alias", "integ_tests",
"-keystore", keystoreName,
"-storepass", "password",
"-dname", "CN=localhost"
)
workingDir = keystoreDir
doFirst {
workingDir.mkdirs()
outputFile.delete()
}
}
val exportCerts by tasks.registering(JavaExec::class) {
val outputFile = file("$keystoreDir/$certsFileName")
dependsOn(generateKeys)
inputs.file("$keystoreDir/$keystoreName")
outputs.file(outputFile)
mainClass.set("sun.security.tools.keytool.Main")
args = listOf(
"-exportcert",
"-alias", "integ_tests",
"-storepass", "password",
"-keystore", keystoreName,
"-rfc",
"-file", certsFileName
)
workingDir = keystoreDir
doFirst {
workingDir.mkdirs()
outputFile.delete()
}
}
fun toHex(hash: ByteArray): String {
val hexDigitTable = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
val builder = StringBuilder(hash.size * 2)
for (b in hash) {
builder.append(hexDigitTable[b.toInt() shr 4 and 0xF])
builder.append(hexDigitTable[b.toInt() and 0xF])
return buildString(hash.size * 2) {
for (b in hash) {
append(hexDigitTable[b.toInt() shr 4 and 0xF])
append(hexDigitTable[b.toInt() and 0xF])
}
}
return builder.toString()
}
fun File.computeChecksum(): String {
@@ -40,92 +125,3 @@ fun File.computeChecksum(): String {
val hash = md.digest(readBytes())
return toHex(hash)
}
tasks.processResources {
dependsOn(createTestPackages)
dependsOn(generateCerts)
}
val mainSourceSet by sourceSets.named("main") {
resources {
srcDir(buildDir.resolve("test-packages/"))
srcDir(buildDir.resolve("keystore/"))
}
}
val sourcesJar = tasks.named("sourcesJar").get()
for (packageDir in file("src/main/files/packages").listFiles()!!) {
if (!packageDir.isDirectory) continue
val destinationDir = buildDir.resolve("test-packages/org/pkl/commons/test/packages/${packageDir.name}")
val metadataJson = packageDir.resolve("${packageDir.name}.json")
val packageContents = packageDir.resolve("package")
val zipFileName = "${packageDir.name}.zip"
val archiveFile = destinationDir.resolve(zipFileName)
tasks.create("zip-${packageDir.name}", Zip::class) {
archiveFileName.set(zipFileName)
from(packageContents)
destinationDirectory.set(destinationDir)
// required so that checksums are reproducible
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
}
val copyTask = tasks.create("copy-${packageDir.name}", Copy::class) {
dependsOn("zip-${packageDir.name}")
from(metadataJson)
into(destinationDir)
val shasumFile = file("$destinationDir/${packageDir.name}.json.sha256")
outputs.file(shasumFile)
doFirst {
expand(mapOf("computedChecksum" to archiveFile.computeChecksum()))
}
doLast {
val outputFile = file("$destinationDir").resolve("${packageDir.name}.json")
shasumFile.writeText(outputFile.computeChecksum())
}
createTestPackages.dependsOn(this)
}
sourcesJar.dependsOn.add(copyTask)
}
val generateKeys by tasks.registering(JavaExec::class) {
val outputFile = file("$buildDir/keystore/localhost.p12")
outputs.file(outputFile)
mainClass.set("sun.security.tools.keytool.Main")
args = listOf(
"-genkeypair",
"-keyalg", "RSA",
"-alias", "integ_tests",
"-keystore", "localhost.p12",
"-storepass", "password",
"-dname", "CN=localhost"
)
workingDir = file("$buildDir/keystore/")
onlyIf { !outputFile.exists() }
doFirst {
workingDir.mkdirs()
}
}
val generateCerts by tasks.registering(JavaExec::class) {
dependsOn("generateKeys")
val outputFile = file("$buildDir/keystore/localhost.pem")
outputs.file(outputFile)
mainClass.set("sun.security.tools.keytool.Main")
args = listOf(
"-exportcert",
"-alias", "integ_tests",
"-storepass", "password",
"-keystore", "localhost.p12",
"-rfc",
"-file", "localhost.pem"
)
workingDir = file("$buildDir/keystore/")
onlyIf { !outputFile.exists() }
doFirst {
workingDir.mkdirs()
}
}

View File

@@ -7,7 +7,7 @@
"fruities": {
"uri": "package://localhost:12110/fruit@1.0.5",
"checksums": {
"sha256": "b4ea243de781feeab7921227591e6584db5d0673340f30fab2ffe8ad5c9f75f5"
"sha256": "abd173e8a25f5b930b0e34269a441e32c9d95e0b0a715bc6eff918f0afd0688e"
}
}
},

View File

@@ -22,6 +22,7 @@ import java.security.KeyStore
import java.util.concurrent.Executors
import javax.net.ssl.KeyManagerFactory
import javax.net.ssl.SSLContext
import kotlin.io.path.inputStream
import kotlin.io.path.isRegularFile
import org.pkl.commons.createParentDirectories
import org.pkl.commons.deleteRecursively
@@ -66,26 +67,20 @@ class PackageServer : AutoCloseable {
// Modified by RequestRewritingClient if testPort is set.
private const val PORT = 12110
// When tests are run via Gradle (i.e. from ./gradlew check), resources are packaged into a jar.
// When run directly in IntelliJ, resources are just directories.
private val packagesDir: Path by lazy {
val uri = PackageServer::class.java.getResource("packages")!!.toURI()
try {
Path.of(uri)
} catch (e: FileSystemNotFoundException) {
FileSystems.newFileSystem(uri, mapOf<String, String>())
Path.of(uri)
}
}
private val packagesDir: Path =
FileTestUtils.rootProjectDir.resolve("pkl-commons-test/build/test-packages")
private val simpleHttpsConfigurator by lazy {
val sslContext =
SSLContext.getInstance("SSL").apply {
val pass = "password".toCharArray()
val keystore = PackageServer::class.java.getResource("/localhost.p12")!!
val ks = KeyStore.getInstance("PKCS12").apply { load(keystore.openStream(), pass) }
val kmf = KeyManagerFactory.getInstance("SunX509").apply { init(ks, pass) }
init(kmf.keyManagers, null, null)
val keystore =
FileTestUtils.rootProjectDir.resolve("pkl-commons-test/build/keystore/localhost.p12")
keystore.inputStream().use { stream ->
val ks = KeyStore.getInstance("PKCS12").apply { load(stream, pass) }
val kmf = KeyManagerFactory.getInstance("SunX509").apply { init(ks, pass) }
init(kmf.keyManagers, null, null)
}
}
val engine = sslContext.createSSLEngine()
object : HttpsConfigurator(sslContext) {