Skip to content

Commit 9074bba

Browse files
authored
Fixes in SARIF mode (#479)
### What's done: * Rework the logic of comparing the `test` file and its copy * Enable test for SARIF mode in fix plugin
1 parent b00599a commit 9074bba

6 files changed

Lines changed: 69 additions & 22 deletions

File tree

build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ createDetektTask()
1212
installGitHooks()
1313
// publishing to maven central
1414
configurePublishing()
15+
16+
allprojects {
17+
configurations.all {
18+
// if SNAPSHOT dependencies are used, refresh them periodically
19+
resolutionStrategy.cacheDynamicVersionsFor(10, TimeUnit.MINUTES)
20+
resolutionStrategy.cacheChangingModulesFor(10, TimeUnit.MINUTES)
21+
}
22+
}

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

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

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"
6+
[fix]
7+
actualFixFormat = "SARIF"
8+
actualFixSarifFileName = "save-fixes.sarif"

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ multiplatform-diff = "0.4.0"
1212
kotlinpoet = "1.12.0"
1313
kotest = "5.5.4"
1414
sarif4k = "0.3.0"
15-
sarif-utils = "0.1.0"
15+
sarif-utils = "0.2.0-SNAPSHOT"
1616

1717
[plugins]
1818
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }

save-core/src/commonNonJsTest/kotlin/com/saveourtool/save/core/test/utils/TestUtilsCommon.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fun runTestsWithDiktat(
5353
).let(overrideProperties)
5454

5555
// logger is not set from save properties without a config reader, need to set it explicitly
56-
logType.set(LogType.ALL)
56+
logType.set(LogType.WARN)
5757
// In this test we need to merge with emulated empty save.properties file in aim to use default values,
5858
// since initially all fields are null
5959
val save = Save(saveProperties, fs)

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

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import com.saveourtool.save.core.utils.ProcessTimeoutException
2626
import com.saveourtool.save.core.utils.calculatePathToSarifFile
2727
import com.saveourtool.save.core.utils.singleIsInstance
2828

29-
import com.saveourtool.sarifutils.cli.adapter.SarifFixAdapter
29+
import com.saveourtool.sarifutils.adapter.SarifFixAdapter
3030
import io.github.petertrr.diffutils.diff
3131
import io.github.petertrr.diffutils.patch.ChangeDelta
3232
import io.github.petertrr.diffutils.patch.Delta
@@ -124,7 +124,13 @@ class FixPlugin(
124124
val stdout = executionResult.stdout
125125
val stderr = executionResult.stderr
126126

127-
buildTestResultsForChunk(testToExpectedFilesMap, adjustedTestCopyToExpectedFilesMap, execCmd, stdout, stderr)
127+
buildTestResultsForChunk(
128+
testToExpectedFilesMap,
129+
adjustedTestCopyToExpectedFilesMap,
130+
execCmd,
131+
stdout,
132+
stderr
133+
)
128134
}
129135
.flatten()
130136
}
@@ -202,9 +208,10 @@ class FixPlugin(
202208
// modify existing map, replace test copies to fixed test copies
203209
val fixedTestCopyToExpectedFilesMap = testCopyToExpectedFilesMap.toMutableList().map { (testCopy, expected) ->
204210
val fixedTestCopy = fixedFiles.first {
205-
// FixMe: Until https://github.com/saveourtool/sarif-utils/issues/23
206-
// FixMe: compare only by names, but should by full paths
207-
it.name == testCopy.name
211+
isComparingTestAndCopy(
212+
it,
213+
testCopy
214+
)
208215
}
209216
fixedTestCopy to expected
210217
}
@@ -226,7 +233,7 @@ class FixPlugin(
226233
testCopyToExpectedFilesMap: List<PathPair>,
227234
execCmd: String,
228235
stdout: List<String>,
229-
stderr: List<String>,
236+
stderr: List<String>
230237
): List<TestResult> = testCopyToExpectedFilesMap.map { (testCopy, expected) ->
231238
val fixedLines = fs.readLines(testCopy)
232239
val expectedLines = fs.readLines(expected)
@@ -246,19 +253,49 @@ class FixPlugin(
246253
)
247254
}
248255

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+
private fun isComparingTestAndCopy(
257+
test: Path,
258+
testCopy: Path,
259+
): Boolean {
260+
val testPath = test.trimTmpDir().trimTestRootPath().replaceSeparators()
261+
val testCopyPath = testCopy.trimTmpDir().trimTestRootPath().replaceSeparators()
262+
return testCopyPath.compareTo(testPath) == 0
263+
}
256264

257-
val testPath = test.createRelativePathToTheRoot(testConfig.getRootConfig().directory).toPath()
265+
private fun Path.trimTmpDir(): Path {
266+
val currentPathAdjusted = this.toString().replaceSeparators()
267+
val tmpDirAdjusted = FileSystem.SYSTEM_TEMPORARY_DIRECTORY.toString().replaceSeparators()
268+
return if (currentPathAdjusted.startsWith(tmpDirAdjusted)) {
269+
currentPathAdjusted
270+
// trim tmpDir
271+
.substringAfter("$tmpDirAdjusted/")
272+
// trim tmp FixPlugin dir
273+
.substringAfter("/")
274+
.toPath()
275+
} else {
276+
this
277+
}
278+
}
258279

259-
return testCopyPath.compareTo(testPath) == 0
280+
private fun Path.trimTestRootPath(): Path {
281+
val currentPathAdjusted = this.toString().replaceSeparators()
282+
val testRootPathAdjusted = testConfig.getRootConfig()
283+
.directory
284+
.toString()
285+
.replaceSeparators()
286+
return if (currentPathAdjusted.startsWith(testRootPathAdjusted)) {
287+
currentPathAdjusted
288+
.substringAfter("$testRootPathAdjusted/")
289+
.toPath()
290+
} else {
291+
this
292+
}
260293
}
261294

295+
private fun String.replaceSeparators(): String = this.replace("\\", "/")
296+
297+
private fun Path.replaceSeparators(): Path = this.toString().replaceSeparators().toPath()
298+
262299
private fun failTestResult(
263300
chunk: List<FixTestFiles>,
264301
ex: ProcessExecutionException,

settings.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ include("save-common-test")
1111
dependencyResolutionManagement {
1212
repositories {
1313
mavenCentral()
14+
// add sonatype repository
15+
maven {
16+
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
17+
}
1418
}
1519
}
1620
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

0 commit comments

Comments
 (0)