Skip to content

Commit

Permalink
Allow skipping of sourceDirectory or testSourceDirectory
Browse files Browse the repository at this point in the history
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 <sflanigan@securecodewarrior.com>
  • Loading branch information
camac and seanf committed Mar 1, 2022
1 parent f627de7 commit 60d2dfa
Show file tree
Hide file tree
Showing 19 changed files with 328 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`.

example:
Expand All @@ -117,6 +121,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 @@ -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;

Expand Down Expand Up @@ -82,12 +88,12 @@ public void execute() throws MojoExecutionException, 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 @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
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!");
}
}

0 comments on commit 60d2dfa

Please sign in to comment.