mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Better zip file test.
This commit is contained in:
parent
469419fe2e
commit
f788761e4c
1 changed files with 56 additions and 30 deletions
|
@ -2,16 +2,19 @@ package com.beust.kobalt
|
||||||
|
|
||||||
import com.beust.kobalt.misc.KFiles
|
import com.beust.kobalt.misc.KFiles
|
||||||
import com.beust.kobalt.misc.kobaltLog
|
import com.beust.kobalt.misc.kobaltLog
|
||||||
import org.testng.Assert
|
|
||||||
import org.testng.annotations.Test
|
import org.testng.annotations.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.io.FileReader
|
import java.io.FileReader
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import java.util.jar.JarEntry
|
||||||
import java.util.jar.JarFile
|
import java.util.jar.JarFile
|
||||||
import java.util.jar.JarInputStream
|
import java.util.jar.JarInputStream
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure the distribution zip file contains all the right files and no bad files.
|
||||||
|
*/
|
||||||
class VerifyKobaltZipTest : KobaltTest() {
|
class VerifyKobaltZipTest : KobaltTest() {
|
||||||
@Test
|
@Test
|
||||||
fun verifySourceJarFile() {
|
fun verifySourceJarFile() {
|
||||||
|
@ -43,7 +46,7 @@ class VerifyKobaltZipTest : KobaltTest() {
|
||||||
} else if (entry.name.endsWith("kobalt-wrapper.jar")) {
|
} else if (entry.name.endsWith("kobalt-wrapper.jar")) {
|
||||||
val ins = zipFile.getInputStream(entry)
|
val ins = zipFile.getInputStream(entry)
|
||||||
foundWrapperJar = true
|
foundWrapperJar = true
|
||||||
assertExistsInJar(jarContents(JarInputStream(ins)), "kobalt.properties")
|
assertExistence(ins, listOf("kobalt.properties"))
|
||||||
}
|
}
|
||||||
entry = stream.nextEntry
|
entry = stream.nextEntry
|
||||||
}
|
}
|
||||||
|
@ -72,31 +75,35 @@ class VerifyKobaltZipTest : KobaltTest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun verifyMainJarFile(ins: InputStream) {
|
private fun verifyMainJarFile(ins: InputStream) {
|
||||||
JarInputStream(ins).let { jar ->
|
assertExistence(ins,
|
||||||
val setContent = jarContents(jar)
|
listOf("com/beust/kobalt/MainKt.class",
|
||||||
assertExistsInJar(setContent, "com/beust/kobalt/MainKt.class",
|
|
||||||
"templates/kobaltPlugin/kobaltPlugin.jar", "com/beust/kobalt/Args.class",
|
"templates/kobaltPlugin/kobaltPlugin.jar", "com/beust/kobalt/Args.class",
|
||||||
"com/beust/kobalt/wrapper/Main.class")
|
"com/beust/kobalt/wrapper/Main.class"),
|
||||||
assertDoesNotExistInJar(setContent, "BuildKt.class")
|
listOf("BuildKt.class"))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun assertExistsInJar(content: Set<String>, vararg fileNames: String)
|
private fun assertExistence(ins: InputStream,
|
||||||
= assertExistence(content, true, *fileNames)
|
included: List<String>,
|
||||||
|
excluded: List<String> = emptyList(),
|
||||||
|
toName: (JarEntry) -> String = JarEntry::toString) {
|
||||||
|
val seq = toSequence(ins)
|
||||||
|
val foundItems = hashSetOf<String>()
|
||||||
|
seq.forEach { entry ->
|
||||||
|
val entryName = toName(entry)
|
||||||
|
|
||||||
private fun assertDoesNotExistInJar(content: Set<String>, vararg fileNames: String)
|
if (included.contains(entryName)) {
|
||||||
= assertExistence(content, false, *fileNames)
|
foundItems.add(entryName)
|
||||||
|
|
||||||
private fun assertExistence(content: Set<String>, verifyExistence: Boolean, vararg fileNames: String) {
|
|
||||||
with(content) {
|
|
||||||
fileNames.forEach { fileName ->
|
|
||||||
if (verifyExistence) {
|
|
||||||
Assert.assertTrue(contains(fileName), "Couldn't find $fileName")
|
|
||||||
} else {
|
|
||||||
val exists = content.contains(fileName)
|
|
||||||
Assert.assertFalse(exists, "The jar file should not contain $fileName")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (excluded.any { entryName.contains(it) }) {
|
||||||
|
throw AssertionError(entryName + " should not be in the jar file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (foundItems != included.toSet()) {
|
||||||
|
val missing = arrayListOf<String>().apply { addAll(included) }
|
||||||
|
missing.removeAll(foundItems)
|
||||||
|
throw AssertionError("Didn't find a few items: " + missing)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,19 +111,38 @@ class VerifyKobaltZipTest : KobaltTest() {
|
||||||
val sourceJarPath = KFiles.joinDir("kobaltBuild", "libs", jarName)
|
val sourceJarPath = KFiles.joinDir("kobaltBuild", "libs", jarName)
|
||||||
val file = File(sourceJarPath)
|
val file = File(sourceJarPath)
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
assertExistsInJar(jarContents(JarInputStream(FileInputStream(file))), *fileNames)
|
assertExistence(FileInputStream(file), arrayListOf<String>().apply { addAll(fileNames) })
|
||||||
} else {
|
} else {
|
||||||
kobaltLog(1, "Couldn't find $file, skipping test")
|
kobaltLog(1, "Couldn't find $file, skipping test")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun jarContents(stream: JarInputStream) : Set<String> {
|
private fun toSequence(ins: InputStream) = Sequence<JarEntry> { JarInputStreamIterator(JarInputStream(ins)) }
|
||||||
val result = hashSetOf<String>()
|
}
|
||||||
var entry = stream.nextEntry
|
|
||||||
while (entry != null) {
|
/**
|
||||||
result.add(entry.name)
|
* I don't want to hold the whole content of the jar file in memory to run tests on its content,
|
||||||
entry = stream.nextEntry
|
* so this iterator allows me to create a sequence out of it, so each entry in the jar file can
|
||||||
|
* be verified lazily
|
||||||
|
*/
|
||||||
|
class JarInputStreamIterator(val ins: JarInputStream) : Iterator<JarEntry> {
|
||||||
|
var next: JarEntry? = null
|
||||||
|
|
||||||
|
override fun next(): JarEntry {
|
||||||
|
if (next != null) {
|
||||||
|
next?.let {
|
||||||
|
val result = it
|
||||||
|
next = null
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return ins.nextJarEntry
|
||||||
}
|
}
|
||||||
return result
|
throw IllegalArgumentException("Should never happen")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hasNext(): Boolean {
|
||||||
|
if (next == null) next = ins.nextJarEntry
|
||||||
|
return next != null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue