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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
18 changes: 17 additions & 1 deletion src/main/java/io/micronaut/build/BuildEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class BuildEnvironment {

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 +47,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")
.orElse(providers.systemProperty("predictiveTestSelection"))
.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 @@ -35,6 +35,10 @@
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"
melix marked this conversation as resolved.
Show resolved Hide resolved
};

@Override
public void apply(Settings settings) {
PluginManager pluginManager = settings.getPluginManager();
Expand Down Expand Up @@ -89,6 +93,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