From e564c9c608ab52f1a2381cba198d6f0c892855af Mon Sep 17 00:00:00 2001 From: Rene Groeschke Date: Thu, 2 Sep 2021 12:31:17 +0200 Subject: [PATCH 1/3] Simplify test build output normalization --- .../InternalBwcGitPluginFuncTest.groovy | 2 +- ...tributionArchiveCheckPluginFuncTest.groovy | 14 +- ...lDistributionBwcSetupPluginFuncTest.groovy | 12 +- .../internal/JdkDownloadPluginFuncTest.groovy | 2 +- ...censeHeadersPrecommitPluginFuncTest.groovy | 2 +- .../InternalTestRerunPluginFuncTest.groovy | 14 +- .../InternalYamlRestTestPluginFuncTest.groovy | 4 +- .../gradle/TestClustersPluginFuncTest.groovy | 4 +- .../fixtures/AbstractGradleFuncTest.groovy | 24 ++- .../test/InternalAwareGradleRunner.java | 42 ++-- .../test/NormalizeOutputGradleRunner.java | 195 ++++++++++++++++++ .../test/NormalizeOutputGradleRunner.java.new | 193 +++++++++++++++++ ...TestClasspathUtils.java => TestUtils.java} | 19 +- 13 files changed, 474 insertions(+), 53 deletions(-) create mode 100644 build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java create mode 100644 build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java.new rename build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/{TestClasspathUtils.java => TestUtils.java} (65%) diff --git a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalBwcGitPluginFuncTest.groovy b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalBwcGitPluginFuncTest.groovy index 310b747dd14a0..e24d3f1824e03 100644 --- a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalBwcGitPluginFuncTest.groovy +++ b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalBwcGitPluginFuncTest.groovy @@ -63,6 +63,6 @@ class InternalBwcGitPluginFuncTest extends AbstractGitAwareGradleFuncTest { then: result.task(":checkoutBwcBranch").outcome == TaskOutcome.SUCCESS result.task(":consumer:register").outcome == TaskOutcome.SUCCESS - normalized(result.output).contains("/cloned/build/checkout") + result.output.contains("./build/checkout") } } diff --git a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionArchiveCheckPluginFuncTest.groovy b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionArchiveCheckPluginFuncTest.groovy index 74eacb0b5a5ac..587343133b08e 100644 --- a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionArchiveCheckPluginFuncTest.groovy +++ b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionArchiveCheckPluginFuncTest.groovy @@ -80,11 +80,16 @@ unknown license content line 2 } """ + + when: - def result = gradleRunner(":darwin-tar:check").buildAndFail() + def runner = gradleRunner(":darwin-tar:check") + println "{runner.getClass()} = ${runner.getClass()}" + def result = runner.buildAndFail() + println "result.getClass() = ${result.getClass()}" then: result.task(":darwin-tar:checkLicense").outcome == TaskOutcome.FAILED - normalized(result.output).contains("> expected line [2] in " + + result.output.contains("> expected line [2] in " + "[./darwin-tar/build/tar-extracted/elasticsearch-${VersionProperties.getElasticsearch()}/LICENSE.txt] " + "to be [elastic license coorp stuff line 2] but was [unknown license content line 2]") } @@ -110,7 +115,7 @@ Copyright 2009-2018 Acme Coorp""" def result = gradleRunner(":darwin-tar:checkNotice").buildAndFail() then: result.task(":darwin-tar:checkNotice").outcome == TaskOutcome.FAILED - normalized(result.output).contains("> expected line [2] in " + + result.output.contains("> expected line [2] in " + "[./darwin-tar/build/tar-extracted/elasticsearch-${VersionProperties.getElasticsearch()}/NOTICE.txt] " + "to be [Copyright 2009-2021 Elasticsearch] but was [Copyright 2009-2018 Acme Coorp]") } @@ -146,8 +151,7 @@ Copyright 2009-2021 Elasticsearch""" def result = gradleRunner(":darwin-tar:check").buildAndFail() then: result.task(":darwin-tar:checkMlCppNotice").outcome == TaskOutcome.FAILED - normalized(result.output) - .contains("> expected [./darwin-tar/build/tar-extracted/elasticsearch-" + + result.output.contains("> expected [./darwin-tar/build/tar-extracted/elasticsearch-" + "${VersionProperties.getElasticsearch()}/modules/x-pack-ml/NOTICE.txt " + "to contain [foo license] but it did not") } diff --git a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionBwcSetupPluginFuncTest.groovy b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionBwcSetupPluginFuncTest.groovy index 3cb898076cc1a..a03f6ef8c9e79 100644 --- a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionBwcSetupPluginFuncTest.groovy +++ b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionBwcSetupPluginFuncTest.groovy @@ -17,6 +17,7 @@ import spock.lang.Unroll /* * Test is ignored on ARM since this test case tests the ability to build certain older BWC branches that we don't support on ARM */ + @IgnoreIf({ Architecture.current() == Architecture.AARCH64 }) class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleFuncTest { @@ -138,9 +139,8 @@ class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleF and: "assemble task triggered" result.output.contains("[7.10.1] > Task :distribution:archives:darwin-tar:assemble") - normalized(result.output) - .contains("distfile /distribution/bwc/bugfix/build/bwc/checkout-7.10/distribution/archives/darwin-tar/" + - "build/distributions/elasticsearch-7.10.1-SNAPSHOT-darwin-x86_64.tar.gz") + result.output.contains("distfile /distribution/bwc/bugfix/build/bwc/checkout-7.10/distribution/archives/darwin-tar/" + + "build/distributions/elasticsearch-7.10.1-SNAPSHOT-darwin-x86_64.tar.gz") } def "bwc expanded distribution folder can be resolved as bwc project artifact"() { @@ -177,11 +177,9 @@ class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleF result.task(":distribution:bwc:minor:buildBwcDarwinTar").outcome == TaskOutcome.SUCCESS and: "assemble task triggered" result.output.contains("[7.12.0] > Task :distribution:archives:darwin-tar:extractedAssemble") - normalized(result.output) - .contains("expandedRootPath /distribution/bwc/minor/build/bwc/checkout-7.x/" + + result.output.contains("expandedRootPath /distribution/bwc/minor/build/bwc/checkout-7.x/" + "distribution/archives/darwin-tar/build/install") - normalized(result.output) - .contains("nested folder /distribution/bwc/minor/build/bwc/checkout-7.x/" + + result.output.contains("nested folder /distribution/bwc/minor/build/bwc/checkout-7.x/" + "distribution/archives/darwin-tar/build/install/elasticsearch-7.12.0-SNAPSHOT") } } diff --git a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/JdkDownloadPluginFuncTest.groovy b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/JdkDownloadPluginFuncTest.groovy index f92931f862921..677bd6de8673c 100644 --- a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/JdkDownloadPluginFuncTest.groovy +++ b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/JdkDownloadPluginFuncTest.groovy @@ -182,7 +182,7 @@ class JdkDownloadPluginFuncTest extends AbstractGradleFuncTest { } then: - normalized(result.output).contains("Unpacking $expectedArchiveName using $transformType") == false + result.output.contains("Unpacking $expectedArchiveName using $transformType") == false where: platform | expectedArchiveName | transformType diff --git a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/LicenseHeadersPrecommitPluginFuncTest.groovy b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/LicenseHeadersPrecommitPluginFuncTest.groovy index b727235a242f3..0481d3315d010 100644 --- a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/LicenseHeadersPrecommitPluginFuncTest.groovy +++ b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/LicenseHeadersPrecommitPluginFuncTest.groovy @@ -33,7 +33,7 @@ class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleFuncTest { assertOutputContains(result.output, "> Check failed. License header problems were found. Full details: ./build/reports/licenseHeaders/rat.xml") assertOutputContains(result.output, "./src/main/java/org/acme/UnknownLicensed.java") assertOutputContains(result.output, "./src/main/java/org/acme/UnapprovedLicensed.java") - normalized(result.output).contains("./src/main/java/org/acme/DualLicensed.java") == false + result.output.contains("./src/main/java/org/acme/DualLicensed.java") == false } def "can filter source files"() { diff --git a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rerun/InternalTestRerunPluginFuncTest.groovy b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rerun/InternalTestRerunPluginFuncTest.groovy index f119d9b4114ed..0dd982665808a 100644 --- a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rerun/InternalTestRerunPluginFuncTest.groovy +++ b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rerun/InternalTestRerunPluginFuncTest.groovy @@ -57,7 +57,7 @@ class InternalTestRerunPluginFuncTest extends AbstractGradleFuncTest { def result = gradleRunner("test").buildAndFail() result.output.contains("total executions: 2") == false and: "no jvm system exit tracing provided" - normalized(result.output).contains("""Test jvm exited unexpectedly. + result.output.contains("""Test jvm exited unexpectedly. Test jvm system exit trace:""") == false } @@ -119,7 +119,7 @@ Test jvm system exit trace:""") == false result.output.contains("AnotherTest6 total executions: 2") // triggered only in the second overall run and: 'Tracing is provided' - normalized(result.output).contains("""================ + result.output.contains("""================ Test jvm exited unexpectedly. Test jvm system exit trace (run: 1) Gradle Test Executor 1 > AnotherTest6 > someTest @@ -200,11 +200,11 @@ Gradle Test Executor 1 > AnotherTest6 > someTest result.output.contains("JdkKillingTest total executions: 5") result.output.contains("Max retries(4) hit") and: 'Tracing is provided' - normalized(result.output).contains("Test jvm system exit trace (run: 1)") - normalized(result.output).contains("Test jvm system exit trace (run: 2)") - normalized(result.output).contains("Test jvm system exit trace (run: 3)") - normalized(result.output).contains("Test jvm system exit trace (run: 4)") - normalized(result.output).contains("Test jvm system exit trace (run: 5)") + result.output.contains("Test jvm system exit trace (run: 1)") + result.output.contains("Test jvm system exit trace (run: 2)") + result.output.contains("Test jvm system exit trace (run: 3)") + result.output.contains("Test jvm system exit trace (run: 4)") + result.output.contains("Test jvm system exit trace (run: 5)") } private String testMethodContent(boolean withSystemExit, boolean fail, int timesFailing = 1) { diff --git a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rest/InternalYamlRestTestPluginFuncTest.groovy b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rest/InternalYamlRestTestPluginFuncTest.groovy index 23554785dde1a..527cd6277c722 100644 --- a/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rest/InternalYamlRestTestPluginFuncTest.groovy +++ b/build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rest/InternalYamlRestTestPluginFuncTest.groovy @@ -67,8 +67,8 @@ class InternalYamlRestTestPluginFuncTest extends AbstractRestResourcesFuncTest { file("/build/classes/java/yamlRestTest/MockIT.class").exists() // check that our copied specs and tests are on the yamlRestTest classpath - normalized(result.output).contains("./build/restResources/yamlSpecs") - normalized(result.output).contains("./build/restResources/yamlTests") + result.output.contains("./build/restResources/yamlSpecs") + result.output.contains("./build/restResources/yamlTests") when: result = gradleRunner("yamlRestTest").build() diff --git a/build-tools/src/integTest/groovy/org/elasticsearch/gradle/TestClustersPluginFuncTest.groovy b/build-tools/src/integTest/groovy/org/elasticsearch/gradle/TestClustersPluginFuncTest.groovy index 326b7a68360ae..1fcc36172a417 100644 --- a/build-tools/src/integTest/groovy/org/elasticsearch/gradle/TestClustersPluginFuncTest.groovy +++ b/build-tools/src/integTest/groovy/org/elasticsearch/gradle/TestClustersPluginFuncTest.groovy @@ -109,7 +109,7 @@ class TestClustersPluginFuncTest extends AbstractGradleFuncTest { withChangedConfigMockedDistributionDownload(runner, runningClosure) then: - normalized(result.output).contains("Task ':myTask' is not up-to-date because:\n Input property 'clusters.myCluster\$0.nodes.\$0.$inputProperty'") + result.output.contains("Task ':myTask' is not up-to-date because:\n Input property 'clusters.myCluster\$0.nodes.\$0.$inputProperty'") result.output.contains("elasticsearch-keystore script executed!") assertEsLogContains("myCluster", "Starting Elasticsearch process") assertEsLogContains("myCluster", "Stopping node") @@ -163,7 +163,7 @@ class TestClustersPluginFuncTest extends AbstractGradleFuncTest { } then: - normalized(result.output).contains("Task ':myTask' is not up-to-date because:\n" + + result.output.contains("Task ':myTask' is not up-to-date because:\n" + " Input property 'clusters.myCluster\$0.nodes.\$0.$propertyName'") result.output.contains("elasticsearch-keystore script executed!") assertEsLogContains("myCluster", "Starting Elasticsearch process") diff --git a/build-tools/src/testFixtures/groovy/org/elasticsearch/gradle/fixtures/AbstractGradleFuncTest.groovy b/build-tools/src/testFixtures/groovy/org/elasticsearch/gradle/fixtures/AbstractGradleFuncTest.groovy index 5791744a31b31..0bd8768ccc6c6 100644 --- a/build-tools/src/testFixtures/groovy/org/elasticsearch/gradle/fixtures/AbstractGradleFuncTest.groovy +++ b/build-tools/src/testFixtures/groovy/org/elasticsearch/gradle/fixtures/AbstractGradleFuncTest.groovy @@ -9,6 +9,7 @@ package org.elasticsearch.gradle.fixtures import org.elasticsearch.gradle.internal.test.InternalAwareGradleRunner +import org.elasticsearch.gradle.internal.test.NormalizeOutputGradleRunner import org.gradle.testkit.runner.GradleRunner import org.junit.Rule import org.junit.rules.TemporaryFolder @@ -18,6 +19,8 @@ import java.lang.management.ManagementFactory import java.util.jar.JarEntry import java.util.jar.JarOutputStream +import static org.elasticsearch.gradle.internal.test.TestUtils.normalizeString + abstract class AbstractGradleFuncTest extends Specification { @Rule @@ -46,11 +49,14 @@ abstract class AbstractGradleFuncTest extends Specification { } GradleRunner gradleRunner(File projectDir, String... arguments) { - new InternalAwareGradleRunner(GradleRunner.create() - .withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0) - .withProjectDir(projectDir) - .withPluginClasspath() - .forwardOutput() + return new NormalizeOutputGradleRunner( + new InternalAwareGradleRunner(GradleRunner.create() + .withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0) + .withProjectDir(projectDir) + .withPluginClasspath() + .forwardOutput() + ), + projectDir ).withArguments(arguments) } @@ -63,13 +69,9 @@ abstract class AbstractGradleFuncTest extends Specification { assert normalized(givenOutput).contains(normalized(expected)) == false true } + String normalized(String input) { - String normalizedPathPrefix = testProjectDir.root.canonicalPath.replace('\\', '/') - return input.readLines() - .collect { it.replace('\\', '/') } - .collect {it.replace(normalizedPathPrefix , '.') } - .collect {it.replaceAll(/Gradle Test Executor \d/ , 'Gradle Test Executor 1') } - .join("\n") + return normalizeString(input, testProjectDir.root) } File file(String path) { diff --git a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java index 789780511c733..843f98055f9e6 100644 --- a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java +++ b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java @@ -33,22 +33,26 @@ public InternalAwareGradleRunner(GradleRunner delegate) { @Override public GradleRunner withGradleVersion(String gradleVersion) { - return delegate.withGradleVersion(gradleVersion); + delegate.withGradleVersion(gradleVersion); + return this; } @Override public GradleRunner withGradleInstallation(File file) { - return delegate.withGradleInstallation(file); + delegate.withGradleInstallation(file); + return this; } @Override public GradleRunner withGradleDistribution(URI uri) { - return delegate.withGradleDistribution(uri); + delegate.withGradleDistribution(uri); + return this; } @Override public GradleRunner withTestKitDir(File file) { - return delegate.withTestKitDir(file); + delegate.withTestKitDir(file); + return this; } @Override @@ -58,7 +62,8 @@ public File getProjectDir() { @Override public GradleRunner withProjectDir(File projectDir) { - return delegate.withProjectDir(projectDir); + delegate.withProjectDir(projectDir); + return this; } @Override @@ -70,12 +75,14 @@ public List getArguments() { public GradleRunner withArguments(List arguments) { List collect = Stream.concat(arguments.stream(), Stream.of("-Dtest.external=true")) .collect(Collectors.toList()); - return delegate.withArguments(collect); + delegate.withArguments(collect); + return this; } @Override public GradleRunner withArguments(String... arguments) { - return withArguments(List.of(arguments)); + withArguments(List.of(arguments)); + return this; } @Override @@ -85,12 +92,14 @@ public List getPluginClasspath() { @Override public GradleRunner withPluginClasspath() throws InvalidPluginMetadataException { - return delegate.withPluginClasspath(); + delegate.withPluginClasspath(); + return this; } @Override public GradleRunner withPluginClasspath(Iterable iterable) { - return delegate.withPluginClasspath(iterable); + delegate.withPluginClasspath(iterable); + return this; } @Override @@ -100,7 +109,8 @@ public boolean isDebug() { @Override public GradleRunner withDebug(boolean b) { - return delegate.withDebug(b); + delegate.withDebug(b); + return this; } @Nullable @@ -111,22 +121,26 @@ public Map getEnvironment() { @Override public GradleRunner withEnvironment(@Nullable Map map) { - return delegate.withEnvironment(map); + delegate.withEnvironment(map); + return this; } @Override public GradleRunner forwardStdOutput(Writer writer) { - return delegate.forwardStdOutput(writer); + delegate.forwardStdOutput(writer); + return this; } @Override public GradleRunner forwardStdError(Writer writer) { - return delegate.forwardStdOutput(writer); + delegate.forwardStdOutput(writer); + return this; } @Override public GradleRunner forwardOutput() { - return delegate.forwardOutput(); + delegate.forwardOutput(); + return this; } @Override diff --git a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java new file mode 100644 index 0000000000000..75b9aa7480499 --- /dev/null +++ b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java @@ -0,0 +1,195 @@ +/* + * 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.BuildTask; +import org.gradle.testkit.runner.GradleRunner; +import org.gradle.testkit.runner.InvalidPluginMetadataException; +import org.gradle.testkit.runner.InvalidRunnerConfigurationException; +import org.gradle.testkit.runner.TaskOutcome; +import org.gradle.testkit.runner.UnexpectedBuildFailure; +import org.gradle.testkit.runner.UnexpectedBuildSuccess; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.Writer; +import java.net.URI; +import java.util.List; +import java.util.Map; + +import static org.elasticsearch.gradle.internal.test.TestUtils.normalizeString; + +public class NormalizeOutputGradleRunner extends GradleRunner { + + public NormalizeOutputGradleRunner(GradleRunner delegate, File projectRootDir) { + this.delegate = delegate; + this.projectRootDir = projectRootDir; + } + + @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 getArguments() { + return delegate.getArguments(); + } + + @Override + public GradleRunner withArguments(List arguments) { + return delegate.withArguments(arguments); + } + + @Override + public GradleRunner withArguments(String... arguments) { + withArguments(List.of(arguments)); + return this; + } + + @Override + public List getPluginClasspath() { + return delegate.getPluginClasspath(); + } + + @Override + public GradleRunner withPluginClasspath() throws InvalidPluginMetadataException { + delegate.withPluginClasspath(); + return this; + } + + @Override + public GradleRunner withPluginClasspath(Iterable 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 getEnvironment() { + return delegate.getEnvironment(); + } + + @Override + public GradleRunner withEnvironment(Map 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 new NormalizedBuildResult(delegate.build()); + } + + @Override + public BuildResult buildAndFail() throws InvalidRunnerConfigurationException, UnexpectedBuildSuccess { + return new NormalizedBuildResult(delegate.buildAndFail()); + } + + private GradleRunner delegate; + private File projectRootDir; + + private class NormalizedBuildResult implements BuildResult { + private BuildResult delegate; + private String normalizedString; + + NormalizedBuildResult(BuildResult delegate) { + this.delegate = delegate; + } + + @Override + public String getOutput() { + if (normalizedString == null) { + normalizedString = normalizeString(delegate.getOutput(), projectRootDir); + } + return normalizedString; + } + + @Override + public List getTasks() { + return delegate.getTasks(); + } + + @Override + public List tasks(TaskOutcome taskOutcome) { + return delegate.tasks(taskOutcome); + } + + @Override + public List taskPaths(TaskOutcome taskOutcome) { + return delegate.taskPaths(taskOutcome); + } + + @Nullable + @Override + public BuildTask task(String s) { + return delegate.task(s); + } + } +} diff --git a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java.new b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java.new new file mode 100644 index 0000000000000..9beaa41bc7dd3 --- /dev/null +++ b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java.new @@ -0,0 +1,193 @@ +package org.elasticsearch.gradle.internal.test; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.BuildTask; +import org.gradle.testkit.runner.GradleRunner; +import org.gradle.testkit.runner.InvalidPluginMetadataException; +import org.gradle.testkit.runner.InvalidRunnerConfigurationException; +import org.gradle.testkit.runner.TaskOutcome; +import org.gradle.testkit.runner.UnexpectedBuildFailure; +import org.gradle.testkit.runner.UnexpectedBuildSuccess; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.Writer; +import java.net.URI; +import java.util.List; +import java.util.Map; + +import static org.elasticsearch.gradle.internal.test.TestUtils.normalizeString; + +public class NormalizeOutputGradleRunner extends GradleRunner { + + public NormalizeOutputGradleRunner(GradleRunner delegate, File projectRootDir) { + this.delegate = delegate; + this.projectRootDir = projectRootDir; + } + + @Override + public GradleRunner withGradleVersion(String gradleVersion) { + return delegate.withGradleVersion(gradleVersion); + } + + @Override + public GradleRunner withGradleInstallation(File file) { + return delegate.withGradleInstallation(file); + } + + @Override + public GradleRunner withGradleDistribution(URI uri) { + return delegate.withGradleDistribution(uri); + } + + @Override + public GradleRunner withTestKitDir(File file) { + return delegate.withTestKitDir(file); + } + + @Override + public File getProjectDir() { + return delegate.getProjectDir(); + } + + @Override + public GradleRunner withProjectDir(File projectDir) { + return delegate.withProjectDir(projectDir); + } + + @Override + public List getArguments() { + return delegate.getArguments(); + } + + @Override + public GradleRunner withArguments(List arguments) { + return delegate.withArguments(arguments); + } + + @Override + public GradleRunner withArguments(String... arguments) { + return withArguments(List.of(arguments)); + } + + @Override + public List getPluginClasspath() { + return delegate.getPluginClasspath(); + } + + @Override + public GradleRunner withPluginClasspath() throws InvalidPluginMetadataException { + return delegate.withPluginClasspath(); + } + + @Override + public GradleRunner withPluginClasspath(Iterable iterable) { + return delegate.withPluginClasspath(iterable); + } + + @Override + public boolean isDebug() { + return delegate.isDebug(); + } + + @Override + public GradleRunner withDebug(boolean b) { + return delegate.withDebug(b); + } + + @Override + public Map getEnvironment() { + return delegate.getEnvironment(); + } + + @Override + public GradleRunner withEnvironment(Map map) { + return delegate.withEnvironment(map); + } + + @Override + public GradleRunner forwardStdOutput(Writer writer) { + return delegate.forwardStdOutput(writer); + } + + @Override + public GradleRunner forwardStdError(Writer writer) { + return delegate.forwardStdOutput(writer); + } + + @Override + public GradleRunner forwardOutput() { + return delegate.forwardOutput(); + } + + @Override + public BuildResult build() throws InvalidRunnerConfigurationException, UnexpectedBuildFailure { + return new NormalizedBuildResult(delegate.build()); + } + + @Override + public BuildResult buildAndFail() throws InvalidRunnerConfigurationException, UnexpectedBuildSuccess { + return delegate.buildAndFail(); + } + + private GradleRunner delegate; + private File projectRootDir; + + private class NormalizedBuildResult implements BuildResult { + private BuildResult delegate; + private String normalizedString = null; + + NormalizedBuildResult(BuildResult delegate) { + this.delegate = delegate; + } + + @Override + public String getOutput() { + if(normalizedString == null) { + normalizedString = normalizeString(delegate.getOutput(), projectRootDir); + } + return normalizedString; + } + + @Override + public List getTasks() { + return delegate.getTasks(); + } + + @Override + public List tasks(TaskOutcome taskOutcome) { + return delegate.tasks(taskOutcome); + } + + @Override + public List taskPaths(TaskOutcome taskOutcome) { + return delegate.taskPaths(taskOutcome); + } + + @Nullable + @Override + public BuildTask task(String s) { + return delegate.task(s); + } + } +} diff --git a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/TestClasspathUtils.java b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/TestUtils.java similarity index 65% rename from build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/TestClasspathUtils.java rename to build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/TestUtils.java index 9876835a15acb..8e69122048c00 100644 --- a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/TestClasspathUtils.java +++ b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/TestUtils.java @@ -16,14 +16,15 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.util.stream.Collectors; import static org.junit.Assert.fail; -public class TestClasspathUtils { +public class TestUtils { public static void setupJarJdkClasspath(File projectRoot) { try { - URL originLocation = TestClasspathUtils.class.getClassLoader() + URL originLocation = TestUtils.class.getClassLoader() .loadClass("org.elasticsearch.jdk.JdkJarHellCheck") .getProtectionDomain() .getCodeSource() @@ -41,4 +42,18 @@ public static void setupJarJdkClasspath(File projectRoot) { } } + public static String normalizeString(String input, File projectRootDir) { + try { + String normalizedPathPrefix = projectRootDir.getCanonicalPath().replaceAll("\\\\", "/"); + System.out.println("normalizedPathPrefix = " + normalizedPathPrefix); + return input.lines() + .map(it -> it.replaceAll("\\\\", "/")) + .map(it -> it.replaceAll(normalizedPathPrefix, ".")) + .map(it -> it.replaceAll("Gradle Test Executor \\d", "Gradle Test Executor 1")) + .collect(Collectors.joining("\n")); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } From 0d3fd5252be0b15c1d58eef39179d973fb780985 Mon Sep 17 00:00:00 2001 From: Rene Groeschke Date: Thu, 2 Sep 2021 12:52:57 +0200 Subject: [PATCH 2/3] Remove .new file --- .../test/NormalizeOutputGradleRunner.java.new | 193 ------------------ 1 file changed, 193 deletions(-) delete mode 100644 build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java.new diff --git a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java.new b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java.new deleted file mode 100644 index 9beaa41bc7dd3..0000000000000 --- a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java.new +++ /dev/null @@ -1,193 +0,0 @@ -package org.elasticsearch.gradle.internal.test; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - - -import org.gradle.testkit.runner.BuildResult; -import org.gradle.testkit.runner.BuildTask; -import org.gradle.testkit.runner.GradleRunner; -import org.gradle.testkit.runner.InvalidPluginMetadataException; -import org.gradle.testkit.runner.InvalidRunnerConfigurationException; -import org.gradle.testkit.runner.TaskOutcome; -import org.gradle.testkit.runner.UnexpectedBuildFailure; -import org.gradle.testkit.runner.UnexpectedBuildSuccess; -import org.jetbrains.annotations.Nullable; - -import java.io.File; -import java.io.Writer; -import java.net.URI; -import java.util.List; -import java.util.Map; - -import static org.elasticsearch.gradle.internal.test.TestUtils.normalizeString; - -public class NormalizeOutputGradleRunner extends GradleRunner { - - public NormalizeOutputGradleRunner(GradleRunner delegate, File projectRootDir) { - this.delegate = delegate; - this.projectRootDir = projectRootDir; - } - - @Override - public GradleRunner withGradleVersion(String gradleVersion) { - return delegate.withGradleVersion(gradleVersion); - } - - @Override - public GradleRunner withGradleInstallation(File file) { - return delegate.withGradleInstallation(file); - } - - @Override - public GradleRunner withGradleDistribution(URI uri) { - return delegate.withGradleDistribution(uri); - } - - @Override - public GradleRunner withTestKitDir(File file) { - return delegate.withTestKitDir(file); - } - - @Override - public File getProjectDir() { - return delegate.getProjectDir(); - } - - @Override - public GradleRunner withProjectDir(File projectDir) { - return delegate.withProjectDir(projectDir); - } - - @Override - public List getArguments() { - return delegate.getArguments(); - } - - @Override - public GradleRunner withArguments(List arguments) { - return delegate.withArguments(arguments); - } - - @Override - public GradleRunner withArguments(String... arguments) { - return withArguments(List.of(arguments)); - } - - @Override - public List getPluginClasspath() { - return delegate.getPluginClasspath(); - } - - @Override - public GradleRunner withPluginClasspath() throws InvalidPluginMetadataException { - return delegate.withPluginClasspath(); - } - - @Override - public GradleRunner withPluginClasspath(Iterable iterable) { - return delegate.withPluginClasspath(iterable); - } - - @Override - public boolean isDebug() { - return delegate.isDebug(); - } - - @Override - public GradleRunner withDebug(boolean b) { - return delegate.withDebug(b); - } - - @Override - public Map getEnvironment() { - return delegate.getEnvironment(); - } - - @Override - public GradleRunner withEnvironment(Map map) { - return delegate.withEnvironment(map); - } - - @Override - public GradleRunner forwardStdOutput(Writer writer) { - return delegate.forwardStdOutput(writer); - } - - @Override - public GradleRunner forwardStdError(Writer writer) { - return delegate.forwardStdOutput(writer); - } - - @Override - public GradleRunner forwardOutput() { - return delegate.forwardOutput(); - } - - @Override - public BuildResult build() throws InvalidRunnerConfigurationException, UnexpectedBuildFailure { - return new NormalizedBuildResult(delegate.build()); - } - - @Override - public BuildResult buildAndFail() throws InvalidRunnerConfigurationException, UnexpectedBuildSuccess { - return delegate.buildAndFail(); - } - - private GradleRunner delegate; - private File projectRootDir; - - private class NormalizedBuildResult implements BuildResult { - private BuildResult delegate; - private String normalizedString = null; - - NormalizedBuildResult(BuildResult delegate) { - this.delegate = delegate; - } - - @Override - public String getOutput() { - if(normalizedString == null) { - normalizedString = normalizeString(delegate.getOutput(), projectRootDir); - } - return normalizedString; - } - - @Override - public List getTasks() { - return delegate.getTasks(); - } - - @Override - public List tasks(TaskOutcome taskOutcome) { - return delegate.tasks(taskOutcome); - } - - @Override - public List taskPaths(TaskOutcome taskOutcome) { - return delegate.taskPaths(taskOutcome); - } - - @Nullable - @Override - public BuildTask task(String s) { - return delegate.task(s); - } - } -} From c52385c607749d2b619f1fc62fc9d4515a178e9b Mon Sep 17 00:00:00 2001 From: Rene Groeschke Date: Thu, 2 Sep 2021 18:31:11 +0200 Subject: [PATCH 3/3] Remove idea Nullable annotation --- .../gradle/internal/test/InternalAwareGradleRunner.java | 4 +--- .../gradle/internal/test/NormalizeOutputGradleRunner.java | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java index 843f98055f9e6..e30455aa0b406 100644 --- a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java +++ b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java @@ -14,7 +14,6 @@ import org.gradle.testkit.runner.InvalidRunnerConfigurationException; import org.gradle.testkit.runner.UnexpectedBuildFailure; import org.gradle.testkit.runner.UnexpectedBuildSuccess; -import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.Writer; @@ -113,14 +112,13 @@ public GradleRunner withDebug(boolean b) { return this; } - @Nullable @Override public Map getEnvironment() { return delegate.getEnvironment(); } @Override - public GradleRunner withEnvironment(@Nullable Map map) { + public GradleRunner withEnvironment(Map map) { delegate.withEnvironment(map); return this; } diff --git a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java index 75b9aa7480499..8d2ae78396435 100644 --- a/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java +++ b/build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/NormalizeOutputGradleRunner.java @@ -16,7 +16,6 @@ import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.UnexpectedBuildFailure; import org.gradle.testkit.runner.UnexpectedBuildSuccess; -import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.Writer; @@ -186,7 +185,6 @@ public List taskPaths(TaskOutcome taskOutcome) { return delegate.taskPaths(taskOutcome); } - @Nullable @Override public BuildTask task(String s) { return delegate.task(s);