Skip to content

Commit 93f3607

Browse files
authored
Simplify test build output normalization (#77172)
* Simplify test build output normalization
1 parent 8dd4231 commit 93f3607

File tree

12 files changed

+280
-56
lines changed

12 files changed

+280
-56
lines changed

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalBwcGitPluginFuncTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ class InternalBwcGitPluginFuncTest extends AbstractGitAwareGradleFuncTest {
6363
then:
6464
result.task(":checkoutBwcBranch").outcome == TaskOutcome.SUCCESS
6565
result.task(":consumer:register").outcome == TaskOutcome.SUCCESS
66-
normalized(result.output).contains("/cloned/build/checkout")
66+
result.output.contains("./build/checkout")
6767
}
6868
}

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionArchiveCheckPluginFuncTest.groovy

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,16 @@ unknown license content line 2
8080
}
8181
"""
8282

83+
84+
8385
when:
84-
def result = gradleRunner(":darwin-tar:check").buildAndFail()
86+
def runner = gradleRunner(":darwin-tar:check")
87+
println "{runner.getClass()} = ${runner.getClass()}"
88+
def result = runner.buildAndFail()
89+
println "result.getClass() = ${result.getClass()}"
8590
then:
8691
result.task(":darwin-tar:checkLicense").outcome == TaskOutcome.FAILED
87-
normalized(result.output).contains("> expected line [2] in " +
92+
result.output.contains("> expected line [2] in " +
8893
"[./darwin-tar/build/tar-extracted/elasticsearch-${VersionProperties.getElasticsearch()}/LICENSE.txt] " +
8994
"to be [elastic license coorp stuff line 2] but was [unknown license content line 2]")
9095
}
@@ -110,7 +115,7 @@ Copyright 2009-2018 Acme Coorp"""
110115
def result = gradleRunner(":darwin-tar:checkNotice").buildAndFail()
111116
then:
112117
result.task(":darwin-tar:checkNotice").outcome == TaskOutcome.FAILED
113-
normalized(result.output).contains("> expected line [2] in " +
118+
result.output.contains("> expected line [2] in " +
114119
"[./darwin-tar/build/tar-extracted/elasticsearch-${VersionProperties.getElasticsearch()}/NOTICE.txt] " +
115120
"to be [Copyright 2009-2021 Elasticsearch] but was [Copyright 2009-2018 Acme Coorp]")
116121
}
@@ -146,8 +151,7 @@ Copyright 2009-2021 Elasticsearch"""
146151
def result = gradleRunner(":darwin-tar:check").buildAndFail()
147152
then:
148153
result.task(":darwin-tar:checkMlCppNotice").outcome == TaskOutcome.FAILED
149-
normalized(result.output)
150-
.contains("> expected [./darwin-tar/build/tar-extracted/elasticsearch-" +
154+
result.output.contains("> expected [./darwin-tar/build/tar-extracted/elasticsearch-" +
151155
"${VersionProperties.getElasticsearch()}/modules/x-pack-ml/NOTICE.txt " +
152156
"to contain [foo license] but it did not")
153157
}

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/InternalDistributionBwcSetupPluginFuncTest.groovy

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import spock.lang.Unroll
1717
/*
1818
* 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
1919
*/
20+
2021
@IgnoreIf({ Architecture.current() == Architecture.AARCH64 })
2122
class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleFuncTest {
2223

@@ -138,9 +139,8 @@ class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleF
138139

139140
and: "assemble task triggered"
140141
result.output.contains("[7.10.1] > Task :distribution:archives:darwin-tar:assemble")
141-
normalized(result.output)
142-
.contains("distfile /distribution/bwc/bugfix/build/bwc/checkout-7.10/distribution/archives/darwin-tar/" +
143-
"build/distributions/elasticsearch-7.10.1-SNAPSHOT-darwin-x86_64.tar.gz")
142+
result.output.contains("distfile /distribution/bwc/bugfix/build/bwc/checkout-7.10/distribution/archives/darwin-tar/" +
143+
"build/distributions/elasticsearch-7.10.1-SNAPSHOT-darwin-x86_64.tar.gz")
144144
}
145145

146146
def "bwc expanded distribution folder can be resolved as bwc project artifact"() {
@@ -177,11 +177,9 @@ class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleF
177177
result.task(":distribution:bwc:minor:buildBwcDarwinTar").outcome == TaskOutcome.SUCCESS
178178
and: "assemble task triggered"
179179
result.output.contains("[7.12.0] > Task :distribution:archives:darwin-tar:extractedAssemble")
180-
normalized(result.output)
181-
.contains("expandedRootPath /distribution/bwc/minor/build/bwc/checkout-7.x/" +
180+
result.output.contains("expandedRootPath /distribution/bwc/minor/build/bwc/checkout-7.x/" +
182181
"distribution/archives/darwin-tar/build/install")
183-
normalized(result.output)
184-
.contains("nested folder /distribution/bwc/minor/build/bwc/checkout-7.x/" +
182+
result.output.contains("nested folder /distribution/bwc/minor/build/bwc/checkout-7.x/" +
185183
"distribution/archives/darwin-tar/build/install/elasticsearch-7.12.0-SNAPSHOT")
186184
}
187185
}

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/JdkDownloadPluginFuncTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class JdkDownloadPluginFuncTest extends AbstractGradleFuncTest {
182182
}
183183

184184
then:
185-
normalized(result.output).contains("Unpacking $expectedArchiveName using $transformType") == false
185+
result.output.contains("Unpacking $expectedArchiveName using $transformType") == false
186186

187187
where:
188188
platform | expectedArchiveName | transformType

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/precommit/LicenseHeadersPrecommitPluginFuncTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleFuncTest {
3333
assertOutputContains(result.output, "> Check failed. License header problems were found. Full details: ./build/reports/licenseHeaders/rat.xml")
3434
assertOutputContains(result.output, "./src/main/java/org/acme/UnknownLicensed.java")
3535
assertOutputContains(result.output, "./src/main/java/org/acme/UnapprovedLicensed.java")
36-
normalized(result.output).contains("./src/main/java/org/acme/DualLicensed.java") == false
36+
result.output.contains("./src/main/java/org/acme/DualLicensed.java") == false
3737
}
3838

3939
def "can filter source files"() {

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rerun/InternalTestRerunPluginFuncTest.groovy

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class InternalTestRerunPluginFuncTest extends AbstractGradleFuncTest {
5757
def result = gradleRunner("test").buildAndFail()
5858
result.output.contains("total executions: 2") == false
5959
and: "no jvm system exit tracing provided"
60-
normalized(result.output).contains("""Test jvm exited unexpectedly.
60+
result.output.contains("""Test jvm exited unexpectedly.
6161
Test jvm system exit trace:""") == false
6262
}
6363

@@ -119,7 +119,7 @@ Test jvm system exit trace:""") == false
119119
result.output.contains("AnotherTest6 total executions: 2")
120120
// triggered only in the second overall run
121121
and: 'Tracing is provided'
122-
normalized(result.output).contains("""================
122+
result.output.contains("""================
123123
Test jvm exited unexpectedly.
124124
Test jvm system exit trace (run: 1)
125125
Gradle Test Executor 1 > AnotherTest6 > someTest
@@ -200,11 +200,11 @@ Gradle Test Executor 1 > AnotherTest6 > someTest
200200
result.output.contains("JdkKillingTest total executions: 5")
201201
result.output.contains("Max retries(4) hit")
202202
and: 'Tracing is provided'
203-
normalized(result.output).contains("Test jvm system exit trace (run: 1)")
204-
normalized(result.output).contains("Test jvm system exit trace (run: 2)")
205-
normalized(result.output).contains("Test jvm system exit trace (run: 3)")
206-
normalized(result.output).contains("Test jvm system exit trace (run: 4)")
207-
normalized(result.output).contains("Test jvm system exit trace (run: 5)")
203+
result.output.contains("Test jvm system exit trace (run: 1)")
204+
result.output.contains("Test jvm system exit trace (run: 2)")
205+
result.output.contains("Test jvm system exit trace (run: 3)")
206+
result.output.contains("Test jvm system exit trace (run: 4)")
207+
result.output.contains("Test jvm system exit trace (run: 5)")
208208
}
209209

210210
private String testMethodContent(boolean withSystemExit, boolean fail, int timesFailing = 1) {

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/test/rest/InternalYamlRestTestPluginFuncTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class InternalYamlRestTestPluginFuncTest extends AbstractRestResourcesFuncTest {
6767
file("/build/classes/java/yamlRestTest/MockIT.class").exists()
6868

6969
// check that our copied specs and tests are on the yamlRestTest classpath
70-
normalized(result.output).contains("./build/restResources/yamlSpecs")
71-
normalized(result.output).contains("./build/restResources/yamlTests")
70+
result.output.contains("./build/restResources/yamlSpecs")
71+
result.output.contains("./build/restResources/yamlTests")
7272

7373
when:
7474
result = gradleRunner("yamlRestTest").build()

build-tools/src/integTest/groovy/org/elasticsearch/gradle/TestClustersPluginFuncTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class TestClustersPluginFuncTest extends AbstractGradleFuncTest {
109109
withChangedConfigMockedDistributionDownload(runner, runningClosure)
110110

111111
then:
112-
normalized(result.output).contains("Task ':myTask' is not up-to-date because:\n Input property 'clusters.myCluster\$0.nodes.\$0.$inputProperty'")
112+
result.output.contains("Task ':myTask' is not up-to-date because:\n Input property 'clusters.myCluster\$0.nodes.\$0.$inputProperty'")
113113
result.output.contains("elasticsearch-keystore script executed!")
114114
assertEsLogContains("myCluster", "Starting Elasticsearch process")
115115
assertEsLogContains("myCluster", "Stopping node")
@@ -163,7 +163,7 @@ class TestClustersPluginFuncTest extends AbstractGradleFuncTest {
163163
}
164164

165165
then:
166-
normalized(result.output).contains("Task ':myTask' is not up-to-date because:\n" +
166+
result.output.contains("Task ':myTask' is not up-to-date because:\n" +
167167
" Input property 'clusters.myCluster\$0.nodes.\$0.$propertyName'")
168168
result.output.contains("elasticsearch-keystore script executed!")
169169
assertEsLogContains("myCluster", "Starting Elasticsearch process")

build-tools/src/testFixtures/groovy/org/elasticsearch/gradle/fixtures/AbstractGradleFuncTest.groovy

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.elasticsearch.gradle.fixtures
1010

1111
import org.elasticsearch.gradle.internal.test.InternalAwareGradleRunner
12+
import org.elasticsearch.gradle.internal.test.NormalizeOutputGradleRunner
1213
import org.gradle.testkit.runner.GradleRunner
1314
import org.junit.Rule
1415
import org.junit.rules.TemporaryFolder
@@ -18,6 +19,8 @@ import java.lang.management.ManagementFactory
1819
import java.util.jar.JarEntry
1920
import java.util.jar.JarOutputStream
2021

22+
import static org.elasticsearch.gradle.internal.test.TestUtils.normalizeString
23+
2124
abstract class AbstractGradleFuncTest extends Specification {
2225

2326
@Rule
@@ -46,11 +49,14 @@ abstract class AbstractGradleFuncTest extends Specification {
4649
}
4750

4851
GradleRunner gradleRunner(File projectDir, String... arguments) {
49-
new InternalAwareGradleRunner(GradleRunner.create()
50-
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0)
51-
.withProjectDir(projectDir)
52-
.withPluginClasspath()
53-
.forwardOutput()
52+
return new NormalizeOutputGradleRunner(
53+
new InternalAwareGradleRunner(GradleRunner.create()
54+
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0)
55+
.withProjectDir(projectDir)
56+
.withPluginClasspath()
57+
.forwardOutput()
58+
),
59+
projectDir
5460
).withArguments(arguments)
5561
}
5662

@@ -63,13 +69,9 @@ abstract class AbstractGradleFuncTest extends Specification {
6369
assert normalized(givenOutput).contains(normalized(expected)) == false
6470
true
6571
}
72+
6673
String normalized(String input) {
67-
String normalizedPathPrefix = testProjectDir.root.canonicalPath.replace('\\', '/')
68-
return input.readLines()
69-
.collect { it.replace('\\', '/') }
70-
.collect {it.replace(normalizedPathPrefix , '.') }
71-
.collect {it.replaceAll(/Gradle Test Executor \d/ , 'Gradle Test Executor 1') }
72-
.join("\n")
74+
return normalizeString(input, testProjectDir.root)
7375
}
7476

7577
File file(String path) {

build-tools/src/testFixtures/java/org/elasticsearch/gradle/internal/test/InternalAwareGradleRunner.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.gradle.testkit.runner.InvalidRunnerConfigurationException;
1515
import org.gradle.testkit.runner.UnexpectedBuildFailure;
1616
import org.gradle.testkit.runner.UnexpectedBuildSuccess;
17-
import org.jetbrains.annotations.Nullable;
1817

1918
import java.io.File;
2019
import java.io.Writer;
@@ -33,22 +32,26 @@ public InternalAwareGradleRunner(GradleRunner delegate) {
3332

3433
@Override
3534
public GradleRunner withGradleVersion(String gradleVersion) {
36-
return delegate.withGradleVersion(gradleVersion);
35+
delegate.withGradleVersion(gradleVersion);
36+
return this;
3737
}
3838

3939
@Override
4040
public GradleRunner withGradleInstallation(File file) {
41-
return delegate.withGradleInstallation(file);
41+
delegate.withGradleInstallation(file);
42+
return this;
4243
}
4344

4445
@Override
4546
public GradleRunner withGradleDistribution(URI uri) {
46-
return delegate.withGradleDistribution(uri);
47+
delegate.withGradleDistribution(uri);
48+
return this;
4749
}
4850

4951
@Override
5052
public GradleRunner withTestKitDir(File file) {
51-
return delegate.withTestKitDir(file);
53+
delegate.withTestKitDir(file);
54+
return this;
5255
}
5356

5457
@Override
@@ -58,7 +61,8 @@ public File getProjectDir() {
5861

5962
@Override
6063
public GradleRunner withProjectDir(File projectDir) {
61-
return delegate.withProjectDir(projectDir);
64+
delegate.withProjectDir(projectDir);
65+
return this;
6266
}
6367

6468
@Override
@@ -70,12 +74,14 @@ public List<String> getArguments() {
7074
public GradleRunner withArguments(List<String> arguments) {
7175
List<String> collect = Stream.concat(arguments.stream(), Stream.of("-Dtest.external=true"))
7276
.collect(Collectors.toList());
73-
return delegate.withArguments(collect);
77+
delegate.withArguments(collect);
78+
return this;
7479
}
7580

7681
@Override
7782
public GradleRunner withArguments(String... arguments) {
78-
return withArguments(List.of(arguments));
83+
withArguments(List.of(arguments));
84+
return this;
7985
}
8086

8187
@Override
@@ -85,12 +91,14 @@ public List<? extends File> getPluginClasspath() {
8591

8692
@Override
8793
public GradleRunner withPluginClasspath() throws InvalidPluginMetadataException {
88-
return delegate.withPluginClasspath();
94+
delegate.withPluginClasspath();
95+
return this;
8996
}
9097

9198
@Override
9299
public GradleRunner withPluginClasspath(Iterable<? extends File> iterable) {
93-
return delegate.withPluginClasspath(iterable);
100+
delegate.withPluginClasspath(iterable);
101+
return this;
94102
}
95103

96104
@Override
@@ -100,33 +108,37 @@ public boolean isDebug() {
100108

101109
@Override
102110
public GradleRunner withDebug(boolean b) {
103-
return delegate.withDebug(b);
111+
delegate.withDebug(b);
112+
return this;
104113
}
105114

106-
@Nullable
107115
@Override
108116
public Map<String, String> getEnvironment() {
109117
return delegate.getEnvironment();
110118
}
111119

112120
@Override
113-
public GradleRunner withEnvironment(@Nullable Map<String, String> map) {
114-
return delegate.withEnvironment(map);
121+
public GradleRunner withEnvironment(Map<String, String> map) {
122+
delegate.withEnvironment(map);
123+
return this;
115124
}
116125

117126
@Override
118127
public GradleRunner forwardStdOutput(Writer writer) {
119-
return delegate.forwardStdOutput(writer);
128+
delegate.forwardStdOutput(writer);
129+
return this;
120130
}
121131

122132
@Override
123133
public GradleRunner forwardStdError(Writer writer) {
124-
return delegate.forwardStdOutput(writer);
134+
delegate.forwardStdOutput(writer);
135+
return this;
125136
}
126137

127138
@Override
128139
public GradleRunner forwardOutput() {
129-
return delegate.forwardOutput();
140+
delegate.forwardOutput();
141+
return this;
130142
}
131143

132144
@Override

0 commit comments

Comments
 (0)