Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow skipping of sourceDirectory or testSourceDirectory #128

Merged
merged 6 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 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 = (FMT) mojoRule.lookupConfiguredMojo(loadPom("skipsourcedirectory"), FORMAT);
fmt.execute();

assertThat(fmt.getFilesProcessed()).hasSize(1);
camac marked this conversation as resolved.
Show resolved Hide resolved
}

@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 = (FMT) mojoRule.lookupConfiguredMojo(loadPom("skiptestsourcedirectory"), FORMAT);
fmt.execute();

assertThat(fmt.getFilesProcessed()).hasSize(2);
camac marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void withOnlyTestSources() throws Exception {
FMT fmt = loadMojo("onlytestsources", FORMAT);
Expand Down Expand Up @@ -199,6 +215,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 = loadMojo("check_formatted", CHECK);
Expand Down
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.coveo</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!");
}
}
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.coveo</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!");
}
}
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.coveo</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!");
}
}
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.coveo</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!");
}
}