|
| 1 | +/* |
| 2 | + * Copyright 2025 https://github.com/openapi-processor/openapi-processor-intellij |
| 3 | + * PDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.openapiprocessor.intellij |
| 7 | + |
| 8 | +import com.intellij.openapi.application.ReadAction |
| 9 | +import com.intellij.openapi.application.runWriteActionAndWait |
| 10 | +import com.intellij.openapi.components.service |
| 11 | +import com.intellij.openapi.module.ModuleType |
| 12 | +import com.intellij.openapi.module.ModuleUtil |
| 13 | +import com.intellij.openapi.vfs.VfsUtil |
| 14 | +import com.intellij.psi.PsiDirectory |
| 15 | +import com.intellij.testFramework.PsiTestUtil |
| 16 | +import com.intellij.testFramework.junit5.TestApplication |
| 17 | +import com.intellij.testFramework.junit5.fixture.projectFixture |
| 18 | +import com.intellij.testFramework.junit5.fixture.testFixture |
| 19 | +import com.intellij.testFramework.utils.vfs.refreshAndGetVirtualDirectory |
| 20 | +import com.intellij.util.io.directoryContent |
| 21 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 22 | +import org.junit.jupiter.api.Test |
| 23 | +import kotlin.io.path.Path |
| 24 | +import kotlin.io.path.absolute |
| 25 | + |
| 26 | +@TestApplication |
| 27 | +class TargetPackageFinderSpec { |
| 28 | + val projectFixture = projectFixture() |
| 29 | + |
| 30 | + val moduleFixture = testFixture { |
| 31 | + val project = projectFixture.init() |
| 32 | + |
| 33 | + val path = Path(project.basePath!!).absolute() |
| 34 | + |
| 35 | + directoryContent { |
| 36 | + dir("build") { |
| 37 | + dir("out") { |
| 38 | + dir("main") {} |
| 39 | + dir("test") {} |
| 40 | + } |
| 41 | + dir("openapi") { |
| 42 | + dir("io") { |
| 43 | + dir("openapiprocessor") {} |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + dir("src") { |
| 48 | + dir("api") { |
| 49 | + file("mapping.yaml", """ |
| 50 | + openapi-processor-mapping: v0 |
| 51 | + options: |
| 52 | + package-name: io.openapiprocessor |
| 53 | + """.trimIndent()) |
| 54 | + } |
| 55 | + dir("main") { |
| 56 | + dir("io") { |
| 57 | + dir("openapiprocessor") {} |
| 58 | + } |
| 59 | + } |
| 60 | + dir("test") { |
| 61 | + dir("io") { |
| 62 | + dir("openapiprocessor") {} |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + }.generate(path) |
| 67 | + |
| 68 | + val mappingModule = runWriteActionAndWait { |
| 69 | + // main module |
| 70 | + val main = path.resolve("src/main").refreshAndGetVirtualDirectory() |
| 71 | + val mainModule = PsiTestUtil.addModule(project, ModuleType.EMPTY, "main", main) |
| 72 | + val mainContent = PsiTestUtil.addContentRoot(mainModule, main) |
| 73 | + val mainSource = PsiTestUtil.addSourceRoot(mainModule, main) |
| 74 | + val mainOut = path.resolve("build/out/main").refreshAndGetVirtualDirectory() |
| 75 | + PsiTestUtil.setCompilerOutputPath(mainModule, mainOut.path, false) |
| 76 | + |
| 77 | + // api module |
| 78 | + val api = path.resolve("src/api").refreshAndGetVirtualDirectory() |
| 79 | + val apiModule = PsiTestUtil.addModule(project, ModuleType.EMPTY, "api", api) |
| 80 | + val apiContent = PsiTestUtil.addContentRoot(apiModule, api) |
| 81 | + val apiSource = PsiTestUtil.addSourceRoot(apiModule, api) |
| 82 | + // api module output is an additional source root of the main module |
| 83 | + val apiOut = path.resolve("build/openapi").refreshAndGetVirtualDirectory() |
| 84 | + PsiTestUtil.setCompilerOutputPath(apiModule, apiOut.path, false) |
| 85 | + PsiTestUtil.addSourceRoot(mainModule, apiOut) |
| 86 | + |
| 87 | + // test module |
| 88 | + val test = path.resolve("src/test").refreshAndGetVirtualDirectory() |
| 89 | + val testModule = PsiTestUtil.addModule(project, ModuleType.EMPTY, "test", test) |
| 90 | + val testContent = PsiTestUtil.addContentRoot(testModule, test) |
| 91 | + val testSource = PsiTestUtil.addSourceRoot(testModule, test) |
| 92 | + val testOut = path.resolve("build/out/test").refreshAndGetVirtualDirectory() |
| 93 | + PsiTestUtil.setCompilerOutputPath(testModule, testOut.path, true) |
| 94 | + |
| 95 | + val virtualFilePath = path.refreshAndGetVirtualDirectory() |
| 96 | + val mapping = VfsUtil.findRelativeFile(virtualFilePath, "src", "api", "mapping.yaml")!! |
| 97 | + |
| 98 | + ModuleUtil.findModuleForFile(mapping, project) |
| 99 | + } |
| 100 | + |
| 101 | + initialized(mappingModule!!) {} |
| 102 | + } |
| 103 | + |
| 104 | + fun actualDirs(dirs: List<PsiDirectory>): List<String> { |
| 105 | + val basePathLength = projectFixture.get().basePath!!.length + 1 |
| 106 | + return dirs |
| 107 | + .map { it.virtualFile.path.substring(basePathLength) } |
| 108 | + .sorted() |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun `find target package directories`() { |
| 113 | + val service = service<TargetPackageService>() |
| 114 | + |
| 115 | + val pkgDirs = ReadAction.compute<List<PsiDirectory>, Throwable> { |
| 116 | + service.findPackageDirs("io.openapiprocessor", moduleFixture.get()) |
| 117 | + } |
| 118 | + |
| 119 | + assertEquals(3, pkgDirs.size) |
| 120 | + assertEquals( |
| 121 | + listOf( |
| 122 | + "src/main/io/openapiprocessor", |
| 123 | + "src/test/io/openapiprocessor", |
| 124 | + "build/openapi/io/openapiprocessor") |
| 125 | + .sorted(), |
| 126 | + actualDirs(pkgDirs)) |
| 127 | + } |
| 128 | +} |
| 129 | + |
0 commit comments