Skip to content

Commit

Permalink
Add validation mode (#8) (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
olim7t authored and malaporte committed Mar 7, 2017
1 parent 38c4762 commit e8e164e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/main/java/com/coveo/FMT.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public class FMT extends AbstractMojo {
@Parameter(defaultValue = ".*\\.java", property = "filesNamePattern")
private String filesNamePattern;

@Parameter(defaultValue = "false", property = "validateOnly")
private boolean validateOnly;

private List<String> filesFormatted = new ArrayList<String>();
private int nonComplyingFiles;

/**
* execute.
Expand Down Expand Up @@ -90,6 +94,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
formatSourceFilesInDirectory(directoryToFormat);
}

maybeFailIfNonComplying();
logNumberOfFilesFormatted();
}

Expand Down Expand Up @@ -146,7 +151,10 @@ private void formatSourceFile(File file) {
String input = source.read();
String output = formatter.formatSource(input);
if (!input.equals(output)) {
sink.write(output);
if (!validateOnly) {
sink.write(output);
}
nonComplyingFiles += 1;
}
filesFormatted.add(file.getAbsolutePath());
if (filesFormatted.size() % 100 == 0) {
Expand Down Expand Up @@ -176,6 +184,22 @@ private void handleMissingDirectory(String directoryDisplayName, File directory)
}

private void logNumberOfFilesFormatted() {
logger.info("Successfully formatted " + filesFormatted.size() + " files.");
logger.info(
String.format(
"Processed %d files (%d %s).",
filesFormatted.size(),
nonComplyingFiles,
(validateOnly ? "non-complying" : "reformatted")));
}

private void maybeFailIfNonComplying() throws MojoFailureException {
if (validateOnly && nonComplyingFiles > 0) {
String message =
"Found "
+ nonComplyingFiles
+ " non-complying files, failing build (validateOnly is true)";
logger.error(message);
throw new MojoFailureException(message);
}
}
}
12 changes: 12 additions & 0 deletions src/test/java/com/coveo/FMTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ public void withOnlyAvajFiles() throws Exception {
assertThat(fmt.getFilesFormatted()).hasSize(1);
}

@Test(expected = MojoFailureException.class)
public void validateOnlyFailsWhenNotFormatted() throws Exception {
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("validateonly_notformatted"), FORMAT);
fmt.execute();
}

@Test
public void validateOnlySucceedsWhenFormatted() throws Exception {
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("validateonly_formatted"), FORMAT);
fmt.execute();
}

public File loadPom(String folderName) {
return new File("src/test/resources/", folderName);
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/resources/validateonly_formatted/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<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>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<validateOnly>true</validateOnly>
</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!");
}
}
41 changes: 41 additions & 0 deletions src/test/resources/validateonly_notformatted/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<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>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<validateOnly>true</validateOnly>
</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!");
}
}

0 comments on commit e8e164e

Please sign in to comment.