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 12, 2022
1 parent bb5a101 commit fe8f8eb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import java.io.File
import java.io.FileOutputStream
Expand All @@ -15,12 +16,48 @@ import kotlin.io.path.name
import kotlin.io.path.pathString

class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
override fun fixAndCompareBase(
override fun fixAndCompare(
config: String,
test: String,
expected: String
) {
saveSmokeTest(config, test, expected)
saveSmokeTest(config, test)
}

/**
* @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath]
* @param configFilePath path of diktat-analysis file
*/
@Suppress("TOO_LONG_FUNCTION")
protected fun saveSmokeTest(
configFilePath: String,
testPath: String
) {
val processBuilder = createProcessBuilderWithCmd(testPath)

val file = File("src/test/resources/test/smoke/tmpSave.txt")
val configFile = File("src/test/resources/test/smoke/diktat-analysis.yml")
val configFileFrom = File(configFilePath)

configFile.createNewFile()
file.createNewFile()

FileUtils.copyFile(configFileFrom, configFile)

processBuilder.redirectErrorStream(true)
processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(file))

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

val output = file.readLines()
val saveOutput = output.joinToString("\n")

file.delete()

Assertions.assertTrue(
saveOutput.contains("SUCCESS")
)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import java.io.File
* may change after some changes to text or other rules.
*/
class DiktatSmokeTest : DiktatSmokeTestBase() {
override fun fixAndCompareBase(
override fun fixAndCompare(
config: String,
test: String,
expected: String
Expand Down Expand Up @@ -105,4 +105,15 @@ class DiktatSmokeTest : DiktatSmokeTestBase() {
LintError(18, 40, "$DIKTAT_RULE_SET_ID:${KdocFormatting.NAME_ID}", "${KDOC_NO_EMPTY_TAGS.warnText()} @return", false)
)
}

/**
* @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]
*/
protected fun fixAndCompareSmokeTest(expectedPath: String, testPath: String) {
Assertions.assertTrue(
testComparatorUnit
.compareFilesFromResources(expectedPath, testPath, true)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin",
fun `regression - should not fail if package is not set`() {
overrideRulesConfig(listOf(Warnings.PACKAGE_NAME_MISSING, Warnings.PACKAGE_NAME_INCORRECT_PATH,
Warnings.PACKAGE_NAME_INCORRECT_PREFIX))
fixAndCompareBase(configFilePath, "DefaultPackageExpected.kt", "DefaultPackageTest.kt")
fixAndCompare(configFilePath, "DefaultPackageExpected.kt", "DefaultPackageTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #8 - anonymous function`() {
fixAndCompareBase(configFilePath, "Example8Expected.kt", "Example8Test.kt")
fixAndCompare(configFilePath, "Example8Expected.kt", "Example8Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #7`() {
fixAndCompareBase(configFilePath, "Example7Expected.kt", "Example7Test.kt")
fixAndCompare(configFilePath, "Example7Expected.kt", "Example7Test.kt")
}

@Test
Expand All @@ -105,7 +105,7 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin",
)
)
)
fixAndCompareBase(configFilePath, "Example6Expected.kt", "Example6Test.kt")
fixAndCompare(configFilePath, "Example6Expected.kt", "Example6Test.kt")
}

@Test
Expand All @@ -129,7 +129,7 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin",
)
)
)
fixAndCompareBase(configFilePath, "Example5Expected.kt", "Example5Test.kt")
fixAndCompare(configFilePath, "Example5Expected.kt", "Example5Test.kt")
}

@Test
Expand All @@ -144,19 +144,19 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin",
)
)
)
fixAndCompareBase(configFilePath, "Example4Expected.kt", "Example4Test.kt")
fixAndCompare(configFilePath, "Example4Expected.kt", "Example4Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #3`() {
fixAndCompareBase(configFilePath, "Example3Expected.kt", "Example3Test.kt")
fixAndCompare(configFilePath, "Example3Expected.kt", "Example3Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - shouldn't throw exception in cases similar to #371`() {
fixAndCompareBase(configFilePath, "Bug1Expected.kt", "Bug1Test.kt")
fixAndCompare(configFilePath, "Bug1Expected.kt", "Bug1Test.kt")
}

@Test
Expand All @@ -171,7 +171,7 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin",
)
)
)
fixAndCompareBase(configFilePath, "Example2Expected.kt", "Example2Test.kt")
fixAndCompare(configFilePath, "Example2Expected.kt", "Example2Test.kt")
}

@Test
Expand All @@ -186,40 +186,40 @@ abstract class DiktatSmokeTestBase : FixTestBase("test/smoke/src/main/kotlin",
)
)
)
fixAndCompareBase(configFilePath, "Example1Expected.kt", "Example1Test.kt")
fixAndCompare(configFilePath, "Example1Expected.kt", "Example1Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with kts files #2`() {
fixAndCompareBase(configFilePath, "script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts")
fixAndCompare(configFilePath, "script/SimpleRunInScriptExpected.kts", "script/SimpleRunInScriptTest.kts")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test with kts files with package name`() {
fixAndCompareBase(configFilePath, "script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts")
fixAndCompare(configFilePath, "script/PackageInScriptExpected.kts", "script/PackageInScriptTest.kts")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - should correctly handle tags with empty lines`() {
fixAndCompareBase(configFilePath, "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
fixAndCompare(configFilePath, "KdocFormattingMultilineTagsExpected.kt", "KdocFormattingMultilineTagsTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `regression - FP of local variables rule`() {
fixAndCompareBase(configFilePath, "LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt")
fixAndCompare(configFilePath, "LocalVariableWithOffsetExpected.kt", "LocalVariableWithOffsetTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `fix can cause long line`() {
fixAndCompareBase(configFilePath, "ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt")
fixAndCompare(configFilePath, "ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt")
}

abstract fun fixAndCompareBase(
abstract fun fixAndCompare(
config: String,
test: String,
expected: String
Expand Down
66 changes: 11 additions & 55 deletions diktat-rules/src/test/kotlin/org/cqfn/diktat/util/FixTestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ 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.intellij.lang.annotations.Language
import org.junit.jupiter.api.Assertions
import java.io.File
import java.nio.file.Path
import kotlin.io.path.bufferedWriter
import kotlin.io.path.div
Expand All @@ -22,7 +20,10 @@ open class FixTestBase(
private val cb: LintErrorCallback = defaultCallback,
private val rulesConfigList: List<RulesConfig>? = null,
) {
private val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName ->
/**
* testComparatorUnit
*/
protected val testComparatorUnit = TestComparatorUnit(resourceFilePath) { text, fileName ->
format(ruleSetProviderRef, text, fileName, rulesConfigList, cb = cb)
}

Expand All @@ -48,75 +49,30 @@ open class FixTestBase(
)
}

/**
* @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]
*/
protected fun fixAndCompareSmokeTest(expectedPath: String, testPath: String) {
Assertions.assertTrue(
testComparatorUnit
.compareFilesFromResources(expectedPath, testPath, true)
)
}

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

private fun createProcessBuilderWithCmd(expectedPath: String, testPath: String): ProcessBuilder {
/**
* @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath]
* @return ProcessBuilder
*/
protected fun createProcessBuilderWithCmd(testPath: String): ProcessBuilder {
val filesDir = "src/test/resources/test/smoke"
val savePath = "$filesDir/${getSaveForCurrentOs()}"

val systemName = System.getProperty("os.name")
val result = when {
systemName.startsWith("Linux", ignoreCase = true) || systemName.startsWith("Mac", ignoreCase = true) ->
ProcessBuilder("sh", "-c", "pwd ; chmod 777 $savePath ; ./$savePath src/test/resources/test/smoke/src/main/kotlin $testPath --log all")
else -> ProcessBuilder(savePath, "src/test/resources/test/smoke/src/main/kotlin", expectedPath, testPath)
ProcessBuilder("sh", "-c", "chmod 777 $savePath ; ./$savePath src/test/resources/test/smoke/src/main/kotlin $testPath --log all")
else -> ProcessBuilder(savePath, "src/test/resources/test/smoke/src/main/kotlin", testPath)
}
return result
}

/**
* @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]
* @param configFilePath path of diktat-analysis file
*/
@Suppress("TOO_LONG_FUNCTION")
protected fun saveSmokeTest(
configFilePath: String,
expectedPath: String,
testPath: String
) {
val processBuilder = createProcessBuilderWithCmd(expectedPath, testPath)

val file = File("src/test/resources/test/smoke/tmpSave.txt")
val configFile = File("src/test/resources/test/smoke/diktat-analysis.yml")
val configFileFrom = File(configFilePath)

configFile.createNewFile()
file.createNewFile()

copyFile(configFileFrom, configFile)

processBuilder.redirectErrorStream(true)
processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(file))

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

val output = file.readLines()
val saveOutput = output.joinToString("\n")

file.delete()

Assertions.assertTrue(
saveOutput.contains("SUCCESS")
)
}

/**
* @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 Down

0 comments on commit fe8f8eb

Please sign in to comment.