Skip to content

Commit 4a3e5e1

Browse files
Improve JAVA_HOME comparison for forked tests (#1480)
### What's done: * Now, `/path/to/jdk` and `/path/to/jdk/jre` are considered the same `JAVA_HOME`.
1 parent ad66a04 commit 4a3e5e1

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.cqfn.diktat.ruleset.smoke
33
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
44
import org.cqfn.diktat.util.SAVE_VERSION
55
import org.cqfn.diktat.util.deleteIfExistsSilently
6+
import org.cqfn.diktat.util.isSameJavaHomeAs
67
import org.cqfn.diktat.util.prependPath
78
import org.cqfn.diktat.util.retry
89

@@ -22,7 +23,6 @@ import kotlin.io.path.copyTo
2223
import kotlin.io.path.createDirectories
2324
import kotlin.io.path.div
2425
import kotlin.io.path.exists
25-
import kotlin.io.path.isSameFileAs
2626
import kotlin.io.path.listDirectoryEntries
2727
import kotlin.io.path.name
2828
import kotlin.io.path.outputStream
@@ -150,7 +150,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
150150
val forkedJavaHome = System.getenv("JAVA_HOME")
151151
if (forkedJavaHome != null) {
152152
val javaHome = System.getProperty("java.home")
153-
if (javaHome != null && !Path(javaHome).isSameFileAs(Path(forkedJavaHome))) {
153+
if (javaHome != null && !Path(javaHome).isSameJavaHomeAs(Path(forkedJavaHome))) {
154154
logger.warn {
155155
"Current JDK home is $javaHome. Forked tests may use a different JDK at $forkedJavaHome."
156156
}

diktat-rules/src/test/kotlin/org/cqfn/diktat/util/TestUtils.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import org.intellij.lang.annotations.Language
2323
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
2424

2525
import java.io.File
26+
import java.nio.file.NoSuchFileException
2627
import java.nio.file.Path
2728
import java.util.concurrent.atomic.AtomicInteger
2829
import java.util.function.Consumer
2930
import kotlin.io.path.absolute
3031
import kotlin.io.path.deleteIfExists
3132
import kotlin.io.path.isDirectory
33+
import kotlin.io.path.isSameFileAs
3234

3335
internal const val TEST_FILE_NAME = "TestFileName.kt"
3436

@@ -216,6 +218,33 @@ internal fun Path.deleteIfExistsSilently() {
216218
}
217219
}
218220

221+
/**
222+
* @receiver the 1st operand.
223+
* @param other the 2nd operand.
224+
* @return `true` if, and only if, the two paths locate the same `JAVA_HOME`.
225+
*/
226+
internal fun Path.isSameJavaHomeAs(other: Path): Boolean =
227+
isDirectory() &&
228+
(isSameFileAsSafe(other) ||
229+
resolve("jre").isSameFileAsSafe(other) ||
230+
other.resolve("jre").isSameFileAsSafe(this))
231+
232+
/**
233+
* The same as [Path.isSameFileAs], but doesn't throw any [NoSuchFileException]
234+
* if either of the operands doesn't exist.
235+
*
236+
* @receiver the 1st operand.
237+
* @param other the 2nd operand.
238+
* @return `true` if, and only if, the two paths locate the same file.
239+
* @see Path.isSameFileAs
240+
*/
241+
internal fun Path.isSameFileAsSafe(other: Path): Boolean =
242+
try {
243+
isSameFileAs(other)
244+
} catch (_: NoSuchFileException) {
245+
false
246+
}
247+
219248
/**
220249
* Prepends the `PATH` of this process builder with [pathEntry].
221250
*

0 commit comments

Comments
 (0)