Skip to content

Commit

Permalink
When running save-cli, override the temporary directory on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6675636b796f75676974687562 committed Oct 28, 2022
1 parent 62825ef commit a5db5d6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
}
}
44 changes: 44 additions & 0 deletions diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,6 +84,42 @@ internal fun Path.deleteIfExistsSilently() {
}
}

/**
* Deletes this directory recursively.
*
* @see Path.deleteIfExistsRecursively
*/
internal fun Path.deleteRecursively() {
walkFileTree(this, object : SimpleFileVisitor<Path>() {
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.
Expand Down
1 change: 1 addition & 0 deletions diktat-rules/src/test/resources/test/smoke/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/ktlint
/save-*-mingwX64.exe
/tmpSave.txt
/.save-cli/

0 comments on commit a5db5d6

Please sign in to comment.