diff --git a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java index b2424cbe2a..e5d990d807 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java @@ -58,7 +58,11 @@ public static FormatterStep create(String version, String style, Provisioner pro } public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) { - return create(groupArtifact, version, style, provisioner, reflowLongStrings, false, DEFAULT_FORMAT_JAVADOC); + return create(groupArtifact, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS); + } + + public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) { + return create(groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports, DEFAULT_FORMAT_JAVADOC); } /** 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 { } State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception { - this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS, DEFAULT_FORMAT_JAVADOC); + this(stepName, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS); + } + + State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception { + this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, reorderImports, DEFAULT_FORMAT_JAVADOC); } State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports, boolean formatJavadoc) throws Exception { diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 8e803eacf1..2d9788576a 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changes * Bump default `ktlint` version to latest `1.0.0` -> `1.0.1`. ([#1855](https://github.com/diffplug/spotless/pull/1855)) +### Fixed +* Fix `GoogleJavaFormatConfig.reorderImports` not working. ([#1872](https://github.com/diffplug/spotless/issues/1872)) ## [6.22.0] - 2023-09-28 ### Added diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index c576eb7da8..ca73508f27 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -211,6 +211,7 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) { public GoogleJavaFormatConfig reorderImports(boolean reorderImports) { this.reorderImports = reorderImports; + replaceStep(createStep()); return this; } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GoogleJavaFormatIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GoogleJavaFormatIntegrationTest.java index 42d9f4314a..b2ee012091 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GoogleJavaFormatIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GoogleJavaFormatIntegrationTest.java @@ -46,6 +46,32 @@ void integration() throws IOException { checkRunsThenUpToDate(); } + @Test + void integrationWithReorderImports() throws IOException { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "", + "spotless {", + " java {", + " target file('test.java')", + " googleJavaFormat('1.12.0').aosp().reorderImports(true)", + " }", + "}"); + + setFile("test.java").toResource("java/googlejavaformat/JavaWithReorderImportsUnformatted.test"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("test.java").sameAsResource("java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test"); + + checkRunsThenUpToDate(); + replace("build.gradle", + "googleJavaFormat('1.12.0')", + "googleJavaFormat()"); + checkRunsThenUpToDate(); + } + @Test void integrationWithSkipJavadocFormatting() throws IOException { setFile("build.gradle").toLines( diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java index 76b2c40fd3..40d8edfed2 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java @@ -52,6 +52,18 @@ void specificVersionReflowLongStrings() throws Exception { runTest("java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test"); } + @Test + void specificVersionReorderImports() throws Exception { + writePomWithJavaSteps( + "", + " 1.12.0", + " ", + " true", + ""); + + runTest("java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test", "java/googlejavaformat/JavaWithReorderImportsUnformatted.test"); + } + @Test void specificVersionSkipJavadocFormatting() throws Exception { writePomWithJavaSteps( @@ -64,8 +76,12 @@ void specificVersionSkipJavadocFormatting() throws Exception { } private void runTest(String targetResource) throws Exception { + runTest(targetResource, "java/googlejavaformat/JavaCodeUnformatted.test"); + } + + private void runTest(String targetResource, String sourceResource) throws Exception { String path = "src/main/java/test.java"; - setFile(path).toResource("java/googlejavaformat/JavaCodeUnformatted.test"); + setFile(path).toResource(sourceResource); mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource(targetResource); } diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsDisabledFormatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsDisabledFormatted.test new file mode 100644 index 0000000000..5e62d24108 --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsDisabledFormatted.test @@ -0,0 +1,11 @@ +import java.nio.file.Paths; +import my.UsedB; +import org.xml.sax.InputSource; + +public class Java { + public static void main(String[] args) { + UsedB b = new UsedB(); + InputSource inputSource = new InputSource(); + Paths.get("dir"); + } +} diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test new file mode 100644 index 0000000000..d0d4951601 --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test @@ -0,0 +1,13 @@ +import my.UsedB; + +import org.xml.sax.InputSource; + +import java.nio.file.Paths; + +public class Java { + public static void main(String[] args) { + UsedB b = new UsedB(); + InputSource inputSource = new InputSource(); + Paths.get("dir"); + } +} diff --git a/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsUnformatted.test b/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsUnformatted.test new file mode 100644 index 0000000000..bfdbe83d88 --- /dev/null +++ b/testlib/src/main/resources/java/googlejavaformat/JavaWithReorderImportsUnformatted.test @@ -0,0 +1,11 @@ +import my.UsedB; +import java.nio.file.Paths; +import org.xml.sax.InputSource; + +public class Java { + public static void main(String[] args) { + UsedB b = new UsedB(); + InputSource inputSource = new InputSource(); + Paths.get("dir"); + } +} \ No newline at end of file diff --git a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java index df08c483df..f8766d7c6f 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/GoogleJavaFormatStepTest.java @@ -119,6 +119,19 @@ void behaviorWithCustomGroupArtifact() throws Exception { .testResource("java/googlejavaformat/JavaCodeWithPackageUnformatted.test", "java/googlejavaformat/JavaCodeWithPackageFormatted.test"); } + @Test + void behaviorWithReorderImports() throws Exception { + FormatterStep enabled = GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), GoogleJavaFormatStep.defaultVersion(), "AOSP", TestProvisioner.mavenCentral(), GoogleJavaFormatStep.defaultReflowLongStrings(), true); + FormatterStep disabled = GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), GoogleJavaFormatStep.defaultVersion(), "AOSP", TestProvisioner.mavenCentral(), GoogleJavaFormatStep.defaultReflowLongStrings(), false); + String unformatted = "java/googlejavaformat/JavaWithReorderImportsUnformatted.test"; + try (StepHarness step = StepHarness.forStep(enabled)) { + step.testResource(unformatted, "java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test"); + } + try (StepHarness step = StepHarness.forStep(disabled)) { + step.testResource(unformatted, "java/googlejavaformat/JavaWithReorderImportsDisabledFormatted.test"); + } + } + @Test void equality() throws Exception { new SerializableEqualityTester() {