From ea4e29db063adfb3e44f5ea11443eaaa450b47c9 Mon Sep 17 00:00:00 2001 From: Guido Date: Wed, 3 Jun 2020 10:34:48 +0200 Subject: [PATCH] fail check task when there are failures in formatting (#58) Co-authored-by: Guido Schreuder Co-authored-by: Martin Laporte --- src/main/java/com/coveo/AbstractFMT.java | 36 +++++++++++------- src/test/java/com/coveo/FMTTest.java | 9 ++++- src/test/resources/failed_formatting/pom.xml | 38 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 25 ++++++++++++ 4 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 src/test/resources/failed_formatting/pom.xml create mode 100644 src/test/resources/failed_formatting/src/main/java/HelloWorld1.java diff --git a/src/main/java/com/coveo/AbstractFMT.java b/src/main/java/com/coveo/AbstractFMT.java index 48a5f4d..bcad6ee 100644 --- a/src/main/java/com/coveo/AbstractFMT.java +++ b/src/main/java/com/coveo/AbstractFMT.java @@ -16,7 +16,6 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Collectors; import java.util.stream.Stream; - import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -73,6 +72,7 @@ public abstract class AbstractFMT extends AbstractMojo { * * @throws org.apache.maven.plugin.MojoExecutionException if any. */ + @Override public void execute() throws MojoExecutionException, MojoFailureException { if (skip) { getLog().info("Skipping format check"); @@ -85,7 +85,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { if (skipSortingImports) { getLog().info("Skipping sorting imports"); } - List directoriesToFormat = new ArrayList(); + List directoriesToFormat = new ArrayList<>(); if (sourceDirectory.exists()) { directoriesToFormat.add(sourceDirectory); } else { @@ -145,14 +145,22 @@ public void formatSourceFilesInDirectory(File directory, Formatter formatter) try (Stream paths = Files.walk(Paths.get(directory.getPath()))) { FileFilter fileNameFilter = getFileNameFilter(); FileFilter pathFilter = getPathFilter(); - paths - .collect(Collectors.toList()) - .parallelStream() - .filter(Files::isRegularFile) - .map(Path::toFile) - .filter(fileNameFilter::accept) - .filter(pathFilter::accept) - .forEach(file -> formatSourceFile(file, formatter)); + long failures = + paths + .collect(Collectors.toList()) + .parallelStream() + .filter(p -> p.toFile().exists()) + .map(Path::toFile) + .filter(fileNameFilter::accept) + .filter(pathFilter::accept) + .map(file -> formatSourceFile(file, formatter)) + .filter(r -> !r) + .count(); + + if (failures > 0) { + throw new MojoFailureException( + "There where errors when formatting files. Error count: " + failures); + } } catch (IOException exception) { throw new MojoFailureException(exception.getMessage()); } @@ -190,10 +198,10 @@ private FileFilter getPathFilter() { return pathname -> pathname.isDirectory() || pathname.getPath().matches(filesPathPattern); } - private void formatSourceFile(File file, Formatter formatter) { + private boolean formatSourceFile(File file, Formatter formatter) { if (file.isDirectory()) { getLog().info("File '" + file + "' is a directory. Skipping."); - return; + return true; } if (verbose) { @@ -217,8 +225,10 @@ private void formatSourceFile(File file, Formatter formatter) { logNumberOfFilesProcessed(); } } catch (FormatterException | IOException e) { - getLog().warn("Failed to format file '" + file + "'.", e); + getLog().error("Failed to format file '" + file + "'.", e); + return false; } + return true; } private void handleMissingDirectory(String directoryDisplayName, File directory) diff --git a/src/test/java/com/coveo/FMTTest.java b/src/test/java/com/coveo/FMTTest.java index 56e3b02..6c6cc2b 100644 --- a/src/test/java/com/coveo/FMTTest.java +++ b/src/test/java/com/coveo/FMTTest.java @@ -1,10 +1,9 @@ package com.coveo; -import static com.google.common.truth.Truth.*; +import static com.google.common.truth.Truth.assertThat; import java.io.File; import java.util.List; - import org.apache.commons.io.IOUtils; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.testing.MojoRule; @@ -163,6 +162,12 @@ public void checkSucceedsWhenNotFormattedButIgnored() throws Exception { check.execute(); } + @Test(expected = MojoFailureException.class) + public void checkFailsWhenFormattingFails() throws Exception { + Check check = (Check) mojoRule.lookupConfiguredMojo(loadPom("failed_formatting"), CHECK); + check.execute(); + } + private File loadPom(String folderName) { return new File("src/test/resources/", folderName); } diff --git a/src/test/resources/failed_formatting/pom.xml b/src/test/resources/failed_formatting/pom.xml new file mode 100644 index 0000000..79160e0 --- /dev/null +++ b/src/test/resources/failed_formatting/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 3.8.1 + test + + + + + + + com.coveo + fmt-maven-plugin + 1.0.0 + + + + format + + + + + + + + diff --git a/src/test/resources/failed_formatting/src/main/java/HelloWorld1.java b/src/test/resources/failed_formatting/src/main/java/HelloWorld1.java new file mode 100644 index 0000000..6092be6 --- /dev/null +++ b/src/test/resources/failed_formatting/src/main/java/HelloWorld1.java @@ -0,0 +1,25 @@ +package notestsource.src.main.java; + +import com.google.common.base.Preconditions; +import java.util.List; +// force non-contiguous imports +import javax.annotations.Nullable; +import static com.google.truth.Truth.assertThat; +import static org.junit.Assert.fail; + +public class HelloWorld1 { + + void check(@Nullable List x) { + Preconditions.checkNodeNull(x); + } + + void f() { + List xs = null; + assertThat(xs).isNull(); + try { + check(xs); + fail(); + } catch (NullPointerException e) { + } + } +}