From 8b21d578e7c9826ce1040d93b904cf772e884620 Mon Sep 17 00:00:00 2001 From: Klara Ward Date: Tue, 24 May 2022 11:38:49 +0200 Subject: [PATCH] Add warningOnly parameter --- pom.xml | 6 +++ src/main/java/com/spotify/fmt/Check.java | 30 +++++++++----- src/test/java/com/spotify/fmt/FMTTest.java | 41 +++++++++++++++++++ .../warningonly_formatted/invoker.properties | 3 ++ .../resources/warningonly_formatted/pom.xml | 41 +++++++++++++++++++ .../warningonly_formatted/postbuild.groovy | 2 + .../src/main/java/HelloWorld1.java | 7 ++++ .../invoker.properties | 3 ++ .../warningonly_notformatted/pom.xml | 41 +++++++++++++++++++ .../warningonly_notformatted/postbuild.groovy | 2 + .../src/main/java/HelloWorld1.java | 8 ++++ .../invoker.properties | 2 + .../warningonlyfalse_notformatted/pom.xml | 41 +++++++++++++++++++ .../src/main/java/HelloWorld1.java | 7 ++++ 14 files changed, 224 insertions(+), 10 deletions(-) create mode 100644 src/test/resources/warningonly_formatted/invoker.properties create mode 100644 src/test/resources/warningonly_formatted/pom.xml create mode 100644 src/test/resources/warningonly_formatted/postbuild.groovy create mode 100644 src/test/resources/warningonly_formatted/src/main/java/HelloWorld1.java create mode 100644 src/test/resources/warningonly_notformatted/invoker.properties create mode 100644 src/test/resources/warningonly_notformatted/pom.xml create mode 100644 src/test/resources/warningonly_notformatted/postbuild.groovy create mode 100644 src/test/resources/warningonly_notformatted/src/main/java/HelloWorld1.java create mode 100644 src/test/resources/warningonlyfalse_notformatted/invoker.properties create mode 100644 src/test/resources/warningonlyfalse_notformatted/pom.xml create mode 100644 src/test/resources/warningonlyfalse_notformatted/src/main/java/HelloWorld1.java diff --git a/pom.xml b/pom.xml index 86836c7..fba5961 100644 --- a/pom.xml +++ b/pom.xml @@ -123,6 +123,12 @@ 0.28 test + + org.mockito + mockito-core + 4.3.1 + test + diff --git a/src/main/java/com/spotify/fmt/Check.java b/src/main/java/com/spotify/fmt/Check.java index e0e9e96..1484d28 100644 --- a/src/main/java/com/spotify/fmt/Check.java +++ b/src/main/java/com/spotify/fmt/Check.java @@ -3,6 +3,7 @@ import static java.lang.Math.max; import static java.lang.String.format; +import java.util.function.Consumer; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -23,6 +24,10 @@ public class Check extends AbstractFMT { @Parameter(defaultValue = "100", property = "displayLimit") private int displayLimit; + /** Only show warnings instead of failing */ + @Parameter(defaultValue = "false", property = "warningOnly") + private boolean warningOnly; + /** * Post Execute action. It is called at the end of the execute method. Subclasses can add extra * checks. @@ -32,12 +37,16 @@ public class Check extends AbstractFMT { */ @Override protected void postExecute(FormattingResult result) throws MojoFailureException { + Consumer messageConsumer = warningOnly ? getLog()::warn : getLog()::error; if (!result.nonComplyingFiles().isEmpty()) { String message = - "Found " + result.nonComplyingFiles().size() + " non-complying files, failing build"; - getLog().error(message); - getLog() - .error("To fix formatting errors, run \"mvn com.spotify.fmt:fmt-maven-plugin:format\""); + "Found " + + result.nonComplyingFiles().size() + + " non-complying files" + + (warningOnly ? "" : ", failing build"); + messageConsumer.accept(message); + messageConsumer.accept( + "To fix formatting errors, run \"mvn com.spotify.fmt:fmt-maven-plugin:format\""); // do not support limit < 1 displayLimit = max(1, displayLimit); @@ -45,16 +54,17 @@ protected void postExecute(FormattingResult result) throws MojoFailureException if (displayFiles) { result.nonComplyingFiles().stream() .limit(displayLimit) - .forEach(path -> getLog().error("Non complying file: " + path)); + .forEach(path -> messageConsumer.accept("Non complying file: " + path)); if (result.nonComplyingFiles().size() > displayLimit) { - getLog() - .error( - format( - "... and %d more files.", result.nonComplyingFiles().size() - displayLimit)); + messageConsumer.accept( + format("... and %d more files.", result.nonComplyingFiles().size() - displayLimit)); } } - throw new MojoFailureException(message); + + if (!warningOnly) { + throw new MojoFailureException(message); + } } } diff --git a/src/test/java/com/spotify/fmt/FMTTest.java b/src/test/java/com/spotify/fmt/FMTTest.java index cb6b142..65705b0 100644 --- a/src/test/java/com/spotify/fmt/FMTTest.java +++ b/src/test/java/com/spotify/fmt/FMTTest.java @@ -3,14 +3,18 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; +import static org.mockito.AdditionalMatchers.not; import java.io.File; import java.util.List; import org.apache.commons.io.IOUtils; +import org.apache.maven.plugin.Mojo; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.testing.MojoRule; import org.junit.Rule; import org.junit.Test; +import org.mockito.Mockito; public class FMTTest { private static String FORMAT = "format"; @@ -213,6 +217,37 @@ public void checkFailsWhenFormattingFails() throws Exception { check.execute(); } + @Test + public void checkWarnsWhenNotFormattedAndConfiguredWithWarningOnlyTrue() throws Exception { + Check check = loadMojo("warningonly_notformatted", CHECK); + Log logSpy = setupLogSpy(check); + + check.execute(); + + Mockito.verify(logSpy).warn(Mockito.matches(".*non-complying files.*")); + } + + @Test + public void checkDoesNotWarnWhenFormattedAndConfiguredWithWarningOnlyTrue() throws Exception { + Check check = loadMojo("warningonly_formatted", CHECK); + Log logSpy = setupLogSpy(check); + + check.execute(); + + Mockito.verify(logSpy).warn(not(Mockito.matches(".*non-complying files.*"))); + } + + @Test(expected = MojoFailureException.class) + public void checkFailsAndLogsErrorWhenFormattingFailsAndConfiguredWithWarningOnlyFalse() + throws Exception { + Check check = loadMojo("warningonlyfalse_notformatted", CHECK); + Log logSpy = setupLogSpy(check); + + check.execute(); + + Mockito.verify(logSpy).error(Mockito.matches(".*non-complying files.*")); + } + @SuppressWarnings("unchecked") private T loadMojo(String pomFilePath, String goal) throws Exception { File pomFile = loadPom(pomFilePath); @@ -226,6 +261,12 @@ private File loadPom(String folderName) { return new File("src/test/resources/", folderName); } + private Log setupLogSpy(Mojo mojo) { + Log spy = Mockito.spy(mojo.getLog()); + mojo.setLog(spy); + return spy; + } + private static boolean javaRuntimeStronglyEncapsulatesByDefault() { return Runtime.version().compareTo(Runtime.Version.parse("16")) >= 0; } diff --git a/src/test/resources/warningonly_formatted/invoker.properties b/src/test/resources/warningonly_formatted/invoker.properties new file mode 100644 index 0000000..55d7553 --- /dev/null +++ b/src/test/resources/warningonly_formatted/invoker.properties @@ -0,0 +1,3 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:check +invoker.streamLogs = false +invoker.logDirectory = logs diff --git a/src/test/resources/warningonly_formatted/pom.xml b/src/test/resources/warningonly_formatted/pom.xml new file mode 100644 index 0000000..e7dfac7 --- /dev/null +++ b/src/test/resources/warningonly_formatted/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 4.13.1 + test + + + + + + + com.spotify.fmt + fmt-maven-plugin + 2.12 + + true + + + + + check + + + + + + + + diff --git a/src/test/resources/warningonly_formatted/postbuild.groovy b/src/test/resources/warningonly_formatted/postbuild.groovy new file mode 100644 index 0000000..4018013 --- /dev/null +++ b/src/test/resources/warningonly_formatted/postbuild.groovy @@ -0,0 +1,2 @@ +String buildLog = new File("${basedir}/build.log").getText("UTF-8") +assert !buildLog.contains("non-complying files") \ No newline at end of file diff --git a/src/test/resources/warningonly_formatted/src/main/java/HelloWorld1.java b/src/test/resources/warningonly_formatted/src/main/java/HelloWorld1.java new file mode 100644 index 0000000..b2d5ee8 --- /dev/null +++ b/src/test/resources/warningonly_formatted/src/main/java/HelloWorld1.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/src/test/resources/warningonly_notformatted/invoker.properties b/src/test/resources/warningonly_notformatted/invoker.properties new file mode 100644 index 0000000..55d7553 --- /dev/null +++ b/src/test/resources/warningonly_notformatted/invoker.properties @@ -0,0 +1,3 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:check +invoker.streamLogs = false +invoker.logDirectory = logs diff --git a/src/test/resources/warningonly_notformatted/pom.xml b/src/test/resources/warningonly_notformatted/pom.xml new file mode 100644 index 0000000..e7dfac7 --- /dev/null +++ b/src/test/resources/warningonly_notformatted/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 4.13.1 + test + + + + + + + com.spotify.fmt + fmt-maven-plugin + 2.12 + + true + + + + + check + + + + + + + + diff --git a/src/test/resources/warningonly_notformatted/postbuild.groovy b/src/test/resources/warningonly_notformatted/postbuild.groovy new file mode 100644 index 0000000..c59a767 --- /dev/null +++ b/src/test/resources/warningonly_notformatted/postbuild.groovy @@ -0,0 +1,2 @@ +String buildLog = new File("${basedir}/build.log").getText("UTF-8") +assert buildLog.contains("non-complying files") diff --git a/src/test/resources/warningonly_notformatted/src/main/java/HelloWorld1.java b/src/test/resources/warningonly_notformatted/src/main/java/HelloWorld1.java new file mode 100644 index 0000000..f36184b --- /dev/null +++ b/src/test/resources/warningonly_notformatted/src/main/java/HelloWorld1.java @@ -0,0 +1,8 @@ +package notestsource.src.main.java; + +public +class HelloWorld1 { + public static void main(String[] args) { + System.out.println("Hello World!");; + } +} diff --git a/src/test/resources/warningonlyfalse_notformatted/invoker.properties b/src/test/resources/warningonlyfalse_notformatted/invoker.properties new file mode 100644 index 0000000..4a608bb --- /dev/null +++ b/src/test/resources/warningonlyfalse_notformatted/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:check +invoker.buildResult = failure diff --git a/src/test/resources/warningonlyfalse_notformatted/pom.xml b/src/test/resources/warningonlyfalse_notformatted/pom.xml new file mode 100644 index 0000000..a2ff6cd --- /dev/null +++ b/src/test/resources/warningonlyfalse_notformatted/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 4.13.1 + test + + + + + + + com.spotify.fmt + fmt-maven-plugin + 2.12 + + false + + + + + check + + + + + + + + diff --git a/src/test/resources/warningonlyfalse_notformatted/src/main/java/HelloWorld1.java b/src/test/resources/warningonlyfalse_notformatted/src/main/java/HelloWorld1.java new file mode 100644 index 0000000..e8a2500 --- /dev/null +++ b/src/test/resources/warningonlyfalse_notformatted/src/main/java/HelloWorld1.java @@ -0,0 +1,7 @@ +package notestsource.src.main.java; + +public class HelloWorld1 { +public static void main(String[] args) { +System.out.println("Hello World!"); +} +}