Skip to content

Commit 6305e7e

Browse files
authored
Merge pull request #457 from jonesbusy/feature/bootstrap-it
Bootstrap integration tests
2 parents f476a50 + 64c99c2 commit 6305e7e

4 files changed

Lines changed: 212 additions & 0 deletions

File tree

plugin-modernizer-cli/pom-it.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.jenkins.plugin-modernizer</groupId>
6+
<artifactId>plugin-modernizer-it</artifactId>
7+
<version>${changelist}</version>
8+
<properties>
9+
<changelist>999999-SNAPSHOT</changelist>
10+
<maven.test.skip>true</maven.test.skip>
11+
<skipTests>true</skipTests> <!-- No test for this pom, only exec plugin -->
12+
<exec.executable>java</exec.executable>
13+
<exec.args>-jar target/jenkins-plugin-modernizer-${project.version}.jar ${test.cliArgs}</exec.args>
14+
<test.cliArgs>--version</test.cliArgs>
15+
</properties>
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.codehaus.mojo</groupId>
20+
<artifactId>exec-maven-plugin</artifactId>
21+
<version>3.5.0</version>
22+
<executions>
23+
<execution>
24+
<id>exec</id>
25+
<phase>verify</phase>
26+
<goals>
27+
<goal>exec</goal>
28+
</goals>
29+
<configuration>
30+
<executable>${exec.executable}</executable>
31+
<arguments>
32+
${exec.args}
33+
</arguments>
34+
</configuration>
35+
</execution>
36+
</executions>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>

plugin-modernizer-cli/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
</exclusion>
4040
</exclusions>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.apache.maven.shared</groupId>
44+
<artifactId>maven-invoker</artifactId>
45+
<version>3.3.0</version>
46+
</dependency>
4247
<dependency>
4348
<groupId>org.slf4j</groupId>
4449
<artifactId>jcl-over-slf4j</artifactId>
@@ -83,12 +88,37 @@
8388
<groupId>org.apache.maven.plugins</groupId>
8489
<artifactId>maven-surefire-plugin</artifactId>
8590
<configuration>
91+
<excludes>
92+
<exclude>**/*ITCase.java</exclude>
93+
</excludes>
8694
<environmentVariables>
8795
<MAVEN_HOME>${project.build.directory}/apache-maven-${maven.version}</MAVEN_HOME>
8896
<M2_HOME>${project.build.directory}/apache-maven-${maven.version}</M2_HOME>
8997
</environmentVariables>
9098
</configuration>
9199
</plugin>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-failsafe-plugin</artifactId>
103+
<executions>
104+
<execution>
105+
<id>integration-tests</id>
106+
<goals>
107+
<goal>integration-test</goal>
108+
</goals>
109+
<phase>verify</phase>
110+
<configuration>
111+
<useModulePath>false</useModulePath>
112+
<includes>
113+
<include>**/*ITCase.java</include>
114+
</includes>
115+
<environmentVariables>
116+
<MAVEN_HOME>${project.build.directory}/apache-maven-${maven.version}</MAVEN_HOME>
117+
</environmentVariables>
118+
</configuration>
119+
</execution>
120+
</executions>
121+
</plugin>
92122
<plugin>
93123
<groupId>org.apache.maven.plugins</groupId>
94124
<artifactId>maven-dependency-plugin</artifactId>
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
<jakarta.inject.version>2.0.1</jakarta.inject.version>
7575
<jwt.version>0.12.6</jwt.version>
7676
<bouncycastle.version>1.79</bouncycastle.version>
77+
<maven.surefire.version>3.5.2</maven.surefire.version>
78+
<maven.failsafe.version>3.5.2</maven.failsafe.version>
7779
</properties>
7880

7981
<dependencyManagement>
@@ -307,6 +309,16 @@
307309
<build>
308310
<pluginManagement>
309311
<plugins>
312+
<plugin>
313+
<groupId>org.apache.maven.plugins</groupId>
314+
<artifactId>maven-surefire-plugin</artifactId>
315+
<version>${maven.surefire.version}</version>
316+
</plugin>
317+
<plugin>
318+
<groupId>org.apache.maven.plugins</groupId>
319+
<artifactId>maven-failsafe-plugin</artifactId>
320+
<version>${maven.failsafe.version}</version>
321+
</plugin>
310322
<plugin>
311323
<groupId>org.openrewrite.maven</groupId>
312324
<artifactId>rewrite-maven-plugin</artifactId>

0 commit comments

Comments
 (0)