|
| 1 | +package software.aws.toolkits.jetbrains.services.amazonq.workspace.context |
| 2 | + |
| 3 | +import com.intellij.openapi.vfs.VfsUtil |
| 4 | +import com.intellij.openapi.vfs.VirtualFile |
| 5 | +import com.intellij.openapi.vfs.refreshAndFindVirtualDirectory |
| 6 | +import com.intellij.openapi.vfs.refreshAndFindVirtualFile |
| 7 | +import com.intellij.testFramework.LightPlatformTestCase |
| 8 | +import software.aws.toolkits.jetbrains.services.amazonq.project.findWorkspaceRoot |
| 9 | +import software.aws.toolkits.jetbrains.services.amazonq.project.isContentInWorkspace |
| 10 | +import java.nio.file.Files |
| 11 | +import java.nio.file.Path |
| 12 | +import kotlin.io.path.createDirectories |
| 13 | +import kotlin.io.path.deleteExisting |
| 14 | +import kotlin.io.path.exists |
| 15 | + |
| 16 | +class WorkspaceTest : LightPlatformTestCase() { |
| 17 | + private lateinit var tempDir: Path |
| 18 | + |
| 19 | + |
| 20 | + override fun setUp() { |
| 21 | + super.setUp() |
| 22 | + tempDir = Files.createTempDirectory("workspace-test") |
| 23 | + } |
| 24 | + |
| 25 | + override fun tearDown() { |
| 26 | + if (tempDir.exists()) { |
| 27 | + Files.walk(tempDir) |
| 28 | + .sorted(Comparator.reverseOrder()) |
| 29 | + .forEach { it.deleteExisting() } |
| 30 | + } |
| 31 | + super.tearDown() |
| 32 | + } |
| 33 | + |
| 34 | + private fun createDir(relativePath: String): VirtualFile { |
| 35 | + val normalizedPath = relativePath.removePrefix("/") |
| 36 | + return if (normalizedPath.isEmpty()) { |
| 37 | + tempDir.refreshAndFindVirtualDirectory()!! |
| 38 | + } else { |
| 39 | + tempDir.resolve(normalizedPath).createDirectories().refreshAndFindVirtualDirectory()!! |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private fun createFile(path: String): VirtualFile { |
| 44 | + val filePath = tempDir.resolve(path.removePrefix("/")) |
| 45 | + Files.createDirectories(filePath.parent) |
| 46 | + return Files.createFile(filePath).refreshAndFindVirtualFile()!! |
| 47 | + } |
| 48 | + |
| 49 | + fun `testFindWorkspaceRoot returns null when no projects`() { |
| 50 | + assertNull(findWorkspaceRoot(emptySet())) |
| 51 | + } |
| 52 | + |
| 53 | + fun `test findWorkspaceRoot returns project path for single project`() { |
| 54 | + val projectPath = createDir("test/project") |
| 55 | + |
| 56 | + assertEquals(projectPath, findWorkspaceRoot(setOf(projectPath))) |
| 57 | + } |
| 58 | + |
| 59 | + fun `test findWorkspaceRoot returns project path for project with inner modules`() { |
| 60 | + val path1 = createDir("test/projects/project1") |
| 61 | + val path2 = createDir("test/projects/project1/module") |
| 62 | + |
| 63 | + assertEquals(path1, findWorkspaceRoot(setOf(path1, path2))) |
| 64 | + } |
| 65 | + |
| 66 | + fun `test findWorkspaceRoot returns common root of multiple projects`() { |
| 67 | + val path1 = createDir("test/projects/project1") |
| 68 | + val path2 = createDir("test/projects/project2") |
| 69 | + |
| 70 | + assertEquals(createDir("test/projects"), findWorkspaceRoot(setOf(path1, path2))) |
| 71 | + } |
| 72 | + |
| 73 | + fun `test findWorkspaceRoot returns common root of a project and external modules`() { |
| 74 | + val projectPath = createDir("test/project") |
| 75 | + val modulePath = createDir("test/external/module") |
| 76 | + |
| 77 | + assertEquals(createDir("test"), findWorkspaceRoot(setOf(projectPath, modulePath))) |
| 78 | + } |
| 79 | + |
| 80 | + fun `test isContentInWorkspace returns false when workspace has no directories`() { |
| 81 | + assertFalse(isContentInWorkspace(createDir("any/path"), emptySet())) |
| 82 | + } |
| 83 | + |
| 84 | + fun `test isContentInWorkspace returns true for path in project`() { |
| 85 | + val projectPath = createDir("test/project") |
| 86 | + val testPath = createFile("test/project/src/file.txt") |
| 87 | + |
| 88 | + assertTrue(isContentInWorkspace(testPath, setOf(projectPath))) |
| 89 | + } |
| 90 | + |
| 91 | + fun `test isContentInWorkspace returns true for path in external module`() { |
| 92 | + val projectPath = createDir("test/project") |
| 93 | + val modulePath = createDir("test/external/module") |
| 94 | + |
| 95 | + val testPath = createFile("test/external/module/src/file.txt") |
| 96 | + |
| 97 | + assertTrue(isContentInWorkspace(testPath, setOf(projectPath, modulePath))) |
| 98 | + } |
| 99 | + |
| 100 | + fun `test isContentInWorkspace returns false for path outside project and modules`() { |
| 101 | + val projectPath = createDir("test/project") |
| 102 | + val modulePath = createDir("test/external/module") |
| 103 | + |
| 104 | + val testPath = createFile("other/path/file.txt") |
| 105 | + |
| 106 | + assertFalse(isContentInWorkspace(testPath, setOf(projectPath, modulePath))) |
| 107 | + } |
| 108 | +} |
0 commit comments