Use new kotlin.io.path API in tests

This commit is contained in:
Ilya Gorbunov
2020-11-16 08:30:04 +03:00
parent d38e145529
commit b2b2629e79
27 changed files with 173 additions and 116 deletions
@@ -6,6 +6,7 @@
package kotlin.script.experimental.test
import java.io.File
import java.nio.file.Files
import kotlin.contracts.ExperimentalContracts
import kotlin.script.experimental.dependencies.*
import kotlin.script.experimental.api.ResultWithDiagnostics
@@ -17,8 +18,7 @@ import kotlin.script.experimental.dependencies.impl.makeResolveFailureResult
class ResolversTest : ResolversTestBase() {
private fun <T> withTempFile(body: (file: File) -> T): T {
createTempFile()
val file = createTempFile()
val file = Files.createTempFile(null, null).toFile()
file.deleteOnExit()
try {
return body(file)
@@ -3,17 +3,20 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:OptIn(ExperimentalPathApi::class)
package kotlin.script.experimental.jvmhost.test
import junit.framework.TestCase
import org.junit.Assert
import org.junit.Test
import java.io.File
import java.io.FileOutputStream
import java.net.URLClassLoader
import java.nio.file.Path
import java.util.jar.JarEntry
import java.util.jar.JarOutputStream
import java.util.jar.Manifest
import kotlin.io.path.*
import kotlin.script.experimental.jvm.util.classPathFromTypicalResourceUrls
import kotlin.script.experimental.jvm.util.classpathFromClass
import kotlin.script.experimental.jvm.util.classpathFromClassloader
@@ -21,22 +24,22 @@ import kotlin.script.experimental.jvm.util.scriptCompilationClasspathFromContext
class ClassPathTest : TestCase() {
lateinit var tempDir: File
lateinit var tempDir: Path
override fun setUp() {
tempDir = createTempDir(ClassPathTest::class.simpleName!!)
tempDir = createTempDirectory(ClassPathTest::class.simpleName!!)
super.setUp()
}
override fun tearDown() {
super.tearDown()
tempDir.deleteRecursively()
tempDir.toFile().deleteRecursively()
}
@Test
fun testExtractFromFat() {
val collection = createTempFile("col", ".jar", directory = tempDir).apply { createCollectionJar(emulatedCollectionFiles, "BOOT-INF") }
val cl = URLClassLoader(arrayOf(collection.toURI().toURL()), null)
val collection = createTempFile(directory = tempDir, "col", ".jar").apply { createCollectionJar(emulatedCollectionFiles, "BOOT-INF") }
val cl = URLClassLoader(arrayOf(collection.toUri().toURL()), null)
val cp = classpathFromClassloader(cl, true)
Assert.assertTrue(cp != null && cp.isNotEmpty())
@@ -45,40 +48,40 @@ class ClassPathTest : TestCase() {
@Test
fun testDetectClasspathFromResources() {
val root1 = createTempDir("root1", directory = tempDir)
val jar = createTempFile("jar1", ".jar", directory = tempDir).apply { createJarWithManifest() }
val root1 = createTempDirectory(directory = tempDir, "root1")
val jar = createTempFile(directory = tempDir, "jar1", ".jar").apply { createJarWithManifest() }
val cl = URLClassLoader(
(emulatedClasspath.map { File(root1, it).apply { mkdirs() }.toURI().toURL() }
+ jar.toURI().toURL()).toTypedArray(),
(emulatedClasspath.map { (root1 / it).apply { createDirectories() }.toUri().toURL() }
+ jar.toUri().toURL()).toTypedArray(),
null
)
val cp = cl.classPathFromTypicalResourceUrls().toList().map { it.canonicalFile }
Assert.assertTrue(cp.contains(jar.canonicalFile))
Assert.assertTrue(cp.contains(jar.toFile().canonicalFile))
for (el in emulatedClasspath) {
Assert.assertTrue(cp.contains(File(root1, el).canonicalFile))
Assert.assertTrue(cp.contains((root1 / el).toFile().canonicalFile))
}
}
@Test
fun testFilterClasspath() {
val tempDir = createTempDir().canonicalFile
val tempDir = createTempDirectory().toRealPath()
try {
val files = listOf(
File(tempDir, "projX/classes"),
File(tempDir, "projX/test-classes"),
File(tempDir, "projY/classes")
(tempDir / "projX/classes"),
(tempDir / "projX/test-classes"),
(tempDir / "projY/classes")
)
files.forEach { it.mkdirs() }
files.forEach { it.createDirectories() }
val classloader = URLClassLoader(files.map { it.toURI().toURL() }.toTypedArray(), null)
val classloader = URLClassLoader(files.map { it.toUri().toURL() }.toTypedArray(), null)
val classpath =
scriptCompilationClasspathFromContextOrNull("projX", classLoader = classloader)!!.map { it.toRelativeString(tempDir) }
scriptCompilationClasspathFromContextOrNull("projX", classLoader = classloader)!!.map { it.toPath().relativeTo(tempDir) }
Assert.assertEquals(files.dropLast(1).map { it.toRelativeString(tempDir) }, classpath)
Assert.assertEquals(files.dropLast(1).map { it.relativeTo(tempDir) }, classpath)
} finally {
tempDir.deleteRecursively()
tempDir.toFile().deleteRecursively()
}
}
@@ -103,8 +106,8 @@ private val emulatedClasspath = arrayOf(
"module2/classes/java/test/"
)
fun File.createCollectionJar(fileNames: Array<String>, infDirName: String) {
FileOutputStream(this).use { fileStream ->
fun Path.createCollectionJar(fileNames: Array<String>, infDirName: String) {
this.outputStream().use { fileStream ->
val jarStream = JarOutputStream(fileStream)
jarStream.putNextEntry(JarEntry("$infDirName/classes/"))
jarStream.putNextEntry(JarEntry("$infDirName/lib/"))
@@ -131,8 +134,8 @@ fun testUnpackedCollection(classpath: List<File>, fileNames: Array<String>) {
jars.checkFiles(cpJars.first().parentFile.parentFile)
}
fun File.createJarWithManifest() {
FileOutputStream(this).use { fileStream ->
fun Path.createJarWithManifest() {
this.outputStream().use { fileStream ->
val jarStream = JarOutputStream(fileStream, Manifest())
jarStream.finish()
}
@@ -23,6 +23,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.io.File
import org.jetbrains.kotlin.gradle.util.createTempDir
import kotlin.test.assertEquals
private val DEFAULT_GRADLE_VERSION = GradleVersionRequired.AtLeast("5.6.4")
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.gradle
import org.jetbrains.kotlin.gradle.util.findFileByName
import org.jetbrains.kotlin.gradle.util.createTempDir
import org.junit.Test
import java.io.File
import java.net.URI
@@ -123,7 +124,7 @@ abstract class AbstractConfigurationCacheIT : BaseGradleIT() {
* directory.
*/
private fun copyReportToTempDir(htmlReportFile: File): File =
createTempDir().let { tempDir ->
createTempDir("report").let { tempDir ->
htmlReportFile.parentFile.copyRecursively(tempDir)
tempDir.resolve(htmlReportFile.name)
}
@@ -15,6 +15,7 @@ import org.junit.After
import org.junit.Before
import java.io.File
import java.lang.IllegalStateException
import java.nio.file.Files
import kotlin.test.*
class BuildSessionLoggerTest {
@@ -25,7 +26,7 @@ class BuildSessionLoggerTest {
@Before
fun prepareFolder() {
rootFolder = File.createTempFile("kotlin-stats", "")
rootFolder = Files.createTempFile("kotlin-stats", "").toFile()
rootFolder.delete()
rootFolder.mkdirs()
}
@@ -9,6 +9,7 @@ dependencies {
testCompile(project(":kotlin-main-kts"))
testCompileOnly(project(":compiler:cli"))
testCompileOnly(project(":kotlin-scripting-jvm-host-unshaded"))
testCompile(kotlinStdlib("jdk8"))
testCompile(commonDep("junit"))
testCompile(projectTests(":kotlin-scripting-compiler")) { isTransitive = false }
testRuntime(project(":kotlin-compiler-embeddable"))
@@ -17,6 +17,8 @@ import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
import java.io.File
import java.nio.file.Path
import kotlin.io.path.*
class MainKtsIT {
@@ -51,42 +53,44 @@ class MainKtsIT {
runWithKotlincAndMainKts("$TEST_DATA_ROOT/context-classloader.main.kts", listOf("MainKtsConfigurator"))
}
@OptIn(ExperimentalPathApi::class)
@Test
fun testCachedReflection() {
val cache = createTempDir("main.kts.test")
val cache = createTempDirectory("main.kts.test")
try {
runWithKotlinRunner("$TEST_DATA_ROOT/use-reflect.main.kts", listOf("false"), cacheDir = cache)
// second run uses the cached script
runWithKotlinRunner("$TEST_DATA_ROOT/use-reflect.main.kts", listOf("false"), cacheDir = cache)
} finally {
cache.deleteRecursively()
cache.toFile().deleteRecursively()
}
}
@OptIn(ExperimentalPathApi::class)
@Test
fun testCache() {
val script = File("$TEST_DATA_ROOT/import-test.main.kts").absolutePath
val cache = createTempDir("main.kts.test")
val cache = createTempDirectory("main.kts.test")
try {
Assert.assertTrue(cache.exists() && cache.listFiles { f: File -> f.extension == "jar" }?.isEmpty() == true)
Assert.assertTrue(cache.exists() && cache.listDirectoryEntries("*.jar").isEmpty())
runWithKotlinRunner(script, OUT_FROM_IMPORT_TEST, cacheDir = cache)
val cacheFile = cache.listFiles { f: File -> f.extension.equals("jar", ignoreCase = true) }?.firstOrNull()
val cacheFile = cache.listDirectoryEntries("*.jar").firstOrNull()
Assert.assertTrue(cacheFile != null && cacheFile.exists())
// run generated jar with java
val javaExecutable = File(File(System.getProperty("java.home"), "bin"), "java")
val args = listOf(javaExecutable.absolutePath, "-jar", cacheFile!!.path)
val args = listOf(javaExecutable.absolutePath, "-jar", cacheFile!!.toString())
runAndCheckResults(
args, OUT_FROM_IMPORT_TEST,
additionalEnvVars = listOf(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR to cache.absolutePath)
additionalEnvVars = listOf(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR to cache.toAbsolutePath().toString())
)
// this run should use the cached script
runWithKotlinRunner(script, OUT_FROM_IMPORT_TEST, cacheDir = cache)
} finally {
cache.deleteRecursively()
cache.toFile().deleteRecursively()
}
}
@@ -127,11 +131,11 @@ fun runWithKotlinRunner(
scriptPath: String,
expectedOutPatterns: List<String> = emptyList(),
expectedExitCode: Int = 0,
cacheDir: File? = null
cacheDir: Path? = null
) {
runWithKotlinLauncherScript(
"kotlin", listOf(scriptPath), expectedOutPatterns, expectedExitCode,
additionalEnvVars = listOf(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR to (cacheDir?.absolutePath ?: ""))
additionalEnvVars = listOf(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR to (cacheDir?.toAbsolutePath()?.toString() ?: ""))
)
}