Skip to content
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 108 additions & 18 deletions src/main/java/org/jenkins/tools/test/PluginCompatTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.commons.io.FileUtils;
import org.jenkins.tools.test.exception.PluginCompatibilityTesterException;
import org.jenkins.tools.test.exception.PluginSourcesUnavailableException;
import org.jenkins.tools.test.gradle.ExternalGradleRunner;
import org.jenkins.tools.test.maven.ExpressionEvaluator;
import org.jenkins.tools.test.maven.ExternalMavenRunner;
import org.jenkins.tools.test.model.PluginCompatTesterConfig;
Expand All @@ -57,6 +58,8 @@
import org.jenkins.tools.test.model.hook.PluginCompatTesterHooks;
import org.jenkins.tools.test.model.plugin_metadata.LocalCheckoutPluginMetadataExtractor;
import org.jenkins.tools.test.model.plugin_metadata.Plugin;
import org.jenkins.tools.test.util.BuildSystem;
import org.jenkins.tools.test.util.BuildSystemUtils;
import org.jenkins.tools.test.util.ServiceHelper;
import org.jenkins.tools.test.util.StreamGobbler;
import org.jenkins.tools.test.util.WarExtractor;
Expand All @@ -78,10 +81,12 @@

private final PluginCompatTesterConfig config;
private final ExternalMavenRunner runner;
private final ExternalGradleRunner gradleRunner;

public PluginCompatTester(PluginCompatTesterConfig config) {
this.config = config;
runner = new ExternalMavenRunner(config);
gradleRunner = new ExternalGradleRunner(config);
}

@SuppressFBWarnings(
Expand Down Expand Up @@ -156,11 +161,15 @@
continue;
}
}

BuildSystem buildSystem = BuildSystemUtils.detectBuildSystem(cloneDir);
LOGGER.log(Level.INFO, "Detected build system: {0} for repository {1}", new Object[] {buildSystem, gitUrl});

if (!config.isCompileOnly()) {
// For each of the plugin metadata entries, go test the plugin
for (Plugin plugin : entry.getValue()) {
try {
testPluginAgainst(coreVersion, plugin, cloneDir, pcth);
testPluginAgainst(coreVersion, plugin, cloneDir, pcth, buildSystem);
} catch (PluginCompatibilityTesterException e) {
lastException = throwOrAddSuppressed(lastException, e, config.isFailFast());
LOGGER.log(
Expand All @@ -173,7 +182,7 @@
}
} else {
try {
testCompilationAgainst(coreVersion, gitUrl, cloneDir);
testCompilationAgainst(coreVersion, gitUrl, cloneDir, buildSystem);

Check warning on line 185 in src/main/java/org/jenkins/tools/test/PluginCompatTester.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 185 is not covered by tests
} catch (PluginCompatibilityTesterException e) {
lastException = throwOrAddSuppressed(lastException, e, config.isFailFast());
LOGGER.log(
Expand Down Expand Up @@ -219,19 +228,24 @@
}
}

private void testPluginAgainst(String coreVersion, Plugin plugin, File cloneLocation, PluginCompatTesterHooks pcth)
private void testPluginAgainst(
String coreVersion,
Plugin plugin,
File cloneLocation,
PluginCompatTesterHooks pcth,
BuildSystem buildSystem)
throws PluginCompatibilityTesterException {
LOGGER.log(
Level.INFO,
"\n\n\n\n\n\n"
+ "#############################################\n"
+ "#############################################\n"
+ "##\n"
+ "## Starting to test {0} {1} against core version {2}\n"
+ "## Starting to test {0} {1} against core version {2} using {3}\n"
+ "##\n"
+ "#############################################\n"
+ "#############################################\n\n\n\n\n",
new Object[] {plugin.getName(), plugin.getVersion(), coreVersion});
new Object[] {plugin.getName(), plugin.getVersion(), coreVersion, buildSystem.name()});

File buildLogFile = createBuildLogFile(config.getWorkingDir(), plugin, coreVersion);

Expand All @@ -240,6 +254,16 @@
new BeforeCompilationContext(coreVersion, plugin, config, cloneLocation);
pcth.runBeforeCompilation(beforeCompile);

if (buildSystem == BuildSystem.GRADLE) {

Check warning on line 257 in src/main/java/org/jenkins/tools/test/PluginCompatTester.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 257 is only partially covered, one branch is missing
testGradlePluginAgainst(coreVersion, plugin, cloneLocation, pcth, buildLogFile);

Check warning on line 258 in src/main/java/org/jenkins/tools/test/PluginCompatTester.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 258 is not covered by tests
} else {
testMavenPluginAgainst(coreVersion, plugin, cloneLocation, pcth, buildLogFile);
}
}

private void testMavenPluginAgainst(
String coreVersion, Plugin plugin, File cloneLocation, PluginCompatTesterHooks pcth, File buildLogFile)
throws PluginCompatibilityTesterException {
// First build against the original POM. This defends against source incompatibilities
// (which we do not care about for this purpose); and ensures that we are testing a
// plugin binary as close as possible to what was actually released. We also skip
Expand Down Expand Up @@ -308,64 +332,129 @@
args.toArray(new String[0]));
}

private void testCompilationAgainst(String coreVersion, String gitUrl, File cloneLocation)
private void testGradlePluginAgainst(
String coreVersion, Plugin plugin, File cloneLocation, PluginCompatTesterHooks pcth, File buildLogFile)
throws PluginCompatibilityTesterException {

Map<String, String> properties = new LinkedHashMap<>();

// Initial Compile
gradleRunner.run(properties, cloneLocation, plugin.getModule(), buildLogFile);

List<String> tasks = new ArrayList<>();

if (!config.getGradleTasks().isEmpty()) {
tasks.addAll(config.getGradleTasks());
} else {
tasks.add("test");
tasks.add("assemble");
}

BeforeExecutionContext forExecutionHooks =
new BeforeExecutionContext(coreVersion, plugin, config, cloneLocation, tasks);
pcth.runBeforeExecution(forExecutionHooks);

properties = new LinkedHashMap<>(config.getGradleSystemProperties());
properties.put("jenkinsVersion", coreVersion);
properties.put("jenkins.version", coreVersion);

gradleRunner.run(
Collections.unmodifiableMap(properties),
cloneLocation,
plugin.getModule(),
buildLogFile,
tasks.toArray(new String[0]));
}

private void testCompilationAgainst(String coreVersion, String gitUrl, File cloneLocation, BuildSystem buildSystem)
throws PluginCompatibilityTesterException {
LOGGER.log(
Level.INFO,
"\n\n\n\n\n\n"
+ "#############################################\n"
+ "#############################################\n"
+ "##\n"
+ "## Compiling {0} against core version {1}\n"
+ "## Compiling {0} against core version {1} using {2}\n"
+ "##\n"
+ "#############################################\n"
+ "#############################################\n\n\n\n\n",
new Object[] {getRepoNameFromGitUrl(gitUrl), coreVersion});
new Object[] {getRepoNameFromGitUrl(gitUrl), coreVersion, buildSystem.name()});

File buildLogFile = createBuildLogFile(config.getWorkingDir(), gitUrl, coreVersion);

if (buildSystem == BuildSystem.GRADLE) {
testGradleCompilationAgainst(coreVersion, cloneLocation, buildLogFile);
} else {
testMavenCompilationAgainst(coreVersion, cloneLocation, buildLogFile);
}
}

private void testMavenCompilationAgainst(String coreVersion, File cloneLocation, File buildLogFile)
throws PluginCompatibilityTesterException {
Map<String, String> properties = new LinkedHashMap<>(config.getMavenProperties());
properties.put("jenkins.version", coreVersion);
properties.put("checkstyle.skip", "true");
properties.put("enforcer.skip", "true");
properties.put("invoker.skip", "true");
properties.put("maven.javadoc.skip", "true");
properties.put("maven.site.skip", "true");
properties.put("skip.bower", "true");
properties.put("skip.bun", "true");
properties.put("skip.corepack", "true");
properties.put("skip.ember", "true");
properties.put("skip.grunt", "true");
properties.put("skip.gulp", "true");
properties.put("skip.installbun", "true");
properties.put("skip.installnodecorepack", "true");
properties.put("skip.installnodenpm", "true");
properties.put("skip.installnodepnpm", "true");
properties.put("skip.installyarn", "true");
properties.put("skip.jspm", "true");
properties.put("skip.karma", "true");
properties.put("skip.npm", "true");
properties.put("skip.npx", "true");
properties.put("skip.pnpm", "true");
properties.put("skip.webpack", "true");
properties.put("skip.yarn", "true");
properties.put("skipTests", "true");
properties.put("spotbugs.skip", "true");
properties.put("spotless.check.skip", "true");
properties.put("tidy.skip", "true");

List<String> args = new ArrayList<>();
args.add("clean");
args.add("verify");

runner.run(
Collections.unmodifiableMap(properties),
cloneLocation,
null,
buildLogFile,
args.toArray(new String[0]));
}

private void testGradleCompilationAgainst(String coreVersion, File cloneLocation, File buildLogFile)
throws PluginCompatibilityTesterException {
Map<String, String> properties = new LinkedHashMap<>(config.getGradleSystemProperties());

properties.put("jenkins.version", coreVersion);
properties.put("jenkinsVersion", coreVersion);

properties.putIfAbsent("quality.enabled", "false");

List<String> tasks = new ArrayList<>();
tasks.add("clean");
tasks.add("classes");
tasks.add("jar");

gradleRunner.run(
Collections.unmodifiableMap(properties),
cloneLocation,
null,
buildLogFile,
tasks.toArray(new String[0]));
}

Check warning on line 456 in src/main/java/org/jenkins/tools/test/PluginCompatTester.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 339-456 are not covered by tests

private static void cloneFromScm(
String url, String fallbackGitHubOrganization, String scmTag, File checkoutDirectory)
throws PluginSourcesUnavailableException {
Expand Down Expand Up @@ -402,10 +491,10 @@
* <li><code>git checkout FETCH_HEAD</code>
* </ul>
*
* @param gitUrl The git native URL, see the <a
* href="https://git-scm.com/docs/git-clone#_git_urls">git documentation</a> for the
* supported syntax
* @param scmTag the tag or sha1 hash to clone
* @param gitUrl The git native URL, see the <a
* href="https://git-scm.com/docs/git-clone#_git_urls">git documentation</a> for the
* supported syntax
* @param scmTag the tag or sha1 hash to clone
* @param checkoutDirectory the directory in which to clone the Git repository
* @throws IOException if an error occurs
*/
Expand Down Expand Up @@ -475,13 +564,13 @@
* with {@code current} added (if non-null) as a suppressed exception.
*
* @param <T>
* @param current the PluginCompatibilityTesterException if any
* @param caught the newly caught exception
* @param current the PluginCompatibilityTesterException if any
* @param caught the newly caught exception
* @param throwException {@code true} if we should immediately rethrow {@code caught}, {@code
* false} indicating we should return {@caught}.
* false} indicating we should return {@caught}.
* @return {@code caught}
* @throws PluginCompatibilityTesterException if {@code throwException == true} then {@caught}
* is thrown.
* is thrown.
*/
private static <T extends PluginCompatibilityTesterException> T throwOrAddSuppressed(
@CheckForNull PluginCompatibilityTesterException current, T caught, boolean throwException) throws T {
Expand All @@ -496,9 +585,10 @@

/**
* Runs the given command, waiting until it has completed before returning.
* @param directory the directory to run the command in.
*
* @param directory the directory to run the command in.
* @param commandAndArgs the command and arguments to run.
* @throws IOException if the process could not be started.
* @throws IOException if the process could not be started.
* @throws PluginSourcesUnavailableException if the command failed (either it was interrupted or exited with a non zero status.
*/
@SuppressFBWarnings(value = "COMMAND_INJECTION", justification = "intended behaviour")
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/jenkins/tools/test/PluginCompatTesterCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,47 @@
"Comma-separated list of arguments to pass to Maven (like -Pxxx; not to be confused with Java arguments or Maven properties). These arguments will be passed to Maven both during compilation and when running tests.")
private List<String> mavenArgs;

@CheckForNull
@CommandLine.Option(
names = "--gradle",
description =
"The path to the Gradle executable. If not specified, the system will use the Gradle available on the PATH by default.",
converter = ExistingFileTypeConverter.class)
private File externalGradle;

@CheckForNull
@CommandLine.Option(
names = "--gradle-properties",
description = "Path to gradle.properties file to use when executing Gradle.",
converter = ExistingFileTypeConverter.class)
private File gradleProperties;

@CheckForNull
@CommandLine.Option(
names = {"-P", "--gradle-property"},
description =
"Define a system property to be passed to Gradle when running tests (using -D flag). These will be passed to Gradle both during compilation and when running tests.")
private Map<String, String> gradleSystemProperties;

@CheckForNull
@CommandLine.Option(
names = "--gradle-args",
split = ",",
arity = "1",
paramLabel = "arg",
description =
"Comma-separated list of arguments to pass to Gradle (like --parallel, --build-cache). These arguments will be passed to Gradle both during compilation and when running tests.")
private List<String> gradleArgs;

@CheckForNull
@CommandLine.Option(
names = "--gradle-tasks",
split = ",",
arity = "1",
paramLabel = "task",
description = "Comma-separated list of Gradle tasks to execute for testing Jenkins plugins.")
private List<String> gradleTasks;

@CheckForNull
@CommandLine.Option(
names = "--external-hooks-jars",
Expand Down Expand Up @@ -198,6 +239,22 @@
if (mavenArgs != null) {
config.setMavenArgs(mavenArgs);
}

config.setExternalGradle(externalGradle);
config.setGradleProperties(gradleProperties);

if (gradleSystemProperties != null) {
config.setGradleSystemProperties(gradleSystemProperties);
}

if (gradleArgs != null) {
config.setGradleArgs(gradleArgs);
}

if (gradleTasks != null) {
config.setGradleTasks(gradleTasks);

Check warning on line 255 in src/main/java/org/jenkins/tools/test/PluginCompatTesterCli.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 243-255 are not covered by tests
}

if (externalHooksJars != null) {
config.setExternalHooksJars(externalHooksJars);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jenkins.tools.test.exception;

public class GradleExecutionException extends PluginCompatibilityTesterException {

private static final long serialVersionUID = 1L;

public GradleExecutionException(String message) {
super(message);
}

public GradleExecutionException(String message, Throwable cause) {
super(message, cause);
}

public GradleExecutionException(Throwable cause) {
super(cause);
}

Check warning on line 17 in src/main/java/org/jenkins/tools/test/exception/GradleExecutionException.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 8-17 are not covered by tests
}
Loading
Loading