Skip to content

Commit

Permalink
fix: tests and erroneous mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Jan 9, 2024
1 parent 89d266e commit 2a1d4f7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 29 deletions.
18 changes: 11 additions & 7 deletions src/main/kotlin/snyk/oss/OssService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package snyk.oss

import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.services.CliAdapter
import snyk.common.RelativePathHelper
import snyk.common.SnykError
import snyk.pluginInfo

Expand All @@ -21,13 +23,14 @@ class OssService(project: Project) : CliAdapter<OssVulnerabilitiesForFile, OssRe

override fun sanitizeCliIssues(cliIssues: OssVulnerabilitiesForFile): OssVulnerabilitiesForFile {
// .copy() will check nullability of fields
val sanitized = cliIssues.copy(
vulnerabilities = cliIssues.vulnerabilities.map { it.copy() }
)
val virtualFile = cliIssues.virtualFile ?: LocalFileSystem.getInstance().findFileByPath(cliIssues.path)
// determine relative path for each issue at scan time
sanitized.project = project
sanitized.relativePath
return sanitized
return cliIssues.copy(
vulnerabilities = cliIssues.vulnerabilities.map { it.copy() },
project = project,
virtualFile = virtualFile,
relativePath = virtualFile?.let { RelativePathHelper().getRelativePath(virtualFile, project) }
)
}

override fun getCliIIssuesClass(): Class<OssVulnerabilitiesForFile> = OssVulnerabilitiesForFile::class.java
Expand All @@ -43,7 +46,8 @@ class OssService(project: Project) : CliAdapter<OssVulnerabilitiesForFile, OssRe
additionalParameters.contains(ALL_PROJECTS_PARAM)

if (pluginInfo.integrationEnvironment.contains("RIDER") &&
!hasAllProjectsParam) {
!hasAllProjectsParam
) {
options.add(ALL_PROJECTS_PARAM)
}

Expand Down
22 changes: 4 additions & 18 deletions src/main/kotlin/snyk/oss/OssVulnerabilitiesForFile.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package snyk.oss

import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import snyk.common.RelativePathHelper

data class OssVulnerabilitiesForFile(
val vulnerabilities: List<Vulnerability>,
private val displayTargetFile: String,
val packageManager: String,
val path: String,
val remediation: Remediation? = null
val remediation: Remediation? = null,
val virtualFile: VirtualFile? = null,
val relativePath: String? = null,
val project: Project? = null
) {
val uniqueCount: Int get() = vulnerabilities.groupBy { it.id }.size

val sanitizedTargetFile: String get() = displayTargetFile.replace("-lock", "")
val virtualFile: VirtualFile
get() = LocalFileSystem.getInstance().findFileByPath(this.path)!!

var project: Project? = null

// this is necessary as the creation of the class by the GSon is not initializing fields
private var relativePathHelper: RelativePathHelper? = null
get() = field ?: RelativePathHelper()

var relativePath: String? = null
get() = field ?: project?.let {
field = relativePathHelper!!.getRelativePath(virtualFile, it)
return field
}

fun toGroupedResult(): OssGroupedResult {
val id2vulnerabilities = vulnerabilities.groupBy({ it.id }, { it })
Expand Down
6 changes: 2 additions & 4 deletions src/test/kotlin/io/snyk/plugin/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.mockk.mockk
import io.mockk.mockkObject
import io.snyk.plugin.services.SnykApplicationSettingsStateService
import io.snyk.plugin.services.SnykProjectSettingsStateService
import io.snyk.plugin.services.download.CliDownloader
import io.snyk.plugin.services.download.HttpRequestHelper
import java.io.File
import java.nio.file.Path
Expand Down Expand Up @@ -48,10 +47,9 @@ fun resetSettings(project: Project?) {
/** low level avoiding download the CLI file */
fun mockCliDownload(): RequestBuilder {
val requestBuilderMockk = mockk<RequestBuilder>(relaxed = true)
mockkObject(HttpRequestHelper)
every { HttpRequestHelper.createRequest(any()) } returns requestBuilderMockk
justRun { requestBuilderMockk.saveToFile(any<File>(), any()) }
justRun { requestBuilderMockk.saveToFile(any<Path>(), any()) }
mockkObject(HttpRequestHelper)
every { HttpRequestHelper.createRequest(CliDownloader.LATEST_RELEASE_DOWNLOAD_URL) } returns requestBuilderMockk
every { HttpRequestHelper.createRequest(CliDownloader.LATEST_RELEASES_URL) } returns requestBuilderMockk
return requestBuilderMockk
}
1 change: 1 addition & 0 deletions src/test/kotlin/snyk/oss/OssServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class OssServiceTest : LightPlatformTestCase() {
settingsStateService.organization = ""

project.service<SnykProjectSettingsStateService>().additionalParameters = ""

mockkStatic(GotoFileCellRenderer::class)
every { GotoFileCellRenderer.getRelativePath(any(), any()) } returns "abc/"
}
Expand Down

0 comments on commit 2a1d4f7

Please sign in to comment.