From a5db5d65ba17e97bc309eed5157016622f70594e Mon Sep 17 00:00:00 2001 From: Andrey Shcheglov Date: Fri, 28 Oct 2022 17:48:29 +0300 Subject: [PATCH] When running save-cli, override the temporary directory on Windows --- .../ruleset/smoke/DiktatSaveSmokeTest.kt | 19 ++++++++ .../kotlin/org/cqfn/diktat/util/TestUtils.kt | 44 +++++++++++++++++++ .../src/test/resources/test/smoke/.gitignore | 1 + 3 files changed, 64 insertions(+) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index d3ab755b48..85c6f19bfb 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.smoke import org.cqfn.diktat.common.utils.loggerWithKtlintConfig import org.cqfn.diktat.util.SAVE_VERSION +import org.cqfn.diktat.util.deleteIfExistsRecursively import org.cqfn.diktat.util.deleteIfExistsSilently import org.cqfn.diktat.util.isSameJavaHomeAs import org.cqfn.diktat.util.prependPath @@ -20,6 +21,7 @@ import java.net.URL import java.nio.file.Path import kotlin.io.path.Path import kotlin.io.path.absolute +import kotlin.io.path.absolutePathString import kotlin.io.path.copyTo import kotlin.io.path.createDirectories import kotlin.io.path.div @@ -74,6 +76,21 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { val javaHome = System.getProperty("java.home") environment()["JAVA_HOME"] = javaHome prependPath(Path(javaHome) / "bin") + + /* + * On Windows, ktlint is often unable to relativize paths + * (see https://github.com/pinterest/ktlint/issues/1608). + * + * So let's force the temporary directory to be the + * sub-directory of the project root. + */ + if (System.getProperty("os.name").startsWith("Windows")) { + val tempDirectory = baseDirectory / ".save-cli" + tempDirectory.createDirectories() + val tempDirectoryPath = tempDirectory.absolutePathString() + environment()["TMP"] = tempDirectoryPath + environment()["TEMP"] = tempDirectoryPath + } } val saveProcess = processBuilder.start() @@ -193,10 +210,12 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { val diktat = baseDirectory / "diktat.jar" val save = baseDirectory / getSaveForCurrentOs() val ktlint = baseDirectory / "ktlint" + val tempDirectory = baseDirectory / ".save-cli" diktat.deleteIfExistsSilently() ktlint.deleteIfExistsSilently() save.deleteIfExistsSilently() + tempDirectory.deleteIfExistsRecursively() } } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt index f668416c48..bc9412e7a5 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt @@ -25,10 +25,18 @@ import org.intellij.lang.annotations.Language import org.jetbrains.kotlin.com.intellij.lang.ASTNode import java.io.File +import java.io.IOException +import java.nio.file.FileVisitResult +import java.nio.file.FileVisitResult.CONTINUE +import java.nio.file.Files +import java.nio.file.Files.walkFileTree import java.nio.file.NoSuchFileException import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes import java.util.concurrent.atomic.AtomicInteger import kotlin.io.path.absolute +import kotlin.io.path.deleteExisting import kotlin.io.path.deleteIfExists import kotlin.io.path.isDirectory import kotlin.io.path.isSameFileAs @@ -76,6 +84,42 @@ internal fun Path.deleteIfExistsSilently() { } } +/** + * Deletes this directory recursively. + * + * @see Path.deleteIfExistsRecursively + */ +internal fun Path.deleteRecursively() { + walkFileTree(this, object : SimpleFileVisitor() { + override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { + file.deleteIfExistsSilently() + return CONTINUE + } + + override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult { + dir.deleteExisting() + return CONTINUE + } + }) +} + +/** + * Deletes this directory recursively if it exists. + * + * @return `true` if the existing directory was successfully deleted, `false` if + * the directory doesn't exist. + * @see Files.deleteIfExists + * @see Path.deleteRecursively + */ +@Suppress("FUNCTION_BOOLEAN_PREFIX") +internal fun Path.deleteIfExistsRecursively(): Boolean = + try { + deleteRecursively() + true + } catch (_: NoSuchFileException) { + false + } + /** * @receiver the 1st operand. * @param other the 2nd operand. diff --git a/diktat-rules/src/test/resources/test/smoke/.gitignore b/diktat-rules/src/test/resources/test/smoke/.gitignore index 389aa463e4..fa1aea4e83 100644 --- a/diktat-rules/src/test/resources/test/smoke/.gitignore +++ b/diktat-rules/src/test/resources/test/smoke/.gitignore @@ -3,3 +3,4 @@ /ktlint /save-*-mingwX64.exe /tmpSave.txt +/.save-cli/