Skip to content

Commit

Permalink
test: add simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
teodora-sandu committed Apr 3, 2024
1 parent ee9dc70 commit f446f5c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,6 @@ class SuggestionDescriptionPanelFromLS(
val project = snykCodeFile.project
private val unexpectedErrorMessage = "Snyk encountered an issue while rendering the vulnerability description. Please try again, or contact support if the problem persists. We apologize for any inconvenience caused.";


fun openFile(value: String): JBCefJSQuery.Response {
var values = value.replace("\n", "").split(":")
var filePath = values[0]
var startLine = values[1].toInt()
var endLine = values[2].toInt()
var startCharacter = values[3].toInt()
var endCharacter = values[4].toInt()

ApplicationManager.getApplication().invokeLater {
val virtualFile = filePath.toVirtualFile()
val document = virtualFile.getDocument()
val startLineStartOffset = document?.getLineStartOffset(startLine) ?: 0
val startOffset = startLineStartOffset + (startCharacter)
val endLineStartOffset = document?.getLineStartOffset(endLine) ?: 0
val endOffset = endLineStartOffset + endCharacter - 1

navigateToSource(project, virtualFile, startOffset, endOffset)
}

return JBCefJSQuery.Response("success")
}
init {
if (pluginSettings().isGlobalIgnoresFeatureEnabled && issue.additionalData.details != null) {
val (jbCefBrowser, jbCefBrowserUrl) = getJBCefBrowserIfSupported()
Expand Down Expand Up @@ -130,6 +108,28 @@ class SuggestionDescriptionPanelFromLS(
}
}

fun openFile(value: String): JBCefJSQuery.Response {
var values = value.replace("\n", "").split(":")
var filePath = values[0]
var startLine = values[1].toInt()
var endLine = values[2].toInt()
var startCharacter = values[3].toInt()
var endCharacter = values[4].toInt()

ApplicationManager.getApplication().invokeLater {
val virtualFile = filePath.toVirtualFile()
val document = virtualFile.getDocument()
val startLineStartOffset = document?.getLineStartOffset(startLine) ?: 0
val startOffset = startLineStartOffset + (startCharacter)
val endLineStartOffset = document?.getLineStartOffset(endLine) ?: 0
val endOffset = endLineStartOffset + endCharacter - 1

navigateToSource(project, virtualFile, startOffset, endOffset)
}

return JBCefJSQuery.Response("success")
}

override fun secondRowTitlePanel(): DescriptionHeaderPanel = descriptionHeaderPanel(
issueNaming = if (issue.additionalData.isSecurityType) "Vulnerability" else "Quality Issue",
cwes = issue.additionalData.cwe ?: emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.snyk.plugin.Severity
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.resetSettings
import io.snyk.plugin.snykcode.core.SnykCodeFile
import io.snyk.plugin.toVirtualFile
import io.snyk.plugin.ui.getJBCefBrowserIfSupported
import io.snyk.plugin.ui.toolwindow.panels.SuggestionDescriptionPanelFromLS
import org.eclipse.lsp4j.Position
Expand Down Expand Up @@ -128,4 +129,17 @@ class SuggestionDescriptionPanelFromLSTest : BasePlatformTestCase() {
val actual = getJLabelByText(cut, "<html>Test message</html>")
assertNull(actual)
}


@Test
fun `test openFile should navigate to source`() {
pluginSettings().isGlobalIgnoresFeatureEnabled = false

mockkStatic("io.snyk.plugin.UtilsKt")
every { fileName.toVirtualFile() } returns psiFile.virtualFile

cut = SuggestionDescriptionPanelFromLS(snykCodeFile, issue)
val res = cut.openFile("$fileName:1:2:3:4")
assertNotNull(res)
}
}

0 comments on commit f446f5c

Please sign in to comment.