From 33fba68033bcc9da68b288b1f1728c51caad6f72 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 8 Nov 2021 01:03:07 -0800 Subject: [PATCH] Fixup tests broken by the change to RegisterDependenciesTask. --- .../spotless/ErrorShouldRethrowTest.java | 31 ++++++++++--------- .../spotless/GradleIntegrationHarness.java | 23 +++++++++----- .../gradle/spotless/UpToDateTest.java | 2 +- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowTest.java index ab1db7a632..d1ced01609 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowTest.java @@ -27,7 +27,6 @@ import com.diffplug.common.base.CharMatcher; import com.diffplug.common.base.Splitter; -import com.diffplug.common.base.StringPrinter; import com.diffplug.spotless.LineEnding; /** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */ @@ -70,7 +69,7 @@ void anyExceptionShouldFail() throws Exception { "> Task :spotlessMisc FAILED\n" + "Step 'no swearing' found problem in 'README.md':\n" + "No swearing!\n" + - "java.lang.RuntimeException: No swearing!\n"); + "java.lang.RuntimeException: No swearing!"); } @Test @@ -80,7 +79,7 @@ void unlessEnforceCheckIsFalse() throws Exception { " enforceCheck false", "} // spotless"); setFile("README.md").toContent("This code is fubar."); - runWithSuccess("> Task :compileJava NO-SOURCE"); + runWithSuccess("> Task :processResources NO-SOURCE"); } @Test @@ -101,7 +100,7 @@ void unlessExemptedByPath() throws Exception { " } // format", "} // spotless"); setFile("README.md").toContent("This code is fubar."); - runWithSuccess("> Task :spotlessMisc", + runWithSuccess("> Task :spotlessMisc\n" + "Unable to apply step 'no swearing' to 'README.md'"); } @@ -116,26 +115,30 @@ void failsIfNeitherStepNorFileExempted() throws Exception { runWithFailure("> Task :spotlessMisc FAILED\n" + "Step 'no swearing' found problem in 'README.md':\n" + "No swearing!\n" + - "java.lang.RuntimeException: No swearing!\n"); + "java.lang.RuntimeException: No swearing!"); } - private void runWithSuccess(String... messages) throws Exception { + private void runWithSuccess(String expectedToStartWith) throws Exception { BuildResult result = gradleRunner().withArguments("check").build(); - assertResultAndMessages(result, TaskOutcome.SUCCESS, messages); + assertResultAndMessages(result, TaskOutcome.SUCCESS, expectedToStartWith); } - private void runWithFailure(String... messages) throws Exception { + private void runWithFailure(String expectedToStartWith) throws Exception { BuildResult result = gradleRunner().withArguments("check").buildAndFail(); - assertResultAndMessages(result, TaskOutcome.FAILED, messages); + assertResultAndMessages(result, TaskOutcome.FAILED, expectedToStartWith); } - private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String... messages) { - String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim(); + private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String expectedToStartWith) { + String output = result.getOutput(); + int register = output.indexOf(":spotlessInternalRegisterDependencies"); + int firstNewlineAfterThat = output.indexOf('\n', register + 1); + String useThisToMatch = output.substring(firstNewlineAfterThat); + int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith); - List actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput().trim())); + List actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(useThisToMatch.trim())); String actualStart = String.join("\n", actualLines.subList(0, numNewlines + 1)); Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith); - Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size() + result.tasks(TaskOutcome.NO_SOURCE).size()) - .isEqualTo(result.getTasks().size()); + Assertions.assertThat(outcomes(result, outcome).size() + outcomes(result, TaskOutcome.UP_TO_DATE).size() + outcomes(result, TaskOutcome.NO_SOURCE).size()) + .isEqualTo(outcomes(result).size()); } } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java index 8d76850488..45db3f008e 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java @@ -138,17 +138,26 @@ private void taskIsUpToDate(String task, boolean upToDate) throws IOException { pauseForFilesystem(); BuildResult buildResult = gradleRunner().withArguments(task).build(); - TaskOutcome expected = upToDate ? TaskOutcome.UP_TO_DATE : TaskOutcome.SUCCESS; - TaskOutcome notExpected = upToDate ? TaskOutcome.SUCCESS : TaskOutcome.UP_TO_DATE; - - boolean everythingAsExpected = !buildResult.tasks(expected).isEmpty() && - buildResult.tasks(notExpected).isEmpty() && - buildResult.getTasks().size() == buildResult.tasks(expected).size(); + List expected = outcomes(buildResult, upToDate ? TaskOutcome.UP_TO_DATE : TaskOutcome.SUCCESS); + List notExpected = outcomes(buildResult, upToDate ? TaskOutcome.SUCCESS : TaskOutcome.UP_TO_DATE); + boolean everythingAsExpected = !expected.isEmpty() && notExpected.isEmpty() && buildResult.getTasks().size() - 1 == expected.size(); if (!everythingAsExpected) { - fail("Expected all tasks to be " + expected + ", but instead was\n" + buildResultToString(buildResult)); + fail("Expected all tasks to be " + (upToDate ? TaskOutcome.UP_TO_DATE : TaskOutcome.SUCCESS) + ", but instead was\n" + buildResultToString(buildResult)); } } + protected static List outcomes(BuildResult build, TaskOutcome outcome) { + return build.taskPaths(outcome).stream() + .filter(s -> !s.equals(":spotlessInternalRegisterDependencies")) + .collect(Collectors.toList()); + } + + protected static List outcomes(BuildResult build) { + return build.getTasks().stream() + .filter(t -> !t.getPath().equals(":spotlessInternalRegisterDependencies")) + .collect(Collectors.toList()); + } + static String buildResultToString(BuildResult result) { return StringPrinter.buildString(printer -> { for (BuildTask task : result.getTasks()) { diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java index c3966fe24f..9b7eccdfff 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/UpToDateTest.java @@ -88,7 +88,7 @@ void testPathologicalCase() throws IOException { // the format task is UP-TO-DATE (same inputs), but the apply tasks will run again pauseForFilesystem(); BuildResult buildResult = gradleRunner().withArguments("spotlessApply").build(); - Assertions.assertThat(buildResult.taskPaths(TaskOutcome.UP_TO_DATE)).containsExactly(":spotlessMisc"); + Assertions.assertThat(buildResult.taskPaths(TaskOutcome.UP_TO_DATE)).containsExactly(":spotlessInternalRegisterDependencies", ":spotlessMisc"); Assertions.assertThat(buildResult.taskPaths(TaskOutcome.SUCCESS)).containsExactly(":spotlessMiscApply", ":spotlessApply"); assertFile("README.md").hasContent("abc");