Skip to content
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

Expose test selection property via build environment #329

Merged
merged 2 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,7 @@ class MicronautBuildCommonPlugin implements Plugin<Project> {
}
}

// Predictive test selection is enabled if:
// an environment variable is explicitly configured and set to true
// or a system property is explicitly configured and set to true
// or it's a local build
def testSelectionEnabled = project.providers.environmentVariable("PREDICTIVE_TEST_SELECTION")
.orElse(project.providers.systemProperty("predictiveTestSelection"))
.map {
if (it.trim()) {
Boolean.parseBoolean(it)
} else {
true
}
}
.orElse(micronautBuildExtension.environment.isNotGithubAction())

def testSelectionEnabled = micronautBuildExtension.environment.isTestSelectionEnabled()
project.dependencies {
testImplementation(testSelectionEnabled.map { enabled ->
if (enabled) {
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/io/micronaut/build/BuildEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

public class BuildEnvironment {

public static final String PREDICTIVE_TEST_SELECTION_ENV_VAR = "PREDICTIVE_TEST_SELECTION";
public static final String PREDICTIVE_TEST_SELECTION_SYSPROP = "predictiveTestSelection";
private final ProviderFactory providers;
private final Provider<Boolean> githubAction;
private final boolean isMigrationActive;

public BuildEnvironment(ProviderFactory providers) {
this.providers = providers;
this.githubAction = trueWhenEnvVarPresent( "GITHUB_ACTIONS");
this.githubAction = trueWhenEnvVarPresent("GITHUB_ACTIONS");
this.isMigrationActive = !providers.systemProperty("strictBuild")
.forUseAtConfigurationTime()
.isPresent();
Expand All @@ -47,6 +49,22 @@ public Provider<Boolean> trueWhenEnvVarPresent(String envVar) {
.orElse(false);
}

public Provider<Boolean> isTestSelectionEnabled() {
// Predictive test selection is enabled if:
// an environment variable is explicitly configured and set to true
// or a system property is explicitly configured and set to true
// or it's a local build
return providers.environmentVariable(PREDICTIVE_TEST_SELECTION_ENV_VAR)
.orElse(providers.systemProperty(PREDICTIVE_TEST_SELECTION_SYSPROP))
.map(it -> {
if (it.trim().length() > 0) {
return Boolean.parseBoolean(it);
}
return false;
})
.orElse(isNotGithubAction());
}

public void duringMigration(Runnable action) {
if (isMigrationActive) {
action.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@

import java.lang.reflect.InvocationTargetException;

import static io.micronaut.build.BuildEnvironment.PREDICTIVE_TEST_SELECTION_ENV_VAR;
import static io.micronaut.build.ProviderUtils.envOrSystemProperty;

public class MicronautGradleEnterprisePlugin implements Plugin<Settings> {
private static final String[] SAFE_TO_LOG_ENV_VARIABLES = new String[] {
PREDICTIVE_TEST_SELECTION_ENV_VAR
};

@Override
public void apply(Settings settings) {
PluginManager pluginManager = settings.getPluginManager();
Expand Down Expand Up @@ -89,6 +94,18 @@ private void configureGradleEnterprise(Settings settings,
});
}
});
BuildEnvironment buildEnvironment = new BuildEnvironment(providers);
if (Boolean.TRUE.equals(buildEnvironment.isTestSelectionEnabled().get())) {
ge.getBuildScan().tag("Predictive Test Selection");
}
captureSafeEnvironmentVariables(ge);
}
}

private void captureSafeEnvironmentVariables(GradleEnterpriseExtension ge) {
for (String variable : SAFE_TO_LOG_ENV_VARIABLES) {
String value = System.getenv(variable);
ge.getBuildScan().value("env." + variable, value == null ? "<undefined>" : value);
}
}

Expand Down