Skip to content

Commit ee59bb7

Browse files
authored
Fix incorrect highlighting of the method call in sarif (#876)
1 parent 49e7fb7 commit ee59bb7

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

utbot-framework-test/src/test/kotlin/org/utbot/sarif/SarifReportTest.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class SarifReportTest {
159159
it.location.physicalLocation
160160
}
161161
assert(codeFlowPhysicalLocations[0].artifactLocation.uri.contains("MainTest.java"))
162-
assert(codeFlowPhysicalLocations[0].region.startLine == 3)
162+
assert(codeFlowPhysicalLocations[0].region.startLine == 5)
163163
assert(codeFlowPhysicalLocations[0].region.startColumn == 7)
164164
}
165165

@@ -183,7 +183,7 @@ class SarifReportTest {
183183
it.location.physicalLocation
184184
}
185185
assert(codeFlowPhysicalLocations[0].artifactLocation.uri.contains("MainTest.java"))
186-
assert(codeFlowPhysicalLocations[0].region.startLine == 4)
186+
assert(codeFlowPhysicalLocations[0].region.startLine == 6)
187187
assert(codeFlowPhysicalLocations[0].region.startColumn == 5)
188188
}
189189

@@ -330,13 +330,17 @@ class SarifReportTest {
330330

331331
private val generatedTestsCodeMain = """
332332
public void testMain_ThrowArithmeticException() {
333+
/* This test fails because method [Main.main] produces [java.lang.ArithmeticException: / by zero]
334+
Main.main(Main.java:15) */
333335
Main main = new Main();
334336
main.main(0); // shift for `startColumn` == 7
335337
}
336338
""".trimIndent()
337339

338340
private val generatedTestsCodePrivateMain = """
339341
public void testMain_ThrowArithmeticException() {
342+
/* This test fails because method [Main.main] produces [java.lang.ArithmeticException: / by zero]
343+
Main.main(Main.java:15) */
340344
Main main = new Main();
341345
// ...
342346
mainMethod.invoke(main, mainMethodArguments);

utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,21 @@ class SarifReport(
273273
)
274274
}
275275

276+
private val testsBodyLines by lazy {
277+
generatedTestsCode.split('\n')
278+
}
279+
276280
private fun findMethodCallInTestBody(testMethodName: String?, methodName: String): SarifPhysicalLocation? {
277281
if (testMethodName == null)
278282
return null
279283

280284
// searching needed test
281-
val testsBodyLines = generatedTestsCode.split('\n')
282285
val testMethodStartsAt = testsBodyLines.indexOfFirst { line ->
283286
line.contains(testMethodName)
284287
}
285288
if (testMethodStartsAt == -1)
286289
return null
287-
/**
290+
/*
288291
* ...
289292
* public void testMethodName() { // <- `testMethodStartsAt`
290293
* ...
@@ -294,8 +297,10 @@ class SarifReport(
294297
*/
295298

296299
// searching needed method call
297-
val publicMethodCallPattern = "$methodName("
298-
val privateMethodCallPattern = Regex("""$methodName.*\.invoke\(""") // using reflection
300+
// Regex("[^:]*") satisfies every character except ':'
301+
// It is necessary to avoid strings from the stacktrace, such as "className.methodName(FileName.java:10)"
302+
val publicMethodCallPattern = Regex("""$methodName\([^:]*\)""")
303+
val privateMethodCallPattern = Regex("""$methodName.*\.invoke\([^:]*\)""") // using reflection
299304
val methodCallShiftInTestMethod = testsBodyLines
300305
.drop(testMethodStartsAt + 1) // for search after it
301306
.indexOfFirst { line ->

0 commit comments

Comments
 (0)