Skip to content

Commit e03d973

Browse files
authored
Test coverage (#172)
* Cleanup tests * Improve test coverage and remove another unnecessary IO dispatch
1 parent 4b4f4c5 commit e03d973

4 files changed

Lines changed: 73 additions & 38 deletions

File tree

src/main/kotlin/com/mituuz/fuzzier/grep/FuzzyGrep.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ open class FuzzyGrep : FuzzyAction() {
8181

8282
val projectBasePath = project.basePath.toString()
8383
currentLaunchJob = actionScope?.launch(Dispatchers.EDT) {
84-
val backendResult: Result<BackendStrategy> = withContext(Dispatchers.IO) {
85-
backendResolver.resolveBackend(commandRunner, projectBasePath)
86-
}
84+
val backendResult: Result<BackendStrategy> = backendResolver.resolveBackend(commandRunner, projectBasePath)
8785
if (backendResult.isFailure) {
8886
showNotification(
8987
"No search command found", "Fuzzy Grep failed: no suitable grep command found", project
@@ -92,7 +90,7 @@ open class FuzzyGrep : FuzzyAction() {
9290
}
9391

9492
val resolvedBackend = backendResult.getOrNull() ?: return@launch
95-
updateBackend(resolvedBackend)
93+
backend = resolvedBackend
9694
val popupTitle = grepConfig.getPopupTitle(resolvedBackend.name)
9795

9896
yield()
@@ -149,7 +147,8 @@ open class FuzzyGrep : FuzzyAction() {
149147
searchString,
150148
project,
151149
changelistManager,
152-
backend
150+
backend,
151+
(component as FuzzyFinderComponent),
153152
)
154153
coroutineContext.ensureActive()
155154

@@ -170,13 +169,14 @@ open class FuzzyGrep : FuzzyAction() {
170169
searchString: String,
171170
project: Project,
172171
clm: ChangeListManager,
173-
resolvedBackend: BackendStrategy?
172+
resolvedBackend: BackendStrategy?,
173+
fuzzyFinderComponent: FuzzyFinderComponent,
174174
): ListModel<FuzzyContainer> {
175175
val listModel = DefaultListModel<FuzzyContainer>()
176176
val projectBasePath = project.basePath
177177

178178
if (resolvedBackend != null && projectBasePath != null) {
179-
val secondaryFieldText = (component as FuzzyFinderComponent).getSecondaryText()
179+
val secondaryFieldText = fuzzyFinderComponent.getSecondaryText()
180180
resolvedBackend.handleSearch(
181181
grepConfig, searchString, secondaryFieldText, commandRunner, listModel, projectBasePath, project
182182
) { vf ->
@@ -240,10 +240,6 @@ open class FuzzyGrep : FuzzyAction() {
240240
}
241241
}
242242

243-
fun updateBackend(resolvedBackend: BackendStrategy?) {
244-
backend = resolvedBackend
245-
}
246-
247243
fun updateGrepConfig(config: GrepConfig) {
248244
grepConfig = config
249245
}

src/main/kotlin/com/mituuz/fuzzier/grep/backend/BackendResolver.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,22 @@ import com.intellij.openapi.components.service
2929
import com.mituuz.fuzzier.runner.CommandRunner
3030
import com.mituuz.fuzzier.settings.FuzzierGlobalSettingsService
3131
import com.mituuz.fuzzier.settings.FuzzierGlobalSettingsService.GrepBackend
32+
import kotlinx.coroutines.Dispatchers
33+
import kotlinx.coroutines.withContext
3234

3335
class BackendResolver(val isWindows: Boolean) {
34-
suspend fun resolveBackend(commandRunner: CommandRunner, projectBasePath: String): Result<BackendStrategy> {
35-
val grepBackendSetting = service<FuzzierGlobalSettingsService>().state.grepBackend
36+
suspend fun resolveBackend(commandRunner: CommandRunner, projectBasePath: String): Result<BackendStrategy> =
37+
withContext(Dispatchers.IO) {
38+
val grepBackendSetting = service<FuzzierGlobalSettingsService>().state.grepBackend
3639

37-
return when (grepBackendSetting) {
38-
GrepBackend.FUZZIER -> Result.success(FuzzierGrep)
39-
GrepBackend.DYNAMIC -> when {
40-
isInstalled(commandRunner, "rg", projectBasePath) -> Result.success(Ripgrep)
41-
else -> Result.success(FuzzierGrep)
40+
when (grepBackendSetting) {
41+
GrepBackend.FUZZIER -> Result.success(FuzzierGrep)
42+
GrepBackend.DYNAMIC -> when {
43+
isInstalled(commandRunner, "rg", projectBasePath) -> Result.success(Ripgrep)
44+
else -> Result.success(FuzzierGrep)
45+
}
4246
}
4347
}
44-
}
4548

4649
private suspend fun isInstalled(
4750
commandRunner: CommandRunner,

src/main/kotlin/com/mituuz/fuzzier/grep/backend/BackendStrategy.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import com.mituuz.fuzzier.entities.GrepConfig
3131
import com.mituuz.fuzzier.runner.CommandRunner
3232
import javax.swing.DefaultListModel
3333

34-
sealed interface BackendStrategy {
34+
interface BackendStrategy {
3535
val name: String
3636

3737
suspend fun handleSearch(

src/test/kotlin/com/mituuz/fuzzier/grep/FuzzyGrepTest.kt

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,30 @@ import com.intellij.openapi.vfs.VirtualFile
3030
import com.intellij.testFramework.TestApplicationManager
3131
import com.mituuz.fuzzier.components.FuzzyFinderComponent
3232
import com.mituuz.fuzzier.entities.CaseMode
33+
import com.mituuz.fuzzier.entities.FuzzyContainer
3334
import com.mituuz.fuzzier.entities.GrepConfig
3435
import com.mituuz.fuzzier.grep.backend.BackendStrategy
36+
import com.mituuz.fuzzier.runner.CommandRunner
3537
import io.mockk.every
3638
import io.mockk.mockk
3739
import io.mockk.unmockkAll
3840
import kotlinx.coroutines.runBlocking
3941
import org.junit.jupiter.api.AfterEach
40-
import org.junit.jupiter.api.Assertions.assertEquals
41-
import org.junit.jupiter.api.Assertions.assertNotNull
42+
import org.junit.jupiter.api.Assertions.*
4243
import org.junit.jupiter.api.BeforeEach
4344
import org.junit.jupiter.api.Test
45+
import javax.swing.DefaultListModel
4446

4547
class FuzzyGrepTest {
4648
private lateinit var fGrep: FuzzyGrep
4749

4850
private data class ValidVfContext(
49-
val file: VirtualFile,
50-
val clm: ChangeListManager
51+
val file: VirtualFile, val clm: ChangeListManager
5152
)
5253

5354
private data class FindInFilesContext(
5455
val project: Project,
5556
val component: FuzzyFinderComponent,
56-
val backend: BackendStrategy,
5757
val clm: ChangeListManager
5858
)
5959

@@ -69,10 +69,7 @@ class FuzzyGrepTest {
6969
}
7070

7171
private fun createValidVfContext(
72-
isDirectory: Boolean = false,
73-
isBinary: Boolean = false,
74-
isIgnored: Boolean = false,
75-
extension: String? = null
72+
isDirectory: Boolean = false, isBinary: Boolean = false, isIgnored: Boolean = false, extension: String? = null
7673
): ValidVfContext {
7774
val file = mockk<VirtualFile>()
7875
val clm = mockk<ChangeListManager>()
@@ -88,24 +85,21 @@ class FuzzyGrepTest {
8885
}
8986

9087
private fun createFindInFilesContext(
91-
projectBasePath: String? = "/tmp/project",
92-
secondaryText: String = "kt"
88+
projectBasePath: String? = "/tmp/project", secondaryText: String = "kt"
9389
): FindInFilesContext {
9490
val project = mockk<Project>()
9591
val component = mockk<FuzzyFinderComponent>()
96-
val backend = mockk<BackendStrategy>()
9792
val clm = mockk<ChangeListManager>()
9893

9994
every { project.basePath } returns projectBasePath
10095
every { component.getSecondaryText() } returns secondaryText
10196

10297
fGrep.component = component
103-
fGrep.updateBackend(backend)
10498
fGrep.updateGrepConfig(
10599
GrepConfig(targets = null, caseMode = CaseMode.SENSITIVE, title = "Fuzzy Grep")
106100
)
107101

108-
return FindInFilesContext(project, component, backend, clm)
102+
return FindInFilesContext(project, component, clm)
109103
}
110104

111105
@Test
@@ -158,21 +152,63 @@ class FuzzyGrepTest {
158152

159153
@Test
160154
fun `findInFiles should skip backend when backend is null`() = runBlocking {
161-
val (project, _, _, clm) = createFindInFilesContext()
155+
val (project, component, clm) = createFindInFilesContext()
162156

163-
val model = fGrep.findInFiles("needle", project, clm, null)
157+
val model = fGrep.findInFiles("needle", project, clm, null, component)
164158

165159
assertNotNull(model)
166160
assertEquals(0, model.size)
167161
}
168162

169163
@Test
170164
fun `findInFiles should skip backend when project base path is null`() = runBlocking {
171-
val (project, _, backend, clm) = createFindInFilesContext(projectBasePath = null)
165+
val (project, component, clm) = createFindInFilesContext(projectBasePath = null)
166+
val backend = MockBackend()
172167

173-
val model = fGrep.findInFiles("needle", project, clm, backend)
168+
val model = fGrep.findInFiles("needle", project, clm, backend, component)
174169

175170
assertNotNull(model)
176171
assertEquals(0, model.size)
172+
assertFalse(backend.wasCalled)
173+
}
174+
175+
@Test
176+
fun `findInFiles calls backend when backend and project base path are available`() = runBlocking {
177+
val (project, component, clm) = createFindInFilesContext(projectBasePath = "not/null")
178+
val backend = MockBackend()
179+
180+
val model = fGrep.findInFiles("needle", project, clm, backend, component)
181+
182+
assertNotNull(model)
183+
assertEquals(0, model.size)
184+
assertTrue(backend.wasCalled)
185+
assertEquals("needle", backend.receivedSearchString)
186+
assertEquals("kt", backend.receivedSecondarySearchString)
187+
assertEquals("not/null", backend.receivedProjectBasePath)
188+
}
189+
190+
private class MockBackend : BackendStrategy {
191+
override val name: String = "Mock"
192+
193+
var wasCalled = false
194+
var receivedSearchString: String? = null
195+
var receivedSecondarySearchString: String? = null
196+
var receivedProjectBasePath: String? = null
197+
198+
override suspend fun handleSearch(
199+
grepConfig: GrepConfig,
200+
searchString: String,
201+
secondarySearchString: String?,
202+
commandRunner: CommandRunner,
203+
listModel: DefaultListModel<FuzzyContainer>,
204+
projectBasePath: String,
205+
project: Project,
206+
fileFilter: (VirtualFile) -> Boolean
207+
) {
208+
wasCalled = true
209+
receivedSearchString = searchString
210+
receivedSecondarySearchString = secondarySearchString
211+
receivedProjectBasePath = projectBasePath
212+
}
177213
}
178214
}

0 commit comments

Comments
 (0)