Skip to content

Commit

Permalink
Remove diktat-test-framework (#1785)
Browse files Browse the repository at this point in the history
- removed `diktat-test-framework`
- added `diktat-test-common` with common util methods for tests
- removed usage of diff multiplatform tool
  • Loading branch information
nulls authored Nov 8, 2023
1 parent dc0f747 commit d48f947
Show file tree
Hide file tree
Showing 41 changed files with 172 additions and 961 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ jobs:
gradle-build-module: |-
:diktat-api
:diktat-common
:diktat-common-test
:diktat-ktlint-engine
:diktat-gradle-plugin
:diktat-maven-plugin
:diktat-rules
:diktat-ruleset
:diktat-test-framework
:diktat-dev-ksp
:diktat-cli
gradle-build-configuration: |-
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Rules are very simple:

Main components are:
1) diktat-rules — number of rules that are supported by diKTat;
2) diktat-test-framework — functional/unit test framework that can be used for running your code fixer on the initial code and compare it with the expected result;
2) diktat-common-testutil methods for functional/unit tests that can be used for running your code fixer on the initial code and compare it with the expected result;
3) also see our demo: diktat-demo in a separate repository.

Mainly we wanted to create a common configurable mechanism that
Expand All @@ -31,7 +31,7 @@ Before you make a pull request, make sure the build is clean as we have lot of t
$ mvn clean install
```

# Hooks
# Hooks

We have some hooks to a commit messages:
1) your commit message should have the following format:
Expand All @@ -40,7 +40,7 @@ Brief Description
### What's done:
1) Long description
2) Long description
2) Long description
```

2) Please also do not forget to update documentation on Wiki after the merge approval and before merge.
2 changes: 1 addition & 1 deletion diktat-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
implementation(libs.log4j2.core)
implementation(libs.log4j2.slf4j2)

testImplementation(projects.diktatTestFramework)
testImplementation(projects.diktatCommonTest)
testImplementation(libs.junit.jupiter)
testImplementation(libs.junit.platform.suite)
testImplementation(libs.assertj.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ class DiktatSmokeTest : DiktatSmokeTestBase() {
) {
val result = getTestComparatorUnit(config)
.compareFilesFromResources(expected, test)
Assertions.assertAll(
{
Assertions.assertTrue(result.isSuccessful)
},
{
Assertions.assertEquals(result.expectedContentWithoutWarns, result.actualContent)
}
)

result.assertSuccessful()
}

@BeforeEach
Expand Down
14 changes: 14 additions & 0 deletions diktat-common-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id("com.saveourtool.diktat.buildutils.kotlin-jvm-configuration")
id("com.saveourtool.diktat.buildutils.code-quality-convention")
id("com.saveourtool.diktat.buildutils.publishing-default-configuration")
}

project.description = "Diktat common for tests"

dependencies {
api(projects.diktatCommon)
implementation(libs.kotlin.logging)
implementation(libs.junit.jupiter.api)
implementation(libs.assertj.core)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.saveourtool.diktat.test.framework.processing

import com.saveourtool.diktat.test.framework.processing.ResourceReader.Companion.withPrefix
import com.saveourtool.diktat.test.framework.util.NEWLINE
import com.saveourtool.diktat.test.framework.util.readTextOrNull
import com.saveourtool.diktat.test.framework.util.toUnixEndLines
import io.github.oshai.kotlinlogging.KotlinLogging
import java.nio.file.Path
import kotlin.io.path.isRegularFile
import kotlin.io.path.name

/**
* Class that can apply transformation to an input file and then compare with expected result and output difference.
Expand Down Expand Up @@ -45,17 +45,18 @@ class TestComparatorUnit(
expectedResult: String,
testFileStr: String,
overrideResourceReader: (ResourceReader) -> ResourceReader = { it },
): FileComparisonResult {
): TestFileContent {
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 = "// $expectedResult is found: ${testPath != null}",
expectedContent = "// $testFileStr is found: ${expectedPath != null}")
return NotFoundResourcesTestFileContent(
expectedResource = expectedResult,
expectedPath = expectedPath,
actualResource = testFileStr,
actualPath = testPath,
)
}

return compareFilesFromFileSystem(
Expand All @@ -78,35 +79,35 @@ class TestComparatorUnit(
fun compareFilesFromFileSystem(
expectedFile: Path,
testFile: Path,
): FileComparisonResult {
): TestFileContent {
if (!testFile.isRegularFile() || !expectedFile.isRegularFile()) {
log.error { "Not able to find files for running test: $expectedFile and $testFile" }
return FileComparisonResult(
isSuccessful = false,
delta = null,
actualContent = "// $testFile is a regular file: ${testFile.isRegularFile()}",
expectedContent = "// $expectedFile is a regular file: ${expectedFile.isRegularFile()}")
return NotFoundFilesTestFileContent(
expectedPath = expectedFile,
actualPath = testFile,
)
}

val actualFileContent = function(testFile).toUnixEndLines()
val expectedFileContent = expectedFile.readTextOrNull().orEmpty()

val comparator = FileComparator(
expectedFile.name,
expectedFileContent,
actualFileContent,
)

return FileComparisonResult(
isSuccessful = comparator.compareFilesEqual(),
delta = comparator.delta,
return DefaultTestFileContent(
actualContent = actualFileContent,
expectedContent = expectedFileContent,
expectedContentWithoutWarns = comparator.expectedResultWithoutWarns,
expectedContent = expectedFileContent.withoutWarns(),
)
}

private companion object {
private val log = KotlinLogging.logger {}
private val warnRegex = (".*// ;warn:?(.*):(\\d*): (.+)").toRegex()

/**
* @return Expected result without lines with warns
*/
private fun String.withoutWarns(): String = split(NEWLINE)
.filterNot { line ->
line.contains(warnRegex)
}
.joinToString(NEWLINE.toString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* It's a class container for test file content.
* Plus exception cases when resource or file is not found
*/

package com.saveourtool.diktat.test.framework.processing

import com.saveourtool.diktat.test.framework.util.describe

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.SoftAssertions.assertSoftly
import org.intellij.lang.annotations.Language

import java.nio.file.Path

import kotlin.io.path.absolutePathString

/**
* A base interface for content of test file
*/
sealed interface TestFileContent {
/**
* Asserts [TestFileContent] that content are equal
*/
fun assertSuccessful()
}

/**
* Implementation of [TestFileContent] when resources are not found
*
* @param expectedResource
* @param expectedPath
* @param actualResource
* @param actualPath
*/
data class NotFoundResourcesTestFileContent(
private val expectedResource: String,
private val expectedPath: Path?,
private val actualResource: String,
private val actualPath: Path?,
) : TestFileContent {
override fun assertSuccessful() {
assertSoftly { softly ->
softly.assertThat(expectedPath)
.describedAs("Expected resource <%s>", expectedResource)
.isNotNull
softly.assertThat(actualPath)
.describedAs("Actual resource <%s>", actualResource)
.isNotNull
}
}
}

/**
* Implementation of [TestFileContent] when files are not found
*
* @param expectedPath
* @param actualPath
*/
data class NotFoundFilesTestFileContent(
private val expectedPath: Path,
private val actualPath: Path,
) : TestFileContent {
override fun assertSuccessful() {
assertSoftly { softly ->
softly.assertThat(expectedPath)
.describedAs("Expected file <%s>", expectedPath.absolutePathString())
.isRegularFile
softly.assertThat(actualPath)
.describedAs("Actual resource <%s>", actualPath.absolutePathString())
.isRegularFile
}
}
}

/**
* The result of files being compared by their content.
*
* @param actualContent the actual file content (possibly slightly different
* from the original after `diktat:check` is run).
* @param expectedContent the expected file content without warns.
*/
data class DefaultTestFileContent(
@Language("kotlin") private val actualContent: String,
@Language("kotlin") private val expectedContent: String,
) : TestFileContent {
override fun assertSuccessful() {
assertThat(actualContent)
.describedAs("lint result for ", actualContent.describe())
.isEqualTo(expectedContent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import kotlin.io.path.isDirectory
import kotlin.io.path.isSameFileAs
import kotlin.io.path.readText

const val NEWLINE = '\n'

private val logger = KotlinLogging.logger {}

/**
Expand Down Expand Up @@ -291,6 +293,26 @@ fun Path.readTextOrNull(): String? = try {
null
}

/**
* @return a brief description of this code fragment.
*/
fun String.describe(): String {
val lines = splitToSequence(NEWLINE)

var first: String? = null

val count = lines.onEachIndexed { index, line ->
if (index == 0) {
first = line
}
}.count()

return when (count) {
1 -> "\"$this\""
else -> "\"$first\u2026\" ($count line(s))"
}
}

/**
* Retries the execution of the [block].
*
Expand Down
2 changes: 1 addition & 1 deletion diktat-rules/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies {
// guava is used for string case utils
implementation(libs.guava)
implementation(libs.kotlin.logging)
testImplementation(projects.diktatTestFramework)
testImplementation(projects.diktatCommonTest)
testImplementation(projects.diktatKtlintEngine)
testImplementation(libs.log4j2.slf4j2)
testImplementation(libs.junit.jupiter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.saveourtool.diktat.ruleset.chapter2

import com.saveourtool.diktat.ruleset.chapter2.CommentsFormattingTest.Companion.indentStyleComment
import com.saveourtool.diktat.ruleset.chapter3.spaces.describe
import com.saveourtool.diktat.ruleset.rules.chapter2.kdoc.CommentsFormatting
import com.saveourtool.diktat.util.FixTestBase

import generated.WarningNames.COMMENT_WHITE_SPACE
import generated.WarningNames.FIRST_COMMENT_NO_BLANK_LINE
import generated.WarningNames.IF_ELSE_COMMENTS
import generated.WarningNames.WRONG_NEWLINES_AROUND_KDOC
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Tags
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -55,8 +52,6 @@ class CommentsFormattingFixTest : FixTestBase("test/paragraph2/kdoc/", ::Comment
@Tag(COMMENT_WHITE_SPACE)
fun `indent-style header in a block comment should be preserved`(@TempDir tempDir: Path) {
val lintResult = fixAndCompareContent(indentStyleComment, tempDir = tempDir)
assertThat(lintResult.actualContent)
.describedAs("lint result for ${indentStyleComment.describe()}")
.isEqualTo(lintResult.expectedContent)
lintResult.assertSuccessful()
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.saveourtool.diktat.ruleset.chapter3

import com.saveourtool.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import com.saveourtool.diktat.ruleset.chapter3.spaces.describe
import com.saveourtool.diktat.ruleset.constants.Warnings.BLANK_LINE_BETWEEN_PROPERTIES
import com.saveourtool.diktat.ruleset.constants.Warnings.WRONG_ORDER_IN_CLASS_LIKE_STRUCTURES
import com.saveourtool.diktat.ruleset.rules.chapter3.ClassLikeStructuresOrderRule
import com.saveourtool.diktat.util.LintTestBase

import com.saveourtool.diktat.api.DiktatError
import com.saveourtool.diktat.test.framework.util.describe
import generated.WarningNames
import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language
Expand Down
Loading

0 comments on commit d48f947

Please sign in to comment.