|
1 | 1 | package io.snyk.plugin |
2 | 2 |
|
| 3 | +import com.intellij.ide.util.PsiNavigationSupport |
3 | 4 | import com.intellij.openapi.application.ApplicationManager |
4 | 5 | import com.intellij.openapi.project.Project |
5 | 6 | import com.intellij.openapi.vfs.VirtualFile |
| 7 | +import com.intellij.pom.Navigatable |
6 | 8 | import com.intellij.util.messages.MessageBus |
7 | 9 | import com.intellij.util.messages.Topic |
8 | 10 | import io.mockk.every |
@@ -311,6 +313,89 @@ class UtilsKtTest { |
311 | 313 | verify(exactly = 0) { messageBus.syncPublisher(any<Topic<TestListener>>()) } |
312 | 314 | } |
313 | 315 |
|
| 316 | + @Test |
| 317 | + fun `navigateToSource should still open file when getDocument returns null`() { |
| 318 | + unmockkAll() |
| 319 | + mockkStatic("io.snyk.plugin.UtilsKt") |
| 320 | + mockkStatic(ApplicationManager::class) |
| 321 | + mockkStatic(PsiNavigationSupport::class) |
| 322 | + |
| 323 | + val appMock = mockk<com.intellij.openapi.application.Application>(relaxed = true) |
| 324 | + every { ApplicationManager.getApplication() } returns appMock |
| 325 | + every { appMock.isDisposed } returns false |
| 326 | + // Mock invokeLater to execute runnables immediately |
| 327 | + // The top-level invokeLater delegates to Application.invokeLater with ModalityState |
| 328 | + every { appMock.invokeLater(any<Runnable>()) } answers { firstArg<Runnable>().run() } |
| 329 | + every { |
| 330 | + appMock.invokeLater(any<Runnable>(), any<com.intellij.openapi.application.ModalityState>()) |
| 331 | + } answers { firstArg<Runnable>().run() } |
| 332 | + |
| 333 | + val project = mockk<Project>(relaxed = true) |
| 334 | + every { project.isDisposed } returns false |
| 335 | + |
| 336 | + val virtualFile = mockk<VirtualFile>(relaxed = true) |
| 337 | + every { virtualFile.isValid } returns true |
| 338 | + // Simulate a file type that IntelliJ doesn't recognize (e.g., .gitleaksignore) |
| 339 | + every { virtualFile.getDocument() } returns null |
| 340 | + |
| 341 | + val latch = CountDownLatch(1) |
| 342 | + val navigatable = mockk<Navigatable>(relaxed = true) |
| 343 | + every { navigatable.canNavigateToSource() } returns true |
| 344 | + every { navigatable.canNavigate() } returns true |
| 345 | + every { navigatable.navigate(any()) } answers { latch.countDown() } |
| 346 | + |
| 347 | + val psiNavSupport = mockk<PsiNavigationSupport>(relaxed = true) |
| 348 | + every { PsiNavigationSupport.getInstance() } returns psiNavSupport |
| 349 | + every { psiNavSupport.createNavigatable(project, virtualFile, 5) } returns navigatable |
| 350 | + |
| 351 | + navigateToSource(project, virtualFile, 5, 10) |
| 352 | + |
| 353 | + assertTrue("Navigation should complete within timeout", latch.await(2, TimeUnit.SECONDS)) |
| 354 | + verify { navigatable.navigate(false) } |
| 355 | + } |
| 356 | + |
| 357 | + @Test |
| 358 | + fun `navigateToSource should still open file when offset is out of bounds`() { |
| 359 | + unmockkAll() |
| 360 | + mockkStatic("io.snyk.plugin.UtilsKt") |
| 361 | + mockkStatic(ApplicationManager::class) |
| 362 | + mockkStatic(PsiNavigationSupport::class) |
| 363 | + |
| 364 | + val appMock = mockk<com.intellij.openapi.application.Application>(relaxed = true) |
| 365 | + every { ApplicationManager.getApplication() } returns appMock |
| 366 | + every { appMock.isDisposed } returns false |
| 367 | + every { appMock.invokeLater(any<Runnable>()) } answers { firstArg<Runnable>().run() } |
| 368 | + every { |
| 369 | + appMock.invokeLater(any<Runnable>(), any<com.intellij.openapi.application.ModalityState>()) |
| 370 | + } answers { firstArg<Runnable>().run() } |
| 371 | + |
| 372 | + val project = mockk<Project>(relaxed = true) |
| 373 | + every { project.isDisposed } returns false |
| 374 | + |
| 375 | + val document = mockk<com.intellij.openapi.editor.Document>(relaxed = true) |
| 376 | + every { document.textLength } returns 50 |
| 377 | + |
| 378 | + val virtualFile = mockk<VirtualFile>(relaxed = true) |
| 379 | + every { virtualFile.isValid } returns true |
| 380 | + every { virtualFile.getDocument() } returns document |
| 381 | + |
| 382 | + // Offset 100 is beyond document length of 50 |
| 383 | + val latch = CountDownLatch(1) |
| 384 | + val navigatable = mockk<Navigatable>(relaxed = true) |
| 385 | + every { navigatable.canNavigateToSource() } returns true |
| 386 | + every { navigatable.canNavigate() } returns true |
| 387 | + every { navigatable.navigate(any()) } answers { latch.countDown() } |
| 388 | + |
| 389 | + val psiNavSupport = mockk<PsiNavigationSupport>(relaxed = true) |
| 390 | + every { PsiNavigationSupport.getInstance() } returns psiNavSupport |
| 391 | + every { psiNavSupport.createNavigatable(project, virtualFile, 100) } returns navigatable |
| 392 | + |
| 393 | + navigateToSource(project, virtualFile, 100, 110) |
| 394 | + |
| 395 | + assertTrue("Navigation should complete within timeout", latch.await(2, TimeUnit.SECONDS)) |
| 396 | + verify { navigatable.navigate(false) } |
| 397 | + } |
| 398 | + |
314 | 399 | interface TestListener { |
315 | 400 | fun onEvent() |
316 | 401 | } |
|
0 commit comments