From 1cf19c0cb5a88272a50bfafed8935c79b946c62f Mon Sep 17 00:00:00 2001 From: Cameron Gregor Date: Fri, 30 Sep 2022 21:57:05 +1000 Subject: [PATCH] Allow skipping of sourceDirectory or testSourceDirectory (#128) * 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 * Apply suggestions from code review Co-authored-by: Klara * Use loadMojo * Add invoker.properties for skip directory tests * Update groupId to com.spotify.fmt for skip directory test pom.xml Co-authored-by: Sean Flanigan Co-authored-by: Klara Co-authored-by: Klara --- README.md | 6 +++ .../java/com/spotify/fmt/AbstractFMT.java | 10 +++- src/test/java/com/spotify/fmt/FMTTest.java | 28 +++++++++++ .../invoker.properties | 1 + .../check_skipsourcedirectory/pom.xml | 50 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 +++ .../src/main/java/HelloWorld2.java | 7 +++ .../src/test/java/HelloWorldTest.java | 7 +++ .../invoker.properties | 1 + .../check_skiptestsourcedirectory/pom.xml | 50 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 +++ .../src/main/java/HelloWorld2.java | 7 +++ .../src/test/java/HelloWorldTest.java | 7 +++ .../skipsourcedirectory/invoker.properties | 1 + .../resources/skipsourcedirectory/pom.xml | 50 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 +++ .../src/main/java/HelloWorld2.java | 7 +++ .../src/test/java/HelloWorldTest.java | 7 +++ .../invoker.properties | 1 + .../resources/skiptestsourcedirectory/pom.xml | 50 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 +++ .../src/main/java/HelloWorld2.java | 7 +++ .../src/test/java/HelloWorldTest.java | 7 +++ 23 files changed, 330 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/check_skipsourcedirectory/invoker.properties 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/invoker.properties 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/invoker.properties 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/invoker.properties 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 b1b358f..4374ec4 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`. `forkMode` lets you specify whether to run google-java-format in a fork or in-process. Also adds JVM arguments to expose JDK internal javac APIs. Value `default` (which is the default) will fork (to avoid warnings for JDK9+ and to be able to run at all for JDK16+), `never` runs in-process, regardless of JDK version and `always` will always fork. @@ -119,6 +123,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 8ccdf95..f0c6797 100644 --- a/src/main/java/com/spotify/fmt/AbstractFMT.java +++ b/src/main/java/com/spotify/fmt/AbstractFMT.java @@ -47,6 +47,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; @@ -90,12 +96,12 @@ public void execute() throws 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 26488d5..a31cf08 100644 --- a/src/test/java/com/spotify/fmt/FMTTest.java +++ b/src/test/java/com/spotify/fmt/FMTTest.java @@ -30,6 +30,14 @@ public void noSource() throws Exception { assertThat(fmt.getResult().processedFiles()).isEmpty(); } + @Test + public void skipSource() throws Exception { + FMT fmt = loadMojo("skipsourcedirectory", FORMAT); + fmt.execute(); + + assertThat(fmt.getResult().processedFiles()).hasSize(1); + } + @Test public void withoutTestSources() throws Exception { FMT fmt = loadMojo("notestsource", FORMAT); @@ -38,6 +46,14 @@ public void withoutTestSources() throws Exception { assertThat(fmt.getResult().processedFiles()).hasSize(2); } + @Test + public void skipTestSources() throws Exception { + FMT fmt = loadMojo("skiptestsourcedirectory", FORMAT); + fmt.execute(); + + assertThat(fmt.getResult().processedFiles()).hasSize(2); + } + @Test public void withOnlyTestSources() throws Exception { FMT fmt = loadMojo("onlytestsources", FORMAT); @@ -199,6 +215,18 @@ public void checkFailsWhenNotFormatted() throws Exception { check.execute(); } + @Test + public void checkSucceedsWhenSkipSourceDirectory() throws Exception { + Check check = loadMojo("check_skipsourcedirectory", CHECK); + check.execute(); + } + + @Test + public void checkSucceedsWhenSkipTestSourceDirectory() throws Exception { + Check check = loadMojo("check_skiptestsourcedirectory", CHECK); + check.execute(); + } + @Test public void checkSucceedsWhenFormatted() throws Exception { Check check = loadMojo("check_formatted", CHECK); diff --git a/src/test/resources/check_skipsourcedirectory/invoker.properties b/src/test/resources/check_skipsourcedirectory/invoker.properties new file mode 100644 index 0000000..b577428 --- /dev/null +++ b/src/test/resources/check_skipsourcedirectory/invoker.properties @@ -0,0 +1 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}: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..a62db40 --- /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.spotify.fmt + 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/invoker.properties b/src/test/resources/check_skiptestsourcedirectory/invoker.properties new file mode 100644 index 0000000..b577428 --- /dev/null +++ b/src/test/resources/check_skiptestsourcedirectory/invoker.properties @@ -0,0 +1 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:check diff --git a/src/test/resources/check_skiptestsourcedirectory/pom.xml b/src/test/resources/check_skiptestsourcedirectory/pom.xml new file mode 100644 index 0000000..45741bb --- /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.spotify.fmt + 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/invoker.properties b/src/test/resources/skipsourcedirectory/invoker.properties new file mode 100644 index 0000000..908e5e3 --- /dev/null +++ b/src/test/resources/skipsourcedirectory/invoker.properties @@ -0,0 +1 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:format diff --git a/src/test/resources/skipsourcedirectory/pom.xml b/src/test/resources/skipsourcedirectory/pom.xml new file mode 100644 index 0000000..a62db40 --- /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.spotify.fmt + 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/invoker.properties b/src/test/resources/skiptestsourcedirectory/invoker.properties new file mode 100644 index 0000000..908e5e3 --- /dev/null +++ b/src/test/resources/skiptestsourcedirectory/invoker.properties @@ -0,0 +1 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:format diff --git a/src/test/resources/skiptestsourcedirectory/pom.xml b/src/test/resources/skiptestsourcedirectory/pom.xml new file mode 100644 index 0000000..45741bb --- /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.spotify.fmt + 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!"); + } +}