Skip to content

Commit

Permalink
Merge pull request #457 from jonesbusy/feature/bootstrap-it
Browse files Browse the repository at this point in the history
Bootstrap integration tests
  • Loading branch information
jonesbusy authored Dec 14, 2024
2 parents f476a50 + 64c99c2 commit 6305e7e
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
40 changes: 40 additions & 0 deletions plugin-modernizer-cli/pom-it.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.jenkins.plugin-modernizer</groupId>
<artifactId>plugin-modernizer-it</artifactId>
<version>${changelist}</version>
<properties>
<changelist>999999-SNAPSHOT</changelist>
<maven.test.skip>true</maven.test.skip>
<skipTests>true</skipTests> <!-- No test for this pom, only exec plugin -->
<exec.executable>java</exec.executable>
<exec.args>-jar target/jenkins-plugin-modernizer-${project.version}.jar ${test.cliArgs}</exec.args>
<test.cliArgs>--version</test.cliArgs>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>exec</id>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${exec.executable}</executable>
<arguments>
${exec.args}
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
30 changes: 30 additions & 0 deletions plugin-modernizer-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
Expand Down Expand Up @@ -83,12 +88,37 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*ITCase.java</exclude>
</excludes>
<environmentVariables>
<MAVEN_HOME>${project.build.directory}/apache-maven-${maven.version}</MAVEN_HOME>
<M2_HOME>${project.build.directory}/apache-maven-${maven.version}</M2_HOME>
</environmentVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
</goals>
<phase>verify</phase>
<configuration>
<useModulePath>false</useModulePath>
<includes>
<include>**/*ITCase.java</include>
</includes>
<environmentVariables>
<MAVEN_HOME>${project.build.directory}/apache-maven-${maven.version}</MAVEN_HOME>
</environmentVariables>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package io.jenkins.tools.pluginmodernizer.cli;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Properties;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Integration test for the command line interface
*/
public class CommandLineITCase {

/**
* Logger
*/
private static final Logger LOG = LoggerFactory.getLogger(CommandLineITCase.class);

@TempDir
private Path outputPath;

@Test
public void testVersion() throws Exception {
LOG.info("Running testVersion");
Invoker invoker = buildInvoker();
InvocationRequest request = buildRequest("--version");
InvocationResult result = invoker.execute(request);
assertAll(
() -> assertEquals(0, result.getExitCode()),
() -> assertTrue(Files.readAllLines(outputPath.resolve("stdout.txt")).stream()
.anyMatch(line -> line.matches("plugin modernizer (.*) (.*)"))));
}

@Test
public void testListRecipes() throws Exception {
LOG.info("Running testListRecipes");
Invoker invoker = buildInvoker();
InvocationRequest request = buildRequest("recipes");
InvocationResult result = invoker.execute(request);
assertAll(
() -> assertEquals(0, result.getExitCode()),
() -> assertTrue(Files.readAllLines(outputPath.resolve("stdout.txt")).stream()
.anyMatch(
line -> line.matches(".*FetchMetadata - Extracts metadata from a Jenkins plugin.*"))));
}

/**
* Build the invoker
* @return the invoker
*/
private Invoker buildInvoker() {
Path javaHome = Path.of(System.getenv("JAVA_HOME"));
assertNotNull(javaHome, "JAVA_HOME is not set");
Path mavenHome = Path.of(System.getenv("MAVEN_HOME"));
assertNotNull(mavenHome, "MAVEN_HOME is not set");
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(Path.of(System.getenv("MAVEN_HOME")).toFile());
invoker.setMavenHome(mavenHome.toFile());
return invoker;
}

/**
* Build the request
* @return the request
*/
private InvocationRequest buildRequest(String args) {
Path javaHome = Path.of(System.getenv("JAVA_HOME"));
assertNotNull(javaHome, "JAVA_HOME is not set");

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File("pom-it.xml"));
request.addArg("verify");

// Add properties
Properties properties = new Properties();
String changeList = System.getProperty("set.changelist");
if (changeList != null) {
properties.put("set.changelist", "true");
}
properties.put("exec.executable", javaHome.resolve("bin/java").toString());
properties.put("test.cliArgs", args);
request.setProperties(properties);

// Other options
request.setBatchMode(true);
request.setNoTransferProgress(true);
request.setJavaHome(javaHome.toFile());
request.setOutputHandler(line -> {
try {
Files.write(
outputPath.resolve("stdout.txt"),
(line + System.lineSeparator()).getBytes(),
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
} catch (Exception e) {
LOG.error("Error writing to stdout", e);
throw new RuntimeException(e);
}
LOG.info(line);
});
request.setErrorHandler(line -> {
try {
Files.write(
outputPath.resolve("stderr.txt"),
(line + System.lineSeparator()).getBytes(),
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
} catch (Exception e) {
LOG.error("Error writing to stderr", e);
throw new RuntimeException(e);
}
LOG.error(line);
});
return request;
}
}
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<jakarta.inject.version>2.0.1</jakarta.inject.version>
<jwt.version>0.12.6</jwt.version>
<bouncycastle.version>1.79</bouncycastle.version>
<maven.surefire.version>3.5.2</maven.surefire.version>
<maven.failsafe.version>3.5.2</maven.failsafe.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -307,6 +309,16 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.version}</version>
</plugin>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
Expand Down

0 comments on commit 6305e7e

Please sign in to comment.