Skip to content

Commit

Permalink
Move muted tests file and allow for additional files to be configured (
Browse files Browse the repository at this point in the history
…elastic#107916)

Some refactoring to the muted tests plugin to better support usage in a
composite build configuration.
# Conflicts:
#	build-tools/src/testFixtures/groovy/org/elasticsearch/gradle/fixtures/AbstractGradleFuncTest.groovy
  • Loading branch information
mark-vieira committed Apr 29, 2024
1 parent 28e54f6 commit dc650c1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,34 @@

package org.elasticsearch.gradle.internal.test;

import org.elasticsearch.gradle.internal.conventions.util.Util;
import org.elasticsearch.gradle.internal.info.BuildParams;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.testing.Test;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class MutedTestPlugin implements Plugin<Project> {
private static final String ADDITIONAL_FILES_PROPERTY = "org.elasticsearch.additional.muted.tests";

@Override
public void apply(Project project) {
File infoPath = new File(Util.locateElasticsearchWorkspace(project.getGradle()), "build-tools-internal");
String additionalFilePaths = project.hasProperty(ADDITIONAL_FILES_PROPERTY)
? project.property(ADDITIONAL_FILES_PROPERTY).toString()
: "";
List<RegularFile> additionalFiles = Arrays.stream(additionalFilePaths.split(","))
.filter(p -> p.isEmpty() == false)
.map(p -> project.getRootProject().getLayout().getProjectDirectory().file(p))
.toList();

Provider<MutedTestsBuildService> mutedTestsProvider = project.getGradle()
.getSharedServices()
.registerIfAbsent("mutedTests", MutedTestsBuildService.class, spec -> {
spec.getParameters().getInfoPath().set(infoPath);
spec.getParameters().getInfoPath().set(project.getRootProject().getProjectDir());
spec.getParameters().getAdditionalFiles().set(additionalFiles);
});

project.getTasks().withType(Test.class).configureEach(test -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import org.gradle.api.file.RegularFile;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.services.BuildService;
import org.gradle.api.services.BuildServiceParameters;

Expand All @@ -28,25 +30,34 @@
import java.util.List;

public abstract class MutedTestsBuildService implements BuildService<MutedTestsBuildService.Params> {
private final List<String> excludePatterns;
private final List<String> excludePatterns = new ArrayList<>();
private final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());

public MutedTestsBuildService() {
File infoPath = getParameters().getInfoPath().get().getAsFile();
File mutedTestsFile = new File(infoPath, "muted-tests.yml");
try (InputStream is = new BufferedInputStream(new FileInputStream(mutedTestsFile))) {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
List<MutedTest> mutedTests = objectMapper.readValue(is, MutedTests.class).getTests();
excludePatterns = buildExcludePatterns(mutedTests == null ? Collections.emptyList() : mutedTests);
} catch (IOException e) {
throw new UncheckedIOException(e);
excludePatterns.addAll(buildExcludePatterns(mutedTestsFile));
for (RegularFile regularFile : getParameters().getAdditionalFiles().get()) {
excludePatterns.addAll(buildExcludePatterns(regularFile.getAsFile()));
}
}

public List<String> getExcludePatterns() {
return excludePatterns;
}

private static List<String> buildExcludePatterns(List<MutedTest> mutedTests) {
private List<String> buildExcludePatterns(File file) {
List<MutedTest> mutedTests;

try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
mutedTests = objectMapper.readValue(is, MutedTests.class).getTests();
if (mutedTests == null) {
return Collections.emptyList();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}

List<String> excludes = new ArrayList<>();
if (mutedTests.isEmpty() == false) {
for (MutedTestsBuildService.MutedTest mutedTest : mutedTests) {
Expand Down Expand Up @@ -84,6 +95,8 @@ private static List<String> buildExcludePatterns(List<MutedTest> mutedTests) {

public interface Params extends BuildServiceParameters {
RegularFileProperty getInfoPath();

ListProperty<RegularFile> getAdditionalFiles();
}

public static class MutedTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class AbstractGradleFuncTest extends Specification {
propertiesFile <<
"org.gradle.java.installations.fromEnv=JAVA_HOME,RUNTIME_JAVA_HOME,JAVA15_HOME,JAVA14_HOME,JAVA13_HOME,JAVA12_HOME,JAVA11_HOME,JAVA8_HOME"

def mutedTestsFile = Files.createFile(Path.of(testProjectDir.newFolder("build-tools-internal").path, "muted-tests.yml"))
def mutedTestsFile = testProjectDir.newFile("muted-tests.yml")
mutedTestsFile << """
tests: []
"""
Expand Down
File renamed without changes.

0 comments on commit dc650c1

Please sign in to comment.