Skip to content

Commit

Permalink
Allow skipping of sourceDirectory or testSourceDirectory (#128)
Browse files Browse the repository at this point in the history
* Allow skipping of sourceDirectory or testSourceDirectory

Fixes #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 <[email protected]>

* Apply suggestions from code review

Co-authored-by: Klara <[email protected]>

* 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 <[email protected]>
Co-authored-by: Klara <[email protected]>
Co-authored-by: Klara <[email protected]>
  • Loading branch information
4 people authored Sep 30, 2022
1 parent 25fa19d commit 1cf19c0
Show file tree
Hide file tree
Showing 23 changed files with 330 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -119,6 +123,8 @@ example:
<param>some/other/dir</param>
</additionalSourceDirectories>
<skip>false</skip>
<skipSourceDirectory>false</skipSourceDirectory>
<skipTestSourceDirectory>false</skipTestSourceDirectory>
<skipSortingImports>false</skipSortingImports>
<style>google</style>
</configuration>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/spotify/fmt/AbstractFMT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -90,12 +96,12 @@ public void execute() throws MojoFailureException {
getLog().info("Skipping sorting imports");
}
List<File> 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);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/spotify/fmt/FMTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:check
50 changes: 50 additions & 0 deletions src/test/resources/check_skipsourcedirectory/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipSourceDirectory>true</skipSourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>









Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorldTest {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:check
50 changes: 50 additions & 0 deletions src/test/resources/check_skiptestsourcedirectory/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTestSourceDirectory>true</skipTestSourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>









Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorldTest {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
1 change: 1 addition & 0 deletions src/test/resources/skipsourcedirectory/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:format
50 changes: 50 additions & 0 deletions src/test/resources/skipsourcedirectory/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipSourceDirectory>true</skipSourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>









Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorldTest {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:format
50 changes: 50 additions & 0 deletions src/test/resources/skiptestsourcedirectory/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTestSourceDirectory>true</skipTestSourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>









Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Loading

0 comments on commit 1cf19c0

Please sign in to comment.