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

Enhance tests in diktat #1780

Merged
merged 1 commit into from
Nov 7, 2023
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 @@ -849,7 +849,7 @@
* @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})"

Check warning on line 852 in diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtils.kt

View check run for this annotation

Codecov / codecov/patch

diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtils.kt#L852

Added line #L852 was not covered by tests
}

/**
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() {
kgevorkyan marked this conversation as resolved.
Show resolved Hide resolved
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()
}
}
}
Loading