From 57bfc042180e42fd3570ab714ec0a681eb8ca28b Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 26 Oct 2023 17:16:25 +0300 Subject: [PATCH 01/12] Fixed smoke tests ### What's done: - added copying test data It closes #1738 --- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 16 +++-- .../ruleset/smoke/DiktatSmokeTestBase.kt | 65 ++++++++++++------- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt index 01874baabc..fd971760c9 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -23,11 +23,17 @@ class DiktatSmokeTest : DiktatSmokeTestBase() { expected: String, test: String, ) { - Assertions.assertTrue( - getTestComparatorUnit(config) - .compareFilesFromResources(expected, test) - .isSuccessful + val result = getTestComparatorUnit(config) + .compareFilesFromResources(expected, test) + Assertions.assertAll( + { + Assertions.assertTrue(result.isSuccessful) + }, + { + Assertions.assertEquals(result.expectedContentWithoutWarns, result.actualContent) + } ) + } @BeforeEach @@ -40,7 +46,7 @@ class DiktatSmokeTest : DiktatSmokeTestBase() { } private fun getTestComparatorUnit(config: Path) = TestComparatorUnit( - resourceFilePath = RESOURCE_FILE_PATH, + resourceFilePath = tempDir.toString(), function = { testFile -> format( ruleSetSupplier = { diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 9156ce21a6..5c7421b799 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -45,14 +45,24 @@ import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout +import org.junit.jupiter.api.io.TempDir import java.io.File import java.nio.file.Path import java.nio.file.Paths import java.time.LocalDate import java.util.concurrent.TimeUnit.SECONDS +import kotlin.io.path.ExperimentalPathApi +import kotlin.io.path.PathWalkOption +import kotlin.io.path.copyTo +import kotlin.io.path.createDirectories import kotlin.io.path.createTempFile import kotlin.io.path.inputStream +import kotlin.io.path.isDirectory +import kotlin.io.path.moveTo +import kotlin.io.path.name +import kotlin.io.path.toPath +import kotlin.io.path.walk import kotlin.io.path.writeText import kotlinx.serialization.builtins.ListSerializer @@ -70,7 +80,10 @@ abstract class DiktatSmokeTestBase { * @param rulesToOverride */ @Suppress("UnsafeCallOnNullableType") - private fun prepareOverriddenRulesConfig(rulesToDisable: List = emptyList(), rulesToOverride: RuleToConfig = emptyMap()): Path { + private fun prepareOverriddenRulesConfig( + rulesToDisable: List = emptyList(), + rulesToOverride: RuleToConfig = emptyMap(), + ): Path { val rulesConfig = RulesConfigReader().read(Paths.get(DEFAULT_CONFIG_PATH).inputStream())!! .toMutableList() .also { rulesConfig -> @@ -294,7 +307,6 @@ abstract class DiktatSmokeTestBase { @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) - @Disabled("https://github.com/saveourtool/diktat/issues/1737") fun `fix can cause long line`() { val configFilePath = prepareOverriddenRulesConfig( rulesToDisable = emptyList(), @@ -390,36 +402,41 @@ abstract class DiktatSmokeTestBase { companion object { private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" - const val RESOURCE_FILE_PATH = "test/smoke/src/main/kotlin" + private const val ROOT_RESOURCE_FILE_PATH = "test/smoke" + private const val RESOURCE_FILE_PATH = "$ROOT_RESOURCE_FILE_PATH/src/main/kotlin" private const val TEST_TIMEOUT_SECONDS = 30L - private val tmpFiles: MutableList = mutableListOf() + + @JvmStatic + @TempDir + internal var tempDir: Path = Paths.get("/invalid") @BeforeAll @JvmStatic + @OptIn(ExperimentalPathApi::class) @Suppress("AVOID_NULL_CHECKS") internal fun createTmpFiles() { - listOf( - "$RESOURCE_FILE_PATH/../../../build.gradle.kts_" to "build.gradle.kts", - "$RESOURCE_FILE_PATH/Example1Test.kt" to "Example1-2Test.kt", - ) - .map { (resource, targetFileName) -> - DiktatSmokeTestBase::class.java - .classLoader - .getResource(resource)!! - .toURI() - .let { - val tmpTestFile = File(it).parentFile.resolve(targetFileName) - File(it).copyTo(tmpTestFile, true) - } - .let { tmpFiles.add(it) } + val resourceFilePath = DiktatSmokeTestBase::class.java + .classLoader + .getResource(ROOT_RESOURCE_FILE_PATH) + .let { resource -> + requireNotNull(resource) { + "$ROOT_RESOURCE_FILE_PATH not found" + } + } + .toURI() + .toPath() + resourceFilePath.walk(PathWalkOption.INCLUDE_DIRECTORIES).forEach { file -> + if (file.isDirectory()) { + tempDir.resolve(file.relativize(resourceFilePath)).createDirectories() + } else { + val dest = tempDir.resolve(file.relativize(resourceFilePath)) + file.copyTo(dest) + when (file.name) { + "build.gradle.kts_" -> dest.moveTo(dest.parent.resolve("build.gradle.kts")) + "Example1Test.kt" -> dest.copyTo(dest.parent.resolve("Example1-2Test.kt")) + } } - } - @AfterAll - @JvmStatic - internal fun deleteTmpFiles() { - tmpFiles.forEach { - it.toPath().deleteIfExistsSilently() } } } From 36f7ec041db7818b26f6ee834aa2279aeab9052d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 26 Oct 2023 17:19:23 +0300 Subject: [PATCH 02/12] fixed using relativize --- .../saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 5c7421b799..43746f1892 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -427,9 +427,9 @@ abstract class DiktatSmokeTestBase { .toPath() resourceFilePath.walk(PathWalkOption.INCLUDE_DIRECTORIES).forEach { file -> if (file.isDirectory()) { - tempDir.resolve(file.relativize(resourceFilePath)).createDirectories() + tempDir.resolve(resourceFilePath.relativize(file)).createDirectories() } else { - val dest = tempDir.resolve(file.relativize(resourceFilePath)) + val dest = tempDir.resolve(resourceFilePath.relativize(file)) file.copyTo(dest) when (file.name) { "build.gradle.kts_" -> dest.moveTo(dest.parent.resolve("build.gradle.kts")) From 1d1ab021a9c62c6da46eae9d242b3c0fec736770 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 12:35:40 +0300 Subject: [PATCH 03/12] refactored TestComparatorUnit to use ResourceReader as main property --- .../chapter2/HeaderCommentRuleFixTest.kt | 28 +++++++++---------- .../saveourtool/diktat/util/FixTestBase.kt | 6 ++-- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 3 +- .../ruleset/smoke/DiktatSmokeTestBase.kt | 3 -- .../framework/processing/ResourceReader.kt | 16 +++++++++-- .../processing/TestComparatorUnit.kt | 22 +++++++++------ 6 files changed, 46 insertions(+), 32 deletions(-) diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/HeaderCommentRuleFixTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/HeaderCommentRuleFixTest.kt index eddec48bd0..77ae0de187 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/HeaderCommentRuleFixTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter2/HeaderCommentRuleFixTest.kt @@ -4,7 +4,7 @@ import com.saveourtool.diktat.common.config.rules.RulesConfig import com.saveourtool.diktat.ruleset.constants.Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT import com.saveourtool.diktat.ruleset.constants.Warnings.HEADER_WRONG_FORMAT import com.saveourtool.diktat.ruleset.rules.chapter2.comments.HeaderCommentRule -import com.saveourtool.diktat.test.framework.processing.ResourceReader +import com.saveourtool.diktat.test.framework.processing.ResourceReader.Companion.withReplacements import com.saveourtool.diktat.util.FixTestBase import generated.WarningNames @@ -31,13 +31,13 @@ class HeaderCommentRuleFixTest : FixTestBase( @Test @Tag(WarningNames.HEADER_WRONG_FORMAT) fun `new line should be inserted after header KDoc`(@TempDir tempDir: Path) { - fixAndCompare("NewlineAfterHeaderKdocExpected.kt", "NewlineAfterHeaderKdocTest.kt", resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement)) + fixAndCompare("NewlineAfterHeaderKdocExpected.kt", "NewlineAfterHeaderKdocTest.kt", overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }) } @Test @Tag(WarningNames.HEADER_MISSING_OR_WRONG_COPYRIGHT) fun `if no copyright is present and mandatoryCopyright=true, it is added`(@TempDir tempDir: Path) { - fixAndCompare("AutoCopyrightExpected.kt", "AutoCopyrightTest.kt", resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement)) + fixAndCompare("AutoCopyrightExpected.kt", "AutoCopyrightTest.kt", overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }) } @Test @@ -54,7 +54,7 @@ class HeaderCommentRuleFixTest : FixTestBase( ), RulesConfig(HEADER_WRONG_FORMAT.name, true, emptyMap()) ), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -64,7 +64,7 @@ class HeaderCommentRuleFixTest : FixTestBase( @Test @Tag(WarningNames.HEADER_NOT_BEFORE_PACKAGE) fun `header KDoc should be moved before package`(@TempDir tempDir: Path) { - fixAndCompare("MisplacedHeaderKdocExpected.kt", "MisplacedHeaderKdocTest.kt", resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement)) + fixAndCompare("MisplacedHeaderKdocExpected.kt", "MisplacedHeaderKdocTest.kt", overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }) } @Test @@ -72,7 +72,7 @@ class HeaderCommentRuleFixTest : FixTestBase( fun `header KDoc should be moved before package - no copyright`(@TempDir tempDir: Path) { fixAndCompare("MisplacedHeaderKdocNoCopyrightExpected.kt", "MisplacedHeaderKdocNoCopyrightTest.kt", listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, false, emptyMap()), RulesConfig(HEADER_WRONG_FORMAT.name, true, emptyMap())), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -82,7 +82,7 @@ class HeaderCommentRuleFixTest : FixTestBase( fixAndCompare( "MisplacedHeaderKdocAppendedCopyrightExpected.kt", "MisplacedHeaderKdocAppendedCopyrightTest.kt", - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -94,7 +94,7 @@ class HeaderCommentRuleFixTest : FixTestBase( "isCopyrightMandatory" to "true", "copyrightText" to "Copyright (c) My Company., Ltd. 2012-2019. All rights reserved." ))), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -106,7 +106,7 @@ class HeaderCommentRuleFixTest : FixTestBase( "isCopyrightMandatory" to "true", "copyrightText" to "Copyright (c) My Company., Ltd. 2021. All rights reserved." ))), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -118,7 +118,7 @@ class HeaderCommentRuleFixTest : FixTestBase( "isCopyrightMandatory" to "true", "copyrightText" to "Copyright (c) My Company., Ltd. 2012-2019. All rights reserved." ))), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -130,7 +130,7 @@ class HeaderCommentRuleFixTest : FixTestBase( "isCopyrightMandatory" to "true", "copyrightText" to "Copyright (c) My Company., Ltd. 2012-2019. All rights reserved." ))), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -142,7 +142,7 @@ class HeaderCommentRuleFixTest : FixTestBase( "isCopyrightMandatory" to "true", "copyrightText" to "Copyright (c) My Company., Ltd. 2012-2021. All rights reserved." ))), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -168,7 +168,7 @@ class HeaderCommentRuleFixTest : FixTestBase( | limitations under the License. """.trimMargin() ))), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } @@ -186,7 +186,7 @@ class HeaderCommentRuleFixTest : FixTestBase( | You may obtain a copy of the License at """.trimMargin() ))), - resourceReader = ResourceReader.withReplacements(tempDir, currentYearReplacement), + overrideResourceReader = { it.withReplacements(tempDir, currentYearReplacement) }, ) } 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 a5a3c2aaf9..483402f033 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 @@ -44,18 +44,18 @@ 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] * @param overrideRulesConfigList optional override to [defaultRulesConfigList] - * @param resourceReader [ResourceReader] to read resource content. + * @param overrideResourceReader [ResourceReader] to read resource content. * @see fixAndCompareContent */ protected fun fixAndCompare( expectedPath: String, testPath: String, overrideRulesConfigList: List? = null, - resourceReader: ResourceReader = ResourceReader.default, + overrideResourceReader: (ResourceReader) -> ResourceReader = { it }, ) { val testComparatorUnit = testComparatorUnitSupplier(overrideRulesConfigList) val result = testComparatorUnit - .compareFilesFromResources(expectedPath, testPath, resourceReader) + .compareFilesFromResources(expectedPath, testPath, overrideResourceReader) if (!result.isSuccessful) { Assertions.assertEquals( result.expectedContentWithoutWarns, diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt index fd971760c9..2162031d38 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -4,6 +4,7 @@ import com.saveourtool.diktat.api.DiktatError import com.saveourtool.diktat.ktlint.format import com.saveourtool.diktat.ruleset.rules.DiktatRuleConfigReaderImpl import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl +import com.saveourtool.diktat.test.framework.processing.ResourceReader import com.saveourtool.diktat.test.framework.processing.TestComparatorUnit import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach @@ -46,7 +47,7 @@ class DiktatSmokeTest : DiktatSmokeTestBase() { } private fun getTestComparatorUnit(config: Path) = TestComparatorUnit( - resourceFilePath = tempDir.toString(), + resourceReader = { tempDir.resolve("src/main/kotlin").resolve(it) }, function = { testFile -> format( ruleSetSupplier = { diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 43746f1892..5b414945e7 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -33,12 +33,10 @@ import com.saveourtool.diktat.ruleset.utils.indentation.IndentationConfig import com.saveourtool.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_AFTER_OPERATORS import com.saveourtool.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_BEFORE_DOT import com.saveourtool.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_FOR_EXPRESSION_BODIES -import com.saveourtool.diktat.test.framework.util.deleteIfExistsSilently import com.charleskorn.kaml.Yaml import com.charleskorn.kaml.YamlConfiguration import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Disabled @@ -46,7 +44,6 @@ import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout import org.junit.jupiter.api.io.TempDir -import java.io.File import java.nio.file.Path import java.nio.file.Paths diff --git a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt index ccb413c92d..6792ea9853 100644 --- a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt +++ b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt @@ -11,7 +11,7 @@ import kotlin.io.path.writeText /** * A base interface to read resources for testing purposes */ -interface ResourceReader : Function1 { +fun interface ResourceReader : Function1 { /** * @param resourceName * @return [Path] for provider [resourceName] @@ -40,11 +40,11 @@ interface ResourceReader : Function1 { * @param replacements a map of replacements which will be applied to actual and expected content before comparing. * @return Instance of [ResourceReader] with replacements of content */ - fun withReplacements( + fun ResourceReader.withReplacements( tempDir: Path, replacements: Map, ): ResourceReader = object : ResourceReader { - override fun invoke(resourceName: String): Path? = default.invoke(resourceName) + override fun invoke(resourceName: String): Path? = this@withReplacements.invoke(resourceName) ?.let { originalFile -> tempDir.resolve(resourceName) .also { resultFile -> @@ -57,6 +57,16 @@ interface ResourceReader : Function1 { } } + /** + * @param resourceFilePath a prefix for loading resources + * @return Instance of [ResourceReader] which loads resource with [resourceFilePath] as prefix + */ + fun ResourceReader.withPrefix( + resourceFilePath: String, + ): ResourceReader = object : ResourceReader { + override fun invoke(resourceName: String): Path? = this@withPrefix.invoke("$resourceFilePath/$resourceName") + } + private fun String.replaceAll(replacements: Map): String = replacements.entries .fold(this) { result, replacement -> result.replace(replacement.key, replacement.value) diff --git a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/TestComparatorUnit.kt b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/TestComparatorUnit.kt index 8731fb2033..78a619ffc0 100644 --- a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/TestComparatorUnit.kt +++ b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/TestComparatorUnit.kt @@ -1,5 +1,6 @@ package com.saveourtool.diktat.test.framework.processing +import com.saveourtool.diktat.test.framework.processing.ResourceReader.Companion.withPrefix import com.saveourtool.diktat.test.framework.util.readTextOrNull import com.saveourtool.diktat.test.framework.util.toUnixEndLines import io.github.oshai.kotlinlogging.KotlinLogging @@ -10,15 +11,19 @@ import kotlin.io.path.name /** * Class that can apply transformation to an input file and then compare with expected result and output difference. * - * @param resourceFilePath only used when the files are loaded as resources, + * @param resourceReader only used when the files are loaded as resources, * via [compareFilesFromResources]. * @param function a transformation that will be applied to the file */ @Suppress("ForbiddenComment", "TYPE_ALIAS") class TestComparatorUnit( - private val resourceFilePath: String, + private val resourceReader: ResourceReader = ResourceReader.default, private val function: (testFile: Path) -> String, ) { + constructor( + resourceFilePath: String, + function: (testFile: Path) -> String, + ): this(resourceReader = ResourceReader.default.withPrefix(resourceFilePath), function = function) /** * @param expectedResult the name of the resource which has the expected * content. The trailing newline, if any, **won't be read** as a separate @@ -27,7 +32,7 @@ class TestComparatorUnit( * `newlineAtEnd` is `true`), then the file should end with **two** * consecutive linebreaks. * @param testFileStr the name of the resource which has the original content. - * @param resourceReader [ResourceReader] to read resource content + * @param overrideResourceReader [ResourceReader] to read resource content * @return the result of file comparison by their content. * @see compareFilesFromFileSystem */ @@ -35,17 +40,18 @@ class TestComparatorUnit( fun compareFilesFromResources( expectedResult: String, testFileStr: String, - resourceReader: ResourceReader = ResourceReader.default, + overrideResourceReader: (ResourceReader) -> ResourceReader = { it }, ): FileComparisonResult { - val expectedPath = resourceReader("$resourceFilePath/$expectedResult") - val testPath = resourceReader("$resourceFilePath/$testFileStr") + val overriddenResourceReader = overrideResourceReader(resourceReader) + val expectedPath = overriddenResourceReader(expectedResult) + val testPath = overriddenResourceReader(testFileStr) if (testPath == null || expectedPath == null) { log.error { "Not able to find files for running test: $expectedResult and $testFileStr" } return FileComparisonResult( isSuccessful = false, delta = null, - actualContent = "// $resourceFilePath/$expectedResult is found: ${testPath != null}", - expectedContent = "// $resourceFilePath/$testFileStr is found: ${expectedPath != null}") + actualContent = "// $expectedResult is found: ${testPath != null}", + expectedContent = "// $testFileStr is found: ${expectedPath != null}") } return compareFilesFromFileSystem( From 0d8e06cc2989c9249ac50147c47b0adddb6232da Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 13:11:22 +0300 Subject: [PATCH 04/12] ktlint requires normalized path --- .../com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt index 2162031d38..61eb2cf45d 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -4,7 +4,6 @@ import com.saveourtool.diktat.api.DiktatError import com.saveourtool.diktat.ktlint.format import com.saveourtool.diktat.ruleset.rules.DiktatRuleConfigReaderImpl import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl -import com.saveourtool.diktat.test.framework.processing.ResourceReader import com.saveourtool.diktat.test.framework.processing.TestComparatorUnit import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach @@ -47,7 +46,7 @@ class DiktatSmokeTest : DiktatSmokeTestBase() { } private fun getTestComparatorUnit(config: Path) = TestComparatorUnit( - resourceReader = { tempDir.resolve("src/main/kotlin").resolve(it) }, + resourceReader = { tempDir.resolve("src/main/kotlin").resolve(it).normalize() }, function = { testFile -> format( ruleSetSupplier = { From c052e8743c73db334b4dc8779f268e659c724e76 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 13:13:37 +0300 Subject: [PATCH 05/12] removed unused property --- .../com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index 5b414945e7..ef6bee4d8c 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -400,7 +400,6 @@ abstract class DiktatSmokeTestBase { companion object { private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" private const val ROOT_RESOURCE_FILE_PATH = "test/smoke" - private const val RESOURCE_FILE_PATH = "$ROOT_RESOURCE_FILE_PATH/src/main/kotlin" private const val TEST_TIMEOUT_SECONDS = 30L @JvmStatic From 2b997f1f7afb07efd65be4f2b439912da32c2727 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 13:21:11 +0300 Subject: [PATCH 06/12] fixed save tests --- .../diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 e5c6c68af1..a477dbfc41 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 @@ -111,9 +111,9 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { * @return ProcessBuilder */ private fun createProcessBuilderWithCmd(testPath: String): ProcessBuilder { - val savePath = "$BASE_DIRECTORY/${getSaveForCurrentOs()}" + val savePath = baseDirectoryPath.resolve(getSaveForCurrentOs()).toString() val saveArgs = arrayOf( - "$BASE_DIRECTORY/src/main/kotlin", + baseDirectoryPath.resolve("src/main/kotlin").toString(), testPath, "--log", "all" @@ -129,10 +129,9 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { companion object { private val logger = KotlinLogging.logger {} - private const val BASE_DIRECTORY = "src/test/resources/test/smoke" private const val SAVE_VERSION: String = "0.3.4" private const val TEMP_DIRECTORY = ".save-cli" - private val baseDirectoryPath = Path(BASE_DIRECTORY).absolute() + private val baseDirectoryPath = tempDir.absolute() private fun getSaveForCurrentOs(): String { val osName = System.getProperty("os.name") From f396844f937113ba093843734791da30d4649027 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 13:23:10 +0300 Subject: [PATCH 07/12] reverted temp data --- .../saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt index ef6bee4d8c..f659022ad3 100644 --- a/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt +++ b/diktat-ruleset/src/test/kotlin/com/saveourtool/diktat/ruleset/smoke/DiktatSmokeTestBase.kt @@ -77,10 +77,7 @@ abstract class DiktatSmokeTestBase { * @param rulesToOverride */ @Suppress("UnsafeCallOnNullableType") - private fun prepareOverriddenRulesConfig( - rulesToDisable: List = emptyList(), - rulesToOverride: RuleToConfig = emptyMap(), - ): Path { + private fun prepareOverriddenRulesConfig(rulesToDisable: List = emptyList(), rulesToOverride: RuleToConfig = emptyMap()): Path { val rulesConfig = RulesConfigReader().read(Paths.get(DEFAULT_CONFIG_PATH).inputStream())!! .toMutableList() .also { rulesConfig -> @@ -304,6 +301,7 @@ abstract class DiktatSmokeTestBase { @Test @Tag("DiktatRuleSetProvider") @Timeout(TEST_TIMEOUT_SECONDS, unit = SECONDS) + @Disabled("https://github.com/saveourtool/diktat/issues/1737") fun `fix can cause long line`() { val configFilePath = prepareOverriddenRulesConfig( rulesToDisable = emptyList(), From 4d00ed2c02fad3414d5edc0994b68c4bcb0fd8a7 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 13:24:30 +0300 Subject: [PATCH 08/12] fixed reference in kdoc --- .../saveourtool/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 a477dbfc41..0a510372a4 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 @@ -43,7 +43,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { override fun assertUnfixedLintErrors(diktatErrorConsumer: (List) -> Unit) = Unit /** - * @param testPath path to file with code that will be transformed by formatter, relative to [TestComparatorUnit.resourceFilePath] + * @param testPath path to file with code that will be transformed by formatter, loaded by [TestComparatorUnit.resourceReader] * @param configFilePath path of diktat-analysis file */ @Suppress("TOO_LONG_FUNCTION") @@ -107,7 +107,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { } /** - * @param testPath path to file with code that will be transformed by formatter, relative to [TestComparatorUnit.resourceFilePath] + * @param testPath path to file with code that will be transformed by formatter, loaded by [TestComparatorUnit.resourceReader] * @return ProcessBuilder */ private fun createProcessBuilderWithCmd(testPath: String): ProcessBuilder { From 763ea9635e790294db721662f667ed84c27c3ac7 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 13:28:17 +0300 Subject: [PATCH 09/12] ResourceReader as lambda --- .../test/framework/processing/ResourceReader.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt index 6792ea9853..5bc13ecb0f 100644 --- a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt +++ b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt @@ -24,8 +24,8 @@ fun interface ResourceReader : Function1 { /** * Default implementation of [ResourceReader] */ - val default: ResourceReader = object : ResourceReader { - override fun invoke(resourceName: String): Path? = javaClass.classLoader.getResource(resourceName) + val default: ResourceReader = ResourceReader { resourceName -> + javaClass.classLoader.getResource(resourceName) ?.toURI() ?.toPath() .also { @@ -43,8 +43,8 @@ fun interface ResourceReader : Function1 { fun ResourceReader.withReplacements( tempDir: Path, replacements: Map, - ): ResourceReader = object : ResourceReader { - override fun invoke(resourceName: String): Path? = this@withReplacements.invoke(resourceName) + ): ResourceReader = ResourceReader { resourceName -> + this@withReplacements.invoke(resourceName) ?.let { originalFile -> tempDir.resolve(resourceName) .also { resultFile -> @@ -63,9 +63,7 @@ fun interface ResourceReader : Function1 { */ fun ResourceReader.withPrefix( resourceFilePath: String, - ): ResourceReader = object : ResourceReader { - override fun invoke(resourceName: String): Path? = this@withPrefix.invoke("$resourceFilePath/$resourceName") - } + ): ResourceReader = ResourceReader { resourceName -> this@withPrefix.invoke("$resourceFilePath/$resourceName") } private fun String.replaceAll(replacements: Map): String = replacements.entries .fold(this) { result, replacement -> From d7bbed39097dcb09e30d8358e96d16b12af70717 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 13:31:14 +0300 Subject: [PATCH 10/12] fixed the issue with class name in component --- .../diktat/test/framework/processing/ResourceReader.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt index 5bc13ecb0f..daa5147506 100644 --- a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt +++ b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/ResourceReader.kt @@ -25,7 +25,9 @@ fun interface ResourceReader : Function1 { * Default implementation of [ResourceReader] */ val default: ResourceReader = ResourceReader { resourceName -> - javaClass.classLoader.getResource(resourceName) + ResourceReader::class.java + .classLoader + .getResource(resourceName) ?.toURI() ?.toPath() .also { From 82b54af5336a9729b86914e763e17e8f91fa9b83 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 13:35:20 +0300 Subject: [PATCH 11/12] diktatFix --- .../kotlin/com/saveourtool/diktat/util/FixTestBase.kt | 2 +- .../test/framework/processing/TestComparatorUnit.kt | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) 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 483402f033..eb07b4e201 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 @@ -44,7 +44,7 @@ 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] * @param overrideRulesConfigList optional override to [defaultRulesConfigList] - * @param overrideResourceReader [ResourceReader] to read resource content. + * @param overrideResourceReader function to override [ResourceReader] to read resource content. * @see fixAndCompareContent */ protected fun fixAndCompare( diff --git a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/TestComparatorUnit.kt b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/TestComparatorUnit.kt index 78a619ffc0..a598538268 100644 --- a/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/TestComparatorUnit.kt +++ b/diktat-test-framework/src/main/kotlin/com/saveourtool/diktat/test/framework/processing/TestComparatorUnit.kt @@ -23,7 +23,11 @@ class TestComparatorUnit( constructor( resourceFilePath: String, function: (testFile: Path) -> String, - ): this(resourceReader = ResourceReader.default.withPrefix(resourceFilePath), function = function) + ) : this( + resourceReader = ResourceReader.default.withPrefix(resourceFilePath), + function = function, + ) + /** * @param expectedResult the name of the resource which has the expected * content. The trailing newline, if any, **won't be read** as a separate @@ -32,7 +36,7 @@ class TestComparatorUnit( * `newlineAtEnd` is `true`), then the file should end with **two** * consecutive linebreaks. * @param testFileStr the name of the resource which has the original content. - * @param overrideResourceReader [ResourceReader] to read resource content + * @param overrideResourceReader function to override [ResourceReader] to read resource content * @return the result of file comparison by their content. * @see compareFilesFromFileSystem */ From d854c291bd706fb6b8fa41be471bf3b01ced120d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 27 Oct 2023 16:13:04 +0300 Subject: [PATCH 12/12] fixed run for linux --- .../com/saveourtool/diktat/ruleset/smoke/DiktatSaveSmokeTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0a510372a4..2ebc6315c1 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 @@ -121,7 +121,7 @@ class DiktatSaveSmokeTest : DiktatSmokeTestBase() { return when { System.getProperty("os.name").isWindows() -> arrayOf(savePath, *saveArgs) - else -> arrayOf("sh", "-c", "chmod 777 $savePath ; ./$savePath ${saveArgs.joinToString(" ")}") + else -> arrayOf("sh", "-c", "chmod 777 $savePath ; $savePath ${saveArgs.joinToString(" ")}") }.let { args -> ProcessBuilder(*args) }