From 60d2dfa97c29fd6635908ebe5200053382324531 Mon Sep 17 00:00:00 2001 From: Cameron Gregor Date: Wed, 16 Feb 2022 23:04:58 +1100 Subject: [PATCH] Allow skipping of sourceDirectory or testSourceDirectory Fixes spotify/fmt-maven-plugin#31 Adds 2 extra configuration options controlling whether to skip relevant directories: - skipSourceDirectory - skipTestSourceDirectory Tests added: - check_skipsourcedirectory: Tests that sourceDirectory is skipped for check goal when skipSourceDirectory is true. sourceDirectory includes unformatted code so it would fail if not skipped. - check_skiptestsourcedirectory: Tests that testSourceDirectory is skipped for check goal when skipTestSourceDirectory is true. testSourceDirectory includes unformatted code so it would fail if not skipped. - skipsourcedirectory: Tests that sourceDirectory files are not formatted with format goal when skipSourceDirectory is true. - skiptestsourcedirectory: Tests that testSourceDirectory files are not formatted with format goal when skipTestSourceDirectory is true. Co-authored-by: Sean Flanigan --- README.md | 6 +++ .../java/com/spotify/fmt/AbstractFMT.java | 10 +++- src/test/java/com/spotify/fmt/FMTTest.java | 30 +++++++++++ .../check_skipsourcedirectory/pom.xml | 50 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 +++ .../src/main/java/HelloWorld2.java | 7 +++ .../src/test/java/HelloWorldTest.java | 7 +++ .../check_skiptestsourcedirectory/pom.xml | 50 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 +++ .../src/main/java/HelloWorld2.java | 7 +++ .../src/test/java/HelloWorldTest.java | 7 +++ .../resources/skipsourcedirectory/pom.xml | 50 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 +++ .../src/main/java/HelloWorld2.java | 7 +++ .../src/test/java/HelloWorldTest.java | 7 +++ .../resources/skiptestsourcedirectory/pom.xml | 50 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 +++ .../src/main/java/HelloWorld2.java | 7 +++ .../src/test/java/HelloWorldTest.java | 7 +++ 19 files changed, 328 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/check_skipsourcedirectory/pom.xml create mode 100644 src/test/resources/check_skipsourcedirectory/src/main/java/HelloWorld1.java create mode 100644 src/test/resources/check_skipsourcedirectory/src/main/java/HelloWorld2.java create mode 100644 src/test/resources/check_skipsourcedirectory/src/test/java/HelloWorldTest.java create mode 100644 src/test/resources/check_skiptestsourcedirectory/pom.xml create mode 100644 src/test/resources/check_skiptestsourcedirectory/src/main/java/HelloWorld1.java create mode 100644 src/test/resources/check_skiptestsourcedirectory/src/main/java/HelloWorld2.java create mode 100644 src/test/resources/check_skiptestsourcedirectory/src/test/java/HelloWorldTest.java create mode 100644 src/test/resources/skipsourcedirectory/pom.xml create mode 100644 src/test/resources/skipsourcedirectory/src/main/java/HelloWorld1.java create mode 100644 src/test/resources/skipsourcedirectory/src/main/java/HelloWorld2.java create mode 100644 src/test/resources/skipsourcedirectory/src/test/java/HelloWorldTest.java create mode 100644 src/test/resources/skiptestsourcedirectory/pom.xml create mode 100644 src/test/resources/skiptestsourcedirectory/src/main/java/HelloWorld1.java create mode 100644 src/test/resources/skiptestsourcedirectory/src/main/java/HelloWorld2.java create mode 100644 src/test/resources/skiptestsourcedirectory/src/test/java/HelloWorldTest.java diff --git a/README.md b/README.md index d420bbb..f31855c 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,10 @@ For example, you may prefer that the `check` goal is performed in an earlier pha `skipSortingImports` is whether the plugin should skip sorting imports. +`skipSourceDirectory` is whether the plugin should skip formatting/checking the `sourceDirectory`. It defaults to `false`. + +`skipTestSourceDirectory` is whether the plugin should skip formatting/checking the `testSourceDirectory`. It defaults to `false`. + `style` sets the formatter style to be _google_ or _aosp_. By default this is 'google'. Projects using Android conventions may prefer `aosp`. example: @@ -117,6 +121,8 @@ example: some/other/dir false + false + false false diff --git a/src/main/java/com/spotify/fmt/AbstractFMT.java b/src/main/java/com/spotify/fmt/AbstractFMT.java index 1a87023..894020f 100644 --- a/src/main/java/com/spotify/fmt/AbstractFMT.java +++ b/src/main/java/com/spotify/fmt/AbstractFMT.java @@ -54,6 +54,12 @@ public abstract class AbstractFMT extends AbstractMojo { @Parameter(defaultValue = "false", property = "fmt.skip") private boolean skip = false; + @Parameter(defaultValue = "false", property = "skipSourceDirectory") + private boolean skipSourceDirectory = false; + + @Parameter(defaultValue = "false", property = "skipTestSourceDirectory") + private boolean skipTestSourceDirectory = false; + @Parameter(defaultValue = "false", property = "skipSortingImports") private boolean skipSortingImports = false; @@ -82,12 +88,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("Skipping sorting imports"); } List directoriesToFormat = new ArrayList<>(); - if (sourceDirectory.exists()) { + if (sourceDirectory.exists() && !skipSourceDirectory) { directoriesToFormat.add(sourceDirectory); } else { handleMissingDirectory("Source", sourceDirectory); } - if (testSourceDirectory.exists()) { + if (testSourceDirectory.exists() && !skipTestSourceDirectory) { directoriesToFormat.add(testSourceDirectory); } else { handleMissingDirectory("Test source", testSourceDirectory); diff --git a/src/test/java/com/spotify/fmt/FMTTest.java b/src/test/java/com/spotify/fmt/FMTTest.java index 1db33d9..96065ab 100644 --- a/src/test/java/com/spotify/fmt/FMTTest.java +++ b/src/test/java/com/spotify/fmt/FMTTest.java @@ -24,6 +24,14 @@ public void noSource() throws Exception { assertThat(fmt.getFilesProcessed()).isEmpty(); } + @Test + public void skipSource() throws Exception { + FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("skipsourcedirectory"), FORMAT); + fmt.execute(); + + assertThat(fmt.getFilesProcessed()).hasSize(1); + } + @Test public void withoutTestSources() throws Exception { FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("notestsource"), FORMAT); @@ -32,6 +40,14 @@ public void withoutTestSources() throws Exception { assertThat(fmt.getFilesProcessed()).hasSize(2); } + @Test + public void skipTestSources() throws Exception { + FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("skiptestsourcedirectory"), FORMAT); + fmt.execute(); + + assertThat(fmt.getFilesProcessed()).hasSize(2); + } + @Test public void withOnlyTestSources() throws Exception { FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("onlytestsources"), FORMAT); @@ -149,6 +165,20 @@ public void checkFailsWhenNotFormatted() throws Exception { check.execute(); } + @Test + public void checkSucceedsWhenSkipSourceDirectory() throws Exception { + Check check = + (Check) mojoRule.lookupConfiguredMojo(loadPom("check_skipsourcedirectory"), CHECK); + check.execute(); + } + + @Test + public void checkSucceedsWhenSkipTestSourceDirectory() throws Exception { + Check check = + (Check) mojoRule.lookupConfiguredMojo(loadPom("check_skiptestsourcedirectory"), CHECK); + check.execute(); + } + @Test public void checkSucceedsWhenFormatted() throws Exception { Check check = (Check) mojoRule.lookupConfiguredMojo(loadPom("check_formatted"), CHECK); diff --git a/src/test/resources/check_skipsourcedirectory/pom.xml b/src/test/resources/check_skipsourcedirectory/pom.xml new file mode 100644 index 0000000..9b40520 --- /dev/null +++ b/src/test/resources/check_skipsourcedirectory/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 4.13.1 + test + + + + + + + com.coveo + fmt-maven-plugin + 2.12 + + true + + + + + format + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/check_skipsourcedirectory/src/main/java/HelloWorld1.java b/src/test/resources/check_skipsourcedirectory/src/main/java/HelloWorld1.java new file mode 100644 index 0000000..b9cba7e --- /dev/null +++ b/src/test/resources/check_skipsourcedirectory/src/main/java/HelloWorld1.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { +public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/check_skipsourcedirectory/src/main/java/HelloWorld2.java b/src/test/resources/check_skipsourcedirectory/src/main/java/HelloWorld2.java new file mode 100644 index 0000000..b9cba7e --- /dev/null +++ b/src/test/resources/check_skipsourcedirectory/src/main/java/HelloWorld2.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { +public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/check_skipsourcedirectory/src/test/java/HelloWorldTest.java b/src/test/resources/check_skipsourcedirectory/src/test/java/HelloWorldTest.java new file mode 100644 index 0000000..3b2afb4 --- /dev/null +++ b/src/test/resources/check_skipsourcedirectory/src/test/java/HelloWorldTest.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorldTest { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/check_skiptestsourcedirectory/pom.xml b/src/test/resources/check_skiptestsourcedirectory/pom.xml new file mode 100644 index 0000000..3f56547 --- /dev/null +++ b/src/test/resources/check_skiptestsourcedirectory/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 4.13.1 + test + + + + + + + com.coveo + fmt-maven-plugin + 2.12 + + true + + + + + format + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/check_skiptestsourcedirectory/src/main/java/HelloWorld1.java b/src/test/resources/check_skiptestsourcedirectory/src/main/java/HelloWorld1.java new file mode 100644 index 0000000..b2d5ee8 --- /dev/null +++ b/src/test/resources/check_skiptestsourcedirectory/src/main/java/HelloWorld1.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/check_skiptestsourcedirectory/src/main/java/HelloWorld2.java b/src/test/resources/check_skiptestsourcedirectory/src/main/java/HelloWorld2.java new file mode 100644 index 0000000..b2d5ee8 --- /dev/null +++ b/src/test/resources/check_skiptestsourcedirectory/src/main/java/HelloWorld2.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/check_skiptestsourcedirectory/src/test/java/HelloWorldTest.java b/src/test/resources/check_skiptestsourcedirectory/src/test/java/HelloWorldTest.java new file mode 100644 index 0000000..3be6792 --- /dev/null +++ b/src/test/resources/check_skiptestsourcedirectory/src/test/java/HelloWorldTest.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorldTest { +public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/skipsourcedirectory/pom.xml b/src/test/resources/skipsourcedirectory/pom.xml new file mode 100644 index 0000000..9b40520 --- /dev/null +++ b/src/test/resources/skipsourcedirectory/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 4.13.1 + test + + + + + + + com.coveo + fmt-maven-plugin + 2.12 + + true + + + + + format + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/skipsourcedirectory/src/main/java/HelloWorld1.java b/src/test/resources/skipsourcedirectory/src/main/java/HelloWorld1.java new file mode 100644 index 0000000..b2d5ee8 --- /dev/null +++ b/src/test/resources/skipsourcedirectory/src/main/java/HelloWorld1.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/skipsourcedirectory/src/main/java/HelloWorld2.java b/src/test/resources/skipsourcedirectory/src/main/java/HelloWorld2.java new file mode 100644 index 0000000..b2d5ee8 --- /dev/null +++ b/src/test/resources/skipsourcedirectory/src/main/java/HelloWorld2.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/skipsourcedirectory/src/test/java/HelloWorldTest.java b/src/test/resources/skipsourcedirectory/src/test/java/HelloWorldTest.java new file mode 100644 index 0000000..3b2afb4 --- /dev/null +++ b/src/test/resources/skipsourcedirectory/src/test/java/HelloWorldTest.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorldTest { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/skiptestsourcedirectory/pom.xml b/src/test/resources/skiptestsourcedirectory/pom.xml new file mode 100644 index 0000000..3f56547 --- /dev/null +++ b/src/test/resources/skiptestsourcedirectory/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 4.13.1 + test + + + + + + + com.coveo + fmt-maven-plugin + 2.12 + + true + + + + + format + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/skiptestsourcedirectory/src/main/java/HelloWorld1.java b/src/test/resources/skiptestsourcedirectory/src/main/java/HelloWorld1.java new file mode 100644 index 0000000..b2d5ee8 --- /dev/null +++ b/src/test/resources/skiptestsourcedirectory/src/main/java/HelloWorld1.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/skiptestsourcedirectory/src/main/java/HelloWorld2.java b/src/test/resources/skiptestsourcedirectory/src/main/java/HelloWorld2.java new file mode 100644 index 0000000..b2d5ee8 --- /dev/null +++ b/src/test/resources/skiptestsourcedirectory/src/main/java/HelloWorld2.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/skiptestsourcedirectory/src/test/java/HelloWorldTest.java b/src/test/resources/skiptestsourcedirectory/src/test/java/HelloWorldTest.java new file mode 100644 index 0000000..3b2afb4 --- /dev/null +++ b/src/test/resources/skiptestsourcedirectory/src/test/java/HelloWorldTest.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorldTest { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +}