Skip to content

Commit

Permalink
Migrate diktat smoke tests to SAVE-cli mechanism
Browse files Browse the repository at this point in the history
### What's done:
* migrated diktat smoke tests to SAVE-cli mechanism
Closes #1383
  • Loading branch information
Cheshiriks committed Jul 4, 2022
1 parent 355fd2b commit e29f5d9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
6 changes: 6 additions & 0 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<apache.api.version>4.5.13</apache.api.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -89,6 +90,11 @@
<groupId>com.bpodgursky</groupId>
<artifactId>jbool_expressions</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.api.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
44 changes: 33 additions & 11 deletions diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package org.cqfn.diktat.util
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.test.framework.processing.FileComparisonResult
import org.cqfn.diktat.test.framework.processing.TestComparatorUnit

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSetProvider
import org.apache.commons.io.FileUtils.copyFile
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Assertions
import java.io.File

import java.io.FileOutputStream
import java.nio.file.Path
import kotlin.io.path.bufferedWriter
import kotlin.io.path.div

/**
* @property resourceFilePath path to files which will be compared in tests
*/
open class FixTestBase(protected val resourceFilePath: String,
private val ruleSetProviderRef: (rulesConfigList: List<RulesConfig>?) -> RuleSetProvider,
private val cb: LintErrorCallback = defaultCallback,
private val rulesConfigList: List<RulesConfig>? = null,
open class FixTestBase(
protected val resourceFilePath: String,
private val ruleSetProviderRef: (rulesConfigList: List<RulesConfig>?) -> RuleSetProvider,
private val cb: LintErrorCallback = defaultCallback,
private val rulesConfigList: List<RulesConfig>? = null,
) {
private val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName ->
format(ruleSetProviderRef, text, fileName, rulesConfigList, cb = cb)
Expand Down Expand Up @@ -62,12 +65,22 @@ open class FixTestBase(protected val resourceFilePath: String,
}

private fun getSaveForCurrentOs() = when {
System.getProperty("os.name").startsWith("Linux", ignoreCase = true) -> "save-linuxX64.kexe"
System.getProperty("os.name").startsWith("Mac", ignoreCase = true) -> "save-macosX64.kexe"
System.getProperty("os.name").startsWith("Windows", ignoreCase = true) -> "save.exe"
System.getProperty("os.name").startsWith("Linux", ignoreCase = true) -> "save-0.3.1-linuxX64.kexe"
System.getProperty("os.name").startsWith("Mac", ignoreCase = true) -> "save-0.3.1-macosX64.kexe"
System.getProperty("os.name").startsWith("Windows", ignoreCase = true) -> "save-0.3.1-mingwX64.exe"
else -> ""
}

private fun downloadFile(url: String, file: File) {
val httpClient = HttpClients.createDefault()
val request = HttpGet(url)
val response: CloseableHttpResponse = httpClient.execute(request)
val fileSave = response.entity
fileSave?.let {
FileOutputStream(file).use { outstream -> fileSave.writeTo(outstream) }
}
}

/**
* @param expectedPath path to file with expected result, relative to [resourceFilePath]
* @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath]
Expand All @@ -78,15 +91,19 @@ open class FixTestBase(protected val resourceFilePath: String,
expectedPath: String,
testPath: String
) {
val processBuilder = ProcessBuilder("src/test/resources/test/smoke/${getSaveForCurrentOs()}", "src/test/resources/test/smoke/src/main/kotlin", expectedPath, testPath,
"--log", "all")
val processBuilder = ProcessBuilder("src/test/resources/test/smoke/${getSaveForCurrentOs()}", "src/test/resources/test/smoke/src/main/kotlin", expectedPath, testPath)

val file = File("tmpSave.txt")
val diktat = File("src/test/resources/test/smoke/diktat.jar")
val configFile = File("src/test/resources/test/smoke/diktat-analysis.yml")
val diktatFrom = File("../diktat-ruleset/target/diktat-$diktatVersion.jar")
val save = File("src/test/resources/test/smoke/${getSaveForCurrentOs()}")
val ktlint = File("src/test/resources/test/smoke/ktlint")

val configFileFrom = File(configFilePath)

ktlint.createNewFile()
save.createNewFile()
file.createNewFile()
diktat.createNewFile()
configFile.createNewFile()
Expand All @@ -96,6 +113,9 @@ open class FixTestBase(protected val resourceFilePath: String,

processBuilder.redirectOutput(file)

downloadFile("https://github.com/saveourtool/save-cli/releases/download/v0.3.1/${getSaveForCurrentOs()}", save)
downloadFile("https://github.com/pinterest/ktlint/releases/download/0.46.1/ktlint", ktlint)

val process = processBuilder.start()
process.waitFor()

Expand All @@ -105,6 +125,8 @@ open class FixTestBase(protected val resourceFilePath: String,
file.delete()
diktat.delete()
configFile.delete()
ktlint.delete()
save.delete()

Assertions.assertTrue(
saveOutput.contains("SUCCESS")
Expand Down
Binary file removed diktat-rules/src/test/resources/test/smoke/ktlint
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed diktat-rules/src/test/resources/test/smoke/save.exe
Binary file not shown.

0 comments on commit e29f5d9

Please sign in to comment.