|
4 | 4 | package software.aws.toolkits.jetbrains.services.amazonq.lsp |
5 | 5 |
|
6 | 6 | import com.intellij.execution.configurations.GeneralCommandLine |
| 7 | +import com.intellij.openapi.util.SystemInfo |
| 8 | +import com.intellij.testFramework.rules.TempDirectory |
| 9 | +import com.intellij.testFramework.utils.io.createDirectory |
| 10 | +import com.intellij.testFramework.utils.io.createFile |
7 | 11 | import org.assertj.core.api.Assertions.assertThat |
| 12 | +import org.junit.Assume.assumeTrue |
8 | 13 | import org.junit.Rule |
9 | 14 | import org.junit.Test |
10 | 15 | import software.aws.toolkits.core.rules.EnvironmentVariableHelper |
11 | | -import kotlin.io.path.Path |
| 16 | +import software.aws.toolkits.core.utils.exists |
| 17 | +import java.nio.file.Paths |
12 | 18 |
|
13 | 19 | class NodeExePatcherTest { |
14 | 20 | @get:Rule |
15 | 21 | val envVarHelper = EnvironmentVariableHelper() |
16 | 22 |
|
17 | | - private val pathToNode = Path("/path/to/node").toAbsolutePath().toString() |
| 23 | + @get:Rule |
| 24 | + val tempDir = TempDirectory() |
| 25 | + |
| 26 | + private val pathToNode = Paths.get("/path/to/node").toAbsolutePath().toString() |
18 | 27 |
|
19 | 28 | @Test |
20 | | - fun `patches if path available`() { |
21 | | - envVarHelper[NodeExePatcher.GLIBC_LINKER_VAR] = "/opt/vsc-sysroot/lib/ld-linux-x86-64.so.2" |
22 | | - envVarHelper[NodeExePatcher.GLIBC_PATH_VAR] = "/opt/vsc-sysroot/lib/" |
| 29 | + fun `patches if environment variables are available`() { |
| 30 | + val path = tempDir.newDirectory("vsc-sysroot").toPath().toAbsolutePath() |
| 31 | + val linker = Paths.get(path.toString(), "someSharedLibrary").createFile() |
23 | 32 |
|
24 | | - assertThat(NodeExePatcher.patch(Path("/path/to/node"))) |
| 33 | + envVarHelper[NodeExePatcher.GLIBC_LINKER_VAR] = linker.toString() |
| 34 | + envVarHelper[NodeExePatcher.GLIBC_PATH_VAR] = path.toString() |
| 35 | + |
| 36 | + assertThat(NodeExePatcher.patch(Paths.get("/path/to/node"))) |
25 | 37 | .usingComparator(Comparator.comparing { it.commandLineString }) |
26 | | - .isEqualTo(GeneralCommandLine("/opt/vsc-sysroot/lib/ld-linux-x86-64.so.2", "--library-path", "/opt/vsc-sysroot/lib/", pathToNode)) |
| 38 | + .isEqualTo(GeneralCommandLine(linker.toString(), "--library-path", path.toString(), pathToNode)) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `patches if hardcoded paths exists`() { |
| 43 | + // explicitly linux because can't run on mac |
| 44 | + assumeTrue(SystemInfo.isLinux) |
| 45 | + |
| 46 | + val path = Paths.get(NodeExePatcher.INTERNAL_GLIBC_PATH) |
| 47 | + val linker = Paths.get(NodeExePatcher.INTERNAL_X86_64_LINKER) |
| 48 | + val needsCreate = !path.exists() && !linker.exists() |
| 49 | + if (needsCreate) { |
| 50 | + path.createDirectory() |
| 51 | + linker.createFile() |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + assertThat(NodeExePatcher.patch(Paths.get("/path/to/node"))) |
| 56 | + .usingComparator(Comparator.comparing { it.commandLineString }) |
| 57 | + .isEqualTo(GeneralCommandLine(linker.toString(), "--library-path", path.toString(), pathToNode)) |
| 58 | + } finally { |
| 59 | + if (needsCreate) { |
| 60 | + linker.toFile().delete() |
| 61 | + path.toFile().deleteRecursively() |
| 62 | + } |
| 63 | + } |
27 | 64 | } |
28 | 65 |
|
29 | 66 | @Test |
30 | 67 | fun `noop if no patch available`() { |
31 | | - assertThat(NodeExePatcher.patch(Path("/path/to/node"))) |
| 68 | + assertThat(NodeExePatcher.patch(Paths.get("/path/to/node"))) |
32 | 69 | .usingComparator(Comparator.comparing { it.commandLineString }) |
33 | 70 | .isEqualTo(GeneralCommandLine(pathToNode)) |
34 | 71 | } |
|
0 commit comments