Skip to content

Commit ffea281

Browse files
authored
Fix reorderImports not working (#1872 fixes #1871)
2 parents e26a99a + 9ac5616 commit ffea281

File tree

9 files changed

+104
-3
lines changed

9 files changed

+104
-3
lines changed

lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public static FormatterStep create(String version, String style, Provisioner pro
5858
}
5959

6060
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
61-
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false, DEFAULT_FORMAT_JAVADOC);
61+
return create(groupArtifact, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS);
62+
}
63+
64+
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) {
65+
return create(groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports, DEFAULT_FORMAT_JAVADOC);
6266
}
6367

6468
/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
@@ -127,7 +131,11 @@ static final class State implements Serializable {
127131
}
128132

129133
State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
130-
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS, DEFAULT_FORMAT_JAVADOC);
134+
this(stepName, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS);
135+
}
136+
137+
State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception {
138+
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, reorderImports, DEFAULT_FORMAT_JAVADOC);
131139
}
132140

133141
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports, boolean formatJavadoc) throws Exception {

plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changes
77
* Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855))
8+
### Fixed
9+
* Fix `GoogleJavaFormatConfig.reorderImports` not working. ([#1872](https://github.com/diffplug/spotless/issues/1872))
810

911
## [6.22.0] - 2023-09-28
1012
### Added

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

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) {
211211

212212
public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
213213
this.reorderImports = reorderImports;
214+
replaceStep(createStep());
214215
return this;
215216
}
216217

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

+26
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ void integration() throws IOException {
4646
checkRunsThenUpToDate();
4747
}
4848

49+
@Test
50+
void integrationWithReorderImports() throws IOException {
51+
setFile("build.gradle").toLines(
52+
"plugins {",
53+
" id 'com.diffplug.spotless'",
54+
"}",
55+
"repositories { mavenCentral() }",
56+
"",
57+
"spotless {",
58+
" java {",
59+
" target file('test.java')",
60+
" googleJavaFormat('1.12.0').aosp().reorderImports(true)",
61+
" }",
62+
"}");
63+
64+
setFile("test.java").toResource("java/googlejavaformat/JavaWithReorderImportsUnformatted.test");
65+
gradleRunner().withArguments("spotlessApply").build();
66+
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test");
67+
68+
checkRunsThenUpToDate();
69+
replace("build.gradle",
70+
"googleJavaFormat('1.12.0')",
71+
"googleJavaFormat()");
72+
checkRunsThenUpToDate();
73+
}
74+
4975
@Test
5076
void integrationWithSkipJavadocFormatting() throws IOException {
5177
setFile("build.gradle").toLines(

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ void specificVersionReflowLongStrings() throws Exception {
5252
runTest("java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test");
5353
}
5454

55+
@Test
56+
void specificVersionReorderImports() throws Exception {
57+
writePomWithJavaSteps(
58+
"<googleJavaFormat>",
59+
" <version>1.12.0</version>",
60+
" <style>AOSP</style>",
61+
" <reorderImports>true</reorderImports>",
62+
"</googleJavaFormat>");
63+
64+
runTest("java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test", "java/googlejavaformat/JavaWithReorderImportsUnformatted.test");
65+
}
66+
5567
@Test
5668
void specificVersionSkipJavadocFormatting() throws Exception {
5769
writePomWithJavaSteps(
@@ -64,8 +76,12 @@ void specificVersionSkipJavadocFormatting() throws Exception {
6476
}
6577

6678
private void runTest(String targetResource) throws Exception {
79+
runTest(targetResource, "java/googlejavaformat/JavaCodeUnformatted.test");
80+
}
81+
82+
private void runTest(String targetResource, String sourceResource) throws Exception {
6783
String path = "src/main/java/test.java";
68-
setFile(path).toResource("java/googlejavaformat/JavaCodeUnformatted.test");
84+
setFile(path).toResource(sourceResource);
6985
mavenRunner().withArguments("spotless:apply").runNoError();
7086
assertFile(path).sameAsResource(targetResource);
7187
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import java.nio.file.Paths;
2+
import my.UsedB;
3+
import org.xml.sax.InputSource;
4+
5+
public class Java {
6+
public static void main(String[] args) {
7+
UsedB b = new UsedB();
8+
InputSource inputSource = new InputSource();
9+
Paths.get("dir");
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import my.UsedB;
2+
3+
import org.xml.sax.InputSource;
4+
5+
import java.nio.file.Paths;
6+
7+
public class Java {
8+
public static void main(String[] args) {
9+
UsedB b = new UsedB();
10+
InputSource inputSource = new InputSource();
11+
Paths.get("dir");
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import my.UsedB;
2+
import java.nio.file.Paths;
3+
import org.xml.sax.InputSource;
4+
5+
public class Java {
6+
public static void main(String[] args) {
7+
UsedB b = new UsedB();
8+
InputSource inputSource = new InputSource();
9+
Paths.get("dir");
10+
}
11+
}

testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ void behaviorWithCustomGroupArtifact() throws Exception {
119119
.testResource("java/googlejavaformat/JavaCodeWithPackageUnformatted.test", "java/googlejavaformat/JavaCodeWithPackageFormatted.test");
120120
}
121121

122+
@Test
123+
void behaviorWithReorderImports() throws Exception {
124+
FormatterStep enabled = GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), GoogleJavaFormatStep.defaultVersion(), "AOSP", TestProvisioner.mavenCentral(), GoogleJavaFormatStep.defaultReflowLongStrings(), true);
125+
FormatterStep disabled = GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), GoogleJavaFormatStep.defaultVersion(), "AOSP", TestProvisioner.mavenCentral(), GoogleJavaFormatStep.defaultReflowLongStrings(), false);
126+
String unformatted = "java/googlejavaformat/JavaWithReorderImportsUnformatted.test";
127+
try (StepHarness step = StepHarness.forStep(enabled)) {
128+
step.testResource(unformatted, "java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test");
129+
}
130+
try (StepHarness step = StepHarness.forStep(disabled)) {
131+
step.testResource(unformatted, "java/googlejavaformat/JavaWithReorderImportsDisabledFormatted.test");
132+
}
133+
}
134+
122135
@Test
123136
void equality() throws Exception {
124137
new SerializableEqualityTester() {

0 commit comments

Comments
 (0)