Skip to content

Commit

Permalink
Enhance tests in diktat (#1780)
Browse files Browse the repository at this point in the history
- removed unused imports in tests
- removed deleting temp files in tests
- added checking assertion for content and status
  • Loading branch information
nulls committed Nov 7, 2023
1 parent d7fbb5a commit c1c3cab
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ fun ASTNode.getFilePathSafely(): String? = run {
* @return name of the file [this] node belongs to
*/
fun ASTNode.getFilePath(): String = requireNotNull(getFilePathSafely()) {
"Failed to retrieve a file path for node ${this::javaClass}"
"Failed to retrieve a file path for node $this (${this.javaClass.simpleName})"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
tempDir: Path
): FileComparisonResult {
val config = IndentationConfig(NEWLINE_AT_END to false).withCustomParameters().asRulesConfigList()
return fixAndCompareContent(actualCode, expectedCode, tempDir, config)
return fixAndCompareContent(actualCode, expectedCode, tempDir, overrideRulesConfigList = config)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class IndentationTestFixExtension(
actualCode,
expectedCode,
tempDir,
defaultConfig.withCustomParameters(customConfig).asRulesConfigList())
overrideRulesConfigList = defaultConfig.withCustomParameters(customConfig).asRulesConfigList(),
)

if (!lintResult.isSuccessful) {
assertThat(lintResult.actualContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.jetbrains.kotlin.KtNodeTypes.CLASS
import org.jetbrains.kotlin.KtNodeTypes.CLASS_BODY
import org.jetbrains.kotlin.lexer.KtTokens.EOL_COMMENT
import org.jetbrains.kotlin.lexer.KtTokens.EQ
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.FILE
import org.jetbrains.kotlin.KtNodeTypes.FUN
import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER
import org.jetbrains.kotlin.KtNodeTypes.INTEGER_CONSTANT
Expand All @@ -27,9 +26,6 @@ import org.jetbrains.kotlin.KtNodeTypes.TYPE_REFERENCE
import org.jetbrains.kotlin.KtNodeTypes.VALUE_PARAMETER_LIST
import org.jetbrains.kotlin.lexer.KtTokens.VAL_KEYWORD
import org.jetbrains.kotlin.lexer.KtTokens.WHITE_SPACE
import com.saveourtool.diktat.ruleset.utils.isLeaf
import com.saveourtool.diktat.ruleset.utils.nextCodeSibling
import com.saveourtool.diktat.ruleset.utils.nextSibling
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.jetbrains.kotlin.KtNodeTypes.CALL_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.CLASS
import org.jetbrains.kotlin.KtNodeTypes.CLASS_BODY
import org.jetbrains.kotlin.lexer.KtTokens.CLASS_KEYWORD
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.FILE
import org.jetbrains.kotlin.KtNodeTypes.FUN
import org.jetbrains.kotlin.KtNodeTypes.IMPORT_DIRECTIVE
import org.jetbrains.kotlin.lexer.KtTokens.IMPORT_KEYWORD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.saveourtool.diktat.ruleset.utils.search.findAllVariablesWithAssignmen
import com.saveourtool.diktat.util.applyToCode
import org.jetbrains.kotlin.psi.stubs.elements.KtFileElementType

import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.FILE
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.saveourtool.diktat.ruleset.utils.search.findAllVariablesWithUsages
import com.saveourtool.diktat.util.applyToCode
import org.jetbrains.kotlin.psi.stubs.elements.KtFileElementType

import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.FILE
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Assertions
import java.nio.file.Path
import kotlin.io.path.bufferedWriter
import kotlin.io.path.createDirectories
import kotlin.io.path.div

/**
Expand Down Expand Up @@ -56,19 +57,7 @@ open class FixTestBase(
val testComparatorUnit = testComparatorUnitSupplier(overrideRulesConfigList)
val result = testComparatorUnit
.compareFilesFromResources(expectedPath, testPath, overrideResourceReader)
if (!result.isSuccessful) {
Assertions.assertEquals(
result.expectedContentWithoutWarns,
result.actualContent,
) {
"Content are different"
}
}
Assertions.assertTrue(
result.isSuccessful
) {
"Detected delta: ${result.delta}"
}
result.assertSuccessful()
}

/**
Expand All @@ -89,14 +78,16 @@ open class FixTestBase(
@Language("kotlin") actualContent: String,
@Language("kotlin") expectedContent: String = actualContent,
tempDir: Path,
subFolder: String? = null,
overrideRulesConfigList: List<RulesConfig>? = null
): FileComparisonResult {
val actual = tempDir / "actual.kt"
val folder = subFolder?.let { tempDir / it }?.also { it.createDirectories() } ?: tempDir
val actual = folder / "actual.kt"
actual.bufferedWriter().use { out ->
out.write(actualContent)
}

val expected = tempDir / "expected.kt"
val expected = folder / "expected.kt"
expected.bufferedWriter().use { out ->
out.write(expectedContent)
}
Expand All @@ -112,5 +103,23 @@ open class FixTestBase(
private val defaultCallback = DiktatCallback { error, _ ->
log.warn { "Received linting error: $error" }
}

/**
* Asserts [FileComparisonResult] as [this] that content are equal and status is successful
*/
internal fun FileComparisonResult.assertSuccessful() {
Assertions.assertAll(
{
Assertions.assertTrue(isSuccessful) {
"Detected delta: $delta"
}
},
{
Assertions.assertEquals(expectedContentWithoutWarns, actualContent) {
"Content are different"
}
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,47 +62,42 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
deleteIfExistsSilently()
}

try {
configFilePath.copyTo(configFile, overwrite = true)

val processBuilder = createProcessBuilderWithCmd(testPath).apply {
redirectErrorStream(true)
redirectOutput(ProcessBuilder.Redirect.appendTo(saveLog.toFile()))

/*
* Inherit JAVA_HOME for the child process.
*/
inheritJavaHome()

/*
* On Windows, ktlint is often unable to relativize paths
* (see https://github.com/pinterest/ktlint/issues/1608).
*
* Also, ktlint needs `.editorconfig` to disable standard rules
*
* So let's force the temporary directory to be the
* sub-directory of the project root.
*/
temporaryDirectory(baseDirectoryPath / TEMP_DIRECTORY)
}

val saveProcess = processBuilder.start()
val saveExitCode = saveProcess.waitFor()
softly.assertThat(saveExitCode).describedAs("The exit code of SAVE").isZero
configFilePath.copyTo(configFile, overwrite = true)

softly.assertThat(saveLog).isRegularFile
val processBuilder = createProcessBuilderWithCmd(testPath).apply {
redirectErrorStream(true)
redirectOutput(ProcessBuilder.Redirect.appendTo(saveLog.toFile()))

val saveOutput = saveLog.readText()
/*
* Inherit JAVA_HOME for the child process.
*/
inheritJavaHome()

val saveCommandLine = processBuilder.command().joinToString(separator = " ")
softly.assertThat(saveOutput)
.describedAs("The output of \"$saveCommandLine\"")
.isNotEmpty
.contains("SUCCESS")
} finally {
configFile.deleteIfExistsSilently()
saveLog.deleteIfExistsSilently()
/*
* On Windows, ktlint is often unable to relativize paths
* (see https://github.com/pinterest/ktlint/issues/1608).
*
* Also, ktlint needs `.editorconfig` to disable standard rules
*
* So let's force the temporary directory to be the
* sub-directory of the project root.
*/
temporaryDirectory(baseDirectoryPath / TEMP_DIRECTORY)
}

val saveProcess = processBuilder.start()
val saveExitCode = saveProcess.waitFor()
softly.assertThat(saveExitCode).describedAs("The exit code of SAVE").isZero

softly.assertThat(saveLog).isRegularFile

val saveOutput = saveLog.readText()

val saveCommandLine = processBuilder.command().joinToString(separator = " ")
softly.assertThat(saveOutput)
.describedAs("The output of \"$saveCommandLine\"")
.isNotEmpty
.contains("SUCCESS")
}
}

Expand Down Expand Up @@ -131,7 +126,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
private val logger = KotlinLogging.logger {}
private const val SAVE_VERSION: String = "0.3.4"
private const val TEMP_DIRECTORY = ".save-cli"
private val baseDirectoryPath = tempDir.absolute()
private val baseDirectoryPath by lazy { tempDir.absolute() }

private fun getSaveForCurrentOs(): String {
val osName = System.getProperty("os.name")
Expand Down Expand Up @@ -183,20 +178,5 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
diktatFrom?.copyTo(diktat, overwrite = true)
}
}

@AfterAll
@JvmStatic
internal fun afterAll() {
val diktat = baseDirectoryPath / DIKTAT_FAT_JAR
val save = baseDirectoryPath / getSaveForCurrentOs()
val ktlint = baseDirectoryPath / KTLINT_FAT_JAR
val tempDirectory = baseDirectoryPath / TEMP_DIRECTORY

diktat.deleteIfExistsSilently()
ktlint.deleteIfExistsSilently()
save.deleteIfExistsSilently()

tempDirectory.deleteIfExistsRecursively()
}
}
}

0 comments on commit c1c3cab

Please sign in to comment.