Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix after review (#452)
  • Loading branch information
mmvpm committed Jul 8, 2022
commit 1741b700756c592edfd2425ad8d5e196a7d938e4
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,11 @@ data class SarifRegion(
* Makes [startColumn] the first non-whitespace character in [startLine] in the [text].
* If the [text] contains less than [startLine] lines, [startColumn] == null.
*/
fun fromStartLine(startLine: Int, text: String): SarifRegion {
fun withStartLine(text: String, startLine: Int): SarifRegion {
val neededLine = text.split('\n').getOrNull(startLine - 1) // to zero-based
Comment thread
mmvpm marked this conversation as resolved.
val startColumnZeroBased = neededLine?.takeWhile { it.toString().isBlank() }?.length
val startColumn = startColumnZeroBased?.let { it + 1 }
val startColumn = neededLine?.let {
Comment thread
mmvpm marked this conversation as resolved.
neededLine.takeWhile { it.toString().isBlank() }.length + 1 // to one-based
Comment thread
mmvpm marked this conversation as resolved.
}
return SarifRegion(startLine = startLine, startColumn = startColumn)
}
}
Expand Down
21 changes: 13 additions & 8 deletions utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class SarifReport(
val sourceRelativePath = sourceFinding.getSourceRelativePath(classFqn)
val startLine = extractLineNumber(utExecution) ?: defaultLineNumber
val sourceCode = sourceFinding.getSourceFile(classFqn)?.readText() ?: ""
val sourceRegion = SarifRegion.fromStartLine(startLine, sourceCode)
val sourceRegion = SarifRegion.withStartLine(sourceCode, startLine)
return listOf(
SarifPhysicalLocationWrapper(
SarifPhysicalLocation(SarifArtifact(sourceRelativePath), sourceRegion)
Expand All @@ -157,11 +157,11 @@ class SarifReport(
private fun getRelatedLocations(utExecution: UtExecution): List<SarifRelatedPhysicalLocationWrapper> {
val startLine = utExecution.testMethodName?.let { testMethodName ->
val neededLine = generatedTestsCode.split('\n').indexOfFirst { line ->
Comment thread
mmvpm marked this conversation as resolved.
line.contains(testMethodName)
line.contains("$testMethodName(")
}
if (neededLine == -1) null else neededLine + 1 // to one-based
} ?: defaultLineNumber
val sourceRegion = SarifRegion.fromStartLine(startLine, generatedTestsCode)
val sourceRegion = SarifRegion.withStartLine(generatedTestsCode, startLine)
return listOf(
SarifRelatedPhysicalLocationWrapper(
relatedLocationId,
Expand Down Expand Up @@ -235,7 +235,7 @@ class SarifReport(
),
physicalLocation = SarifPhysicalLocation(
SarifArtifact(relativePath),
SarifRegion.fromStartLine(lineNumber, sourceCode)
SarifRegion.withStartLine(sourceCode, lineNumber)
)
)
)
Expand All @@ -256,18 +256,23 @@ class SarifReport(
// searching needed method call
val publicMethodCallPattern = "$methodName("
val privateMethodCallPattern = Regex("""$methodName.*\.invoke\(""") // using reflection
val methodCallLineNumber = testsBodyLines
val methodCallShiftInTestMethod = testsBodyLines
.drop(testMethodStartsAt + 1) // for search after it
.indexOfFirst { line ->
line.contains(publicMethodCallPattern) || line.contains(privateMethodCallPattern)
}
if (methodCallLineNumber == -1)
if (methodCallShiftInTestMethod == -1)
return null

val startLine = methodCallLineNumber + 1 + testMethodStartsAt + 1
// `startLine` consists of:
// shift to the testMethod call (+ testMethodStartsAt)
// the line with testMethodName (+ 1)
// shift to the method call (+ methodCallShiftInTestMethod)
// to one-based (+ 1)
val startLine = testMethodStartsAt + 1 + methodCallShiftInTestMethod + 1
return SarifPhysicalLocation(
SarifArtifact(sourceFinding.testsRelativePath),
SarifRegion.fromStartLine(startLine, generatedTestsCode)
SarifRegion.withStartLine(generatedTestsCode, startLine)
)
}

Expand Down