Skip to content

Commit

Permalink
Fix reorderImports not working (#1872 fixes #1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Nov 2, 2023
2 parents e26a99a + 9ac5616 commit ffea281
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) {

public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
this.reorderImports = reorderImports;
replaceStep(createStep());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ void specificVersionReflowLongStrings() throws Exception {
runTest("java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test");
}

@Test
void specificVersionReorderImports() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.12.0</version>",
" <style>AOSP</style>",
" <reorderImports>true</reorderImports>",
"</googleJavaFormat>");

runTest("java/googlejavaformat/JavaWithReorderImportsEnabledFormatted.test", "java/googlejavaformat/JavaWithReorderImportsUnformatted.test");
}

@Test
void specificVersionSkipJavadocFormatting() throws Exception {
writePomWithJavaSteps(
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ffea281

Please sign in to comment.