diff --git a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtils.kt b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtils.kt index 668b23d492..83f84b483f 100644 --- a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtils.kt +++ b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtils.kt @@ -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})" } /** diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/spaces/IndentationRuleFixTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/spaces/IndentationRuleFixTest.kt index eae8025ea7..a5117a2a57 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/spaces/IndentationRuleFixTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/spaces/IndentationRuleFixTest.kt @@ -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) } } } diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/spaces/junit/IndentationTestFixExtension.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/spaces/junit/IndentationTestFixExtension.kt index aea6f3ceb1..2d28b53041 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/spaces/junit/IndentationTestFixExtension.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter3/spaces/junit/IndentationTestFixExtension.kt @@ -45,7 +45,8 @@ class IndentationTestFixExtension( actualCode, expectedCode, tempDir, - defaultConfig.withCustomParameters(customConfig).asRulesConfigList()) + overrideRulesConfigList = defaultConfig.withCustomParameters(customConfig).asRulesConfigList(), + ) if (!lintResult.isSuccessful) { assertThat(lintResult.actualContent) diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtilsTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtilsTest.kt index 022de84285..46406b50a5 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtilsTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtilsTest.kt @@ -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 @@ -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 diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/KotlinParserTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/KotlinParserTest.kt index 35a03bd966..fbd3b534a0 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/KotlinParserTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/KotlinParserTest.kt @@ -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 diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/VariablesWithAssignmentsSearchTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/VariablesWithAssignmentsSearchTest.kt index 757be91cf7..1c260d4fdc 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/VariablesWithAssignmentsSearchTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/VariablesWithAssignmentsSearchTest.kt @@ -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 diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/VariablesWithUsagesSearchTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/VariablesWithUsagesSearchTest.kt index 7d41add002..119f59cb7e 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/VariablesWithUsagesSearchTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/VariablesWithUsagesSearchTest.kt @@ -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 diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/FixTestBase.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/FixTestBase.kt index eb07b4e201..e29b96c527 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/FixTestBase.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/FixTestBase.kt @@ -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 /** @@ -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() } /** @@ -89,14 +78,16 @@ open class FixTestBase( @Language("kotlin") actualContent: String, @Language("kotlin") expectedContent: String = actualContent, tempDir: Path, + subFolder: String? = null, overrideRulesConfigList: List? = 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) } @@ -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" + } + } + ) + } } } diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt index 2ebc6315c1..1e814b7d61 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt @@ -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") } } @@ -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") @@ -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() - } } }