|
| 1 | +package org.jetbrains.kotlinx.dataframe.services |
| 2 | + |
| 3 | +import org.jetbrains.kotlin.test.services.TemporaryDirectoryManager |
| 4 | +import org.jetbrains.kotlin.test.services.TestServices |
| 5 | +import org.jetbrains.kotlin.test.services.testInfo |
| 6 | +import org.jetbrains.kotlin.test.util.KtTestUtil |
| 7 | +import java.io.File |
| 8 | +import java.nio.file.Paths |
| 9 | +import java.util.Locale |
| 10 | +import kotlin.io.path.ExperimentalPathApi |
| 11 | +import kotlin.io.path.deleteRecursively |
| 12 | + |
| 13 | +// Copied from org.jetbrains.kotlin.test.services.impl.TemporaryDirectoryManagerImpl |
| 14 | +// because it uses NioFiles#deleteRecursively and throws method not found as a result. |
| 15 | +class TemporaryDirectoryManagerImplFixed(testServices: TestServices) : TemporaryDirectoryManager(testServices) { |
| 16 | + private val cache = mutableMapOf<String, File>() |
| 17 | + private val rootTempDir = lazy { |
| 18 | + val testInfo = testServices.testInfo |
| 19 | + val className = testInfo.className |
| 20 | + val methodName = testInfo.methodName |
| 21 | + if (!onWindows && className.length + methodName.length < 255) { |
| 22 | + return@lazy KtTestUtil.tmpDirForTest(className, methodName) |
| 23 | + } |
| 24 | + |
| 25 | + // This code will simplify directory name for windows. This is needed because there can occur errors due to long name |
| 26 | + val lastDot = className.lastIndexOf('.') |
| 27 | + val simplifiedClassName = className.substring(lastDot + 1).getOnlyUpperCaseSymbols() |
| 28 | + val simplifiedMethodName = methodName.getOnlyUpperCaseSymbols() |
| 29 | + KtTestUtil.tmpDirForTest(simplifiedClassName, simplifiedMethodName) |
| 30 | + } |
| 31 | + |
| 32 | + override val rootDir: File |
| 33 | + get() = rootTempDir.value |
| 34 | + |
| 35 | + override fun getOrCreateTempDirectory(name: String): File = |
| 36 | + cache.getOrPut(name) { |
| 37 | + KtTestUtil.tmpDir(rootDir, name) |
| 38 | + } |
| 39 | + |
| 40 | + @OptIn(ExperimentalPathApi::class) |
| 41 | + override fun cleanupTemporaryDirectories() { |
| 42 | + cache.clear() |
| 43 | + |
| 44 | + if (rootTempDir.isInitialized()) { |
| 45 | + Paths.get(rootDir.path).deleteRecursively() |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + companion object { |
| 50 | + private val onWindows: Boolean = |
| 51 | + System.getProperty("os.name").lowercase(Locale.getDefault()).contains("windows") |
| 52 | + |
| 53 | + private fun String.getOnlyUpperCaseSymbols(): String = |
| 54 | + this.filter { it.isUpperCase() || it == '$' } |
| 55 | + .toList() |
| 56 | + .joinToString(separator = "") |
| 57 | + } |
| 58 | +} |
0 commit comments