Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate away from Gradle's TaskInternal#execute to our own method #208

Merged
merged 3 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

### Version 3.11.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))

* Migrated `plugin-gradle`'s tests away from `TaskInternal#execute` to a custom method to help with Gradle 5.0 migration later on. ([#208](https://github.com/diffplug/spotless/pull/208))

### Version 3.10.0 - February 15th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.10.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.10.0))

* LicenseHeaderStep now supports customizing the year range separator in copyright notices. ([#199](https://github.com/diffplug/spotless/pull/199)
* LicenseHeaderStep now supports customizing the year range separator in copyright notices. ([#199](https://github.com/diffplug/spotless/pull/199))

### Version 3.9.0 - February 5th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.9.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.9.0))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.diffplug.gradle.spotless;

import static com.diffplug.gradle.spotless.Tasks.execute;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -25,7 +27,6 @@
import org.assertj.core.api.Assertions;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskExecutionException;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Test;

Expand All @@ -49,7 +50,7 @@ private SpotlessTask create(List<File> files) throws IOException {
return task;
}

private void assertTaskFailure(SpotlessTask task, String... expectedLines) {
private void assertTaskFailure(SpotlessTask task, String... expectedLines) throws Exception {
String msg = getTaskErrorMessage(task);

String firstLine = "The following files had format violations:\n";
Expand All @@ -61,13 +62,12 @@ private void assertTaskFailure(SpotlessTask task, String... expectedLines) {
Assertions.assertThat(middle).isEqualTo(expectedMessage.substring(0, expectedMessage.length() - 1));
}

protected String getTaskErrorMessage(SpotlessTask task) {
protected String getTaskErrorMessage(SpotlessTask task) throws Exception {
try {
task.execute();
throw new AssertionError("Expected a TaskExecutionException");
} catch (TaskExecutionException e) {
GradleException cause = (GradleException) e.getCause();
return cause.getMessage();
execute(task);
throw new AssertionError("Expected a GradleException");
} catch (GradleException e) {
return e.getMessage();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.diffplug.gradle.spotless;

import static com.diffplug.gradle.spotless.Tasks.execute;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -44,26 +46,26 @@ public void createTask() {
}

@Test(expected = GradleException.class)
public void testLineEndingsCheckFail() throws IOException {
public void testLineEndingsCheckFail() throws Exception {
checkTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
checkTask.setTarget(Collections.singleton(setFile("testFile").toContent("\r\n")));
checkTask.execute();
execute(checkTask);
}

@Test
public void testLineEndingsCheckPass() throws IOException {
public void testLineEndingsCheckPass() throws Exception {
checkTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
checkTask.setTarget(Collections.singleton(setFile("testFile").toContent("\n")));
checkTask.execute();
execute(checkTask);
}

@Test
public void testLineEndingsApply() throws IOException {
public void testLineEndingsApply() throws Exception {
File testFile = setFile("testFile").toContent("\r\n");

applyTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
applyTask.setTarget(Collections.singleton(testFile));
applyTask.execute();
execute(applyTask);

assertFile(testFile).hasContent("\n");
}
Expand All @@ -79,29 +81,29 @@ public void testStepCheckFail() throws IOException {
" @@ -1 +1 @@",
" -apple",
" +aple");
Assertions.assertThatThrownBy(() -> checkTask.execute()).hasStackTraceContaining(diff);
Assertions.assertThatThrownBy(() -> execute(checkTask)).hasStackTraceContaining(diff);

assertFile(testFile).hasContent("apple");
}

@Test
public void testStepCheckPass() throws IOException {
public void testStepCheckPass() throws Exception {
File testFile = setFile("testFile").toContent("aple");
checkTask.setTarget(Collections.singleton(testFile));

checkTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
checkTask.execute();
execute(checkTask);

assertFile(testFile).hasContent("aple");
}

@Test
public void testStepApply() throws IOException {
public void testStepApply() throws Exception {
File testFile = setFile("testFile").toContent("apple");
applyTask.setTarget(Collections.singleton(testFile));

applyTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
applyTask.execute();
execute(applyTask);

assertFile(testFile).hasContent("aple");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/
package com.diffplug.gradle.spotless;

import static com.diffplug.gradle.spotless.Tasks.execute;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;

import org.assertj.core.api.Assertions;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskExecutionException;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -82,11 +82,10 @@ private Bundle paddedCell() {

private String checkFailureMsg() {
try {
check.execute();
execute(check);
throw new AssertionError();
} catch (TaskExecutionException e) {
GradleException cause = (GradleException) e.getCause();
return cause.getMessage();
} catch (Exception e) {
return e.getMessage();
}
}
}
Expand All @@ -111,29 +110,29 @@ public void failsWithoutPaddedCell() throws IOException {
}

@Test
public void paddedCellApply() throws IOException {
public void paddedCellApply() throws Exception {
Bundle cycle = cycle().paddedCell();
Bundle converge = converge().paddedCell();
Bundle diverge = diverge().paddedCell();

cycle.apply.execute();
converge.apply.execute();
diverge.apply.execute();
execute(cycle.apply);
execute(converge.apply);
execute(diverge.apply);

assertFile(cycle.file).hasContent("A"); // cycle -> first element in cycle
assertFile(converge.file).hasContent(""); // converge -> converges
assertFile(diverge.file).hasContent("CCC"); // diverge -> no change

cycle.check.execute();
converge.check.execute();
diverge.check.execute();
execute(cycle.check);
execute(converge.check);
execute(diverge.check);
}

@Test
public void paddedCellCheckFailureFiles() throws Throwable {
public void paddedCellCheckFailureFiles() throws Exception {
cycle().paddedCell().checkFailureMsg();
converge().paddedCell().checkFailureMsg();
diverge().paddedCell().check.execute();
execute(diverge().paddedCell().check);

assertFolderContents("build",
"spotless-diagnose-converge",
Expand Down Expand Up @@ -167,7 +166,7 @@ private void assertFolderContents(String subfolderName, String... files) throws
}

@Test
public void paddedCellCheckCycleFailureMsg() throws Throwable {
public void paddedCellCheckCycleFailureMsg() throws IOException {
assertFailureMessage(cycle().paddedCell(),
"The following files had format violations:",
slashify(" src/test.cycle"),
Expand All @@ -178,7 +177,7 @@ public void paddedCellCheckCycleFailureMsg() throws Throwable {
}

@Test
public void paddedCellCheckConvergeFailureMsg() throws Throwable {
public void paddedCellCheckConvergeFailureMsg() throws IOException {
assertFailureMessage(converge().paddedCell(),
"The following files had format violations:",
slashify(" src/test.converge"),
Expand All @@ -187,7 +186,7 @@ public void paddedCellCheckConvergeFailureMsg() throws Throwable {
"Run 'gradlew spotlessApply' to fix these violations.");
}

private void assertFailureMessage(Bundle bundle, String... expectedOutput) throws Throwable {
private void assertFailureMessage(Bundle bundle, String... expectedOutput) {
String msg = bundle.checkFailureMsg();
String expected = StringPrinter.buildStringFromLines(expectedOutput).trim();
Assert.assertEquals(expected, msg);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2016 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;

final class Tasks {
private Tasks() {}

static void execute(SpotlessTask task) throws Exception {
task.performAction(Mocks.mockIncrementalTaskInputs(task.getTarget()));
}
}