Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JAVA_HOME comparison for forked tests #1480

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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.deleteIfExistsSilently
import org.cqfn.diktat.util.isSameJavaHomeAs
import org.cqfn.diktat.util.prependPath
import org.cqfn.diktat.util.retry

Expand All @@ -22,7 +23,6 @@ import kotlin.io.path.copyTo
import kotlin.io.path.createDirectories
import kotlin.io.path.div
import kotlin.io.path.exists
import kotlin.io.path.isSameFileAs
import kotlin.io.path.listDirectoryEntries
import kotlin.io.path.name
import kotlin.io.path.outputStream
Expand Down Expand Up @@ -150,7 +150,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
val forkedJavaHome = System.getenv("JAVA_HOME")
if (forkedJavaHome != null) {
val javaHome = System.getProperty("java.home")
if (javaHome != null && !Path(javaHome).isSameFileAs(Path(forkedJavaHome))) {
if (javaHome != null && !Path(javaHome).isSameJavaHomeAs(Path(forkedJavaHome))) {
logger.warn {
"Current JDK home is $javaHome. Forked tests may use a different JDK at $forkedJavaHome."
}
Expand Down
29 changes: 29 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 @@ -23,12 +23,14 @@ import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

import java.io.File
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.util.concurrent.atomic.AtomicInteger
import java.util.function.Consumer
import kotlin.io.path.absolute
import kotlin.io.path.deleteIfExists
import kotlin.io.path.isDirectory
import kotlin.io.path.isSameFileAs

internal const val TEST_FILE_NAME = "TestFileName.kt"

Expand Down Expand Up @@ -216,6 +218,33 @@ internal fun Path.deleteIfExistsSilently() {
}
}

/**
* @receiver the 1st operand.
* @param other the 2nd operand.
* @return `true` if, and only if, the two paths locate the same `JAVA_HOME`.
*/
internal fun Path.isSameJavaHomeAs(other: Path): Boolean =
isDirectory() &&
(isSameFileAsSafe(other) ||
resolve("jre").isSameFileAsSafe(other) ||
other.resolve("jre").isSameFileAsSafe(this))

/**
* The same as [Path.isSameFileAs], but doesn't throw any [NoSuchFileException]
* if either of the operands doesn't exist.
*
* @receiver the 1st operand.
* @param other the 2nd operand.
* @return `true` if, and only if, the two paths locate the same file.
* @see Path.isSameFileAs
*/
internal fun Path.isSameFileAsSafe(other: Path): Boolean =
try {
isSameFileAs(other)
} catch (_: NoSuchFileException) {
false
}

/**
* Prepends the `PATH` of this process builder with [pathEntry].
*
Expand Down