From cd2ff0f345f854f44a87f0944f400c98031b5f8c Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Mon, 27 Feb 2023 10:26:37 +0400 Subject: [PATCH 01/26] Less errors in equoIde --- .../maven/java/CleanthatJavaRefactorerTest.java | 12 ++++++------ .../cleanthat/LiteralsFirstInComparisons.clean.java | 8 -------- .../cleanthat/LiteralsFirstInComparisons.dirty.java | 8 -------- .../cleanthat/LiteralsFirstInComparisons_clean.java | 8 ++++++++ .../cleanthat/LiteralsFirstInComparisons_dirty.java | 8 ++++++++ ...tators.clean.java => MultipleMutators_clean.java} | 4 ++-- ...=> MultipleMutators_clean_onlyLiteralsFirst.java} | 4 ++-- ...ultipleMutators_clean_onlyOptionalIsPresent.java} | 4 ++-- ...tators.dirty.java => MultipleMutators_dirty.java} | 4 ++-- 9 files changed, 30 insertions(+), 30 deletions(-) delete mode 100644 testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java delete mode 100644 testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java create mode 100644 testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_clean.java create mode 100644 testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_dirty.java rename testlib/src/main/resources/java/cleanthat/{MultipleMutators.clean.java => MultipleMutators_clean.java} (62%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators.clean.onlyLiteralsFirst.java => MultipleMutators_clean_onlyLiteralsFirst.java} (61%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators.clean.onlyOptionalIsPresent.java => MultipleMutators_clean_onlyOptionalIsPresent.java} (62%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators.dirty.java => MultipleMutators_dirty.java} (61%) diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java index 379397f55b..67a4c2efb2 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java @@ -33,7 +33,7 @@ void testEnableDraft() throws Exception { " true", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyOptionalIsPresent.java"); + runTest("MultipleMutators_dirty.java", "MultipleMutators_clean_onlyOptionalIsPresent.java"); } @Test @@ -45,7 +45,7 @@ void testLiteralsFirstInComparisons() throws Exception { " ", ""); - runTest("LiteralsFirstInComparisons.dirty.java", "LiteralsFirstInComparisons.clean.java"); + runTest("LiteralsFirstInComparisons_dirty.java", "LiteralsFirstInComparisons_clean.java"); } @Test @@ -59,7 +59,7 @@ void testMultipleMutators_defaultIsJdk7() throws Exception { " ", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); + runTest("MultipleMutators_dirty.java", "MultipleMutators_clean_onlyLiteralsFirst.java"); } @Test @@ -73,7 +73,7 @@ void testMultipleMutators_Jdk11IntroducedOptionalisPresent() throws Exception { " ", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.java"); + runTest("MultipleMutators_dirty.java", "MultipleMutators_clean.java"); } @Test @@ -89,7 +89,7 @@ void testExcludeOptionalNotEmpty() throws Exception { " ", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); + runTest("MultipleMutators_dirty.java", "MultipleMutators_clean_onlyLiteralsFirst.java"); } @Test @@ -101,7 +101,7 @@ void testIncludeOnlyLiteralsFirstInComparisons() throws Exception { " ", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); + runTest("MultipleMutators_dirty.java", "MultipleMutators_clean_onlyLiteralsFirst.java"); } private void runTest(String dirtyPath, String cleanPath) throws Exception { diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java deleted file mode 100644 index 8bacfa58db..0000000000 --- a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java +++ /dev/null @@ -1,8 +0,0 @@ -package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; - -public class LiteralsFirstInComparisonsCases { - - public boolean isHardcoded(String input) { - return "hardcoded".equals(input); - } -} diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java deleted file mode 100644 index 3a1e074c91..0000000000 --- a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java +++ /dev/null @@ -1,8 +0,0 @@ -package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; - -public class LiteralsFirstInComparisonsCases { - - public boolean isHardcoded(String input) { - return input.equals("hardcoded"); - } -} diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_clean.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_clean.java new file mode 100644 index 0000000000..07807a6721 --- /dev/null +++ b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_clean.java @@ -0,0 +1,8 @@ +package java.cleanthat; + +public class LiteralsFirstInComparisons_clean { + + public boolean isHardcoded(String input) { + return "hardcoded".equals(input); + } +} diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_dirty.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_dirty.java new file mode 100644 index 0000000000..249b8da8b3 --- /dev/null +++ b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_dirty.java @@ -0,0 +1,8 @@ +package java.cleanthat; + +public class LiteralsFirstInComparisons_dirty { + + public boolean isHardcoded(String input) { + return input.equals("hardcoded"); + } +} diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean.java similarity index 62% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators_clean.java index 318e1efa15..76837b6893 100644 --- a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java +++ b/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean.java @@ -1,8 +1,8 @@ -package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; +package java.cleanthat; import java.util.Optional; -public class LiteralsFirstInComparisonsCases { +public class MultipleMutators_clean { public boolean isHardcoded(String input) { return "hardcoded".equals(input); diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyLiteralsFirst.java similarity index 61% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyLiteralsFirst.java index 629d24504b..d9fb87dd80 100644 --- a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.java +++ b/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyLiteralsFirst.java @@ -1,8 +1,8 @@ -package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; +package java.cleanthat; import java.util.Optional; -public class LiteralsFirstInComparisonsCases { +public class MultipleMutators_clean_onlyLiteralsFirst { public boolean isHardcoded(String input) { return "hardcoded".equals(input); diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyOptionalIsPresent.java similarity index 62% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyOptionalIsPresent.java index 0829602dc1..54600ebc1c 100644 --- a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.java +++ b/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyOptionalIsPresent.java @@ -1,8 +1,8 @@ -package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; +package java.cleanthat; import java.util.Optional; -public class LiteralsFirstInComparisonsCases { +public class MultipleMutators_clean_onlyOptionalIsPresent { public boolean isHardcoded(String input) { return input.equals("hardcoded"); diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators_dirty.java similarity index 61% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators_dirty.java index 8ac230cabc..e71b810e2f 100644 --- a/testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.java +++ b/testlib/src/main/resources/java/cleanthat/MultipleMutators_dirty.java @@ -1,8 +1,8 @@ -package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; +package java.cleanthat; import java.util.Optional; -public class LiteralsFirstInComparisonsCases { +public class MultipleMutators_dirty { public boolean isHardcoded(String input) { return input.equals("hardcoded"); From a601efc10714797a0324ed280f0578176752b84b Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 27 Feb 2023 15:05:20 -0800 Subject: [PATCH 02/26] Revert "Less errors in equoIde" This reverts commit cd2ff0f345f854f44a87f0944f400c98031b5f8c. --- .../maven/java/CleanthatJavaRefactorerTest.java | 12 ++++++------ .../cleanthat/LiteralsFirstInComparisons.clean.java | 8 ++++++++ .../cleanthat/LiteralsFirstInComparisons.dirty.java | 8 ++++++++ .../cleanthat/LiteralsFirstInComparisons_clean.java | 8 -------- .../cleanthat/LiteralsFirstInComparisons_dirty.java | 8 -------- ...tators_clean.java => MultipleMutators.clean.java} | 4 ++-- ...=> MultipleMutators.clean.onlyLiteralsFirst.java} | 4 ++-- ...ultipleMutators.clean.onlyOptionalIsPresent.java} | 4 ++-- ...tators_dirty.java => MultipleMutators.dirty.java} | 4 ++-- 9 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java create mode 100644 testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java delete mode 100644 testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_clean.java delete mode 100644 testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_dirty.java rename testlib/src/main/resources/java/cleanthat/{MultipleMutators_clean.java => MultipleMutators.clean.java} (62%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators_clean_onlyLiteralsFirst.java => MultipleMutators.clean.onlyLiteralsFirst.java} (61%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators_clean_onlyOptionalIsPresent.java => MultipleMutators.clean.onlyOptionalIsPresent.java} (62%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators_dirty.java => MultipleMutators.dirty.java} (61%) diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java index 67a4c2efb2..379397f55b 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java @@ -33,7 +33,7 @@ void testEnableDraft() throws Exception { " true", ""); - runTest("MultipleMutators_dirty.java", "MultipleMutators_clean_onlyOptionalIsPresent.java"); + runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyOptionalIsPresent.java"); } @Test @@ -45,7 +45,7 @@ void testLiteralsFirstInComparisons() throws Exception { " ", ""); - runTest("LiteralsFirstInComparisons_dirty.java", "LiteralsFirstInComparisons_clean.java"); + runTest("LiteralsFirstInComparisons.dirty.java", "LiteralsFirstInComparisons.clean.java"); } @Test @@ -59,7 +59,7 @@ void testMultipleMutators_defaultIsJdk7() throws Exception { " ", ""); - runTest("MultipleMutators_dirty.java", "MultipleMutators_clean_onlyLiteralsFirst.java"); + runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); } @Test @@ -73,7 +73,7 @@ void testMultipleMutators_Jdk11IntroducedOptionalisPresent() throws Exception { " ", ""); - runTest("MultipleMutators_dirty.java", "MultipleMutators_clean.java"); + runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.java"); } @Test @@ -89,7 +89,7 @@ void testExcludeOptionalNotEmpty() throws Exception { " ", ""); - runTest("MultipleMutators_dirty.java", "MultipleMutators_clean_onlyLiteralsFirst.java"); + runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); } @Test @@ -101,7 +101,7 @@ void testIncludeOnlyLiteralsFirstInComparisons() throws Exception { " ", ""); - runTest("MultipleMutators_dirty.java", "MultipleMutators_clean_onlyLiteralsFirst.java"); + runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); } private void runTest(String dirtyPath, String cleanPath) throws Exception { diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java new file mode 100644 index 0000000000..8bacfa58db --- /dev/null +++ b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java @@ -0,0 +1,8 @@ +package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; + +public class LiteralsFirstInComparisonsCases { + + public boolean isHardcoded(String input) { + return "hardcoded".equals(input); + } +} diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java new file mode 100644 index 0000000000..3a1e074c91 --- /dev/null +++ b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java @@ -0,0 +1,8 @@ +package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; + +public class LiteralsFirstInComparisonsCases { + + public boolean isHardcoded(String input) { + return input.equals("hardcoded"); + } +} diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_clean.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_clean.java deleted file mode 100644 index 07807a6721..0000000000 --- a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_clean.java +++ /dev/null @@ -1,8 +0,0 @@ -package java.cleanthat; - -public class LiteralsFirstInComparisons_clean { - - public boolean isHardcoded(String input) { - return "hardcoded".equals(input); - } -} diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_dirty.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_dirty.java deleted file mode 100644 index 249b8da8b3..0000000000 --- a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons_dirty.java +++ /dev/null @@ -1,8 +0,0 @@ -package java.cleanthat; - -public class LiteralsFirstInComparisons_dirty { - - public boolean isHardcoded(String input) { - return input.equals("hardcoded"); - } -} diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java similarity index 62% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators_clean.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java index 76837b6893..318e1efa15 100644 --- a/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean.java +++ b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java @@ -1,8 +1,8 @@ -package java.cleanthat; +package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; import java.util.Optional; -public class MultipleMutators_clean { +public class LiteralsFirstInComparisonsCases { public boolean isHardcoded(String input) { return "hardcoded".equals(input); diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyLiteralsFirst.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.java similarity index 61% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyLiteralsFirst.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.java index d9fb87dd80..629d24504b 100644 --- a/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyLiteralsFirst.java +++ b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.java @@ -1,8 +1,8 @@ -package java.cleanthat; +package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; import java.util.Optional; -public class MultipleMutators_clean_onlyLiteralsFirst { +public class LiteralsFirstInComparisonsCases { public boolean isHardcoded(String input) { return "hardcoded".equals(input); diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyOptionalIsPresent.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.java similarity index 62% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyOptionalIsPresent.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.java index 54600ebc1c..0829602dc1 100644 --- a/testlib/src/main/resources/java/cleanthat/MultipleMutators_clean_onlyOptionalIsPresent.java +++ b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.java @@ -1,8 +1,8 @@ -package java.cleanthat; +package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; import java.util.Optional; -public class MultipleMutators_clean_onlyOptionalIsPresent { +public class LiteralsFirstInComparisonsCases { public boolean isHardcoded(String input) { return input.equals("hardcoded"); diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators_dirty.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.java similarity index 61% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators_dirty.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.java index e71b810e2f..8ac230cabc 100644 --- a/testlib/src/main/resources/java/cleanthat/MultipleMutators_dirty.java +++ b/testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.java @@ -1,8 +1,8 @@ -package java.cleanthat; +package eu.solven.cleanthat.engine.java.refactorer.cases.do_not_format_me; import java.util.Optional; -public class MultipleMutators_dirty { +public class LiteralsFirstInComparisonsCases { public boolean isHardcoded(String input) { return input.equals("hardcoded"); From 5560214545a3d2655298bcb50691c1bf9325dc7f Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 27 Feb 2023 15:08:12 -0800 Subject: [PATCH 03/26] Rename `.java` to `.test` to match other java formatters. --- .../spotless/CleanthatJavaIntegrationTest.java | 4 ++-- .../maven/java/CleanthatJavaRefactorerTest.java | 14 +++++++------- ....java => LiteralsFirstInComparisons.clean.test} | 0 ....java => LiteralsFirstInComparisons.dirty.test} | 0 ... MultipleMutators.clean.onlyLiteralsFirst.test} | 0 ...tipleMutators.clean.onlyOptionalIsPresent.test} | 0 ...tors.clean.java => MultipleMutators.clean.test} | 0 ...tors.dirty.java => MultipleMutators.dirty.test} | 0 8 files changed, 9 insertions(+), 9 deletions(-) rename testlib/src/main/resources/java/cleanthat/{LiteralsFirstInComparisons.clean.java => LiteralsFirstInComparisons.clean.test} (100%) rename testlib/src/main/resources/java/cleanthat/{LiteralsFirstInComparisons.dirty.java => LiteralsFirstInComparisons.dirty.test} (100%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators.clean.onlyLiteralsFirst.java => MultipleMutators.clean.onlyLiteralsFirst.test} (100%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators.clean.onlyOptionalIsPresent.java => MultipleMutators.clean.onlyOptionalIsPresent.test} (100%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators.clean.java => MultipleMutators.clean.test} (100%) rename testlib/src/main/resources/java/cleanthat/{MultipleMutators.dirty.java => MultipleMutators.dirty.test} (100%) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/CleanthatJavaIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/CleanthatJavaIntegrationTest.java index 581bfe89b2..ad6fff9112 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/CleanthatJavaIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/CleanthatJavaIntegrationTest.java @@ -37,8 +37,8 @@ void integration() throws IOException { " }", "}"); - setFile("test.java").toResource("java/cleanthat/MultipleMutators.dirty.java"); + setFile("test.java").toResource("java/cleanthat/MultipleMutators.dirty.test"); gradleRunner().withArguments("spotlessApply").build(); - assertFile("test.java").sameAsResource("java/cleanthat/MultipleMutators.clean.java"); + assertFile("test.java").sameAsResource("java/cleanthat/MultipleMutators.clean.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java index 379397f55b..738624cd57 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/CleanthatJavaRefactorerTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.diffplug.spotless.maven.java; +package com.diffplug.spotless.maven.test; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; @@ -33,7 +33,7 @@ void testEnableDraft() throws Exception { " true", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyOptionalIsPresent.java"); + runTest("MultipleMutators.dirty.test", "MultipleMutators.clean.onlyOptionalIsPresent.test"); } @Test @@ -45,7 +45,7 @@ void testLiteralsFirstInComparisons() throws Exception { " ", ""); - runTest("LiteralsFirstInComparisons.dirty.java", "LiteralsFirstInComparisons.clean.java"); + runTest("LiteralsFirstInComparisons.dirty.test", "LiteralsFirstInComparisons.clean.test"); } @Test @@ -59,7 +59,7 @@ void testMultipleMutators_defaultIsJdk7() throws Exception { " ", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); + runTest("MultipleMutators.dirty.test", "MultipleMutators.clean.onlyLiteralsFirst.test"); } @Test @@ -73,7 +73,7 @@ void testMultipleMutators_Jdk11IntroducedOptionalisPresent() throws Exception { " ", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.java"); + runTest("MultipleMutators.dirty.test", "MultipleMutators.clean.test"); } @Test @@ -89,7 +89,7 @@ void testExcludeOptionalNotEmpty() throws Exception { " ", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); + runTest("MultipleMutators.dirty.test", "MultipleMutators.clean.onlyLiteralsFirst.test"); } @Test @@ -101,7 +101,7 @@ void testIncludeOnlyLiteralsFirstInComparisons() throws Exception { " ", ""); - runTest("MultipleMutators.dirty.java", "MultipleMutators.clean.onlyLiteralsFirst.java"); + runTest("MultipleMutators.dirty.test", "MultipleMutators.clean.onlyLiteralsFirst.test"); } private void runTest(String dirtyPath, String cleanPath) throws Exception { diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.test similarity index 100% rename from testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.java rename to testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.clean.test diff --git a/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java b/testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.test similarity index 100% rename from testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.java rename to testlib/src/main/resources/java/cleanthat/LiteralsFirstInComparisons.dirty.test diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.test similarity index 100% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyLiteralsFirst.test diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.test similarity index 100% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.onlyOptionalIsPresent.test diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.test similarity index 100% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators.clean.test diff --git a/testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.java b/testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.test similarity index 100% rename from testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.java rename to testlib/src/main/resources/java/cleanthat/MultipleMutators.dirty.test From 5a22a9097e54db081ff6cdc6af4ec1496a7a4f99 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 28 Feb 2023 05:26:08 +0000 Subject: [PATCH 04/26] chore(deps): update plugin com.diffplug.spotless to v6.16.0 --- settings.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.gradle b/settings.gradle index 17a9679abd..1478f5af31 100644 --- a/settings.gradle +++ b/settings.gradle @@ -6,7 +6,7 @@ pluginManagement { } plugins { - id 'com.diffplug.spotless' version '6.15.0' apply false + id 'com.diffplug.spotless' version '6.16.0' apply false // https://plugins.gradle.org/plugin/com.gradle.plugin-publish id 'com.gradle.plugin-publish' version '1.1.0' apply false // https://github.com/gradle-nexus/publish-plugin/releases From 771d5e30f834f3389dcafbcf72ab9dc0b9094f58 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 28 Feb 2023 17:24:03 +0800 Subject: [PATCH 05/26] Run with build cache by default Follow up a9ad736a7b73d0edcbec17603091af0de4bdbf6d. --- .github/workflows/ci.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2c372f1668..2a8078339d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,9 +33,9 @@ jobs: with: gradle-home-cache-cleanup: true - name: spotlessCheck - run: ./gradlew spotlessCheck --build-cache + run: ./gradlew spotlessCheck - name: assemble testClasses - run: ./gradlew assemble testClasses --build-cache + run: ./gradlew assemble testClasses build: needs: sanityCheck strategy: @@ -71,13 +71,13 @@ jobs: gradle-home-cache-cleanup: true - name: build (maven-only) if: matrix.kind == 'maven' - run: ./gradlew :plugin-maven:build -x spotlessCheck --build-cache + run: ./gradlew :plugin-maven:build -x spotlessCheck - name: build (everything-but-maven) if: matrix.kind == 'gradle' - run: ./gradlew build -x spotlessCheck --build-cache -PSPOTLESS_EXCLUDE_MAVEN=true + run: ./gradlew build -x spotlessCheck -PSPOTLESS_EXCLUDE_MAVEN=true - name: test npm if: matrix.kind == 'npm' - run: ./gradlew testNpm --build-cache + run: ./gradlew testNpm - name: junit result uses: mikepenz/action-junit-report@v3 if: always() # always run even if the previous step fails From 0f7293ce8700e87d1ecc952f50e6c6a371005e9d Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 2 Mar 2023 11:56:26 +0000 Subject: [PATCH 06/26] chore(deps): update plugin com.gradle.enterprise to v3.12.4 --- settings.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.gradle b/settings.gradle index 1478f5af31..fb0c52fb35 100644 --- a/settings.gradle +++ b/settings.gradle @@ -23,7 +23,7 @@ plugins { // https://github.com/davidburstrom/version-compatibility-gradle-plugin/tags id 'io.github.davidburstrom.version-compatibility' version '0.4.0' apply false // https://plugins.gradle.org/plugin/com.gradle.enterprise - id 'com.gradle.enterprise' version '3.12.3' + id 'com.gradle.enterprise' version '3.12.4' } dependencyResolutionManagement { From 1913e1c0b549bd382a82f7a42647be87cdc092f3 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 4 Mar 2023 17:06:30 +0900 Subject: [PATCH 07/26] Support a file name field in license headers --- .../spotless/generic/LicenseHeaderStep.java | 24 ++++++++++----- .../com/diffplug/spotless/StepHarness.java | 14 +++++++-- .../generic/LicenseHeaderStepTest.java | 30 +++++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index b4c0cf2a6f..624ea16cc6 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -142,7 +142,7 @@ public FormatterStep build() { throw new IllegalStateException(yearMode.toString()); } return new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear, skipLinesMatching); - }, step -> step::format); + }, step -> FormatterFunc.needsFile(step::format)); } if (contentPattern == null) { @@ -214,6 +214,8 @@ private static class Runtime implements Serializable { private final boolean updateYearWithLatest; private final boolean licenseHeaderWithRange; + private static final Pattern FILENAME_PATTERN = Pattern.compile("\\$FILE"); + /** The license that we'd like enforced. */ private Runtime(String licenseHeader, String delimiter, String yearSeparator, boolean updateYearWithLatest, @Nullable String skipLinesMatching) { if (delimiter.contains("\n")) { @@ -266,9 +268,9 @@ private static Optional getYearToken(String licenseHeader) { } /** Formats the given string. */ - private String format(String raw) { + private String format(String raw, File file) { if (skipLinesMatching == null) { - return addOrUpdateLicenseHeader(raw); + return addOrUpdateLicenseHeader(raw, file); } else { String[] lines = raw.split("\n"); StringBuilder skippedLinesBuilder = new StringBuilder(); @@ -287,11 +289,11 @@ private String format(String raw) { remainingLinesBuilder.append(line).append('\n'); } } - return skippedLinesBuilder + addOrUpdateLicenseHeader(remainingLinesBuilder.toString()); + return skippedLinesBuilder + addOrUpdateLicenseHeader(remainingLinesBuilder.toString(), file); } } - private String addOrUpdateLicenseHeader(String raw) { + private String addOrUpdateLicenseHeader(String raw, File file) { Matcher contentMatcher = delimiterPattern.matcher(raw); if (!contentMatcher.find()) { throw new IllegalArgumentException("Unable to find delimiter regex " + delimiterPattern); @@ -305,13 +307,14 @@ private String addOrUpdateLicenseHeader(String raw) { return raw; } else { // otherwise we'll have to add the header - return yearSepOrFull + content; + return replaceFileName(yearSepOrFull, file) + content; } } else { // the yes year case is a bit harder int beforeYearIdx = raw.indexOf(beforeYear); int afterYearIdx = raw.indexOf(afterYear, beforeYearIdx + beforeYear.length() + 1); + String header; if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear.length() <= contentMatcher.start()) { // and also ends with exactly the right header, so it's easy to parse the existing year String existingYear = raw.substring(beforeYearIdx + beforeYear.length(), afterYearIdx); @@ -323,12 +326,13 @@ private String addOrUpdateLicenseHeader(String raw) { return raw; } } - return beforeYear + newYear + afterYear + content; + header = beforeYear + newYear + afterYear; } else { String newYear = calculateYearBySearching(raw.substring(0, contentMatcher.start())); // at worst, we just say that it was made today - return beforeYear + newYear + afterYear + content; + header = beforeYear + newYear + afterYear; } + return replaceFileName(header, file) + content; } } } @@ -421,6 +425,10 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws return beforeYear + yearRange + afterYear + raw.substring(contentMatcher.start()); } + private String replaceFileName(String header, File file) { + return FILENAME_PATTERN.matcher(header).replaceAll(file.getName()); + } + private static String parseYear(String cmd, File file) throws IOException { String fullCmd = cmd + " -- " + file.getAbsolutePath(); ProcessBuilder builder = new ProcessBuilder().directory(file.getParentFile()); diff --git a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java index c611cc738a..032345827d 100644 --- a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java @@ -57,14 +57,22 @@ public static StepHarness forFormatter(Formatter formatter) { /** Asserts that the given element is transformed as expected, and that the result is idempotent. */ public StepHarness test(String before, String after) { - String actual = formatter.compute(LineEnding.toUnix(before), new File("")); + return test(before, after, ""); + } + + public StepHarness test(String before, String after, String fileName) { + String actual = formatter.compute(LineEnding.toUnix(before), new File(fileName)); assertEquals(after, actual, "Step application failed"); - return testUnaffected(after); + return testUnaffected(after, fileName); } /** Asserts that the given element is idempotent w.r.t the step under test. */ public StepHarness testUnaffected(String idempotentElement) { - String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File("")); + return testUnaffected(idempotentElement, ""); + } + + public StepHarness testUnaffected(String idempotentElement, String fileName) { + String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File(fileName)); assertEquals(idempotentElement, actual, "Step is not idempotent"); return this; } diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 2b1d0aff07..d8d7700be5 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -35,6 +35,8 @@ class LicenseHeaderStepTest extends ResourceHarness { private static final String package_ = "package "; private static final String HEADER_WITH_$YEAR = "This is a fake license, $YEAR. ACME corp."; private static final String HEADER_WITH_RANGE_TO_$YEAR = "This is a fake license with range, 2009-$YEAR. ACME corp."; + private static final String HEADER_WITH_$FILE = "This is a fake license, $FILE. ACME corp."; + private static final String HEADER_WITH_$YEAR_$FILE = "This is a fake license, $FILE, $YEAR. ACME corp."; @Test void parseExistingYear() throws Exception { @@ -163,6 +165,16 @@ private String hasHeaderWithRangeAndWithYearTo(String toYear) throws IOException return hasHeaderYear(HEADER_WITH_RANGE_TO_$YEAR, toYear); } + private String hasHeaderFileName(String license, String fileName) throws IOException { + return header(license).replace("$FILE", fileName) + getTestResource(FILE_NO_LICENSE); + } + + private String hasHeaderYearFileName(String license, String year, String fileName) throws IOException { + return header(license) + .replace("$YEAR", year) + .replace("$FILE", fileName) + getTestResource(FILE_NO_LICENSE); + } + private static String currentYear() { return String.valueOf(YearMonth.now().getYear()); } @@ -250,4 +262,22 @@ void should_preserve_year_for_license_with_address() throws Throwable { hasHeader(licenceWithAddress().replace("$YEAR", "2015").replace("FooBar Inc. All", "FooBar Inc. All")), hasHeader(licenceWithAddress().replace("$YEAR", "2015"))); } + + @Test + void should_apply_license_containing_filename_token() throws Exception { + FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); + StepHarness.forStep(step) + .test(getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java"), "Test.java"); + } + + @Test + void should_apply_license_containing_YEAR_filename_token() throws Exception { + FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR_$FILE), package_).build(); + StepHarness.forStep(step) + .test( + getTestResource(FILE_NO_LICENSE), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java"), + "Test.java" + ); + } } From bdc857e8c1b55c87a138976742da702ef53ff373 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 4 Mar 2023 17:31:22 +0900 Subject: [PATCH 08/26] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 8540a44851..f5033f6d2a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changes * We are now opting in to Gradle's new stable configuration cache. ([#1591](https://github.com/diffplug/spotless/pull/1591)) +* Support a file name field in license headers. ([#1605](https://github.com/diffplug/spotless/pull/1605)) ## [2.36.0] - 2023-02-27 ### Added From 3bcdda8aa510b7ddf8b7ee2e7608ec1ee5a5ccff Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 7 Mar 2023 06:08:34 +0000 Subject: [PATCH 09/26] chore(deps): update plugin io.github.gradle-nexus.publish-plugin to v1.3.0 --- settings.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.gradle b/settings.gradle index 1478f5af31..dbca443e3e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -10,7 +10,7 @@ plugins { // https://plugins.gradle.org/plugin/com.gradle.plugin-publish id 'com.gradle.plugin-publish' version '1.1.0' apply false // https://github.com/gradle-nexus/publish-plugin/releases - id 'io.github.gradle-nexus.publish-plugin' version '1.2.0' apply false + id 'io.github.gradle-nexus.publish-plugin' version '1.3.0' apply false // https://github.com/spotbugs/spotbugs-gradle-plugin/releases id 'com.github.spotbugs' version '5.0.13' apply false // https://github.com/diffplug/spotless-changelog/blob/main/CHANGELOG.md From f3c57711afe80079b2973a27250189192f957c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20Dragojevi=C4=87?= Date: Tue, 7 Mar 2023 14:40:43 +0100 Subject: [PATCH 10/26] Use correct property for scalafmt The example uses an outdated property for configuring scalafmt. --- plugin-gradle/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index b66d423e6b..267136e4ac 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -430,8 +430,8 @@ spotless { ```gradle spotless { scala { - // version and configFile, majorScalaVersion are all optional - scalafmt('3.5.9').configFile('scalafmt.conf').majorScalaVersion('2.13') + // version and configFile, scalaMajorVersion are all optional + scalafmt('3.5.9').configFile('scalafmt.conf').scalaMajorVersion('2.13') ``` From d3c081262ac8ada9440ffff2c6b4c751822ac34e Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:51:50 +0000 Subject: [PATCH 11/26] fix(deps): update dependency org.eclipse.jgit:org.eclipse.jgit to v6.5.0.202303070854-r --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 8820a36e30..3f3ae9a2ee 100644 --- a/gradle.properties +++ b/gradle.properties @@ -29,7 +29,7 @@ VER_SLF4J=[1.6,2.0[ # Used in multiple places VER_DURIAN=1.2.0 -VER_JGIT=6.4.0.202211300538-r +VER_JGIT=6.5.0.202303070854-r VER_JUNIT=5.9.2 VER_ASSERTJ=3.24.2 VER_MOCKITO=5.1.1 From d8b0e20ef0a22d0aa03e668a4917f3f1ba4dc725 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Wed, 8 Mar 2023 09:11:51 +0900 Subject: [PATCH 12/26] Use StepHarnessWithFile --- .../java/com/diffplug/spotless/StepHarness.java | 14 +++----------- .../spotless/generic/LicenseHeaderStepTest.java | 12 +++++++----- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java index 032345827d..c611cc738a 100644 --- a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java @@ -57,22 +57,14 @@ public static StepHarness forFormatter(Formatter formatter) { /** Asserts that the given element is transformed as expected, and that the result is idempotent. */ public StepHarness test(String before, String after) { - return test(before, after, ""); - } - - public StepHarness test(String before, String after, String fileName) { - String actual = formatter.compute(LineEnding.toUnix(before), new File(fileName)); + String actual = formatter.compute(LineEnding.toUnix(before), new File("")); assertEquals(after, actual, "Step application failed"); - return testUnaffected(after, fileName); + return testUnaffected(after); } /** Asserts that the given element is idempotent w.r.t the step under test. */ public StepHarness testUnaffected(String idempotentElement) { - return testUnaffected(idempotentElement, ""); - } - - public StepHarness testUnaffected(String idempotentElement, String fileName) { - String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File(fileName)); + String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File("")); assertEquals(idempotentElement, actual, "Step is not idempotent"); return this; } diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index d8d7700be5..6df0c23950 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -21,6 +21,8 @@ import java.time.YearMonth; import java.time.ZoneOffset; +import com.diffplug.spotless.StepHarnessWithFile; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -266,18 +268,18 @@ void should_preserve_year_for_license_with_address() throws Throwable { @Test void should_apply_license_containing_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); - StepHarness.forStep(step) - .test(getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java"), "Test.java"); + StepHarnessWithFile.forStep(this, step) + .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); } @Test void should_apply_license_containing_YEAR_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR_$FILE), package_).build(); - StepHarness.forStep(step) + StepHarnessWithFile.forStep(this, step) .test( + new File("Test.java"), getTestResource(FILE_NO_LICENSE), - hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java"), - "Test.java" + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java") ); } } From a414e937e2c89e56ea038096c8f0ac1ee6fb1eff Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Wed, 8 Mar 2023 09:16:38 +0900 Subject: [PATCH 13/26] Fix CHANGES.md --- CHANGES.md | 3 ++- plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index f5033f6d2a..c44dfd9050 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,9 +10,10 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* You can now put the filename into a license header template with `$FILE`. ([#1605](https://github.com/diffplug/spotless/pull/1605) fixes [#1147](https://github.com/diffplug/spotless/issues/1147)) ### Changes * We are now opting in to Gradle's new stable configuration cache. ([#1591](https://github.com/diffplug/spotless/pull/1591)) -* Support a file name field in license headers. ([#1605](https://github.com/diffplug/spotless/pull/1605)) ## [2.36.0] - 2023-02-27 ### Added diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index c19a7de733..3ecf10d071 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +* You can now put the filename into a license header template with `$FILE`. ([#1605](https://github.com/diffplug/spotless/pull/1605) fixes [#1147](https://github.com/diffplug/spotless/issues/1147)) ## [6.16.0] - 2023-02-27 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 36a51d6ef2..5c50b62027 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* You can now put the filename into a license header template with `$FILE`. ([#1605](https://github.com/diffplug/spotless/pull/1605) fixes [#1147](https://github.com/diffplug/spotless/issues/1147)) ## [2.34.0] - 2023-02-27 ### Added From bc34a4b8e0b2fe5063df528411163d19e2ee8d85 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Wed, 8 Mar 2023 09:19:04 +0900 Subject: [PATCH 14/26] Run spotlessApply --- .../generic/LicenseHeaderStepTest.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 6df0c23950..7d2e018028 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -21,8 +21,6 @@ import java.time.YearMonth; import java.time.ZoneOffset; -import com.diffplug.spotless.StepHarnessWithFile; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -30,6 +28,7 @@ import com.diffplug.spotless.ResourceHarness; import com.diffplug.spotless.SerializableEqualityTester; import com.diffplug.spotless.StepHarness; +import com.diffplug.spotless.StepHarnessWithFile; import com.diffplug.spotless.generic.LicenseHeaderStep.YearMode; class LicenseHeaderStepTest extends ResourceHarness { @@ -173,8 +172,8 @@ private String hasHeaderFileName(String license, String fileName) throws IOExcep private String hasHeaderYearFileName(String license, String year, String fileName) throws IOException { return header(license) - .replace("$YEAR", year) - .replace("$FILE", fileName) + getTestResource(FILE_NO_LICENSE); + .replace("$YEAR", year) + .replace("$FILE", fileName) + getTestResource(FILE_NO_LICENSE); } private static String currentYear() { @@ -269,17 +268,16 @@ void should_preserve_year_for_license_with_address() throws Throwable { void should_apply_license_containing_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); StepHarnessWithFile.forStep(this, step) - .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); + .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); } @Test void should_apply_license_containing_YEAR_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR_$FILE), package_).build(); StepHarnessWithFile.forStep(this, step) - .test( - new File("Test.java"), - getTestResource(FILE_NO_LICENSE), - hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java") - ); + .test( + new File("Test.java"), + getTestResource(FILE_NO_LICENSE), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")); } } From cd2efd3348a20de5352590e7be9d9a4476d180ef Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Wed, 8 Mar 2023 21:36:30 +0400 Subject: [PATCH 15/26] Improve LicenseHeader default regex for Java --- .../spotless/generic/LicenseHeaderStep.java | 1 + .../gradle/spotless/JavaExtension.java | 4 +--- .../spotless/maven/groovy/Groovy.java | 3 ++- .../diffplug/spotless/maven/java/Java.java | 3 ++- .../diffplug/spotless/maven/scala/Scala.java | 3 ++- .../resources/license/HelloWorld_java.test | 5 +++++ .../license/HelloWorld_withImport_java.test | 7 +++++++ .../main/resources/license/module-info.test | 5 +++++ .../generic/LicenseHeaderStepTest.java | 20 ++++++++++++++++++- 9 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 testlib/src/main/resources/license/HelloWorld_java.test create mode 100644 testlib/src/main/resources/license/HelloWorld_withImport_java.test create mode 100644 testlib/src/main/resources/license/module-info.test diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index b4c0cf2a6f..3b0bf829f8 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -45,6 +45,7 @@ /** Prefixes a license header before the package statement. */ public final class LicenseHeaderStep { + public static final String DEFAULT_JAVA_HEADER_DELIMITER = "(package|import|public|class|module) "; private static final Logger LOGGER = LoggerFactory.getLogger(LicenseHeaderStep.class); public enum YearMode { 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 f351dd6188..bfe1a96955 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 @@ -50,9 +50,7 @@ public JavaExtension(SpotlessExtension spotless) { super(spotless); } - // If this constant changes, don't forget to change the similarly-named one in - // testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java as well - static final String LICENSE_HEADER_DELIMITER = "package "; + static final String LICENSE_HEADER_DELIMITER = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; @Override public LicenseHeaderConfig licenseHeader(String licenseHeader) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java index 8041b812f9..35b91f3677 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java @@ -20,6 +20,7 @@ import org.apache.maven.project.MavenProject; import com.diffplug.common.collect.ImmutableSet; +import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.FormatterFactory; import com.diffplug.spotless.maven.generic.LicenseHeader; @@ -32,7 +33,7 @@ public class Groovy extends FormatterFactory { private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/main/groovy/**/*.groovy", "src/test/groovy/**/*.groovy"); - private static final String LICENSE_HEADER_DELIMITER = "package "; + private static final String LICENSE_HEADER_DELIMITER = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; @Override public Set defaultIncludes(MavenProject project) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java index efdf0827db..b0692446b7 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java @@ -26,6 +26,7 @@ import org.apache.maven.model.Build; import org.apache.maven.project.MavenProject; +import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.FormatterFactory; import com.diffplug.spotless.maven.generic.LicenseHeader; @@ -37,7 +38,7 @@ */ public class Java extends FormatterFactory { - private static final String LICENSE_HEADER_DELIMITER = "package "; + private static final String LICENSE_HEADER_DELIMITER = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; @Override public Set defaultIncludes(MavenProject project) { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java index 7a9e455f5f..116d89263c 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java @@ -20,6 +20,7 @@ import org.apache.maven.project.MavenProject; import com.diffplug.common.collect.ImmutableSet; +import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.maven.FormatterFactory; import com.diffplug.spotless.maven.generic.LicenseHeader; @@ -33,7 +34,7 @@ public class Scala extends FormatterFactory { private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/main/scala/**/*.scala", "src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc"); - private static final String LICENSE_HEADER_DELIMITER = "package "; + private static final String LICENSE_HEADER_DELIMITER = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; @Override public Set defaultIncludes(MavenProject project) { diff --git a/testlib/src/main/resources/license/HelloWorld_java.test b/testlib/src/main/resources/license/HelloWorld_java.test new file mode 100644 index 0000000000..d9be2ea939 --- /dev/null +++ b/testlib/src/main/resources/license/HelloWorld_java.test @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.print("Hello World"); + } +} diff --git a/testlib/src/main/resources/license/HelloWorld_withImport_java.test b/testlib/src/main/resources/license/HelloWorld_withImport_java.test new file mode 100644 index 0000000000..fcfb64117e --- /dev/null +++ b/testlib/src/main/resources/license/HelloWorld_withImport_java.test @@ -0,0 +1,7 @@ +import java.time.LocalDate; + +public class HelloWorld { + public static void main(String[] args) { + System.out.print("Hello World. Date: " + LocalDate.now()); + } +} diff --git a/testlib/src/main/resources/license/module-info.test b/testlib/src/main/resources/license/module-info.test new file mode 100644 index 0000000000..6678147b68 --- /dev/null +++ b/testlib/src/main/resources/license/module-info.test @@ -0,0 +1,5 @@ +module java.sql { + exports java.sql; + exports javax.sql; + exports javax.transaction.xa; +} diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 2b1d0aff07..912975dda5 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -32,7 +32,7 @@ class LicenseHeaderStepTest extends ResourceHarness { private static final String FILE_NO_LICENSE = "license/FileWithoutLicenseHeader.test"; - private static final String package_ = "package "; + private static final String package_ = LicenseHeaderStep.DEFAULT_JAVA_HEADER_DELIMITER; private static final String HEADER_WITH_$YEAR = "This is a fake license, $YEAR. ACME corp."; private static final String HEADER_WITH_RANGE_TO_$YEAR = "This is a fake license with range, 2009-$YEAR. ACME corp."; @@ -250,4 +250,22 @@ void should_preserve_year_for_license_with_address() throws Throwable { hasHeader(licenceWithAddress().replace("$YEAR", "2015").replace("FooBar Inc. All", "FooBar Inc. All")), hasHeader(licenceWithAddress().replace("$YEAR", "2015"))); } + + @Test + void noPackage() throws Throwable { + String header = header(getTestResource("license/TestLicense")); + FormatterStep step = LicenseHeaderStep.headerDelimiter(header, package_).build(); + StepHarness.forStep(step) + .test(ResourceHarness.getTestResource("license/HelloWorld_java.test"), header + ResourceHarness.getTestResource("license/HelloWorld_java.test")) + .test(ResourceHarness.getTestResource("license/HelloWorld_withImport_java.test"), header + ResourceHarness.getTestResource("license/HelloWorld_withImport_java.test")); + } + + // The following demonstrate the use of 'module' keyword + @Test + void moduleInfo() throws Throwable { + String header = header(getTestResource("license/TestLicense")); + FormatterStep step = LicenseHeaderStep.headerDelimiter(header, package_).build(); + StepHarness.forStep(step) + .test(ResourceHarness.getTestResource("license/module-info.test"), header + ResourceHarness.getTestResource("license/module-info.test")); + } } From 318f453ee003aaf16b787ab5e947f44deb95fafb Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Thu, 9 Mar 2023 11:59:13 +0900 Subject: [PATCH 16/26] Fix file name not applied --- .../java/com/diffplug/spotless/generic/LicenseHeaderStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index 624ea16cc6..ccc665bb0d 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -323,7 +323,7 @@ private String addOrUpdateLicenseHeader(String raw, File file) { // fastpath where we don't need to make any changes at all boolean noPadding = beforeYearIdx == 0 && afterYearIdx + afterYear.length() == contentMatcher.start(); // allows fastpath return raw if (noPadding) { - return raw; + return replaceFileName(raw.substring(0, contentMatcher.start()), file) + content; } } header = beforeYear + newYear + afterYear; From 02338b9066d50d9809491686959f7431e8ae338d Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Thu, 9 Mar 2023 10:03:08 +0400 Subject: [PATCH 17/26] Homogeneize gradle and maven behavior around Formatter issues on a specific file --- .../gradle/spotless/SpotlessTaskImpl.java | 18 +++++++++++++----- .../spotless/maven/SpotlessApplyMojo.java | 2 +- .../spotless/maven/SpotlessCheckMojo.java | 17 +++++++++++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java index b37e9f283a..d90c0441ac 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -94,26 +94,34 @@ public void performAction(InputChanges inputs) throws Exception { private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File input) throws IOException { File output = getOutputFile(input); - getLogger().debug("Applying format to " + input + " and writing to " + output); + getLogger().debug("Applying format to {} and writing to {}", input, output); PaddedCell.DirtyState dirtyState; if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), getRootTreeSha(), input)) { dirtyState = PaddedCell.isClean(); } else { - dirtyState = PaddedCell.calculateDirtyState(formatter, input); + try { + dirtyState = PaddedCell.calculateDirtyState(formatter, input); + } catch (IOException e) { + throw new IOException("Issue processing file: " + input, e); + } catch (RuntimeException e) { + throw new IllegalArgumentException("Issue processing file: " + input, e); + } } if (dirtyState.isClean()) { // Remove previous output if it exists Files.deleteIfExists(output.toPath()); } else if (dirtyState.didNotConverge()) { - getLogger().warn("Skipping '" + input + "' because it does not converge. Run {@code spotlessDiagnose} to understand why"); + getLogger().warn("Skipping '{}}' because it does not converge. Run {@code spotlessDiagnose} to understand why", input); } else { Path parentDir = output.toPath().getParent(); if (parentDir == null) { - throw new IllegalStateException("Every file has a parent folder."); + throw new IllegalStateException("Every file has a parent folder. But not: " + output); } Files.createDirectories(parentDir); // Need to copy the original file to the tmp location just to remember the file attributes Files.copy(input.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); + + getLogger().info(String.format("Writing clean file: %s", output)); dirtyState.writeCanonicalTo(output); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java index 028cc3fb9c..5ebe2885c9 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java @@ -54,7 +54,7 @@ protected void process(Iterable files, Formatter formatter, UpToDateChecke } else { counter.checkedButAlreadyClean(); } - } catch (IOException e) { + } catch (IOException | RuntimeException e) { throw new MojoExecutionException("Unable to format file " + file, e); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java index ad3230f583..03e55e224a 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,9 +38,12 @@ public class SpotlessCheckMojo extends AbstractSpotlessMojo { @Override protected void process(Iterable files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException { + ImpactedFilesTracker counter = new ImpactedFilesTracker(); + List problemFiles = new ArrayList<>(); for (File file : files) { if (upToDateChecker.isUpToDate(file.toPath())) { + counter.skippedAsCleanCache(); if (getLog().isDebugEnabled()) { getLog().debug("Spotless will not check an up-to-date file: " + file); } @@ -51,14 +54,24 @@ protected void process(Iterable files, Formatter formatter, UpToDateChecke PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file); if (!dirtyState.isClean() && !dirtyState.didNotConverge()) { problemFiles.add(file); + counter.cleaned(); } else { + counter.checkedButAlreadyClean(); upToDateChecker.setUpToDate(file.toPath()); } - } catch (IOException e) { + } catch (IOException | RuntimeException e) { throw new MojoExecutionException("Unable to format file " + file, e); } } + // We print the number of considered files which is useful when ratchetFrom is setup + if (counter.getTotal() > 0) { + getLog().info(String.format("Spotless.%s is keeping %s files clean - %s needs changes to be clean, %s were already clean, %s were skipped because caching determined they were already clean", + formatter.getName(), counter.getTotal(), counter.getCleaned(), counter.getCheckedButAlreadyClean(), counter.getSkippedAsCleanCache())); + } else { + getLog().debug(String.format("Spotless.%s has no target files. Examine your ``: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", formatter.getName())); + } + if (!problemFiles.isEmpty()) { throw new MojoExecutionException(DiffMessageFormatter.builder() .runToFix("Run 'mvn spotless:apply' to fix these violations.") From 86818f1c24db9f58db75fadeb61c94991c73ac55 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 9 Mar 2023 22:06:31 +0000 Subject: [PATCH 18/26] fix(deps): update dependency org.mockito:mockito-core to v5.2.0 --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 8820a36e30..8d9f90f8b0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -32,4 +32,4 @@ VER_DURIAN=1.2.0 VER_JGIT=6.4.0.202211300538-r VER_JUNIT=5.9.2 VER_ASSERTJ=3.24.2 -VER_MOCKITO=5.1.1 +VER_MOCKITO=5.2.0 From 52d7bf74a22217e6c87aee78ccb3a7bf2d128e78 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Fri, 10 Mar 2023 09:02:28 +0400 Subject: [PATCH 19/26] Fix typo, add Unittest --- .../gradle/spotless/SpotlessTaskImpl.java | 6 ++- .../gradle/spotless/SpotlessTaskImplTest.java | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java index d90c0441ac..ab584fa7b6 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java @@ -35,6 +35,7 @@ import org.gradle.work.FileChange; import org.gradle.work.InputChanges; +import com.diffplug.common.annotations.VisibleForTesting; import com.diffplug.common.base.StringPrinter; import com.diffplug.spotless.Formatter; import com.diffplug.spotless.PaddedCell; @@ -92,7 +93,8 @@ public void performAction(InputChanges inputs) throws Exception { } } - private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File input) throws IOException { + @VisibleForTesting + void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File input) throws IOException { File output = getOutputFile(input); getLogger().debug("Applying format to {} and writing to {}", input, output); PaddedCell.DirtyState dirtyState; @@ -111,7 +113,7 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, // Remove previous output if it exists Files.deleteIfExists(output.toPath()); } else if (dirtyState.didNotConverge()) { - getLogger().warn("Skipping '{}}' because it does not converge. Run {@code spotlessDiagnose} to understand why", input); + getLogger().warn("Skipping '{}' because it does not converge. Run {@code spotlessDiagnose} to understand why", input); } else { Path parentDir = output.toPath().getParent(); if (parentDir == null) { diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java new file mode 100644 index 0000000000..26ad28ae23 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.gradle.spotless; + +import java.io.File; + +import org.assertj.core.api.Assertions; +import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.logging.Logger; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import com.diffplug.spotless.Formatter; + +public class SpotlessTaskImplTest { + @Test + public void testThrowsMessageContainsFilename() throws Exception { + SpotlessTaskImpl task = Mockito.mock(SpotlessTaskImpl.class, Mockito.CALLS_REAL_METHODS); + Mockito.when(task.getLogger()).thenReturn(Mockito.mock(Logger.class)); + + File projectDir = new File("unitTests/projectDir"); + DirectoryProperty projectDirProperty = Mockito.mock(DirectoryProperty.class, Mockito.RETURNS_DEEP_STUBS); + Mockito.when(projectDirProperty.get().getAsFile()).thenReturn(projectDir); + + Mockito.when(task.getProjectDir()).thenReturn(projectDirProperty); + + File input = new File("unitTests/projectDir/someInput"); + Formatter formatter = Mockito.mock(Formatter.class); + + Assertions.assertThatThrownBy(() -> task.processInputFile(null, formatter, input)).hasMessageContaining("unitTests/projectDir/someInput"); + } +} From b6cad0e4af5e5ec8b6b9a00d074447c2fa789fe0 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Fri, 10 Mar 2023 09:05:53 +0400 Subject: [PATCH 20/26] Add changelog entries --- plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 2 ++ 2 files changed, 3 insertions(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index c19a7de733..c39aff545d 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +* `licenseHeader` default pattern for Java files is updated to `(package|import|public|class|module) `. ([#1614](https://github.com/diffplug/spotless/pull/1614)) ## [6.16.0] - 2023-02-27 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 36a51d6ef2..635459368a 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* `licenseHeader` default pattern for Java files is updated to `(package|import|public|class|module) `. ([#1614](https://github.com/diffplug/spotless/pull/1614)) ## [2.34.0] - 2023-02-27 ### Added From e579b74e5f7fb69cd1b5d23ae0be388001400ddc Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Fri, 10 Mar 2023 09:45:58 +0400 Subject: [PATCH 21/26] Fix filePath for both Windows and Unix --- .../com/diffplug/gradle/spotless/SpotlessTaskImplTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java index 26ad28ae23..5f5200d8e5 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java @@ -16,6 +16,7 @@ package com.diffplug.gradle.spotless; import java.io.File; +import java.nio.file.Paths; import org.assertj.core.api.Assertions; import org.gradle.api.file.DirectoryProperty; @@ -31,15 +32,15 @@ public void testThrowsMessageContainsFilename() throws Exception { SpotlessTaskImpl task = Mockito.mock(SpotlessTaskImpl.class, Mockito.CALLS_REAL_METHODS); Mockito.when(task.getLogger()).thenReturn(Mockito.mock(Logger.class)); - File projectDir = new File("unitTests/projectDir"); + File projectDir = Paths.get("unitTests","projectDir").toFile(); DirectoryProperty projectDirProperty = Mockito.mock(DirectoryProperty.class, Mockito.RETURNS_DEEP_STUBS); Mockito.when(projectDirProperty.get().getAsFile()).thenReturn(projectDir); Mockito.when(task.getProjectDir()).thenReturn(projectDirProperty); - File input = new File("unitTests/projectDir/someInput"); + File input = Paths.get("unitTests","projectDir", "someInput").toFile(); Formatter formatter = Mockito.mock(Formatter.class); - Assertions.assertThatThrownBy(() -> task.processInputFile(null, formatter, input)).hasMessageContaining("unitTests/projectDir/someInput"); + Assertions.assertThatThrownBy(() -> task.processInputFile(null, formatter, input)).hasMessageContaining(input.toString()); } } From 7e261671a90b156a0d4077b81f97a5c5f05b4052 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Fri, 10 Mar 2023 09:54:54 +0400 Subject: [PATCH 22/26] Fix style --- .../com/diffplug/gradle/spotless/SpotlessTaskImplTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java index 5f5200d8e5..fc7fab9980 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpotlessTaskImplTest.java @@ -32,13 +32,13 @@ public void testThrowsMessageContainsFilename() throws Exception { SpotlessTaskImpl task = Mockito.mock(SpotlessTaskImpl.class, Mockito.CALLS_REAL_METHODS); Mockito.when(task.getLogger()).thenReturn(Mockito.mock(Logger.class)); - File projectDir = Paths.get("unitTests","projectDir").toFile(); + File projectDir = Paths.get("unitTests", "projectDir").toFile(); DirectoryProperty projectDirProperty = Mockito.mock(DirectoryProperty.class, Mockito.RETURNS_DEEP_STUBS); Mockito.when(projectDirProperty.get().getAsFile()).thenReturn(projectDir); Mockito.when(task.getProjectDir()).thenReturn(projectDirProperty); - File input = Paths.get("unitTests","projectDir", "someInput").toFile(); + File input = Paths.get("unitTests", "projectDir", "someInput").toFile(); Formatter formatter = Mockito.mock(Formatter.class); Assertions.assertThatThrownBy(() -> task.processInputFile(null, formatter, input)).hasMessageContaining(input.toString()); From d4d3814b770219681aa985cb5cc50b76cdfa17f8 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 11 Mar 2023 11:51:05 +0900 Subject: [PATCH 23/26] Refactor LicenseHeaderStep --- .../spotless/generic/LicenseHeaderStep.java | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index ccc665bb0d..0b7d076e12 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -213,6 +213,7 @@ private static class Runtime implements Serializable { private final @Nullable String afterYear; private final boolean updateYearWithLatest; private final boolean licenseHeaderWithRange; + private final boolean hasFileToken; private static final Pattern FILENAME_PATTERN = Pattern.compile("\\$FILE"); @@ -228,6 +229,7 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo } this.delimiterPattern = Pattern.compile('^' + delimiter, Pattern.UNIX_LINES | Pattern.MULTILINE); this.skipLinesMatching = skipLinesMatching == null ? null : Pattern.compile(skipLinesMatching); + this.hasFileToken = FILENAME_PATTERN.matcher(licenseHeader).find(); Optional yearToken = getYearToken(licenseHeader); if (yearToken.isPresent()) { @@ -294,6 +296,12 @@ private String format(String raw, File file) { } private String addOrUpdateLicenseHeader(String raw, File file) { + raw = replaceYear(raw); + raw = replaceFileName(raw, file); + return raw; + } + + private String replaceYear(String raw) { Matcher contentMatcher = delimiterPattern.matcher(raw); if (!contentMatcher.find()) { throw new IllegalArgumentException("Unable to find delimiter regex " + delimiterPattern); @@ -307,14 +315,13 @@ private String addOrUpdateLicenseHeader(String raw, File file) { return raw; } else { // otherwise we'll have to add the header - return replaceFileName(yearSepOrFull, file) + content; + return yearSepOrFull + content; } } else { // the yes year case is a bit harder int beforeYearIdx = raw.indexOf(beforeYear); int afterYearIdx = raw.indexOf(afterYear, beforeYearIdx + beforeYear.length() + 1); - String header; if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear.length() <= contentMatcher.start()) { // and also ends with exactly the right header, so it's easy to parse the existing year String existingYear = raw.substring(beforeYearIdx + beforeYear.length(), afterYearIdx); @@ -323,16 +330,15 @@ private String addOrUpdateLicenseHeader(String raw, File file) { // fastpath where we don't need to make any changes at all boolean noPadding = beforeYearIdx == 0 && afterYearIdx + afterYear.length() == contentMatcher.start(); // allows fastpath return raw if (noPadding) { - return replaceFileName(raw.substring(0, contentMatcher.start()), file) + content; + return raw; } } - header = beforeYear + newYear + afterYear; + return beforeYear + newYear + afterYear + content; } else { String newYear = calculateYearBySearching(raw.substring(0, contentMatcher.start())); // at worst, we just say that it was made today - header = beforeYear + newYear + afterYear; + return beforeYear + newYear + afterYear + content; } - return replaceFileName(header, file) + content; } } } @@ -425,8 +431,17 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws return beforeYear + yearRange + afterYear + raw.substring(contentMatcher.start()); } - private String replaceFileName(String header, File file) { - return FILENAME_PATTERN.matcher(header).replaceAll(file.getName()); + private String replaceFileName(String raw, File file) { + if (!hasFileToken) { + return raw; + } + Matcher contentMatcher = delimiterPattern.matcher(raw); + if (!contentMatcher.find()) { + throw new IllegalArgumentException("Unable to find delimiter regex " + delimiterPattern); + } + String header = raw.substring(0, contentMatcher.start()); + String content = raw.substring(contentMatcher.start()); + return FILENAME_PATTERN.matcher(header).replaceAll(file.getName()) + content; } private static String parseYear(String cmd, File file) throws IOException { From 91977a1a71a0a81a1be2324d30eb0746150b2027 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 11 Mar 2023 11:55:45 +0900 Subject: [PATCH 24/26] Add tests --- .../spotless/generic/LicenseHeaderStepTest.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 7d2e018028..760725229f 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -268,7 +268,18 @@ void should_preserve_year_for_license_with_address() throws Throwable { void should_apply_license_containing_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); StepHarnessWithFile.forStep(this, step) - .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); + .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")) + .testUnaffected(new File("Test.java"), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); + } + + @Test + void should_update_license_containing_filename_token() throws Exception { + FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); + StepHarnessWithFile.forStep(this, step) + .test( + new File("After.java"), + hasHeaderFileName(HEADER_WITH_$FILE, "Before.java"), + hasHeaderFileName(HEADER_WITH_$FILE, "After.java")); } @Test @@ -278,6 +289,9 @@ void should_apply_license_containing_YEAR_filename_token() throws Exception { .test( new File("Test.java"), getTestResource(FILE_NO_LICENSE), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")) + .testUnaffected( + new File("Test.java"), hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")); } } From 40488be7041de75d8bbfedeaf5cff20058202145 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 11 Mar 2023 12:02:45 +0900 Subject: [PATCH 25/26] Run spotlessApply --- .../spotless/generic/LicenseHeaderStepTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index ba65cdbafc..92c6794643 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -286,13 +286,13 @@ void should_update_license_containing_filename_token() throws Exception { void should_apply_license_containing_YEAR_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR_$FILE), package_).build(); StepHarnessWithFile.forStep(this, step) - .test( - new File("Test.java"), - getTestResource(FILE_NO_LICENSE), - hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")) - .testUnaffected( - new File("Test.java"), - hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")); + .test( + new File("Test.java"), + getTestResource(FILE_NO_LICENSE), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")) + .testUnaffected( + new File("Test.java"), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")); } void noPackage() throws Throwable { From 24abb541e5df9c69acc9333ffec1137099494eef Mon Sep 17 00:00:00 2001 From: Kostiantyn Liutovych Date: Sun, 12 Mar 2023 21:05:16 +0100 Subject: [PATCH 26/26] Enable up-to-date checking in Maven plugin by default Up-to-date checking has been supported for a long time. We haven't received any negative feedback about it. Neo4j is an example project that has up-to-date checking enabled to improve performance and haven't had problems with it: https://github.com/neo4j/neo4j/blob/6ebc409772cea13354adb6a11aab4ed38da9d53e/pom.xml#L745-L747. It should be safe to enable up-to-date checking for all users of the Spotless Maven plugin. This commit also fixes the problem where plugin execution would fail on a parent of a multimodule Maven project with up-to-date checking enabled. A parent project can configure Spotless as: ```xml com.diffplug.spotless spotless-maven-plugin ... ``` It allows child projects to inherit plugin's version and configuration but the plugin is not enables by default. PluginFingerprint did not handle such configuration correctly. It could only handle a simple configuration like: ```xml com.diffplug.spotless spotless-maven-plugin ... ``` And failed with "Spotless plugin absent from the project" error for a multimodule parent project with Spotless in ``. --- plugin-maven/CHANGES.md | 2 + plugin-maven/README.md | 2 +- .../spotless/maven/AbstractSpotlessMojo.java | 4 +- .../maven/incremental/PluginFingerprint.java | 24 +++++- .../maven/incremental/UpToDateChecking.java | 8 +- .../maven/MultiModuleProjectTest.java | 34 ++++----- .../incremental/PluginFingerprintTest.java | 36 ++++++++- .../incremental/UpToDateCheckingTest.java | 75 +++++++++---------- 8 files changed, 121 insertions(+), 64 deletions(-) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 73eb026796..f65f5e61d4 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -7,6 +7,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * You can now put the filename into a license header template with `$FILE`. ([#1605](https://github.com/diffplug/spotless/pull/1605) fixes [#1147](https://github.com/diffplug/spotless/issues/1147)) ### Fixed * `licenseHeader` default pattern for Java files is updated to `(package|import|public|class|module) `. ([#1614](https://github.com/diffplug/spotless/pull/1614)) +### Changes +* Enable incremental up-to-date checking by default. ([#1621](https://github.com/diffplug/spotless/pull/1621)) ## [2.34.0] - 2023-02-27 ### Added diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 426b00184e..4af6308b89 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1262,7 +1262,7 @@ To define what lines to skip at the beginning of such files, fill the `skipLines ## Incremental up-to-date checking and formatting -**This feature is turned off by default.** +**This feature is enabled by default starting from version 2.35.0.** Execution of `spotless:check` and `spotless:apply` for large projects can take time. By default, Spotless Maven plugin needs to read and format each source file. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index f6aab0eabb..2c0d6e35e6 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -187,7 +187,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { private String setLicenseHeaderYearsFromGitHistory; @Parameter - private UpToDateChecking upToDateChecking; + private UpToDateChecking upToDateChecking = UpToDateChecking.enabled(); protected abstract void process(Iterable files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException; @@ -373,9 +373,9 @@ private UpToDateChecker createUpToDateChecker(Iterable formatters) { } final UpToDateChecker checker; if (upToDateChecking != null && upToDateChecking.isEnabled()) { - getLog().info("Up-to-date checking enabled"); checker = UpToDateChecker.forProject(project, indexFile, formatters, getLog()); } else { + getLog().info("Up-to-date checking disabled"); checker = UpToDateChecker.noop(project, indexFile, getLog()); } return UpToDateChecker.wrapWithBuildContext(checker, buildContext); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java index 9efd6f2586..34a01e235a 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java @@ -21,6 +21,7 @@ import java.util.Objects; import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginManagement; import org.apache.maven.project.MavenProject; import com.diffplug.spotless.Formatter; @@ -43,10 +44,7 @@ private PluginFingerprint(String value) { } static PluginFingerprint from(MavenProject project, Iterable formatters) { - Plugin spotlessPlugin = project.getPlugin(SPOTLESS_PLUGIN_KEY); - if (spotlessPlugin == null) { - throw new IllegalArgumentException("Spotless plugin absent from the project: " + project); - } + Plugin spotlessPlugin = findSpotlessPlugin(project); byte[] digest = digest(spotlessPlugin, formatters); String value = Base64.getEncoder().encodeToString(digest); return new PluginFingerprint(value); @@ -86,6 +84,24 @@ public String toString() { return "PluginFingerprint[" + value + "]"; } + private static Plugin findSpotlessPlugin(MavenProject project) { + // Try to find the plugin instance from XML element + Plugin plugin = project.getPlugin(SPOTLESS_PLUGIN_KEY); + if (plugin == null) { + // Try to find the plugin instance from XML element. Useful when + // the current module is a parent of a multimodule project + PluginManagement pluginManagement = project.getPluginManagement(); + if (pluginManagement != null) { + plugin = pluginManagement.getPluginsAsMap().get(SPOTLESS_PLUGIN_KEY); + } + } + + if (plugin == null) { + throw new IllegalArgumentException("Spotless plugin absent from the project: " + project); + } + return plugin; + } + private static byte[] digest(Plugin plugin, Iterable formatters) { try (ObjectDigestOutputStream out = ObjectDigestOutputStream.create()) { out.writeObject(plugin.getVersion()); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java index d5eb6aa941..f044b847c9 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2022 DiffPlug + * Copyright 2021-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,4 +38,10 @@ public boolean isEnabled() { public Path getIndexFile() { return indexFile == null ? null : new File(indexFile).toPath(); } + + public static UpToDateChecking enabled() { + UpToDateChecking upToDateChecking = new UpToDateChecking(); + upToDateChecking.enabled = true; + return upToDateChecking; + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java index 1c9521c402..a93b4ffdd0 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java @@ -32,31 +32,31 @@ class MultiModuleProjectTest extends MavenIntegrationHarness { @Test void testConfigurationDependency() throws Exception { /* - create a multi-module project with the following stucture: + create a multi-module project with the following structure: /junit-tmp-dir ├── config - │   ├── pom.xml - │   └── src/main/resources/configs - │   ├── eclipse-formatter.xml - │   └── scalafmt.conf + │ ├── pom.xml + │ └── src/main/resources/configs + │ ├── eclipse-formatter.xml + │ └── scalafmt.conf ├── mvnw ├── mvnw.cmd ├── one - │   ├── pom.xml - │   └── src - │   ├── main/java/test1.java - │   └── test/java/test2.java + │ ├── pom.xml + │ └── src + │ ├── main/java/test1.java + │ └── test/java/test2.java ├── two - │   ├── pom.xml - │   └── src - │   ├── main/java/test1.java - │   └── test/java/test2.java + │ ├── pom.xml + │ └── src + │ ├── main/java/test1.java + │ └── test/java/test2.java ├── three - │   ├── pom.xml - │   └── src - │   ├── main/scala/test1.scala - │   └── test/scala/test2.scala + │ ├── pom.xml + │ └── src + │ ├── main/scala/test1.scala + │ └── test/scala/test2.scala ├── pom.xml ├── .mvn ├── mvnw diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java index c344580d37..ae61dec331 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java @@ -23,9 +23,12 @@ import java.io.ByteArrayInputStream; import java.nio.file.Paths; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginManagement; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.ReaderFactory; @@ -106,7 +109,7 @@ void emptyFingerprint() { } @Test - void failsWhenProjectDoesNotContainSpotlessPlugin() { + void failsForProjectWithoutSpotlessPlugin() { MavenProject projectWithoutSpotless = new MavenProject(); assertThatThrownBy(() -> PluginFingerprint.from(projectWithoutSpotless, FORMATTERS)) @@ -114,6 +117,37 @@ void failsWhenProjectDoesNotContainSpotlessPlugin() { .hasMessageContaining("Spotless plugin absent from the project"); } + @Test + void buildsFingerprintForProjectWithSpotlessPluginInBuildPlugins() { + MavenProject project = new MavenProject(); + Plugin spotlessPlugin = new Plugin(); + spotlessPlugin.setGroupId("com.diffplug.spotless"); + spotlessPlugin.setArtifactId("spotless-maven-plugin"); + spotlessPlugin.setVersion("1.2.3"); + project.getBuild().addPlugin(spotlessPlugin); + + PluginFingerprint fingerprint = PluginFingerprint.from(project, Collections.emptyList()); + + assertThat(fingerprint).isNotNull(); + } + + @Test + void buildsFingerprintForProjectWithSpotlessPluginInPluginManagement() { + MavenProject project = new MavenProject(); + Plugin spotlessPlugin = new Plugin(); + spotlessPlugin.setGroupId("com.diffplug.spotless"); + spotlessPlugin.setArtifactId("spotless-maven-plugin"); + spotlessPlugin.setVersion("1.2.3"); + project.getBuild().addPlugin(spotlessPlugin); + PluginManagement pluginManagement = new PluginManagement(); + pluginManagement.addPlugin(spotlessPlugin); + project.getBuild().setPluginManagement(pluginManagement); + + PluginFingerprint fingerprint = PluginFingerprint.from(project, Collections.emptyList()); + + assertThat(fingerprint).isNotNull(); + } + private MavenProject mavenProject(String spotlessVersion) throws Exception { String xml = createPomXmlContent(spotlessVersion, new String[0], new String[0]); return new MavenProject(readPom(xml)); diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java index 0a9fe8f2d4..cfc17594ca 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java @@ -31,8 +31,10 @@ class UpToDateCheckingTest extends MavenIntegrationHarness { + private static final String DISABLED_MESSAGE = "Up-to-date checking disabled"; + @Test - void upToDateCheckingDisabledByDefault() throws Exception { + void upToDateCheckingEnabledByDefault() throws Exception { writePom( "", " ", @@ -41,75 +43,53 @@ void upToDateCheckingDisabledByDefault() throws Exception { List files = writeUnformattedFiles(1); String output = runSpotlessApply(); - assertThat(output).doesNotContain("Up-to-date checking enabled"); + assertThat(output).doesNotContain(DISABLED_MESSAGE); assertFormatted(files); } @Test - void enableUpToDateChecking() throws Exception { + void explicitlyEnableUpToDateChecking() throws Exception { writePomWithUpToDateCheckingEnabled(true); List files = writeUnformattedFiles(1); String output = runSpotlessApply(); - assertThat(output).contains("Up-to-date checking enabled"); + assertThat(output).doesNotContain(DISABLED_MESSAGE); assertFormatted(files); } @Test - void enableUpToDateCheckingWithPluginDependencies() throws Exception { - writePomWithPluginManagementAndDependency(); + void explicitlyDisableUpToDateChecking() throws Exception { + writePomWithUpToDateCheckingEnabled(false); List files = writeUnformattedFiles(1); String output = runSpotlessApply(); - assertThat(output).contains("Up-to-date checking enabled"); + assertThat(output).contains(DISABLED_MESSAGE); assertFormatted(files); } @Test - void enableUpToDateCheckingWithPluginDependenciesMaven3_6_3() throws Exception { + void enableUpToDateCheckingWithPluginDependencies() throws Exception { writePomWithPluginManagementAndDependency(); - setFile(".mvn/wrapper/maven-wrapper.properties").toContent("distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip\n"); - List files = writeUnformattedFiles(1); String output = runSpotlessApply(); - assertThat(output).contains("Up-to-date checking enabled"); + assertThat(output).doesNotContain(DISABLED_MESSAGE); assertFormatted(files); } - private void writePomWithPluginManagementAndDependency() throws IOException { - setFile("pom.xml").toContent(createPomXmlContent("/pom-test-management.xml.mustache", - null, - null, - new String[]{ - "", - " ", - "", - "", - " true", - ""}, - new String[]{ - "", - " ", - " javax.inject", - " javax.inject", - " 1", - " ", - ""}, - null)); - } - @Test - void disableUpToDateChecking() throws Exception { - writePomWithUpToDateCheckingEnabled(false); + void enableUpToDateCheckingWithPluginDependenciesMaven3_6_3() throws Exception { + writePomWithPluginManagementAndDependency(); + + setFile(".mvn/wrapper/maven-wrapper.properties").toContent("distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip\n"); List files = writeUnformattedFiles(1); String output = runSpotlessApply(); - assertThat(output).doesNotContain("Up-to-date checking enabled"); + assertThat(output).doesNotContain(DISABLED_MESSAGE); assertFormatted(files); } @@ -124,7 +104,7 @@ void enableUpToDateCheckingCustomIndexFile() throws Exception { List files = writeUnformattedFiles(1); String output = runSpotlessApply(); - assertThat(output).contains("Up-to-date checking enabled"); + assertThat(output).doesNotContain(DISABLED_MESSAGE); assertFormatted(files); assertThat(indexFile.getParent()).exists(); assertThat(indexFile).exists(); @@ -143,7 +123,7 @@ void disableUpToDateCheckingCustomIndexFile() throws Exception { List files = writeUnformattedFiles(1); String output = runSpotlessApply(); - assertThat(output).doesNotContain("Up-to-date checking enabled"); + assertThat(output).contains(DISABLED_MESSAGE); assertFormatted(files); assertThat(indexFile.getParent()).exists(); assertThat(indexFile).doesNotExist(); @@ -215,6 +195,25 @@ void spotlessCheckRecordsUnformattedFiles() throws Exception { assertSpotlessCheckSkipped(files, checkOutput3); } + private void writePomWithPluginManagementAndDependency() throws IOException { + setFile("pom.xml").toContent(createPomXmlContent("/pom-test-management.xml.mustache", + null, + null, + new String[]{ + "", + " ", + ""}, + new String[]{ + "", + " ", + " javax.inject", + " javax.inject", + " 1", + " ", + ""}, + null)); + } + private void writePomWithUpToDateCheckingEnabled(boolean enabled) throws IOException { writePom( "",