Skip to content

Commit 0eff425

Browse files
authored
Simplify test build output normalization (#77172) (#77242)
* Simplify test build output normalization
1 parent 9cacd40 commit 0eff425

File tree

12 files changed

+299
-75
lines changed

12 files changed

+299
-75
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: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ class InternalTestRerunPluginFuncTest extends AbstractGradleFuncTest {
2323
repositories {
2424
mavenCentral()
2525
}
26-
26+
2727
dependencies {
2828
testImplementation 'junit:junit:4.13.1'
2929
}
30-
30+
3131
tasks.named("test").configure {
3232
maxParallelForks = 4
3333
testLogging {
3434
events "standard_out", "failed"
3535
exceptionFormat "short"
3636
}
3737
}
38-
38+
3939
"""
4040
createTest("SimpleTest")
4141
createTest("SimpleTest2")
@@ -55,7 +55,7 @@ class InternalTestRerunPluginFuncTest extends AbstractGradleFuncTest {
5555
def result = gradleRunner("test").buildAndFail()
5656
result.output.contains("total executions: 2") == false
5757
and: "no jvm system exit tracing provided"
58-
normalized(result.output).contains("""Test jvm exited unexpectedly.
58+
result.output.contains("""Test jvm exited unexpectedly.
5959
Test jvm system exit trace:""") == false
6060
}
6161

@@ -79,11 +79,11 @@ Test jvm system exit trace:""") == false
7979
repositories {
8080
mavenCentral()
8181
}
82-
82+
8383
dependencies {
8484
testImplementation 'junit:junit:4.13.1'
8585
}
86-
86+
8787
tasks.named("test").configure {
8888
maxParallelForks = 4
8989
testLogging {
@@ -92,7 +92,7 @@ Test jvm system exit trace:""") == false
9292
exceptionFormat "short"
9393
}
9494
}
95-
95+
9696
"""
9797
createTest("AnotherTest")
9898
createTest("AnotherTest2")
@@ -117,7 +117,7 @@ Test jvm system exit trace:""") == false
117117
result.output.contains("AnotherTest6 total executions: 2")
118118
// triggered only in the second overall run
119119
and: 'Tracing is provided'
120-
normalized(result.output).contains("""================
120+
result.output.contains("""================
121121
Test jvm exited unexpectedly.
122122
Test jvm system exit trace (run: 1)
123123
Gradle Test Executor 1 > AnotherTest6 > someTest
@@ -135,19 +135,19 @@ Gradle Test Executor 1 > AnotherTest6 > someTest
135135
repositories {
136136
mavenCentral()
137137
}
138-
138+
139139
dependencies {
140140
testImplementation 'junit:junit:4.13.1'
141141
}
142-
142+
143143
tasks.named("test").configure {
144144
maxParallelForks = 5
145145
testLogging {
146146
events "started", "passed", "standard_out", "failed"
147147
exceptionFormat "short"
148148
}
149149
}
150-
150+
151151
"""
152152
createSystemExitTest("AnotherTest6")
153153
createFailedTest("SimpleTest1")
@@ -176,11 +176,11 @@ Gradle Test Executor 1 > AnotherTest6 > someTest
176176
repositories {
177177
mavenCentral()
178178
}
179-
179+
180180
dependencies {
181181
testImplementation 'junit:junit:4.13.1'
182182
}
183-
183+
184184
tasks.named("test").configure {
185185
rerun {
186186
maxReruns = 4
@@ -198,10 +198,10 @@ Gradle Test Executor 1 > AnotherTest6 > someTest
198198
result.output.contains("JdkKillingTest total executions: 4")
199199
result.output.contains("Max retries(4) hit")
200200
and: 'Tracing is provided'
201-
normalized(result.output).contains("Test jvm system exit trace (run: 1)")
202-
normalized(result.output).contains("Test jvm system exit trace (run: 2)")
203-
normalized(result.output).contains("Test jvm system exit trace (run: 3)")
204-
normalized(result.output).contains("Test jvm system exit trace (run: 4)")
201+
result.output.contains("Test jvm system exit trace (run: 1)")
202+
result.output.contains("Test jvm system exit trace (run: 2)")
203+
result.output.contains("Test jvm system exit trace (run: 3)")
204+
result.output.contains("Test jvm system exit trace (run: 4)")
205205
}
206206

207207
private String testMethodContent(boolean withSystemExit, boolean fail, int timesFailing = 1) {
@@ -244,24 +244,24 @@ Gradle Test Executor 1 > AnotherTest6 > someTest
244244
import java.nio.*;
245245
import java.nio.file.*;
246246
import java.io.IOException;
247-
247+
248248
public class $clazzName {
249249
Path executionLogPath = Paths.get("test-executions" + getClass().getSimpleName() +".log");
250-
251-
@Before
250+
251+
@Before
252252
public void beforeTest() {
253253
logExecution();
254254
}
255-
256-
@After
255+
256+
@After
257257
public void afterTest() {
258258
}
259-
260-
@Test
259+
260+
@Test
261261
public void someTest() {
262262
${content}
263263
}
264-
264+
265265
int countExecutions() {
266266
try {
267267
return Files.readAllLines(executionLogPath).size();
@@ -270,7 +270,7 @@ Gradle Test Executor 1 > AnotherTest6 > someTest
270270
return 0;
271271
}
272272
}
273-
273+
274274
void logExecution() {
275275
try {
276276
Files.write(executionLogPath, "Test executed\\n".getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class YamlRestTestPluginFuncTest 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) {

0 commit comments

Comments
 (0)