Skip to content

Commit

Permalink
Add easier configuration cache test support in build logic tests (#88047
Browse files Browse the repository at this point in the history
)

This is intended to help us getting closer to #57918 by implicitly
testing our build logic configuration-cache support. Plugin and Task
tests can be marked as configuration cache compatible now and we will
always run then with configuration cache enabled.

By default, gradle will fail the build if configuration cache problems
have been detected during build execution. That should be in general
better then adding explicit tests for testing configuration cache
compatibility per Test class
  • Loading branch information
breskeby authored Jun 27, 2022
1 parent 1cbb135 commit 12525ad
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

package org.elasticsearch.gradle.fixtures

import org.elasticsearch.gradle.internal.conventions.precommit.PrecommitPlugin
import org.gradle.api.Plugin

abstract class AbstractGradlePrecommitPluginFuncTest extends AbstractJavaGradleFuncTest {
abstract class AbstractGradleInternalPluginFuncTest extends AbstractJavaGradleFuncTest {

abstract <T extends PrecommitPlugin> Class<T> getPluginClassUnderTest();
abstract <T extends Plugin> Class<T> getPluginClassUnderTest();

def setup() {
buildFile << """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@

package org.elasticsearch.gradle.internal.precommit

import org.elasticsearch.gradle.fixtures.AbstractGradlePrecommitPluginFuncTest
import org.elasticsearch.gradle.fixtures.AbstractGradleInternalPluginFuncTest
import org.elasticsearch.gradle.internal.conventions.precommit.LicenseHeadersPrecommitPlugin
import org.elasticsearch.gradle.internal.conventions.precommit.PrecommitPlugin
import org.gradle.testkit.runner.TaskOutcome

class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradlePrecommitPluginFuncTest {
class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleInternalPluginFuncTest {

Class<? extends PrecommitPlugin> pluginClassUnderTest = LicenseHeadersPrecommitPlugin.class

def setup() {
configurationCacheCompatible = true
buildFile << """
apply plugin:'java'
"""
}

def "detects invalid files with invalid license header"() {
given:
dualLicensedFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.elasticsearch.gradle.internal.precommit

import org.elasticsearch.gradle.fixtures.AbstractGradlePrecommitPluginFuncTest
import org.elasticsearch.gradle.fixtures.AbstractGradleInternalPluginFuncTest
import org.elasticsearch.gradle.fixtures.LocalRepositoryFixture
import org.elasticsearch.gradle.internal.conventions.precommit.PrecommitPlugin
import org.gradle.testkit.runner.TaskOutcome
Expand All @@ -20,7 +20,7 @@ import spock.lang.Unroll

// see https://github.com/elastic/elasticsearch/issues/87913
@IgnoreIf({ os.windows })
class TestingConventionsPrecommitPluginFuncTest extends AbstractGradlePrecommitPluginFuncTest {
class TestingConventionsPrecommitPluginFuncTest extends AbstractGradleInternalPluginFuncTest {

Class<? extends PrecommitPlugin> pluginClassUnderTest = TestingConventionsPrecommitPlugin.class

Expand All @@ -45,6 +45,7 @@ class TestingConventionsPrecommitPluginFuncTest extends AbstractGradlePrecommitP
}

def setup() {
configurationCacheCompatible = true
repository.configureBuild(buildFile)
}

Expand Down Expand Up @@ -84,26 +85,6 @@ class TestingConventionsPrecommitPluginFuncTest extends AbstractGradlePrecommitP
result.task(":testingConventions").outcome == TaskOutcome.UP_TO_DATE
}

def "testing convention plugin is configuration cache compatible"() {
given:
simpleJavaBuild()
testClazz("org.acme.valid.SomeTests", "org.apache.lucene.tests.util.LuceneTestCase") {
"""
public void testMe() {
}
"""
}
when:
def result = gradleRunner("precommit", "--configuration-cache").build()
then:
assertOutputContains(result.getOutput(), "0 problems were found storing the configuration cache.")

when:
result = gradleRunner("precommit", "--configuration-cache").build()
then:
assertOutputContains(result.getOutput(), "Configuration cache entry reused.")
}

def "checks base class convention"() {
given:
simpleJavaBuild()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.gradle.fixtures

import org.apache.commons.io.FileUtils
import org.elasticsearch.gradle.internal.test.ConfigurationCacheCompatibleAwareGradleRunner
import org.elasticsearch.gradle.internal.test.InternalAwareGradleRunner
import org.elasticsearch.gradle.internal.test.NormalizeOutputGradleRunner
import org.gradle.testkit.runner.BuildResult
Expand All @@ -33,6 +34,8 @@ abstract class AbstractGradleFuncTest extends Specification {
File propertiesFile
File projectDir

boolean configurationCacheCompatible = false

def setup() {
projectDir = testProjectDir.root
settingsFile = testProjectDir.newFile('settings.gradle')
Expand Down Expand Up @@ -71,14 +74,17 @@ abstract class AbstractGradleFuncTest extends Specification {

GradleRunner gradleRunner(File projectDir, String... arguments) {
return new NormalizeOutputGradleRunner(
new InternalAwareGradleRunner(
GradleRunner.create()
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0)
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardOutput()
),
projectDir
new ConfigurationCacheCompatibleAwareGradleRunner(
new InternalAwareGradleRunner(
GradleRunner.create()
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments()
.toString().indexOf("-agentlib:jdwp") > 0
)
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardOutput()
), configurationCacheCompatible),
projectDir
).withArguments(arguments)
}

Expand Down Expand Up @@ -127,13 +133,13 @@ abstract class AbstractGradleFuncTest extends Specification {
}

File internalBuild(
List<String> extraPlugins = [],
String bugfix = "7.15.2",
String bugfixLucene = "8.9.0",
String staged = "7.16.0",
String stagedLucene = "8.10.0",
String minor = "8.0.0",
String minorLucene = "9.0.0"
List<String> extraPlugins = [],
String bugfix = "7.15.2",
String bugfixLucene = "8.9.0",
String staged = "7.16.0",
String stagedLucene = "8.10.0",
String minor = "8.0.0",
String minorLucene = "9.0.0"
) {
buildFile << """plugins {
id 'elasticsearch.global-build-info'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.internal.test;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.InvalidPluginMetadataException;
import org.gradle.testkit.runner.InvalidRunnerConfigurationException;
import org.gradle.testkit.runner.UnexpectedBuildFailure;
import org.gradle.testkit.runner.UnexpectedBuildSuccess;

import java.io.File;
import java.io.Writer;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* A Gradle runner that delegates to another runner, optionally enabling the configuring cache parameter.
*/
public class ConfigurationCacheCompatibleAwareGradleRunner extends GradleRunner {
private GradleRunner delegate;
private boolean ccCompatible;

public ConfigurationCacheCompatibleAwareGradleRunner(GradleRunner delegate, boolean ccCompatible) {
this.delegate = delegate;
this.ccCompatible = ccCompatible;
}

@Override
public GradleRunner withGradleVersion(String gradleVersion) {
delegate.withGradleVersion(gradleVersion);
return this;
}

@Override
public GradleRunner withGradleInstallation(File file) {
delegate.withGradleInstallation(file);
return this;
}

@Override
public GradleRunner withGradleDistribution(URI uri) {
delegate.withGradleDistribution(uri);
return this;
}

@Override
public GradleRunner withTestKitDir(File file) {
delegate.withTestKitDir(file);
return this;
}

@Override
public File getProjectDir() {
return delegate.getProjectDir();
}

@Override
public GradleRunner withProjectDir(File projectDir) {
delegate.withProjectDir(projectDir);
return this;
}

@Override
public List<String> getArguments() {
return delegate.getArguments();
}

@Override
public GradleRunner withArguments(List<String> arguments) {
List<String> effectiveArgs = arguments;
if (ccCompatible) {
effectiveArgs = new ArrayList<>(arguments);
effectiveArgs.add("--configuration-cache");
}
delegate.withArguments(effectiveArgs);
return this;
}

@Override
public GradleRunner withArguments(String... arguments) {
withArguments(List.of(arguments));
return this;
}

@Override
public List<? extends File> getPluginClasspath() {
return delegate.getPluginClasspath();
}

@Override
public GradleRunner withPluginClasspath() throws InvalidPluginMetadataException {
delegate.withPluginClasspath();
return this;
}

@Override
public GradleRunner withPluginClasspath(Iterable<? extends File> iterable) {
delegate.withPluginClasspath(iterable);
return this;
}

@Override
public boolean isDebug() {
return delegate.isDebug();
}

@Override
public GradleRunner withDebug(boolean b) {
delegate.withDebug(b);
return this;
}

@Override
public Map<String, String> getEnvironment() {
return delegate.getEnvironment();
}

@Override
public GradleRunner withEnvironment(Map<String, String> map) {
delegate.withEnvironment(map);
return this;
}

@Override
public GradleRunner forwardStdOutput(Writer writer) {
delegate.forwardStdOutput(writer);
return this;
}

@Override
public GradleRunner forwardStdError(Writer writer) {
delegate.forwardStdOutput(writer);
return this;
}

@Override
public GradleRunner forwardOutput() {
delegate.forwardOutput();
return this;
}

@Override
public BuildResult build() throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
return delegate.build();
}

@Override
public BuildResult buildAndFail() throws InvalidRunnerConfigurationException, UnexpectedBuildSuccess {
return delegate.buildAndFail();
}
}

0 comments on commit 12525ad

Please sign in to comment.