From 2a1d4f7f16a24d792bdbf643dafba43cf6c7481e Mon Sep 17 00:00:00 2001 From: Bastian Doetsch Date: Tue, 9 Jan 2024 07:47:36 -0500 Subject: [PATCH] fix: tests and erroneous mocking --- src/main/kotlin/snyk/oss/OssService.kt | 18 +++++++++------ .../snyk/oss/OssVulnerabilitiesForFile.kt | 22 ++++--------------- src/test/kotlin/io/snyk/plugin/TestUtils.kt | 6 ++--- src/test/kotlin/snyk/oss/OssServiceTest.kt | 1 + 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/snyk/oss/OssService.kt b/src/main/kotlin/snyk/oss/OssService.kt index f6054f5ed..481acf458 100644 --- a/src/main/kotlin/snyk/oss/OssService.kt +++ b/src/main/kotlin/snyk/oss/OssService.kt @@ -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 @@ -21,13 +23,14 @@ class OssService(project: Project) : CliAdapter = OssVulnerabilitiesForFile::class.java @@ -43,7 +46,8 @@ class OssService(project: Project) : CliAdapter, 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 }) diff --git a/src/test/kotlin/io/snyk/plugin/TestUtils.kt b/src/test/kotlin/io/snyk/plugin/TestUtils.kt index 107a96942..8da590dea 100644 --- a/src/test/kotlin/io/snyk/plugin/TestUtils.kt +++ b/src/test/kotlin/io/snyk/plugin/TestUtils.kt @@ -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 @@ -48,10 +47,9 @@ fun resetSettings(project: Project?) { /** low level avoiding download the CLI file */ fun mockCliDownload(): RequestBuilder { val requestBuilderMockk = mockk(relaxed = true) + mockkObject(HttpRequestHelper) + every { HttpRequestHelper.createRequest(any()) } returns requestBuilderMockk justRun { requestBuilderMockk.saveToFile(any(), any()) } justRun { requestBuilderMockk.saveToFile(any(), any()) } - mockkObject(HttpRequestHelper) - every { HttpRequestHelper.createRequest(CliDownloader.LATEST_RELEASE_DOWNLOAD_URL) } returns requestBuilderMockk - every { HttpRequestHelper.createRequest(CliDownloader.LATEST_RELEASES_URL) } returns requestBuilderMockk return requestBuilderMockk } diff --git a/src/test/kotlin/snyk/oss/OssServiceTest.kt b/src/test/kotlin/snyk/oss/OssServiceTest.kt index 4d484f6d1..a2bdd2e24 100644 --- a/src/test/kotlin/snyk/oss/OssServiceTest.kt +++ b/src/test/kotlin/snyk/oss/OssServiceTest.kt @@ -41,6 +41,7 @@ class OssServiceTest : LightPlatformTestCase() { settingsStateService.organization = "" project.service().additionalParameters = "" + mockkStatic(GotoFileCellRenderer::class) every { GotoFileCellRenderer.getRelativePath(any(), any()) } returns "abc/" }