From 950590689118a943537508f8bfc526f67539f147 Mon Sep 17 00:00:00 2001 From: Mark Ryan Date: Wed, 16 Oct 2019 15:29:55 -0400 Subject: [PATCH 1/5] Stop crashing on empty file --- .../main/java/com/diffplug/spotless/java/ImportSorter.java | 2 +- .../main/resources/java/importsorter/JavaCodeEmptyFile.test | 0 .../com/diffplug/spotless/java/ImportOrderStepTest.java | 6 ++++++ 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 testlib/src/main/resources/java/importsorter/JavaCodeEmptyFile.test diff --git a/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java b/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java index 3c532182b5..e997a34e0a 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java +++ b/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java @@ -114,7 +114,7 @@ private static String applyImportsToDocument(final String document, int firstImp } } scanner.close(); - if (!document.endsWith("\n")) { + if (!document.endsWith("\n") && sb.length() > 0) { sb.setLength(sb.length() - 1); } return sb.toString(); diff --git a/testlib/src/main/resources/java/importsorter/JavaCodeEmptyFile.test b/testlib/src/main/resources/java/importsorter/JavaCodeEmptyFile.test new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java index 243c951f55..845aea31da 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java @@ -59,6 +59,12 @@ public void misplacedImports() throws Throwable { assertOnResources(step, "java/importsorter/JavaCodeUnsortedMisplacedImports.test", "java/importsorter/JavaCodeSortedMisplacedImports.test"); } + @Test + public void empty() throws Throwable { + FormatterStep step = ImportOrderStep.forJava().createFrom(createTestFile("java/importsorter/import.properties")); + assertOnResources(step, "java/importsorter/JavaCodeEmptyFile.test", "java/importsorter/JavaCodeEmptyFile.test"); + } + @Test public void groovyImports() throws Throwable { FormatterStep step = ImportOrderStep.forGroovy().createFrom(createTestFile("java/importsorter/import.properties")); From 2d8dceb9705a8471fa69178e4db9db41ea1e6060 Mon Sep 17 00:00:00 2001 From: MarkRyanDev Date: Wed, 16 Oct 2019 15:40:00 -0400 Subject: [PATCH 2/5] Update CHANGES.md --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index fc7c59fab0..cb27fd978b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,9 @@ You might be looking for: ### Version 1.26.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/)) +* Fix `ImportSorter` crashing with empty files. + * Fixes [#305](https://github.com/diffplug/spotless/issues/305) StringIndexOutOfBoundsException for empty Groovy file when performing importOrder + ### Version 1.25.0 - October 6th 2019 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.25.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.25.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra))) * Add support for ktlint `0.34+`, and bump default version from `0.32.0` to `0.34.2`. ([#469](https://github.com/diffplug/spotless/pull/469)) From 45f19592edf228de52436f57b7275820714482ba Mon Sep 17 00:00:00 2001 From: MarkRyanDev Date: Wed, 16 Oct 2019 15:40:28 -0400 Subject: [PATCH 3/5] Update CHANGES.md --- plugin-gradle/CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 12449abc2a..67e5a34089 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -2,6 +2,9 @@ ### Version 3.26.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/)) +* Fix `ImportSorter` crashing with empty files. + * Fixes [#305](https://github.com/diffplug/spotless/issues/305) StringIndexOutOfBoundsException for empty Groovy file when performing importOrder + ### Version 3.25.0 - October 6th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.25.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.25.0)) * Spotless no longer breaks configuration avoidance for other tasks (specifically the `check` task and all of its dependees) ([#463](https://github.com/diffplug/spotless/pull/463)). From 05cfb57fab947e76aed262c9ca104533d9fe6d32 Mon Sep 17 00:00:00 2001 From: MarkRyanDev Date: Wed, 16 Oct 2019 15:40:57 -0400 Subject: [PATCH 4/5] Update CHANGES.md --- plugin-maven/CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 447ac18d70..50965b2a7f 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -2,6 +2,9 @@ ### Version 1.26.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/)) +* Fix `ImportSorter` crashing with empty files. + * Fixes [#305](https://github.com/diffplug/spotless/issues/305) StringIndexOutOfBoundsException for empty Groovy file when performing importOrder + ### Version 1.25.1 - October 7th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.25.1/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.25.1)) * Fixed problem which could cause a stale `.jar` to be published. ([#471](https://github.com/diffplug/spotless/pull/471)) From 03dd6b6279d8dfb2f4068d8a77bc2707f1e901db Mon Sep 17 00:00:00 2001 From: Mark Ryan Date: Wed, 16 Oct 2019 16:06:19 -0400 Subject: [PATCH 5/5] implement feedback --- CHANGES.md | 2 +- .../main/java/com/diffplug/spotless/java/ImportSorter.java | 5 ++++- plugin-gradle/CHANGES.md | 2 +- plugin-maven/CHANGES.md | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index cb27fd978b..92cd0de430 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,7 +7,7 @@ You might be looking for: ### Version 1.26.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/)) -* Fix `ImportSorter` crashing with empty files. +* Fix `ImportSorter` crashing with empty files. ([#474](https://github.com/diffplug/spotless/pull/474)) * Fixes [#305](https://github.com/diffplug/spotless/issues/305) StringIndexOutOfBoundsException for empty Groovy file when performing importOrder ### Version 1.25.0 - October 6th 2019 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.25.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.25.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra))) diff --git a/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java b/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java index e997a34e0a..b522f748cc 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java +++ b/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java @@ -92,6 +92,9 @@ private static boolean isBeginningOfScope(String line) { } private static String applyImportsToDocument(final String document, int firstImportLine, int lastImportLine, List strings) { + if (document.isEmpty()) { + return document; + } boolean importsAlreadyAppended = false; Scanner scanner = new Scanner(document); int curentLine = 0; @@ -114,7 +117,7 @@ private static String applyImportsToDocument(final String document, int firstImp } } scanner.close(); - if (!document.endsWith("\n") && sb.length() > 0) { + if (!document.endsWith("\n")) { sb.setLength(sb.length() - 1); } return sb.toString(); diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 67e5a34089..2a5185aec3 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -2,7 +2,7 @@ ### Version 3.26.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/)) -* Fix `ImportSorter` crashing with empty files. +* Fix `ImportSorter` crashing with empty files. ([#474](https://github.com/diffplug/spotless/pull/474)) * Fixes [#305](https://github.com/diffplug/spotless/issues/305) StringIndexOutOfBoundsException for empty Groovy file when performing importOrder ### Version 3.25.0 - October 6th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.25.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.25.0)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 50965b2a7f..bd84ee469e 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -2,7 +2,7 @@ ### Version 1.26.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/)) -* Fix `ImportSorter` crashing with empty files. +* Fix `ImportSorter` crashing with empty files. ([#474](https://github.com/diffplug/spotless/pull/474)) * Fixes [#305](https://github.com/diffplug/spotless/issues/305) StringIndexOutOfBoundsException for empty Groovy file when performing importOrder ### Version 1.25.1 - October 7th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.25.1/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.25.1))