Skip to content

Commit df60414

Browse files
committed
tst
1 parent e6fe035 commit df60414

File tree

2 files changed

+27
-27
lines changed
  • plugins/amazonq/shared/jetbrains-community

2 files changed

+27
-27
lines changed

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

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

66
import com.intellij.openapi.components.Service
77
import com.intellij.openapi.project.Project
8+
import com.intellij.serviceContainer.NonInjectable
89
import com.intellij.util.text.SemVer
910
import kotlinx.coroutines.Deferred
1011
import kotlinx.coroutines.async
@@ -19,9 +20,11 @@ import software.aws.toolkits.jetbrains.services.amazonq.project.manifest.Manifes
1920
import java.nio.file.Path
2021

2122
@Service
22-
class ArtifactManager {
23-
private val manifestFetcher: ManifestFetcher = ManifestFetcher()
24-
private val artifactHelper: ArtifactHelper = ArtifactHelper()
23+
class ArtifactManager @NonInjectable internal constructor(private val manifestFetcher: ManifestFetcher, private val artifactHelper: ArtifactHelper) {
24+
constructor() : this(
25+
ManifestFetcher(),
26+
ArtifactHelper()
27+
)
2528

2629
// we currently cannot handle the versions swithing in the middle of a user's session
2730
private val mutex = Mutex()

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import com.intellij.testFramework.ProjectExtension
77
import com.intellij.util.text.SemVer
88
import io.mockk.Runs
99
import io.mockk.coEvery
10+
import io.mockk.coVerify
1011
import io.mockk.every
1112
import io.mockk.just
1213
import io.mockk.mockkStatic
1314
import io.mockk.spyk
1415
import io.mockk.verify
15-
import kotlinx.coroutines.runBlocking
16-
import org.assertj.core.api.Assertions.assertThatThrownBy
16+
import kotlinx.coroutines.test.runTest
17+
import org.assertj.core.api.Assertions.assertThat
1718
import org.junit.jupiter.api.BeforeEach
1819
import org.junit.jupiter.api.Test
20+
import org.junit.jupiter.api.assertThrows
1921
import org.junit.jupiter.api.extension.RegisterExtension
2022
import org.junit.jupiter.api.io.TempDir
2123
import software.aws.toolkits.jetbrains.services.amazonq.lsp.artifacts.ArtifactManager.SupportedManifestVersionRange
@@ -45,56 +47,53 @@ class ArtifactManagerTest {
4547
startVersion = SemVer("1.0.0", 1, 0, 0),
4648
endVersion = SemVer("2.0.0", 2, 0, 0)
4749
)
48-
artifactManager = ArtifactManager()
50+
artifactManager = spyk(ArtifactManager(manifestFetcher, artifactHelper))
4951
}
5052

5153
@Test
52-
fun `fetch artifact fetcher throws exception if manifest is null`() {
54+
fun `fetch artifact fetcher throws exception if manifest is null`() = runTest {
5355
every { manifestFetcher.fetch() }.returns(null)
5456

55-
assertThatThrownBy {
56-
runBlocking { artifactManager.fetchArtifact(projectExtension.project) }
57+
val exception = assertThrows<LspException> {
58+
artifactManager.fetchArtifact(projectExtension.project)
5759
}
58-
.isInstanceOf(LspException::class.java)
60+
assertThat(exception)
5961
.hasFieldOrPropertyWithValue("errorCode", LspException.ErrorCode.MANIFEST_FETCH_FAILED)
6062
}
6163

6264
@Test
63-
fun `fetch artifact does not have any valid lsp versions`() {
65+
fun `fetch artifact does not have any valid lsp versions`() = runTest {
6466
every { manifestFetcher.fetch() }.returns(ManifestManager.Manifest())
65-
artifactManager = spyk(ArtifactManager())
6667

6768
every { artifactManager.getLSPVersionsFromManifestWithSpecifiedRange(any()) }.returns(
6869
ArtifactManager.LSPVersions(deListedVersions = emptyList(), inRangeVersions = emptyList())
6970
)
7071

71-
assertThatThrownBy {
72-
runBlocking { artifactManager.fetchArtifact(projectExtension.project) }
72+
val exception = assertThrows<LspException> {
73+
artifactManager.fetchArtifact(projectExtension.project)
7374
}
74-
.isInstanceOf(LspException::class.java)
75+
assertThat(exception)
7576
.hasFieldOrPropertyWithValue("errorCode", LspException.ErrorCode.NO_COMPATIBLE_LSP_VERSION)
7677
}
7778

7879
@Test
79-
fun `fetch artifact if inRangeVersions are not available should fallback to local lsp`() {
80+
fun `fetch artifact if inRangeVersions are not available should fallback to local lsp`() = runTest {
8081
val expectedResult = listOf(Pair(tempDir, SemVer("1.0.0", 1, 0, 0)))
8182

8283
every { manifestFetcher.fetch() }.returns(ManifestManager.Manifest())
8384
every { artifactHelper.getAllLocalLspArtifactsWithinManifestRange(any()) }.returns(expectedResult)
8485

85-
runBlocking { artifactManager.fetchArtifact(projectExtension.project) }
86+
artifactManager.fetchArtifact(projectExtension.project)
8687

8788
verify(exactly = 1) { manifestFetcher.fetch() }
8889
verify(exactly = 1) { artifactHelper.getAllLocalLspArtifactsWithinManifestRange(any()) }
8990
}
9091

9192
@Test
92-
fun `fetch artifact have valid version in local system`() {
93+
fun `fetch artifact have valid version in local system`() = runTest {
9394
val target = ManifestManager.VersionTarget(platform = "temp", arch = "temp")
9495
val versions = listOf(ManifestManager.Version("1.0.0", targets = listOf(target)))
9596

96-
artifactManager = spyk(ArtifactManager())
97-
9897
every { artifactManager.getLSPVersionsFromManifestWithSpecifiedRange(any()) }.returns(
9998
ArtifactManager.LSPVersions(deListedVersions = emptyList(), inRangeVersions = versions)
10099
)
@@ -108,20 +107,18 @@ class ArtifactManagerTest {
108107
coEvery { artifactHelper.tryDownloadLspArtifacts(any(), any(), any()) } returns tempDir
109108
every { artifactHelper.deleteOlderLspArtifacts(any()) } just Runs
110109

111-
runBlocking { artifactManager.fetchArtifact(projectExtension.project) }
110+
artifactManager.fetchArtifact(projectExtension.project)
112111

113-
verify(exactly = 1) { runBlocking { artifactHelper.tryDownloadLspArtifacts(any(), any(), any()) } }
112+
coVerify(exactly = 1) { artifactHelper.tryDownloadLspArtifacts(any(), any(), any()) }
114113
verify(exactly = 1) { artifactHelper.deleteOlderLspArtifacts(any()) }
115114
}
116115

117116
@Test
118-
fun `fetch artifact does not have valid version in local system`() {
117+
fun `fetch artifact does not have valid version in local system`() = runTest {
119118
val target = ManifestManager.VersionTarget(platform = "temp", arch = "temp")
120119
val versions = listOf(ManifestManager.Version("1.0.0", targets = listOf(target)))
121120
val expectedResult = listOf(Pair(tempDir, SemVer("1.0.0", 1, 0, 0)))
122121

123-
artifactManager = spyk(ArtifactManager())
124-
125122
every { artifactManager.getLSPVersionsFromManifestWithSpecifiedRange(any()) }.returns(
126123
ArtifactManager.LSPVersions(deListedVersions = emptyList(), inRangeVersions = versions)
127124
)
@@ -135,9 +132,9 @@ class ArtifactManagerTest {
135132
every { artifactHelper.deleteOlderLspArtifacts(any()) } just Runs
136133
every { artifactHelper.getAllLocalLspArtifactsWithinManifestRange(any()) }.returns(expectedResult)
137134

138-
runBlocking { artifactManager.fetchArtifact(projectExtension.project) }
135+
artifactManager.fetchArtifact(projectExtension.project)
139136

140-
verify(exactly = 0) { runBlocking { artifactHelper.tryDownloadLspArtifacts(any(), any(), any()) } }
137+
coVerify(exactly = 0) { artifactHelper.tryDownloadLspArtifacts(any(), any(), any()) }
141138
verify(exactly = 1) { artifactHelper.deleteOlderLspArtifacts(any()) }
142139
}
143140
}

0 commit comments

Comments
 (0)