|
| 1 | +package io.github.easy4j.opencli.contract; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import io.github.easy4j.opencli.OpenCliClient; |
| 6 | +import io.github.easy4j.opencli.OpenCliProperties; |
| 7 | +import io.github.easy4j.opencli.core.OpenCliResult; |
| 8 | +import java.io.File; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.Objects; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.condition.EnabledOnOs; |
| 15 | +import org.junit.jupiter.api.condition.OS; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +class OpenCliInstalledLauncherIT { |
| 22 | + |
| 23 | + @Test |
| 24 | + void nativeNodeLaunchReachesInstalledOpenCli() throws Exception { |
| 25 | + OpenCliProperties properties = new OpenCliProperties(); |
| 26 | + properties.setExecutable(requireFixture("OPENCLI_INSTALLED_NODE").toString()); |
| 27 | + properties.setLeadingArguments(Collections.singletonList(requireFixture("OPENCLI_INSTALLED_ENTRY").toString())); |
| 28 | + properties.setCommandTimeoutMillis(30_000L); |
| 29 | + |
| 30 | + try (OpenCliClient client = new OpenCliClient(properties)) { |
| 31 | + OpenCliResult result = client.meta().list("json"); |
| 32 | + assertTrue(result.isSuccess()); |
| 33 | + ObjectMapper mapper = new ObjectMapper(); |
| 34 | + JsonNode actual = mapper.readTree(result.getStdout()); |
| 35 | + JsonNode expected = mapper.readTree(requireFixture("OPENCLI_INSTALLED_REGISTRY").toFile()); |
| 36 | + assertTrue(actual.isArray()); |
| 37 | + assertTrue(actual.size() > 0); |
| 38 | + assertEquals(expected, actual); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + @EnabledOnOs(OS.WINDOWS) |
| 44 | + void installedNpmBatchShimIsRejectedExplicitly() { |
| 45 | + File shim = requireFixture("OPENCLI_INSTALLED_BATCH_SHIM").toFile(); |
| 46 | + assertTrue(shim.isFile(), "npm must provide the Windows batch shim"); |
| 47 | + OpenCliProperties properties = new OpenCliProperties(); |
| 48 | + properties.setExecutable(shim.getAbsolutePath()); |
| 49 | + try (OpenCliClient client = new OpenCliClient(properties)) { |
| 50 | + UnsupportedOperationException error = assertThrows(UnsupportedOperationException.class, |
| 51 | + () -> client.meta().list("json")); |
| 52 | + assertTrue(error.getMessage().contains("native node executable")); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static Path requireFixture(String name) { |
| 57 | + return Paths.get(Objects.requireNonNull(System.getenv(name), name + " fixture is required")); |
| 58 | + } |
| 59 | +} |
0 commit comments