refactor tests: introduce 'zipFile' method which allows tests to specify content of zip archives via Kotlin DSL

...and use it in some tests to generate zip files instead of using 'directoryContent' method.

GitOrigin-RevId: 7f917a46de1074b95d88ff20662848a7bfa7fbdc
This commit is contained in:
nik
2019-05-15 17:07:05 +03:00
committed by intellij-monorepo-bot
parent 0041cf1220
commit db1099ec60
5 changed files with 64 additions and 30 deletions
@@ -17,31 +17,29 @@ package com.intellij.openapi.roots.ui.configuration.projectRoot
import com.intellij.openapi.vfs.JarFileSystem
import com.intellij.testFramework.LightPlatformTestCase
import com.intellij.util.io.directoryContent
import com.intellij.util.io.generateInVirtualTempDir
import com.intellij.util.io.zipFile
/**
* @author nik
*/
class ConvertToRepositoryLibraryActionTest : LightPlatformTestCase() {
fun `test detect single coordinates`() {
val file = directoryContent {
zip("library.jar") {
dir("META-INF") {
dir("maven") {
dir("myGroupId") {
dir("myArtifactId") {
file("pom.properties", """
val file = zipFile {
dir("META-INF") {
dir("maven") {
dir("myGroupId") {
dir("myArtifactId") {
file("pom.properties", """
|version=1.0
|groupId=myGroupId
|artifactId=myArtifactId
""".trimMargin())
}
}
}
}
}
}.generateInVirtualTempDir().findChild("library.jar")!!
}.generateInVirtualTempDir()
val jarRoot = JarFileSystem.getInstance().getJarRootForLocalFile(file)!!
val coordinates = assertOneElement(ConvertToRepositoryLibraryActionBase.detectMavenCoordinates(arrayOf(jarRoot)))
assertEquals("myGroupId", coordinates.groupId)
@@ -50,30 +48,28 @@ class ConvertToRepositoryLibraryActionTest : LightPlatformTestCase() {
}
fun `test detect multiple coordinates`() {
val file = directoryContent {
zip("library2.jar") {
dir("META-INF") {
dir("maven") {
dir("myGroupId") {
dir("myArtifactId1") {
file("pom.properties", """
val file = zipFile {
dir("META-INF") {
dir("maven") {
dir("myGroupId") {
dir("myArtifactId1") {
file("pom.properties", """
|version=1.0
|groupId=myGroupId
|artifactId=myArtifactId1
""".trimMargin())
}
dir("myArtifactId2") {
file("pom.properties", """
}
dir("myArtifactId2") {
file("pom.properties", """
|version=1.0
|groupId=myGroupId
|artifactId=myArtifactId2
""".trimMargin())
}
}
}
}
}
}.generateInVirtualTempDir().findChild("library2.jar")!!
}.generateInVirtualTempDir()
val jarRoot = JarFileSystem.getInstance().getJarRootForLocalFile(file)!!
val (coordinates1, coordinates2) = ConvertToRepositoryLibraryActionBase.detectMavenCoordinates(arrayOf(jarRoot)).sortedBy { it.artifactId }
assertEquals("myGroupId", coordinates1.groupId)
@@ -19,6 +19,7 @@ import com.intellij.openapi.application.ex.PathManagerEx
import com.intellij.openapi.util.io.FileUtil
import com.intellij.util.PathUtil
import com.intellij.util.io.directoryContent
import com.intellij.util.io.zipFile
import org.jetbrains.jps.builders.CompileScopeTestBuilder
import org.jetbrains.jps.incremental.artifacts.LayoutElementTestUtil.archive
import org.jetbrains.jps.incremental.artifacts.LayoutElementTestUtil.root
@@ -328,14 +329,12 @@ class ArtifactBuilderTest : ArtifactBuilderTestCase() {
}
private fun createXJarFile(): String {
val zipDir = directoryContent {
zip("x.jar") {
dir("dir") {
file("file.txt", "text")
}
val zipFile = zipFile {
dir("dir") {
file("file.txt", "text")
}
}.generateInTempDir()
return FileUtil.toSystemIndependentName(File(zipDir, "x.jar").absolutePath)
return FileUtil.toSystemIndependentName(zipFile.absolutePath)
}
fun testSelfIncludingArtifact() {
@@ -15,6 +15,8 @@
*/
package com.intellij.util.io
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File
import kotlin.test.fail
@@ -137,6 +139,27 @@ class DirectoryContentSpecTest {
}
})
}
@Test
fun `zip file`() {
val zip = zipFile {
file("a.txt", "a")
}.generateInTempDir()
assertTrue(zip.isFile)
assertEquals("zip", zip.extension)
zip.assertMatches(zipFile {
file("a.txt", "a")
})
zip.assertNotMatches(zipFile {
file("b.txt", "a")
})
zip.assertNotMatches(zipFile {
file("a.txt", "b")
})
zip.assertNotMatches(directoryContent {
file("a.txt", "b")
})
}
}
private fun File.assertNotMatches(spec: DirectoryContentSpec) {
@@ -26,8 +26,6 @@ import java.io.File
/**
* Builds a data structure specifying content (files, their content, sub-directories, archives) of a directory. It can be used to either check
* that a given directory matches this specification or to generate files in a directory accordingly to the specification.
*
* @author nik
*/
inline fun directoryContent(content: DirectoryContentBuilder.() -> Unit): DirectoryContentSpec {
val builder = DirectoryContentBuilderImpl(DirectorySpec())
@@ -35,6 +33,16 @@ inline fun directoryContent(content: DirectoryContentBuilder.() -> Unit): Direct
return builder.result
}
/**
* Builds a data structure specifying content (files, their content, sub-directories, archives) of a zip file. It can be used to either check
* that a given zip file matches this specification or to generate a zip file accordingly to the specification.
*/
inline fun zipFile(content: DirectoryContentBuilder.() -> Unit): DirectoryContentSpec {
val builder = DirectoryContentBuilderImpl(ZipSpec())
builder.content()
return builder.result
}
abstract class DirectoryContentBuilder {
/**
* File with name [name] and any content
@@ -67,6 +67,12 @@ class ZipSpec : DirectorySpecBase() {
FileUtil.delete(contentDir)
}
}
override fun generateInTempDir(): File {
val target = FileUtil.createTempFile("zip-by-spec", ".zip", true)
generate(target)
return target
}
}
class FileSpec(val content: ByteArray?) : DirectoryContentSpecImpl() {
@@ -100,11 +106,13 @@ class DirectoryContentBuilderImpl(val result: DirectorySpecBase) : DirectoryCont
}
fun assertDirectoryContentMatches(file: File, spec: DirectoryContentSpecImpl, relativePath: String) {
assertTrue("$file doesn't exist", file.exists())
when (spec) {
is DirectorySpec -> {
assertDirectoryMatches(file, spec, relativePath)
}
is ZipSpec -> {
assertTrue("$file is not a file", file.isFile)
val dirForExtracted = FileUtil.createTempDirectory("extracted-${file.name}", null, false)
ZipUtil.extract(file, dirForExtracted, null)
assertDirectoryMatches(dirForExtracted, spec, relativePath)