Skip to content

Commit c70aae0

Browse files
rlileigaol
authored andcommitted
fix(amazonq): fix failing lsp artifact tests (aws#5433)
Tests were not working correctly due to dependency on application, but tests did not declare usage of ApplicationExtension
1 parent d834277 commit c70aae0

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref =
106106
kotlin-stdLibJdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
107107
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
108108
mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" }
109+
mockito-junit-jupiter = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito" }
109110
mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockitoKotlin" }
110111
mockk = { module = "io.mockk:mockk", version.ref="mockk" }
111112
nimbus-jose-jwt = {module = "com.nimbusds:nimbus-jose-jwt", version.ref = "nimbus-jose-jwt"}
@@ -121,7 +122,7 @@ zjsonpatch = { module = "com.flipkart.zjsonpatch:zjsonpatch", version.ref = "zjs
121122
[bundles]
122123
jackson = ["jackson-datetime", "jackson-kotlin", "jackson-yaml", "jackson-xml"]
123124
kotlin = ["kotlin-stdLibJdk8", "kotlin-reflect"]
124-
mockito = ["mockito-core", "mockito-kotlin"]
125+
mockito = ["mockito-core", "mockito-junit-jupiter", "mockito-kotlin"]
125126
sshd = ["sshd-core", "sshd-scp", "sshd-sftp"]
126127

127128
[plugins]

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelper.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class ArtifactHelper(private val lspArtifactsPath: Path = DEFAULT_ARTIFACT_PATH,
112112
logger.info { "Attempt ${currentAttempt.get()} of $maxDownloadAttempts to download LSP artifacts" }
113113

114114
try {
115-
withBackgroundProgress(
115+
return withBackgroundProgress(
116116
project,
117117
AwsCoreBundle.message("amazonqFeatureDev.placeholder.downloading_and_extracting_lsp_artifacts"),
118118
cancellable = true
@@ -123,9 +123,12 @@ class ArtifactHelper(private val lspArtifactsPath: Path = DEFAULT_ARTIFACT_PATH,
123123
.mapNotNull { it.filename }
124124
.forEach { filename -> extractZipFile(downloadPath.resolve(filename), downloadPath) }
125125
logger.info { "Successfully downloaded and moved LSP artifacts to $downloadPath" }
126+
127+
return@withBackgroundProgress downloadPath
126128
}
129+
130+
return@withBackgroundProgress null
127131
}
128-
return downloadPath
129132
} catch (e: Exception) {
130133
when (e) {
131134
is CancellationException -> {

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcher.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import java.nio.file.Path
1919
class ManifestFetcher(
2020
private val lspManifestUrl: String = DEFAULT_MANIFEST_URL,
2121
private val manifestManager: ManifestManager = ManifestManager(),
22-
private val lspManifestFilePath: Path = DEFAULT_MANIFEST_PATH,
22+
private val manifestPath: Path = DEFAULT_MANIFEST_PATH,
2323
) {
2424
companion object {
2525
private val logger = getLogger<ManifestFetcher>()
@@ -34,6 +34,10 @@ class ManifestFetcher(
3434
.resolve("jetbrains-lsp-manifest.json")
3535
}
3636

37+
@get:VisibleForTesting
38+
internal val lspManifestFilePath: Path
39+
get() = manifestPath
40+
3741
/**
3842
* Method which will be used to fetch latest manifest.
3943
* */

plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelperTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package software.aws.toolkits.jetbrains.services.amazonq.lsp.artifacts
55

66
import com.intellij.openapi.project.Project
7+
import com.intellij.testFramework.ApplicationExtension
78
import com.intellij.util.io.createDirectories
89
import com.intellij.util.text.SemVer
910
import io.mockk.Runs
@@ -14,16 +15,16 @@ import io.mockk.mockkStatic
1415
import io.mockk.spyk
1516
import kotlinx.coroutines.runBlocking
1617
import org.assertj.core.api.Assertions.assertThat
17-
import org.jetbrains.annotations.TestOnly
1818
import org.junit.jupiter.api.BeforeEach
1919
import org.junit.jupiter.api.Test
20+
import org.junit.jupiter.api.extension.ExtendWith
2021
import org.junit.jupiter.api.io.TempDir
2122
import org.mockito.kotlin.mock
2223
import software.aws.toolkits.jetbrains.services.amazonq.lsp.artifacts.ArtifactManager.SupportedManifestVersionRange
2324
import software.aws.toolkits.jetbrains.services.amazonq.project.manifest.ManifestManager
2425
import java.nio.file.Path
2526

26-
@TestOnly
27+
@ExtendWith(ApplicationExtension::class)
2728
class ArtifactHelperTest {
2829
@TempDir
2930
lateinit var tempDir: Path

plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcherTest.kt

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@
33

44
package software.aws.toolkits.jetbrains.services.amazonq.lsp.artifacts
55

6+
import com.intellij.testFramework.ApplicationExtension
7+
import com.intellij.testFramework.utils.io.createFile
68
import io.mockk.every
9+
import io.mockk.junit5.MockKExtension
710
import io.mockk.mockkStatic
811
import org.assertj.core.api.Assertions.assertThat
9-
import org.jetbrains.annotations.TestOnly
1012
import org.junit.jupiter.api.BeforeEach
1113
import org.junit.jupiter.api.Test
14+
import org.junit.jupiter.api.extension.ExtendWith
15+
import org.junit.jupiter.api.io.TempDir
16+
import org.mockito.junit.jupiter.MockitoExtension
1217
import org.mockito.kotlin.atLeastOnce
1318
import org.mockito.kotlin.never
14-
import org.mockito.kotlin.reset
1519
import org.mockito.kotlin.spy
1620
import org.mockito.kotlin.verify
1721
import org.mockito.kotlin.whenever
1822
import software.aws.toolkits.jetbrains.core.getTextFromUrl
1923
import software.aws.toolkits.jetbrains.services.amazonq.project.manifest.ManifestManager
24+
import java.nio.file.Path
25+
import java.nio.file.Paths
2026

21-
@TestOnly
27+
@ExtendWith(ApplicationExtension::class, MockitoExtension::class, MockKExtension::class)
2228
class ManifestFetcherTest {
2329

2430
private lateinit var manifestFetcher: ManifestFetcher
@@ -42,7 +48,6 @@ class ManifestFetcherTest {
4248

4349
@Test
4450
fun `should return valid result from local should not execute remote method`() {
45-
reset(manifestFetcher)
4651
whenever(manifestFetcher.fetchManifestFromLocal()).thenReturn(manifest)
4752

4853
assertThat(manifestFetcher.fetch()).isNotNull().isEqualTo(manifest)
@@ -65,8 +70,6 @@ class ManifestFetcherTest {
6570
mockkStatic("software.aws.toolkits.jetbrains.core.HttpUtilsKt")
6671
every { getTextFromUrl(any()) } returns "ManifestContent"
6772

68-
whenever(manifestManager.readManifestFile("")).thenReturn(null)
69-
7073
assertThat(manifestFetcher.fetchManifestFromRemote()).isNull()
7174
}
7275

@@ -84,17 +87,36 @@ class ManifestFetcherTest {
8487
@Test
8588
fun `fetchManifestFromRemote should return null if manifest is deprecated`() {
8689
mockkStatic("software.aws.toolkits.jetbrains.core.HttpUtilsKt")
87-
every { getTextFromUrl(any()) } returns "ManifestContent"
88-
89-
val deprecatedManifest = ManifestManager.Manifest(isManifestDeprecated = true)
90-
91-
whenever(manifestManager.readManifestFile("")).thenReturn(deprecatedManifest)
90+
every { getTextFromUrl(any()) } returns
91+
// language=JSON
92+
"""
93+
{
94+
"manifestSchemaVersion": "1.0",
95+
"isManifestDeprecated": true
96+
}
97+
""".trimIndent()
9298

9399
assertThat(manifestFetcher.fetchManifestFromRemote()).isNull()
94100
}
95101

96102
@Test
97-
fun `fetchManifestFromLocal should return null`() {
103+
fun `fetchManifestFromLocal should return null if path does not exist locally`() {
104+
whenever(manifestFetcher.lspManifestFilePath).thenReturn(Paths.get("does", "not", "exist"))
105+
assertThat(manifestFetcher.fetchManifestFromLocal()).isNull()
106+
}
107+
108+
@Test
109+
fun `fetchManifestFromLocal should return local path if exists locally`(@TempDir tempDir: Path) {
110+
val manifestFile = tempDir.createFile("manifest.json")
111+
manifestFile.toFile().writeText(
112+
// language=JSON
113+
"""
114+
{
115+
"manifestSchemaVersion": "1.0"
116+
}
117+
""".trimIndent()
118+
)
119+
whenever(manifestFetcher.lspManifestFilePath).thenReturn(manifestFile)
98120
assertThat(manifestFetcher.fetchManifestFromLocal()).isNull()
99121
}
100122
}

0 commit comments

Comments
 (0)