@@ -7,15 +7,17 @@ import com.intellij.testFramework.ProjectExtension
77import com.intellij.util.text.SemVer
88import io.mockk.Runs
99import io.mockk.coEvery
10+ import io.mockk.coVerify
1011import io.mockk.every
1112import io.mockk.just
1213import io.mockk.mockkStatic
1314import io.mockk.spyk
1415import 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
1718import org.junit.jupiter.api.BeforeEach
1819import org.junit.jupiter.api.Test
20+ import org.junit.jupiter.api.assertThrows
1921import org.junit.jupiter.api.extension.RegisterExtension
2022import org.junit.jupiter.api.io.TempDir
2123import 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