Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feat/iac-from-ls
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/kotlin/snyk/common/lsp/SnykLanguageClient.kt
  • Loading branch information
bastiandoetsch committed Oct 2, 2024
2 parents 8598677 + fd79a41 commit 0b695b6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
38 changes: 14 additions & 24 deletions src/main/kotlin/snyk/common/lsp/SnykLanguageClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,23 @@ class SnykLanguageClient :
return
}

val issueList = diagnosticsParams.diagnostics
.filter { it.data != null }
.map {
val issue = gson.fromJson(it.data.toString(), ScanIssue::class.java)
// load textrange for issue so it doesn't happen in UI thread
issue.textRange
issue
}.toList()

when (product) {
LsProductConstants.OpenSource.value -> {
scanPublisher.onPublishDiagnostics(product, snykFile, issueList)
}
val issueList = getScanIssues(diagnosticsParams)
if (product != null) {
scanPublisher.onPublishDiagnostics(product, snykFile, issueList)
}

LsProductConstants.Code.value -> {
scanPublisher.onPublishDiagnostics(product, snykFile, issueList)
}
return
}

LsProductConstants.InfrastructureAsCode.value -> {
scanPublisher.onPublishDiagnostics(product, snykFile, issueList)
}
private fun getScanIssues(diagnosticsParams: PublishDiagnosticsParams): List<ScanIssue> {
val issueList = diagnosticsParams.diagnostics.stream().map {
val issue = Gson().fromJson(it.data.toString(), ScanIssue::class.java)
// load textrange for issue so it doesn't happen in UI thread
issue.textRange
issue
}.toList()

LsProductConstants.Container.value -> {
// TODO implement
}
}
return
return issueList
}

override fun applyEdit(params: ApplyWorkspaceEditParams?): CompletableFuture<ApplyWorkspaceEditResponse> {
Expand Down
38 changes: 35 additions & 3 deletions src/test/kotlin/snyk/common/lsp/SnykLanguageClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import io.mockk.verify
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.services.SnykApplicationSettingsStateService
import io.snyk.plugin.ui.toolwindow.SnykPluginDisposable
import org.eclipse.lsp4j.Diagnostic
import org.eclipse.lsp4j.Position
import org.eclipse.lsp4j.PublishDiagnosticsParams
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Before
import org.junit.Test
import snyk.pluginInfo
import snyk.trust.WorkspaceTrustService
import kotlin.io.path.Path
import org.eclipse.lsp4j.Range


class SnykLanguageClientTest {
private lateinit var cut: SnykLanguageClient
Expand Down Expand Up @@ -100,11 +106,11 @@ class SnykLanguageClientTest {
cut.snykScan(param)

// you cannot display / forward the issues without mapping them to open projects
verify (exactly = 0) { projectManagerMock.openProjects }
verify(exactly = 0) { projectManagerMock.openProjects }
}

@Test
fun `hasAuthenticated does not run when disposed`(){
fun `hasAuthenticated does not run when disposed`() {
every { applicationMock.isDisposed } returns true

val unexpected = "abc"
Expand All @@ -120,7 +126,7 @@ class SnykLanguageClientTest {
val unexpected = "abc"
cut.addTrustedPaths(SnykTrustedFoldersParams(listOf(unexpected)))

verify (exactly = 0){ applicationMock.getService(WorkspaceTrustService::class.java) }
verify(exactly = 0) { applicationMock.getService(WorkspaceTrustService::class.java) }
}

@Test
Expand All @@ -131,5 +137,31 @@ class SnykLanguageClientTest {
verify { trustServiceMock.addTrustedPath(eq(Path(path))) }
}

@Test
fun testGetScanIssues_validDiagnostics() {
val range = Range(Position(0, 0), Position(0, 1))
// Create mock PublishDiagnosticsParams with diagnostics list
val diagnostics: MutableList<Diagnostic> = ArrayList()
diagnostics.add(createMockDiagnostic(range, "Some Issue"))
diagnostics.add(createMockDiagnostic(range, "Another Issue"))
val diagnosticsParams = PublishDiagnosticsParams("test", diagnostics)

// Call scanIssues function
val result: List<ScanIssue> = cut.getScanIssues(diagnosticsParams)

// Assert the returned list contains parsed ScanIssues
assertEquals(2, result.size)
assertEquals("Some Issue", result.get(0).title)
assertEquals("Another Issue", result.get(1).title)
}

private fun createMockDiagnostic(range: Range, message: String): Diagnostic {
val jsonString = """{"id": 12345, "title": "$message"}"""

val mockDiagnostic = Diagnostic()
mockDiagnostic.range = range
mockDiagnostic.data = jsonString
return mockDiagnostic
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ class ContainerBulkFileListenerTest : BasePlatformTestCase() {
}
PlatformTestUtil.dispatchAllEventsInIdeEventQueue()

await().timeout(300, TimeUnit.SECONDS).until {
println(System.currentTimeMillis())
virtualFile?.isValid ?: false
}
await().timeout(5, TimeUnit.SECONDS).until { virtualFile?.isValid ?: false }


ApplicationManager.getApplication().runWriteAction {
Expand Down

0 comments on commit 0b695b6

Please sign in to comment.