From abb7a91c010be35750654a842776ca20bfb00f19 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Mon, 23 Jul 2018 16:25:16 +0300 Subject: [PATCH] Number of utilities for writing gradle integration tests 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. --- .../test/GradleIntegrationTestCase.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java b/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java index 26da663182f7c..5c36fa61550d8 100644 --- a/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java @@ -1,6 +1,11 @@ package org.elasticsearch.gradle.test; +import org.gradle.testkit.runner.GradleRunner; + import java.io.File; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; public abstract class GradleIntegrationTestCase extends GradleUnitTestCase { @@ -13,4 +18,47 @@ protected File getProjectDir(String name) { return new File(root, name); } + protected GradleRunner getGradleRunner(String sampleProject) { + return GradleRunner.create() + .withProjectDir(getProjectDir(sampleProject)) + .withPluginClasspath(); + } + + protected File getBuildDir(String name) { + return new File(getProjectDir(name), "build"); + } + + protected void assertOutputContains(String output, String... lines) { + for (String line : lines) { + assertOutputContains(output, line); + } + List index = Stream.of(lines).map(line -> output.indexOf(line)).collect(Collectors.toList()); + if (index.equals(index.stream().sorted().collect(Collectors.toList())) == false) { + fail("Expected the following lines to appear in this order:\n" + + Stream.of(lines).map(line -> " - `" + line + "`").collect(Collectors.joining("\n")) + + "\nBut they did not. Output is:\n\n```" + output + "\n```\n" + ); + } + } + + protected void assertOutputContains(String output, String line) { + assertTrue( + "Expected the following line in output:\n\n" + line + "\n\nOutput is:\n" + output, + output.contains(line) + ); + } + + protected void assertOutputDoesNotContain(String output, String line) { + assertFalse( + "Expected the following line not to be in output:\n\n" + line + "\n\nOutput is:\n" + output, + output.contains(line) + ); + } + + protected void assertOutputDoesNotContain(String output, String... lines) { + for (String line : lines) { + assertOutputDoesNotContain(line); + } + } + }