-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved tests with CLI from GitHub actions to JUnit test (#1784)
- removed steps from `build_and_test.yml` - added a new test `DiktatCliTest.kt`
- Loading branch information
Showing
5 changed files
with
184 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
diktat-cli/src/test/kotlin/com/saveourtool/diktat/smoke/DiktatCliTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package com.saveourtool.diktat.smoke | ||
|
||
import com.saveourtool.diktat.test.framework.util.checkForkedJavaHome | ||
import com.saveourtool.diktat.test.framework.util.deleteIfExistsSilently | ||
import com.saveourtool.diktat.test.framework.util.inheritJavaHome | ||
import com.saveourtool.diktat.test.framework.util.isWindows | ||
import com.saveourtool.diktat.test.framework.util.temporaryDirectory | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.assertj.core.api.SoftAssertions.assertSoftly | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.io.TempDir | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import kotlin.io.path.ExperimentalPathApi | ||
import kotlin.io.path.PathWalkOption | ||
import kotlin.io.path.absolutePathString | ||
import kotlin.io.path.copyTo | ||
import kotlin.io.path.createDirectories | ||
import kotlin.io.path.div | ||
import kotlin.io.path.isDirectory | ||
import kotlin.io.path.readText | ||
import kotlin.io.path.walk | ||
|
||
class DiktatCliTest { | ||
|
||
@Test | ||
fun `Run diKTat from cli`() { | ||
cliTest("examples/maven/src/main/kotlin/Test.kt") | ||
} | ||
|
||
@Test | ||
fun `Run diKTat from cli (absolute paths)`() { | ||
cliTest(tempDir.resolve("examples/maven/src/main/kotlin/Test.kt").absolutePathString()) | ||
} | ||
|
||
@Test | ||
fun `Run diKTat from cli (glob paths, 1 of 4)`() { | ||
cliTest("examples/maven/src/main/kotlin/*.kt") | ||
} | ||
|
||
@Test | ||
fun `Run diKTat from cli (glob paths, 2 of 4)`() { | ||
cliTest("examples/**/main/kotlin/*.kt") | ||
} | ||
|
||
@Test | ||
fun `Run diKTat from cli (glob paths, 3 of 4)`() { | ||
cliTest("examples/**/*.kt") | ||
} | ||
|
||
@Test | ||
fun `Run diKTat from cli (glob paths, 4 of 4)`() { | ||
cliTest("**/*.kt") | ||
} | ||
|
||
@Suppress("TOO_LONG_FUNCTION") | ||
private fun cliTest( | ||
vararg cliArgs: String, | ||
) { | ||
assertSoftly { softly -> | ||
val diktatCliLog = (tempDir / "log.txt").apply { | ||
parent.createDirectories() | ||
deleteIfExistsSilently() | ||
} | ||
|
||
val processBuilder = createProcessBuilderWithCmd(*cliArgs).apply { | ||
redirectErrorStream(true) | ||
redirectOutput(ProcessBuilder.Redirect.appendTo(diktatCliLog.toFile())) | ||
|
||
/* | ||
* Inherit JAVA_HOME for the child process. | ||
*/ | ||
inheritJavaHome() | ||
|
||
temporaryDirectory(tempDir / ".tmp") | ||
} | ||
|
||
val diktatCliProcess = processBuilder.start() | ||
val exitCode = diktatCliProcess.waitFor() | ||
softly.assertThat(exitCode).describedAs("The exit code of Diktat CLI").isZero | ||
|
||
softly.assertThat(diktatCliLog).isRegularFile | ||
|
||
val diktatCliOutput = diktatCliLog.readText() | ||
|
||
val commandLine = processBuilder.command().joinToString(separator = " ") | ||
softly.assertThat(diktatCliOutput) | ||
.describedAs("The output of \"$commandLine\"") | ||
.isNotEmpty | ||
.contains("[VARIABLE_NAME_INCORRECT_FORMAT]") | ||
.doesNotContain("WARNING:") | ||
} | ||
} | ||
|
||
private fun createProcessBuilderWithCmd(vararg cliArgs: String): ProcessBuilder { | ||
return when { | ||
System.getProperty("os.name").isWindows() -> arrayOf(*javaArgs, DIKTAT_CLI_JAR, *defaultArgs, *cliArgs) | ||
else -> arrayOf("sh", "-c", arrayOf(*javaArgs, DIKTAT_CLI_JAR, *defaultArgs, *cliArgs).joinToString(" ")) | ||
}.let { args -> ProcessBuilder(*args).directory(tempDir.toFile()) } | ||
} | ||
|
||
companion object { | ||
private val logger = KotlinLogging.logger {} | ||
|
||
private val javaArgs = arrayOf("java", "-showversion", "-jar") | ||
private val defaultArgs = arrayOf("--log-level", "debug") | ||
|
||
@JvmStatic | ||
@TempDir | ||
internal var tempDir: Path = Paths.get("/invalid") | ||
|
||
@BeforeAll | ||
@JvmStatic | ||
@OptIn(ExperimentalPathApi::class) | ||
internal fun beforeAll() { | ||
assertSoftly { softly -> | ||
checkForkedJavaHome() | ||
|
||
logger.info { | ||
"The temp directory for the test is $tempDir." | ||
} | ||
val sourceDirectory = Paths.get("../examples") | ||
val targetDirectory = (tempDir / "examples").also { | ||
it.createDirectories() | ||
} | ||
sourceDirectory.walk(PathWalkOption.INCLUDE_DIRECTORIES).forEach { file -> | ||
if (file.isDirectory()) { | ||
targetDirectory.resolve(sourceDirectory.relativize(file)).createDirectories() | ||
} else { | ||
val dest = targetDirectory.resolve(sourceDirectory.relativize(file)) | ||
file.copyTo(dest) | ||
} | ||
} | ||
copyDiktatCli(softly, tempDir / DIKTAT_CLI_JAR) | ||
|
||
val defaultConfigFile = Paths.get("../diktat-analysis.yml") | ||
softly.assertThat(defaultConfigFile) | ||
.describedAs("Default config file for diktat") | ||
.isRegularFile | ||
defaultConfigFile.copyTo(tempDir / "diktat-analysis.yml", overwrite = true) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.