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

FixPlugin: Compare test files in course of building the TestResults by full paths #478

Merged
merged 7 commits into from
Jan 18, 2023
8 changes: 5 additions & 3 deletions examples/kotlin-diktat/fix/sarif/save.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
description = "SARIF fix objects: Smoke tests for diktat"
suiteName = "SARIF fix objects - Autofix"

[fix]
actualFixFormat = "SARIF"
actualFixSarifFileName = "save-fixes.sarif"
# FixMe: Until https://github.com/saveourtool/sarif-utils/issues/23
# FixMe: compare only by names, but should by full paths
#[fix]
#actualFixFormat = "SARIF"
#actualFixSarifFileName = "save-fixes.sarif"
1 change: 1 addition & 0 deletions examples/kotlin-diktat/fix/smoke/save.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
tags = ["smoke"]
description = "Smoke tests for diktat"
suiteName = "Autofix: Smoke Tests"
batchSize = 2

[fix]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.saveourtool.save.chapter2

class Example {
@get:JvmName("getIsValid")
val isValid = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package test.smoke

class example {
@get : JvmName("getIsValid")
val isValid = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[general]
tags = ["smoke"]
description = "Test for tests with same name"
suiteName = "Autofix: Smoke Tests"

[fix]
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class GeneralTest {
fun `examples test from subfolder`() {
// Almost all result statuses should be Pass, except the few cases
doTest("../examples/kotlin-diktat/", "fix/smoke") { reports ->
assertEquals(5, reports.size)
assertEquals(6, reports.size)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ClassicFixTest {
runTestsWithDiktat(
listOf(
"fix/save.toml"
), 6
), 7
)
}

Expand All @@ -28,7 +28,7 @@ class ClassicFixTest {
runTestsWithDiktat(
listOf(
"fix/smoke/save.toml"
), 5
), 6
)
}

Expand All @@ -37,7 +37,7 @@ class ClassicFixTest {
runTestsWithDiktat(
listOf(
"fix/smoke/src/main/kotlin/com/saveourtool/save/"
), 5
), 6
)
}

Expand Down Expand Up @@ -76,4 +76,16 @@ class ClassicFixTest {
), 1
)
}

@Test
fun `tests with the same name`() {
runTestsWithDiktat(
listOf(
"fix/smoke/src/main/kotlin/com/saveourtool/save/Example1Test.kt",
"fix/smoke/src/main/kotlin/com/saveourtool/save/Example1Expected.kt",
"fix/smoke/src/main/kotlin/com/saveourtool/save/chapter2/Example1Test.kt",
"fix/smoke/src/main/kotlin/com/saveourtool/save/chapter2/Example1Expected.kt",
), 2
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.test.Test
class FixDirTest {
@Test
fun `execute fix plugin`() {
runTestsWithDiktat(listOf("fix/smoke/src/main/kotlin/com/saveourtool/save"), 5)
runTestsWithDiktat(listOf("fix/smoke/src/main/kotlin/com/saveourtool/save"), 6)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,9 @@ class FixPlugin(
): List<TestResult> = testCopyToExpectedFilesMap.map { (testCopy, expected) ->
val fixedLines = fs.readLines(testCopy)
val expectedLines = fs.readLines(expected)

// FixMe: https://github.com/saveourtool/save-cli/issues/473
val test = testToExpectedFilesMap.first { (test, _) -> test.name == testCopy.name }.first

val test = testToExpectedFilesMap.first { (test, _) ->
isComparingTestAndCopy(test, testCopy)
}.first
TestResult(
FixTestFiles(test, expected),
checkStatus(expectedLines, fixedLines),
Expand All @@ -247,6 +246,19 @@ class FixPlugin(
)
}

private fun isComparingTestAndCopy(test: Path, testCopy: Path): Boolean {
// TestCopyPath stored in tmpDir, holding the whole hierarchy of original file
// while testPath comes to us with path, starting from testRootPath, so we compare them in such way
val testCopyPath = testCopy.relativeTo(FileSystem.SYSTEM_TEMPORARY_DIRECTORY)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also write a comments, why do we calculate paths in this way?
Some words like: testCopyPath stored in tmpDir, holdind the whole hierarchy of original file
while test file comes to us with path , starting from testRootPath, so we compare them in such way

.toString()
.substringAfter(Path.DIRECTORY_SEPARATOR)
.toPath()

val testPath = test.createRelativePathToTheRoot(testConfig.getRootConfig().directory).toPath()

return testCopyPath.compareTo(testPath) == 0
}

private fun failTestResult(
chunk: List<FixTestFiles>,
ex: ProcessExecutionException,
Expand Down