Skip to content

Commit 4d28488

Browse files
authored
Migrate away from Gradle's TaskInternal#execute to our own method (#208)
Previously, plugin-gradle's tests produced warning messages saying that "The TaskInternal.execute() method has been deprecated and is scheduled to be removed in Gradle 5.0". This commit is an attempt to fix that so that we're better prepared to migrate to Gradle 5.0 when it arrives.
1 parent 419973e commit 4d28488

File tree

5 files changed

+65
-38
lines changed

5 files changed

+65
-38
lines changed

plugin-gradle/CHANGES.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
### 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/))
44

5+
* 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))
6+
57
### 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))
68

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

911
### 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))
1012

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import static com.diffplug.gradle.spotless.Tasks.execute;
19+
1820
import java.io.File;
1921
import java.io.IOException;
2022
import java.util.ArrayList;
@@ -25,7 +27,6 @@
2527
import org.assertj.core.api.Assertions;
2628
import org.gradle.api.GradleException;
2729
import org.gradle.api.Project;
28-
import org.gradle.api.tasks.TaskExecutionException;
2930
import org.gradle.testfixtures.ProjectBuilder;
3031
import org.junit.Test;
3132

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

52-
private void assertTaskFailure(SpotlessTask task, String... expectedLines) {
53+
private void assertTaskFailure(SpotlessTask task, String... expectedLines) throws Exception {
5354
String msg = getTaskErrorMessage(task);
5455

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

64-
protected String getTaskErrorMessage(SpotlessTask task) {
65+
protected String getTaskErrorMessage(SpotlessTask task) throws Exception {
6566
try {
66-
task.execute();
67-
throw new AssertionError("Expected a TaskExecutionException");
68-
} catch (TaskExecutionException e) {
69-
GradleException cause = (GradleException) e.getCause();
70-
return cause.getMessage();
67+
execute(task);
68+
throw new AssertionError("Expected a GradleException");
69+
} catch (GradleException e) {
70+
return e.getMessage();
7171
}
7272
}
7373

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import static com.diffplug.gradle.spotless.Tasks.execute;
19+
1820
import java.io.File;
1921
import java.io.IOException;
2022
import java.util.Collections;
@@ -44,26 +46,26 @@ public void createTask() {
4446
}
4547

4648
@Test(expected = GradleException.class)
47-
public void testLineEndingsCheckFail() throws IOException {
49+
public void testLineEndingsCheckFail() throws Exception {
4850
checkTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
4951
checkTask.setTarget(Collections.singleton(setFile("testFile").toContent("\r\n")));
50-
checkTask.execute();
52+
execute(checkTask);
5153
}
5254

5355
@Test
54-
public void testLineEndingsCheckPass() throws IOException {
56+
public void testLineEndingsCheckPass() throws Exception {
5557
checkTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
5658
checkTask.setTarget(Collections.singleton(setFile("testFile").toContent("\n")));
57-
checkTask.execute();
59+
execute(checkTask);
5860
}
5961

6062
@Test
61-
public void testLineEndingsApply() throws IOException {
63+
public void testLineEndingsApply() throws Exception {
6264
File testFile = setFile("testFile").toContent("\r\n");
6365

6466
applyTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
6567
applyTask.setTarget(Collections.singleton(testFile));
66-
applyTask.execute();
68+
execute(applyTask);
6769

6870
assertFile(testFile).hasContent("\n");
6971
}
@@ -79,29 +81,29 @@ public void testStepCheckFail() throws IOException {
7981
" @@ -1 +1 @@",
8082
" -apple",
8183
" +aple");
82-
Assertions.assertThatThrownBy(() -> checkTask.execute()).hasStackTraceContaining(diff);
84+
Assertions.assertThatThrownBy(() -> execute(checkTask)).hasStackTraceContaining(diff);
8385

8486
assertFile(testFile).hasContent("apple");
8587
}
8688

8789
@Test
88-
public void testStepCheckPass() throws IOException {
90+
public void testStepCheckPass() throws Exception {
8991
File testFile = setFile("testFile").toContent("aple");
9092
checkTask.setTarget(Collections.singleton(testFile));
9193

9294
checkTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
93-
checkTask.execute();
95+
execute(checkTask);
9496

9597
assertFile(testFile).hasContent("aple");
9698
}
9799

98100
@Test
99-
public void testStepApply() throws IOException {
101+
public void testStepApply() throws Exception {
100102
File testFile = setFile("testFile").toContent("apple");
101103
applyTask.setTarget(Collections.singleton(testFile));
102104

103105
applyTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
104-
applyTask.execute();
106+
execute(applyTask);
105107

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

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java

+17-18
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import static com.diffplug.gradle.spotless.Tasks.execute;
19+
1820
import java.io.File;
1921
import java.io.IOException;
2022
import java.util.Arrays;
2123
import java.util.Collections;
2224
import java.util.Locale;
2325

2426
import org.assertj.core.api.Assertions;
25-
import org.gradle.api.GradleException;
2627
import org.gradle.api.Project;
27-
import org.gradle.api.tasks.TaskExecutionException;
2828
import org.gradle.testfixtures.ProjectBuilder;
2929
import org.junit.Assert;
3030
import org.junit.Test;
@@ -82,11 +82,10 @@ private Bundle paddedCell() {
8282

8383
private String checkFailureMsg() {
8484
try {
85-
check.execute();
85+
execute(check);
8686
throw new AssertionError();
87-
} catch (TaskExecutionException e) {
88-
GradleException cause = (GradleException) e.getCause();
89-
return cause.getMessage();
87+
} catch (Exception e) {
88+
return e.getMessage();
9089
}
9190
}
9291
}
@@ -111,29 +110,29 @@ public void failsWithoutPaddedCell() throws IOException {
111110
}
112111

113112
@Test
114-
public void paddedCellApply() throws IOException {
113+
public void paddedCellApply() throws Exception {
115114
Bundle cycle = cycle().paddedCell();
116115
Bundle converge = converge().paddedCell();
117116
Bundle diverge = diverge().paddedCell();
118117

119-
cycle.apply.execute();
120-
converge.apply.execute();
121-
diverge.apply.execute();
118+
execute(cycle.apply);
119+
execute(converge.apply);
120+
execute(diverge.apply);
122121

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

127-
cycle.check.execute();
128-
converge.check.execute();
129-
diverge.check.execute();
126+
execute(cycle.check);
127+
execute(converge.check);
128+
execute(diverge.check);
130129
}
131130

132131
@Test
133-
public void paddedCellCheckFailureFiles() throws Throwable {
132+
public void paddedCellCheckFailureFiles() throws Exception {
134133
cycle().paddedCell().checkFailureMsg();
135134
converge().paddedCell().checkFailureMsg();
136-
diverge().paddedCell().check.execute();
135+
execute(diverge().paddedCell().check);
137136

138137
assertFolderContents("build",
139138
"spotless-diagnose-converge",
@@ -167,7 +166,7 @@ private void assertFolderContents(String subfolderName, String... files) throws
167166
}
168167

169168
@Test
170-
public void paddedCellCheckCycleFailureMsg() throws Throwable {
169+
public void paddedCellCheckCycleFailureMsg() throws IOException {
171170
assertFailureMessage(cycle().paddedCell(),
172171
"The following files had format violations:",
173172
slashify(" src/test.cycle"),
@@ -178,7 +177,7 @@ public void paddedCellCheckCycleFailureMsg() throws Throwable {
178177
}
179178

180179
@Test
181-
public void paddedCellCheckConvergeFailureMsg() throws Throwable {
180+
public void paddedCellCheckConvergeFailureMsg() throws IOException {
182181
assertFailureMessage(converge().paddedCell(),
183182
"The following files had format violations:",
184183
slashify(" src/test.converge"),
@@ -187,7 +186,7 @@ public void paddedCellCheckConvergeFailureMsg() throws Throwable {
187186
"Run 'gradlew spotlessApply' to fix these violations.");
188187
}
189188

190-
private void assertFailureMessage(Bundle bundle, String... expectedOutput) throws Throwable {
189+
private void assertFailureMessage(Bundle bundle, String... expectedOutput) {
191190
String msg = bundle.checkFailureMsg();
192191
String expected = StringPrinter.buildStringFromLines(expectedOutput).trim();
193192
Assert.assertEquals(expected, msg);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
final class Tasks {
19+
private Tasks() {}
20+
21+
static void execute(SpotlessTask task) throws Exception {
22+
task.performAction(Mocks.mockIncrementalTaskInputs(task.getTarget()));
23+
}
24+
}

0 commit comments

Comments
 (0)