-
Notifications
You must be signed in to change notification settings - Fork 79
Allow the test classes directory to be configured for the NativeTestMojo #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thegridman
wants to merge
3
commits into
graalvm:master
Choose a base branch
from
thegridman:test-dir
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
87de555
Allow the test classes directory to be configured for the NativeTestMojo
thegridman a28f722
Refactoring to be able to correctly obtain configuration from either …
thegridman 841966c
Clean up unused code paths due to the fact that a Maven plugin will a…
thegridman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ve-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeIntegrationTestMojo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* The Universal Permissive License (UPL), Version 1.0 | ||
* | ||
* Subject to the condition set forth below, permission is hereby granted to any | ||
* person obtaining a copy of this software, associated documentation and/or | ||
* data (collectively the "Software"), free of charge and under any and all | ||
* copyright rights in the Software, and any and all patent rights owned or | ||
* freely licensable by each licensor hereunder covering either (i) the | ||
* unmodified Software as contributed to or provided by such licensor, or (ii) | ||
* the Larger Works (as defined below), to deal in both | ||
* | ||
* (a) the Software, and | ||
* | ||
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if | ||
* one is included with the Software each a "Larger Work" to which the Software | ||
* is contributed by such licensors), | ||
* | ||
* without restriction, including without limitation the rights to copy, create | ||
* derivative works of, display, perform, and distribute the Software and make, | ||
* use, sell, offer for sale, import, export, have made, and have sold the | ||
* Software and the Larger Work(s), and to sublicense the foregoing rights on | ||
* either these or other terms. | ||
* | ||
* This license is subject to the following condition: | ||
* | ||
* The above copyright notice and either this complete permission notice or at a | ||
* minimum a reference to the UPL must be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package org.graalvm.buildtools.maven; | ||
|
||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.ResolutionScope; | ||
|
||
/** | ||
* This goal builds and runs native integration tests. | ||
* | ||
* @author Jonathan Knight | ||
*/ | ||
@Mojo(name = NativeIntegrationTestMojo.INTEGRATION_TEST_GOAL, | ||
defaultPhase = LifecyclePhase.INTEGRATION_TEST, threadSafe = true, | ||
requiresDependencyResolution = ResolutionScope.TEST, | ||
requiresDependencyCollection = ResolutionScope.TEST) | ||
public class NativeIntegrationTestMojo extends NativeTestMojo { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
import org.apache.maven.artifact.Artifact; | ||
import org.apache.maven.model.FileSet; | ||
import org.apache.maven.model.Plugin; | ||
import org.apache.maven.model.PluginExecution; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
|
@@ -88,21 +89,39 @@ | |
* | ||
* @author Sebastien Deleuze | ||
*/ | ||
@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST, threadSafe = true, | ||
@Mojo(name = NativeTestMojo.TEST_GOAL, defaultPhase = LifecyclePhase.TEST, threadSafe = true, | ||
requiresDependencyResolution = ResolutionScope.TEST, | ||
requiresDependencyCollection = ResolutionScope.TEST) | ||
public class NativeTestMojo extends AbstractNativeImageMojo { | ||
|
||
/** | ||
* The test goal for this plugin. | ||
*/ | ||
public static final String TEST_GOAL = "test"; | ||
|
||
/** | ||
* The integration-test goal for this plugin. | ||
*/ | ||
public static final String INTEGRATION_TEST_GOAL = "integration-test"; | ||
|
||
@Parameter(property = "skipTests", defaultValue = "false") | ||
private boolean skipTests; | ||
|
||
@Parameter(property = "skipNativeTests", defaultValue = "false") | ||
private boolean skipNativeTests; | ||
|
||
/** | ||
* The location of the test classes. | ||
* <p> | ||
* This field will be set at execution time from either the Surefire or | ||
* Failsafe plugin configurations. | ||
*/ | ||
private String testClassesDirectory; | ||
|
||
@Override | ||
protected void populateApplicationClasspath() throws MojoExecutionException { | ||
super.populateApplicationClasspath(); | ||
imageClasspath.add(Paths.get(project.getBuild().getTestOutputDirectory())); | ||
imageClasspath.add(Path.of(testClassesDirectory)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @melix are we handling this case in Gradle properly, i.e. the |
||
project.getBuild() | ||
.getTestResources() | ||
.stream() | ||
|
@@ -146,6 +165,9 @@ public void execute() throws MojoExecutionException { | |
logger.info("Skipping native-image tests (parameter 'skipTests' or 'skipNativeTests' is true)."); | ||
return; | ||
} | ||
|
||
configureEnvironment(); | ||
|
||
if (!hasTests()) { | ||
logger.info("Skipped native-image tests since there are no test classes."); | ||
return; | ||
|
@@ -159,7 +181,6 @@ public void execute() throws MojoExecutionException { | |
logger.info("Initializing project: " + project.getName()); | ||
logger.info("===================="); | ||
|
||
configureEnvironment(); | ||
buildArgs.add("--features=org.graalvm.junit.platform.JUnitPlatformFeature"); | ||
|
||
/* in version 5.12.0 JUnit added initialize-at-build-time properties files which we need to exclude */ | ||
|
@@ -168,8 +189,7 @@ public void execute() throws MojoExecutionException { | |
if (systemProperties == null) { | ||
systemProperties = new HashMap<>(); | ||
} | ||
systemProperties.put("junit.platform.listeners.uid.tracking.output.dir", | ||
NativeExtension.testIdsDirectory(outputDirectory.getAbsolutePath())); | ||
|
||
if (runtimeArgs == null) { | ||
runtimeArgs = new ArrayList<>(); | ||
} | ||
|
@@ -181,25 +201,112 @@ public void execute() throws MojoExecutionException { | |
runNativeTests(outputDirectory.toPath().resolve(NATIVE_TESTS_EXE)); | ||
} | ||
|
||
private void configureEnvironment() { | ||
List<Plugin> plugins = new ArrayList<>(); | ||
protected void configureEnvironment() { | ||
// set the default test classes location | ||
testClassesDirectory = project.getBuild().getTestOutputDirectory(); | ||
|
||
Plugin surefire = project.getPlugin("org.apache.maven.plugins:maven-surefire-plugin"); | ||
Plugin failsafe = project.getPlugin("org.apache.maven.plugins:maven-failsafe-plugin"); | ||
String currentGoal = mojoExecution.getGoal(); | ||
boolean testPluginProcessed = false; | ||
|
||
if (surefire != null) { | ||
plugins.add(surefire); | ||
testPluginProcessed = processTestPluginConfig(surefire, currentGoal); | ||
} | ||
|
||
Plugin failsafe = project.getPlugin("org.apache.maven.plugins:maven-failsafe-plugin"); | ||
if (failsafe != null) { | ||
plugins.add(failsafe); | ||
if (!testPluginProcessed && failsafe != null) { | ||
// Surefire was not configured with an execution for the current goal, so try Failsafe | ||
testPluginProcessed = processTestPluginConfig(failsafe, currentGoal); | ||
} | ||
|
||
if (!testPluginProcessed) { | ||
// neither Surefire nor Failsafe has an execution for the current goal, | ||
// so use the configuration for whichever plugin's default goal matches | ||
// the current goal | ||
if (mojoExecution.getGoal().equals(TEST_GOAL) && surefire != null) { | ||
// the current goal is "test" which is Surefire's default goal | ||
processTestPluginConfig(surefire, null); | ||
getLog().info("Using configuration from " + surefire.getArtifactId()); | ||
} else if (mojoExecution.getGoal().equals(INTEGRATION_TEST_GOAL) && failsafe != null) { | ||
thegridman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// the current goal is "integration-test" which is Failsafe's default goal | ||
processTestPluginConfig(failsafe, null); | ||
getLog().info("Using configuration from " + failsafe.getArtifactId()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Process the configuration from the Surefire or Failsafe plugins. | ||
* <p> | ||
* This method will check to see whether the plugin has an execution | ||
* matching the goa for this plugin. If it does the plugin's configuration | ||
thegridman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* and the configuration for the matching execution will be processed. | ||
* If the plugin has no executions, then just its global configuration | ||
* will be processed. | ||
* | ||
* @param plugin the Surefire or Failsafe plugin | ||
* @param currentGoal the current goal being executed | ||
*/ | ||
private boolean processTestPluginConfig(Plugin plugin, String currentGoal) | ||
{ | ||
List<PluginExecution> executions = plugin.getExecutions(); | ||
boolean found = false; | ||
if (!executions.isEmpty()) { | ||
for (PluginExecution execution : executions) { | ||
if (execution.getGoals().contains(currentGoal)) { | ||
processTestPlugin(plugin, execution); | ||
found = true; | ||
break; | ||
} | ||
} | ||
} | ||
return found; | ||
} | ||
|
||
/** | ||
* Process the configuration from the Surefire or Failsafe plugins. | ||
* | ||
* @param plugin the Surefire or Failsafe plugin configuration | ||
* @param execution the plugin execution to use for configuration | ||
*/ | ||
private void processTestPlugin(Plugin plugin, PluginExecution execution) { | ||
processTestPluginConfig(plugin.getConfiguration()); | ||
if (execution != null) { | ||
processTestPluginConfig(execution.getConfiguration()); | ||
} | ||
systemProperties.put("junit.platform.listeners.uid.tracking.output.dir", | ||
NativeExtension.testIdsDirectory(outputDirectory.getAbsolutePath(), plugin.getArtifactId())); | ||
getLog().info("Using configuration from " + plugin.getArtifactId() | ||
+ (execution != null ? ", execution id " + execution.getId() : "")); | ||
} | ||
|
||
/** | ||
* Process the configuration from the Surefire or Failsafe plugins. | ||
* | ||
* @param configuration the Surefire or Failsafe plugin configuration | ||
*/ | ||
private void processTestPluginConfig(Object configuration) { | ||
if (configuration instanceof Xpp3Dom) { | ||
Xpp3Dom dom = (Xpp3Dom) configuration; | ||
applyPluginProperties(dom.getChild("environmentVariables"), environment); | ||
applyPluginProperties(dom.getChild("systemPropertyVariables"), systemProperties); | ||
setTestClassesDirectory(dom.getChild("testClassesDirectory")); | ||
} | ||
} | ||
|
||
for (Plugin plugin : plugins) { | ||
Object configuration = plugin.getConfiguration(); | ||
if (configuration instanceof Xpp3Dom) { | ||
Xpp3Dom dom = (Xpp3Dom) configuration; | ||
applyPluginProperties(dom.getChild("environmentVariables"), environment); | ||
applyPluginProperties(dom.getChild("systemPropertyVariables"), systemProperties); | ||
/** | ||
* Set the test classes directory from the testClassesDirectory configuration {@link Xpp3Dom}. | ||
* <p> | ||
* The {@link #testClassesDirectory} field is only set if the dom is not {@code null} | ||
* and it contains is non-blank value. | ||
* | ||
* @param dom the testClassesDirectory configuration {@link Xpp3Dom}. | ||
*/ | ||
protected void setTestClassesDirectory(Xpp3Dom dom) { | ||
if (dom != null) { | ||
String value = dom.getValue(); | ||
if (value != null && !value.isBlank()) { | ||
testClassesDirectory = value; | ||
} | ||
} | ||
} | ||
|
@@ -217,7 +324,7 @@ private void applyPluginProperties(Xpp3Dom pluginProperty, Map<String, String> v | |
} | ||
|
||
private boolean hasTests() { | ||
Path testOutputPath = Paths.get(project.getBuild().getTestOutputDirectory()); | ||
Path testOutputPath = Path.of(testClassesDirectory); | ||
if (Files.exists(testOutputPath) && Files.isDirectory(testOutputPath)) { | ||
try (Stream<Path> testClasses = Files.walk(testOutputPath)) { | ||
return testClasses.anyMatch(p -> p.getFileName().toString().endsWith(".class")); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dnestoro do we need to document this? Also, could we execute only native integration tests (once the JUnit issues are fixed)? What would be the command for that?