Skip to content

Commit

Permalink
Fixup tests broken by the change to RegisterDependenciesTask.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Nov 8, 2021
1 parent 3557f57 commit 33fba68
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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'");
}

Expand All @@ -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<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput().trim()));
List<String> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> expected = outcomes(buildResult, upToDate ? TaskOutcome.UP_TO_DATE : TaskOutcome.SUCCESS);
List<String> 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<String> outcomes(BuildResult build, TaskOutcome outcome) {
return build.taskPaths(outcome).stream()
.filter(s -> !s.equals(":spotlessInternalRegisterDependencies"))
.collect(Collectors.toList());
}

protected static List<BuildTask> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 33fba68

Please sign in to comment.