Skip to content

Commit 14ac519

Browse files
authored
Merge pull request #11 from yrodiere/envvar2
Handle Surefire/Failsafe environment variables similarly to system properties - take 2
2 parents 51bea15 + 6de8efc commit 14ac519

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

src/main/java/org/hibernate/search/develocity/GoalMetadataProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99
package org.hibernate.search.develocity;
1010

11+
import java.util.function.Consumer;
12+
1113
import org.hibernate.search.develocity.util.MavenMojoExecutionConfig;
1214
import org.hibernate.search.develocity.util.MavenProperties;
1315

@@ -51,5 +53,10 @@ public MavenMojoExecutionConfig configuration() {
5153
public void buildScanDeduplicatedValue(String key, String value) {
5254
buildScanApi.executeOnce( key + value, ignored -> buildScanApi.value( key, value ) );
5355
}
56+
57+
public void nested(String propertyName, Consumer<? super Context> action) {
58+
metadataContext.nested( propertyName,
59+
nestedMetadataContext -> action.accept( new Context( buildScanApi, nestedMetadataContext ) ) );
60+
}
5461
}
5562
}

src/main/java/org/hibernate/search/develocity/plugins/SurefireConfiguredPlugin.java

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package org.hibernate.search.develocity.plugins;
22

33
import java.util.Map;
4+
import java.util.regex.Pattern;
45

56
import org.hibernate.search.develocity.GoalMetadataProvider;
67
import org.hibernate.search.develocity.SimpleConfiguredPlugin;
78
import org.hibernate.search.develocity.util.JavaVersions;
89

10+
import com.gradle.maven.extension.api.cache.MojoMetadataProvider;
11+
912
public class SurefireConfiguredPlugin extends SimpleConfiguredPlugin {
1013

14+
private static final String SUREFIRE_ENVIRONMENT_VARIABLES = "environmentVariables";
15+
16+
private static final Pattern TEST_INDEXES_PATTERN = Pattern.compile( "(^|/)test-indexes($|/)" );
17+
1118
@Override
1219
protected String getPluginName() {
1320
return "maven-surefire-plugin";
@@ -26,14 +33,56 @@ protected void configureTest(GoalMetadataProvider.Context context) {
2633
dependsOnConfigurableJavaExecutable( inputs, context, "jvm", isSkipped( context ),
2734
JavaVersions::forJavaExecutable );
2835
} );
36+
37+
configureEnvironmentVariables(context);
38+
}
39+
40+
// Develocity handles environment variables as a big blob by default,
41+
// which won't work if some variables point to absolute paths.
42+
private void configureEnvironmentVariables(GoalMetadataProvider.Context context) {
43+
context.metadata().inputs( inputs -> {
44+
// First, override the property to disable handling of environment variables as a blob.
45+
// NOTE: ignoring with inputs.ignore( "environmentVariables" ) doesn't work for some reason:
46+
// we end up with the goal being marked as "not cacheable"
47+
// because "properties were declared both as input and ignored: [environmentVariables]"
48+
// NOTE: we get the same result with context.nested( "environmentVariables" ),
49+
// which is why we don't use that.
50+
inputs.property( SUREFIRE_ENVIRONMENT_VARIABLES, "IGNORED" );
51+
52+
// Then, try to mimic system properties handling.
53+
for ( Map.Entry<String, String> envVariable : context.configuration()
54+
.getStringMap( SUREFIRE_ENVIRONMENT_VARIABLES ).entrySet() ) {
55+
var key = envVariable.getKey();
56+
var keyForDevelocity = SUREFIRE_ENVIRONMENT_VARIABLES + "." + key;
57+
var value = envVariable.getValue();
58+
if ( value == null ) {
59+
value = "";
60+
}
61+
if ( value.startsWith( context.metadata().getSession().getExecutionRootDirectory() ) ) {
62+
if ( TEST_INDEXES_PATTERN.matcher( "test-indexes" ).find() ) {
63+
// Lucene indexes used in tests -- we don't care about these.
64+
inputs.ignore( keyForDevelocity );
65+
}
66+
else {
67+
inputs.fileSet( keyForDevelocity, value, fileSet -> {
68+
fileSet.normalizationStrategy(
69+
MojoMetadataProvider.Context.FileSet.NormalizationStrategy.RELATIVE_PATH );
70+
} );
71+
}
72+
}
73+
else {
74+
inputs.property( keyForDevelocity, value );
75+
}
76+
}
77+
} );
2978
}
3079

3180
protected boolean isSkipped(GoalMetadataProvider.Context context) {
3281
return context.configuration().getBoolean( "skip" )
33-
|| context.properties().getBoolean( "maven.test.skip" )
34-
|| context.configuration().getBoolean( "skipTests" )
35-
|| context.properties().getBoolean( "skipTests" )
36-
|| context.configuration().getBoolean( "skipExec" )
37-
|| context.properties().getBoolean( "maven.test.skip.exec" );
82+
|| context.properties().getBoolean( "maven.test.skip" )
83+
|| context.configuration().getBoolean( "skipTests" )
84+
|| context.properties().getBoolean( "skipTests" )
85+
|| context.configuration().getBoolean( "skipExec" )
86+
|| context.properties().getBoolean( "maven.test.skip.exec" );
3887
}
3988
}

src/main/java/org/hibernate/search/develocity/util/MavenMojoExecutionConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
package org.hibernate.search.develocity.util;
1010

1111
import java.util.ArrayList;
12+
import java.util.LinkedHashMap;
1213
import java.util.List;
14+
import java.util.Map;
1315

1416
import org.hibernate.search.develocity.Log;
1517

@@ -58,6 +60,19 @@ public List<String> getStringList(String key) {
5860
return children;
5961
}
6062

63+
public Map<String, String> getStringMap(String key) {
64+
var configElement = mojoExecution.getConfiguration()
65+
.getChild( key );
66+
if ( configElement == null ) {
67+
return Map.of();
68+
}
69+
Map<String, String> result = new LinkedHashMap<>();
70+
for ( Xpp3Dom configElementChild : configElement.getChildren() ) {
71+
result.put( configElementChild.getName(), configElementChild.getValue() );
72+
}
73+
return result;
74+
}
75+
6176
public String getFailsafeSystemProperty(String key) {
6277
var systemPropertyVariables = mojoExecution.getConfiguration()
6378
.getChild( "systemPropertyVariables" );

0 commit comments

Comments
 (0)