This is an automated email from the ASF dual-hosted git repository. janbednar pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new c3850ba CAMEL-15638: Print detailed diff if source validation fails (#4366) c3850ba is described below commit c3850ba80548b639a2bb636983d5b1c497971085 Author: Jan Bednar <m...@janbednar.eu> AuthorDate: Mon Oct 5 22:17:05 2020 +0200 CAMEL-15638: Print detailed diff if source validation fails (#4366) --- init/camel-format-plugin/pom.xml | 6 ++++ .../net/revelc/code/formatter/FormatterMojo.java | 7 +++- .../net/revelc/code/formatter/ValidateMojo.java | 37 ++++++++++++++++++++-- pom.xml | 1 + 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/init/camel-format-plugin/pom.xml b/init/camel-format-plugin/pom.xml index 6d5bda7..234614b 100644 --- a/init/camel-format-plugin/pom.xml +++ b/init/camel-format-plugin/pom.xml @@ -41,6 +41,12 @@ <version>${formatter-maven-plugin.version}</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>io.github.java-diff-utils</groupId> + <artifactId>java-diff-utils</artifactId> + <version>${java-diff-utils-version}</version> + <scope>compile</scope> + </dependency> <dependency> <groupId>org.apache.maven</groupId> diff --git a/init/camel-format-plugin/src/main/java/net/revelc/code/formatter/FormatterMojo.java b/init/camel-format-plugin/src/main/java/net/revelc/code/formatter/FormatterMojo.java index d394222..b3d96dc 100644 --- a/init/camel-format-plugin/src/main/java/net/revelc/code/formatter/FormatterMojo.java +++ b/init/camel-format-plugin/src/main/java/net/revelc/code/formatter/FormatterMojo.java @@ -32,6 +32,8 @@ import java.util.Properties; import org.xml.sax.SAXException; +import com.github.difflib.DiffUtils; +import com.github.difflib.patch.Patch; import com.google.common.base.Strings; import com.google.common.hash.Hashing; import net.revelc.code.formatter.css.CssFormatter; @@ -609,6 +611,7 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource { rc.skippedCount++; } else if (Result.SUCCESS.equals(result)) { rc.successCount++; + rc.diff.add(DiffUtils.diff(originalCode, formattedCode, null)); } else if (Result.FAIL.equals(result)) { rc.failCount++; return; @@ -824,7 +827,7 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource { return map; } - class ResultCollector { + static class ResultCollector { int successCount; @@ -833,6 +836,8 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource { int skippedCount; int readOnlyCount; + + List<Patch<String>> diff = new ArrayList<>(); } @Override diff --git a/init/camel-format-plugin/src/main/java/net/revelc/code/formatter/ValidateMojo.java b/init/camel-format-plugin/src/main/java/net/revelc/code/formatter/ValidateMojo.java index 9ecf0f8..2638675 100755 --- a/init/camel-format-plugin/src/main/java/net/revelc/code/formatter/ValidateMojo.java +++ b/init/camel-format-plugin/src/main/java/net/revelc/code/formatter/ValidateMojo.java @@ -15,8 +15,11 @@ package net.revelc.code.formatter; import java.io.File; import java.io.IOException; +import java.util.List; import java.util.Properties; +import com.github.difflib.patch.AbstractDelta; +import com.github.difflib.patch.Patch; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; @@ -55,12 +58,42 @@ public class ValidateMojo extends FormatterMojo { super.doFormatFile(file, rc, hashCache, basedirPath, true); if (rc.successCount != 0) { - throw new MojoFailureException("File '" + file - + "' has not been previously formatted. Please format file and commit before running validation!"); + final String message = String.format( + "File '%s' has not been previously formatted. Please format file and commit before running validation!%n%s", + file, diffToString(rc.diff)); + throw new MojoFailureException(message); } if (rc.failCount != 0) { throw new MojoExecutionException("Error formating '" + file + "' "); } } + private static String diffToString(List<Patch<String>> diff) { + final String NL = System.lineSeparator(); + StringBuilder sb = new StringBuilder(); + + for (Patch<String> patch : diff) { + for (AbstractDelta<String> delta : patch.getDeltas()) { + sb.append("Type:\t").append(delta.getType()).append(NL); + if (delta.getSource() != null) { + sb.append("Line:\t").append(delta.getSource().getPosition()).append(NL); + if (delta.getSource().getLines().size() > 0) { + sb.append("Source:").append(NL); + for (String line : delta.getSource().getLines()) { + sb.append(line).append(NL); + } + } + } + + if (delta.getTarget() != null && delta.getTarget().getLines().size() > 0) { + sb.append("Target:").append(NL); + for (String line : delta.getTarget().getLines()) { + sb.append(line).append(NL); + } + } + sb.append("===============").append(NL); + } + } + return sb.toString(); + } } diff --git a/pom.xml b/pom.xml index 689b57e..62efabd 100644 --- a/pom.xml +++ b/pom.xml @@ -118,6 +118,7 @@ <surefire.version>${maven-surefire-plugin-version}</surefire.version> <formatter-maven-plugin.version>2.12.1</formatter-maven-plugin.version> <impsort-maven-plugin.version>1.3.2</impsort-maven-plugin.version> + <java-diff-utils-version>4.7</java-diff-utils-version> <maven-bundle-plugin-version>4.2.1</maven-bundle-plugin-version> <!-- eclipse plugin need the jaxb in this pom.xml file -->