Skip to content

Commit 6aea829

Browse files
authored
Number of utilities for writing gradle integration tests (#32282)
These are collected from a number of open PRs and are required to improove existing and write more readable future tests. I am extracting them to their own PR hoping to be able to merge and use them sooner.
1 parent b6c1493 commit 6aea829

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package org.elasticsearch.gradle.test;
22

3+
import org.gradle.testkit.runner.GradleRunner;
4+
35
import java.io.File;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
49

510
public abstract class GradleIntegrationTestCase extends GradleUnitTestCase {
611

@@ -13,4 +18,47 @@ protected File getProjectDir(String name) {
1318
return new File(root, name);
1419
}
1520

21+
protected GradleRunner getGradleRunner(String sampleProject) {
22+
return GradleRunner.create()
23+
.withProjectDir(getProjectDir(sampleProject))
24+
.withPluginClasspath();
25+
}
26+
27+
protected File getBuildDir(String name) {
28+
return new File(getProjectDir(name), "build");
29+
}
30+
31+
protected void assertOutputContains(String output, String... lines) {
32+
for (String line : lines) {
33+
assertOutputContains(output, line);
34+
}
35+
List<Integer> index = Stream.of(lines).map(line -> output.indexOf(line)).collect(Collectors.toList());
36+
if (index.equals(index.stream().sorted().collect(Collectors.toList())) == false) {
37+
fail("Expected the following lines to appear in this order:\n" +
38+
Stream.of(lines).map(line -> " - `" + line + "`").collect(Collectors.joining("\n")) +
39+
"\nBut they did not. Output is:\n\n```" + output + "\n```\n"
40+
);
41+
}
42+
}
43+
44+
protected void assertOutputContains(String output, String line) {
45+
assertTrue(
46+
"Expected the following line in output:\n\n" + line + "\n\nOutput is:\n" + output,
47+
output.contains(line)
48+
);
49+
}
50+
51+
protected void assertOutputDoesNotContain(String output, String line) {
52+
assertFalse(
53+
"Expected the following line not to be in output:\n\n" + line + "\n\nOutput is:\n" + output,
54+
output.contains(line)
55+
);
56+
}
57+
58+
protected void assertOutputDoesNotContain(String output, String... lines) {
59+
for (String line : lines) {
60+
assertOutputDoesNotContain(line);
61+
}
62+
}
63+
1664
}

0 commit comments

Comments
 (0)