Skip to content

Commit b00599a

Browse files
authored
FixPlugin: Compare test files in course of building the TestResults by full paths (#478)
* FixPlugin: Compare test files in course of building the TestResults by full paths * Added test fore tests with the same name Closes #473
1 parent e5ced44 commit b00599a

9 files changed

Lines changed: 57 additions & 12 deletions

File tree

examples/kotlin-diktat/fix/sarif/save.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
description = "SARIF fix objects: Smoke tests for diktat"
44
suiteName = "SARIF fix objects - Autofix"
55

6-
[fix]
7-
actualFixFormat = "SARIF"
8-
actualFixSarifFileName = "save-fixes.sarif"
6+
# FixMe: Until https://github.com/saveourtool/sarif-utils/issues/23
7+
# FixMe: compare only by names, but should by full paths
8+
#[fix]
9+
#actualFixFormat = "SARIF"
10+
#actualFixSarifFileName = "save-fixes.sarif"

examples/kotlin-diktat/fix/smoke/save.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
tags = ["smoke"]
33
description = "Smoke tests for diktat"
44
suiteName = "Autofix: Smoke Tests"
5+
batchSize = 2
56

67
[fix]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.saveourtool.save.chapter2
2+
3+
class Example {
4+
@get:JvmName("getIsValid")
5+
val isValid = true
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package test.smoke
2+
3+
class example {
4+
@get : JvmName("getIsValid")
5+
val isValid = true
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[general]
2+
tags = ["smoke"]
3+
description = "Test for tests with same name"
4+
suiteName = "Autofix: Smoke Tests"
5+
6+
[fix]

save-cli/src/jvmTest/kotlin/com/saveourtool/save/cli/GeneralTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class GeneralTest {
7070
fun `examples test from subfolder`() {
7171
// Almost all result statuses should be Pass, except the few cases
7272
doTest("../examples/kotlin-diktat/", "fix/smoke") { reports ->
73-
assertEquals(5, reports.size)
73+
assertEquals(6, reports.size)
7474
}
7575
}
7676

save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/integration/ClassicFixTest.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ClassicFixTest {
1919
runTestsWithDiktat(
2020
listOf(
2121
"fix/save.toml"
22-
), 6
22+
), 7
2323
)
2424
}
2525

@@ -28,7 +28,7 @@ class ClassicFixTest {
2828
runTestsWithDiktat(
2929
listOf(
3030
"fix/smoke/save.toml"
31-
), 5
31+
), 6
3232
)
3333
}
3434

@@ -37,7 +37,7 @@ class ClassicFixTest {
3737
runTestsWithDiktat(
3838
listOf(
3939
"fix/smoke/src/main/kotlin/com/saveourtool/save/"
40-
), 5
40+
), 6
4141
)
4242
}
4343

@@ -76,4 +76,16 @@ class ClassicFixTest {
7676
), 1
7777
)
7878
}
79+
80+
@Test
81+
fun `tests with the same name`() {
82+
runTestsWithDiktat(
83+
listOf(
84+
"fix/smoke/src/main/kotlin/com/saveourtool/save/Example1Test.kt",
85+
"fix/smoke/src/main/kotlin/com/saveourtool/save/Example1Expected.kt",
86+
"fix/smoke/src/main/kotlin/com/saveourtool/save/chapter2/Example1Test.kt",
87+
"fix/smoke/src/main/kotlin/com/saveourtool/save/chapter2/Example1Expected.kt",
88+
), 2
89+
)
90+
}
7991
}

save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/integration/FixDirTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import kotlin.test.Test
66
class FixDirTest {
77
@Test
88
fun `execute fix plugin`() {
9-
runTestsWithDiktat(listOf("fix/smoke/src/main/kotlin/com/saveourtool/save"), 5)
9+
runTestsWithDiktat(listOf("fix/smoke/src/main/kotlin/com/saveourtool/save"), 6)
1010
}
1111

1212
@Test

save-plugins/fix-plugin/src/commonMain/kotlin/com/saveourtool/save/plugins/fix/FixPlugin.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,9 @@ class FixPlugin(
230230
): List<TestResult> = testCopyToExpectedFilesMap.map { (testCopy, expected) ->
231231
val fixedLines = fs.readLines(testCopy)
232232
val expectedLines = fs.readLines(expected)
233-
234-
// FixMe: https://github.com/saveourtool/save-cli/issues/473
235-
val test = testToExpectedFilesMap.first { (test, _) -> test.name == testCopy.name }.first
236-
233+
val test = testToExpectedFilesMap.first { (test, _) ->
234+
isComparingTestAndCopy(test, testCopy)
235+
}.first
237236
TestResult(
238237
FixTestFiles(test, expected),
239238
checkStatus(expectedLines, fixedLines),
@@ -247,6 +246,19 @@ class FixPlugin(
247246
)
248247
}
249248

249+
private fun isComparingTestAndCopy(test: Path, testCopy: Path): Boolean {
250+
// TestCopyPath stored in tmpDir, holding the whole hierarchy of original file
251+
// while testPath comes to us with path, starting from testRootPath, so we compare them in such way
252+
val testCopyPath = testCopy.relativeTo(FileSystem.SYSTEM_TEMPORARY_DIRECTORY)
253+
.toString()
254+
.substringAfter(Path.DIRECTORY_SEPARATOR)
255+
.toPath()
256+
257+
val testPath = test.createRelativePathToTheRoot(testConfig.getRootConfig().directory).toPath()
258+
259+
return testCopyPath.compareTo(testPath) == 0
260+
}
261+
250262
private fun failTestResult(
251263
chunk: List<FixTestFiles>,
252264
ex: ProcessExecutionException,

0 commit comments

Comments
 (0)