Skip to content

Commit b8282ae

Browse files
committed
New task :plugin-gradle:modernTest (also CI job) which runs all tests using the new modern implementation.
1 parent 386cf58 commit b8282ae

File tree

9 files changed

+74
-17
lines changed

9 files changed

+74
-17
lines changed

.circleci/config.yml

+11
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ jobs:
9292
command: ./gradlew :plugin-maven:check --build-cache
9393
- store_test_results:
9494
path: plugin-maven/build/test-results/test
95+
test_gradle_modern_8:
96+
<< : *env_gradle
97+
steps:
98+
- checkout
99+
- *restore_cache_wrapper
100+
- *restore_cache_deps
101+
- run:
102+
name: gradlew :plugin-gradle:modernTest
103+
command: ./gradlew :plugin-gradle:modernTest --build-cache
104+
- store_test_results:
105+
path: plugin-gradle/build/test-results/modernTest
95106
test_npm_8:
96107
<< : *env_gradle
97108
docker:

plugin-gradle/build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ task npmTest(type: Test) {
4141
}
4242
}
4343

44+
task modernTest(type: Test) {
45+
systemProperty 'spotlessModern', 'true'
46+
useJUnit {
47+
excludeCategories 'com.diffplug.spotless.category.NpmTest', 'com.diffplug.gradle.spotless.ExcludeFromPluginGradleModern'
48+
}
49+
}
50+
4451
// make it easy for eclipse to run against latest build
4552
tasks.eclipse.dependsOn(pluginUnderTestMetadata)
4653

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public class SpotlessPlugin implements Plugin<Project> {
2828

2929
@Override
3030
public void apply(Project project) {
31+
// if -PspotlessModern=true, then use the modern stuff instead of the legacy stuff
32+
if (project.hasProperty(SpotlessPluginModern.SPOTLESS_MODERN) && project.findProperty(SpotlessPluginModern.SPOTLESS_MODERN).equals("true")) {
33+
new SpotlessPluginModern().apply(project);
34+
return;
35+
}
3136
// make sure there's a `clean` task
3237
project.getPlugins().apply(BasePlugin.class);
3338

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPluginModern.java

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import com.diffplug.spotless.SpotlessCache;
2424

2525
public class SpotlessPluginModern implements Plugin<Project> {
26+
static final String SPOTLESS_MODERN = "spotlessModern";
27+
static final String MINIMUM_GRADLE = "5.4";
28+
2629
@Override
2730
public void apply(Project project) {
2831
// make sure there's a `clean` task

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

+17-14
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void passesIfNoException() throws Exception {
5858
" } // format",
5959
"} // spotless");
6060
setFile("README.md").toContent("This code is fun.");
61-
runWithSuccess(":spotlessMisc");
61+
runWithSuccess("> Task :spotlessMisc");
6262
}
6363

6464
@Test
@@ -68,9 +68,9 @@ public void anyExceptionShouldFail() throws Exception {
6868
"} // spotless");
6969
setFile("README.md").toContent("This code is fubar.");
7070
runWithFailure(
71-
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
72-
"No swearing!",
73-
"java.lang.RuntimeException: No swearing!");
71+
"> Task :spotlessMisc FAILED",
72+
"Step 'no swearing' found problem in 'README.md':",
73+
"No swearing!");
7474
}
7575

7676
@Test
@@ -80,7 +80,7 @@ public void unlessEnforceCheckIsFalse() throws Exception {
8080
" enforceCheck false",
8181
"} // spotless");
8282
setFile("README.md").toContent("This code is fubar.");
83-
runWithSuccess(":compileJava UP-TO-DATE");
83+
runWithSuccess("> Task :compileJava NO-SOURCE");
8484
}
8585

8686
@Test
@@ -90,7 +90,7 @@ public void unlessExemptedByStep() throws Exception {
9090
" } // format",
9191
"} // spotless");
9292
setFile("README.md").toContent("This code is fubar.");
93-
runWithSuccess(":spotlessMisc",
93+
runWithSuccess("> Task :spotlessMisc",
9494
"Unable to apply step 'no swearing' to 'README.md'");
9595
}
9696

@@ -101,7 +101,7 @@ public void unlessExemptedByPath() throws Exception {
101101
" } // format",
102102
"} // spotless");
103103
setFile("README.md").toContent("This code is fubar.");
104-
runWithSuccess(":spotlessMisc",
104+
runWithSuccess("> Task :spotlessMisc",
105105
"Unable to apply step 'no swearing' to 'README.md'");
106106
}
107107

@@ -114,34 +114,37 @@ public void failsIfNeitherStepNorFileExempted() throws Exception {
114114
"} // spotless");
115115
setFile("README.md").toContent("This code is fubar.");
116116
runWithFailure(
117-
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
118-
"No swearing!",
119-
"java.lang.RuntimeException: No swearing!");
117+
"> Task :spotlessMisc FAILED",
118+
"Step 'no swearing' found problem in 'README.md':",
119+
"No swearing!");
120120
}
121121

122122
private void runWithSuccess(String... messages) throws Exception {
123123
if (JreVersion.thisVm() != JreVersion._8) {
124124
return;
125125
}
126-
BuildResult result = gradleRunner().withArguments("check").build();
126+
BuildResult result = gradleRunner().withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE).withArguments("check").build();
127127
assertResultAndMessages(result, TaskOutcome.SUCCESS, messages);
128128
}
129129

130130
private void runWithFailure(String... messages) throws Exception {
131131
if (JreVersion.thisVm() != JreVersion._8) {
132132
return;
133133
}
134-
BuildResult result = gradleRunner().withArguments("check").buildAndFail();
134+
BuildResult result = gradleRunner().withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE).withArguments("check").buildAndFail();
135135
assertResultAndMessages(result, TaskOutcome.FAILED, messages);
136136
}
137137

138138
private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String... messages) {
139139
String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim();
140140
int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith);
141-
List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput()));
141+
List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput().trim()));
142142
String actualStart = String.join("\n", actualLines.subList(0, numNewlines + 1));
143143
Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith);
144-
Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size())
144+
// result.getTasks()
145+
// .stream()
146+
// .forEach(task -> System.out.println("task " + task.getPath() + " " + task.getOutcome()));
147+
Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size() + result.tasks(TaskOutcome.NO_SOURCE).size())
145148
.isEqualTo(result.getTasks().size());
146149
}
147150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2020 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+
public interface ExcludeFromPluginGradleModern {
19+
20+
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.diffplug.common.base.Errors;
3333
import com.diffplug.common.base.StringPrinter;
34+
import com.diffplug.common.collect.ImmutableMap;
3435
import com.diffplug.common.tree.TreeDef;
3536
import com.diffplug.common.tree.TreeStream;
3637
import com.diffplug.spotless.JreVersion;
@@ -74,10 +75,15 @@ protected static String requestGradleForJre8and11(String ver) {
7475
}
7576

7677
protected final GradleRunner gradleRunner() throws IOException {
77-
return GradleRunner.create()
78+
GradleRunner runner = GradleRunner.create()
7879
.withGradleVersion(requestGradleForJre8and11("2.14"))
7980
.withProjectDir(rootFolder())
8081
.withPluginClasspath();
82+
if ("true".equals(System.getProperty(SpotlessPluginModern.SPOTLESS_MODERN))) {
83+
runner.withEnvironment(ImmutableMap.of("ORG_GRADLE_PROJECT_" + SpotlessPluginModern.SPOTLESS_MODERN, "true"));
84+
runner.withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE);
85+
}
86+
return runner;
8187
}
8288

8389
/** Dumps the complete file contents of the folder to the console. */

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.nio.charset.StandardCharsets;
2222

2323
import org.assertj.core.api.Assertions;
24-
import org.gradle.testkit.runner.GradleRunner;
2524
import org.junit.Before;
2625
import org.junit.Test;
2726

@@ -70,7 +69,8 @@ private void runWith(String... arguments) throws IOException {
7069
// gradle 2.14 -> 4.6 confirmed to work
7170
// gradle 4.7 -> 5.1 don't work in tooling API because of https://github.com/gradle/gradle/issues/7617
7271
// gradle 5.1 -> current confirmed to work
73-
GradleRunner.create()
72+
gradleRunner()
73+
.withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE)
7474
.withProjectDir(rootFolder())
7575
.withPluginClasspath()
7676
.withArguments(arguments)

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

+2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import org.gradle.testkit.runner.UnexpectedBuildFailure;
2222
import org.junit.Ignore;
2323
import org.junit.Test;
24+
import org.junit.experimental.categories.Category;
2425

2526
import com.diffplug.spotless.LineEnding;
2627

28+
@Category(ExcludeFromPluginGradleModern.class)
2729
public class SpecificFilesTest extends GradleIntegrationHarness {
2830

2931
private static String regexWinSafe(String input) {

0 commit comments

Comments
 (0)