Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 45
Support startColumn field in the SARIF report#454
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -142,12 +142,27 @@ data class SarifArtifact( | ||
| val uriBaseId: String = "%SRCROOT%" | ||
| ) | ||
| // all fields should be one-based | ||
| data class SarifRegion( | ||
| val startLine: Int, | ||
| val endLine: Int? = null, | ||
| val startColumn: Int? = null, | ||
| val endColumn: Int? = null | ||
| ) | ||
| ) { | ||
| companion object { | ||
| /** | ||
| * Makes [startColumn] the first non-whitespace character in [startLine] in the [text]. | ||
| * If the [text] contains less than [startLine] lines, [startColumn] == null. | ||
| */ | ||
| fun withStartLine(text: String, startLine: Int): SarifRegion { | ||
| val neededLine = text.split('\n').getOrNull(startLine - 1) // to zero-based | ||
| val startColumn = neededLine?.let { | ||
mmvpm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| neededLine.takeWhile { it.toString().isBlank() }.length + 1 // to one-based | ||
mmvpm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| return SarifRegion(startLine = startLine, startColumn = startColumn) | ||
| } | ||
| } | ||
| } | ||
| // related locations | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -144,9 +144,9 @@ class SarifReport( | ||
| if (classFqn == null) | ||
| return listOf() | ||
| val sourceRelativePath = sourceFinding.getSourceRelativePath(classFqn) | ||
| val sourceRegion = SarifRegion( | ||
| startLine = extractLineNumber(utExecution) ?: defaultLineNumber | ||
| ) | ||
| val startLine = extractLineNumber(utExecution) ?: defaultLineNumber | ||
| val sourceCode = sourceFinding.getSourceFile(classFqn)?.readText() ?: "" | ||
| val sourceRegion = SarifRegion.withStartLine(sourceCode, startLine) | ||
| return listOf( | ||
| SarifPhysicalLocationWrapper( | ||
| SarifPhysicalLocation(SarifArtifact(sourceRelativePath), sourceRegion) | ||
| @@ -155,14 +155,13 @@ class SarifReport( | ||
| } | ||
| private fun getRelatedLocations(utExecution: UtExecution): List<SarifRelatedPhysicalLocationWrapper> { | ||
| val lineNumber = generatedTestsCode.split('\n').indexOfFirst { line -> | ||
| utExecution.testMethodName?.let { testMethodName -> | ||
| line.contains(testMethodName) | ||
| } ?: false | ||
| } | ||
| val sourceRegion = SarifRegion( | ||
| startLine = if (lineNumber != -1) lineNumber + 1 else defaultLineNumber | ||
| ) | ||
| val startLine = utExecution.testMethodName?.let { testMethodName -> | ||
| val neededLine = generatedTestsCode.split('\n').indexOfFirst { line -> | ||
mmvpm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| line.contains("$testMethodName(") | ||
| } | ||
| if (neededLine == -1) null else neededLine + 1 // to one-based | ||
| } ?: defaultLineNumber | ||
| val sourceRegion = SarifRegion.withStartLine(generatedTestsCode, startLine) | ||
| return listOf( | ||
| SarifRelatedPhysicalLocationWrapper( | ||
| relatedLocationId, | ||
| @@ -228,14 +227,15 @@ class SarifReport( | ||
| return null | ||
| val extension = stackTraceElement.fileName?.toPath()?.fileExtension | ||
| val relativePath = sourceFinding.getSourceRelativePath(stackTraceElement.className, extension) | ||
| val sourceCode = sourceFinding.getSourceFile(stackTraceElement.className, extension)?.readText() ?: "" | ||
| return SarifFlowLocationWrapper( | ||
| SarifFlowLocation( | ||
| message = Message( | ||
| text = stackTraceElement.toString() | ||
| ), | ||
| physicalLocation = SarifPhysicalLocation( | ||
| SarifArtifact(relativePath), | ||
| SarifRegion(lineNumber) | ||
| SarifRegion.withStartLine(sourceCode, lineNumber) | ||
| ) | ||
| ) | ||
| ) | ||
| @@ -252,21 +252,36 @@ class SarifReport( | ||
| } | ||
| if (testMethodStartsAt == -1) | ||
| return null | ||
| /** | ||
| * ... | ||
| * public void testMethodName() { // <- `testMethodStartsAt` | ||
| * ... | ||
| * className.methodName(...) // <- needed `startLine` | ||
| * ... | ||
| * } | ||
| */ | ||
| // 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 | ||
| // `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(startLine = methodCallLineNumber + 1 + testMethodStartsAt + 1) | ||
| SarifRegion.withStartLine(generatedTestsCode, startLine) | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,6 +7,7 @@ import org.utbot.sarif.SourceFindingStrategy | ||
| import com.intellij.psi.JavaPsiFacade | ||
| import com.intellij.psi.PsiClass | ||
| import org.jetbrains.kotlin.idea.search.allScope | ||
| import java.io.File | ||
| /** | ||
| * The search strategy based on the information available to the PsiClass | ||
| @@ -29,6 +30,16 @@ class SourceFindingStrategyIdea(testClass: PsiClass) : SourceFindingStrategy() { | ||
| safeRelativize(project.basePath, psiClass.containingFile.virtualFile.path) | ||
| } ?: (classFqnToPath(classFqn) + (extension ?: defaultExtension)) | ||
| /** | ||
| * Finds the source file containing the class [classFqn]. | ||
| * Returns null if the file does not exist. | ||
| */ | ||
| override fun getSourceFile(classFqn: String, extension: String?): File? { | ||
| val psiClass = JavaPsiFacade.getInstance(project).findClass(classFqn, project.allScope()) | ||
mmvpm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| val sourceCodeFile = psiClass?.containingFile?.virtualFile?.path?.let(::File) | ||
| return if (sourceCodeFile?.exists() == true) sourceCodeFile else null | ||
| } | ||
| // internal | ||
| private val project = testClass.project | ||
Uh oh!
There was an error while loading. Please reload this page.