|
| 1 | +package io.jenkins.tools.pluginmodernizer.cli; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.StandardOpenOption; |
| 12 | +import java.util.Properties; |
| 13 | +import org.apache.maven.shared.invoker.DefaultInvocationRequest; |
| 14 | +import org.apache.maven.shared.invoker.DefaultInvoker; |
| 15 | +import org.apache.maven.shared.invoker.InvocationRequest; |
| 16 | +import org.apache.maven.shared.invoker.InvocationResult; |
| 17 | +import org.apache.maven.shared.invoker.Invoker; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.io.TempDir; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +/** |
| 24 | + * Integration test for the command line interface |
| 25 | + */ |
| 26 | +public class CommandLineITCase { |
| 27 | + |
| 28 | + /** |
| 29 | + * Logger |
| 30 | + */ |
| 31 | + private static final Logger LOG = LoggerFactory.getLogger(CommandLineITCase.class); |
| 32 | + |
| 33 | + @TempDir |
| 34 | + private Path outputPath; |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testVersion() throws Exception { |
| 38 | + LOG.info("Running testVersion"); |
| 39 | + Invoker invoker = buildInvoker(); |
| 40 | + InvocationRequest request = buildRequest("--version"); |
| 41 | + InvocationResult result = invoker.execute(request); |
| 42 | + assertAll( |
| 43 | + () -> assertEquals(0, result.getExitCode()), |
| 44 | + () -> assertTrue(Files.readAllLines(outputPath.resolve("stdout.txt")).stream() |
| 45 | + .anyMatch(line -> line.matches("plugin modernizer (.*) (.*)")))); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testListRecipes() throws Exception { |
| 50 | + LOG.info("Running testListRecipes"); |
| 51 | + Invoker invoker = buildInvoker(); |
| 52 | + InvocationRequest request = buildRequest("recipes"); |
| 53 | + InvocationResult result = invoker.execute(request); |
| 54 | + assertAll( |
| 55 | + () -> assertEquals(0, result.getExitCode()), |
| 56 | + () -> assertTrue(Files.readAllLines(outputPath.resolve("stdout.txt")).stream() |
| 57 | + .anyMatch( |
| 58 | + line -> line.matches(".*FetchMetadata - Extracts metadata from a Jenkins plugin.*")))); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Build the invoker |
| 63 | + * @return the invoker |
| 64 | + */ |
| 65 | + private Invoker buildInvoker() { |
| 66 | + Path javaHome = Path.of(System.getenv("JAVA_HOME")); |
| 67 | + assertNotNull(javaHome, "JAVA_HOME is not set"); |
| 68 | + Path mavenHome = Path.of(System.getenv("MAVEN_HOME")); |
| 69 | + assertNotNull(mavenHome, "MAVEN_HOME is not set"); |
| 70 | + Invoker invoker = new DefaultInvoker(); |
| 71 | + invoker.setMavenHome(Path.of(System.getenv("MAVEN_HOME")).toFile()); |
| 72 | + invoker.setMavenHome(mavenHome.toFile()); |
| 73 | + return invoker; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Build the request |
| 78 | + * @return the request |
| 79 | + */ |
| 80 | + private InvocationRequest buildRequest(String args) { |
| 81 | + Path javaHome = Path.of(System.getenv("JAVA_HOME")); |
| 82 | + assertNotNull(javaHome, "JAVA_HOME is not set"); |
| 83 | + |
| 84 | + InvocationRequest request = new DefaultInvocationRequest(); |
| 85 | + request.setPomFile(new File("pom-it.xml")); |
| 86 | + request.addArg("verify"); |
| 87 | + |
| 88 | + // Add properties |
| 89 | + Properties properties = new Properties(); |
| 90 | + String changeList = System.getProperty("set.changelist"); |
| 91 | + if (changeList != null) { |
| 92 | + properties.put("set.changelist", "true"); |
| 93 | + } |
| 94 | + properties.put("exec.executable", javaHome.resolve("bin/java").toString()); |
| 95 | + properties.put("test.cliArgs", args); |
| 96 | + request.setProperties(properties); |
| 97 | + |
| 98 | + // Other options |
| 99 | + request.setBatchMode(true); |
| 100 | + request.setNoTransferProgress(true); |
| 101 | + request.setJavaHome(javaHome.toFile()); |
| 102 | + request.setOutputHandler(line -> { |
| 103 | + try { |
| 104 | + Files.write( |
| 105 | + outputPath.resolve("stdout.txt"), |
| 106 | + (line + System.lineSeparator()).getBytes(), |
| 107 | + StandardOpenOption.CREATE, |
| 108 | + StandardOpenOption.APPEND); |
| 109 | + } catch (Exception e) { |
| 110 | + LOG.error("Error writing to stdout", e); |
| 111 | + throw new RuntimeException(e); |
| 112 | + } |
| 113 | + LOG.info(line); |
| 114 | + }); |
| 115 | + request.setErrorHandler(line -> { |
| 116 | + try { |
| 117 | + Files.write( |
| 118 | + outputPath.resolve("stderr.txt"), |
| 119 | + (line + System.lineSeparator()).getBytes(), |
| 120 | + StandardOpenOption.CREATE, |
| 121 | + StandardOpenOption.APPEND); |
| 122 | + } catch (Exception e) { |
| 123 | + LOG.error("Error writing to stderr", e); |
| 124 | + throw new RuntimeException(e); |
| 125 | + } |
| 126 | + LOG.error(line); |
| 127 | + }); |
| 128 | + return request; |
| 129 | + } |
| 130 | +} |
0 commit comments