Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[draft] Solution for issue 270 to show all validation errors #644

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/net/revelc/code/formatter/FormatterMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,11 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro
return;
}

// Now write the file
if (!dryRun) {
// Now write the file or log during dry run
if (dryRun) {
String errorMessage = String.format("File '%s' has not been previously formatted.", file);
this.getLog().error(errorMessage);
} else {
this.writeStringToFile(formattedCode, file);
}
}
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/net/revelc/code/formatter/ValidateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,27 @@ public class ValidateMojo extends FormatterMojo {
@Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
private MojoExecution mojoExecution;

private boolean validationErrors;

/**
* When set to true, fail validation on first file formatter validation error.
*
* @since 2.20.0
*/
@Parameter(defaultValue = "true", property = "formatter.failOnFirstValidationError")
private boolean failOnFirstValidationError;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.aggregator && !this.executionRoot) {
return;
}

super.execute();

if (this.validationErrors) {
throw new MojoFailureException(errorMessage());
}
}

@Override
Expand All @@ -78,10 +92,11 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro
super.doFormatFile(file, rc, hashCache, basedirPath, true);

if (rc.successCount != 0) {
String errorMessage = String.format(
"File '%s' has not been previously formatted. Please format file (for example by invoking `%s`) and commit before running validation!",
file, formatCommand());
throw new MojoFailureException(errorMessage);
if (failOnFirstValidationError) {
throw new MojoFailureException(errorMessage());
} else {
this.validationErrors = true;
}
}
if (rc.failCount != 0) {
throw new MojoExecutionException("Error formatting '" + file + "' ");
Expand All @@ -97,4 +112,14 @@ private String formatCommand() {
return String.format("mvn %s %s", specifyModule, mojoInvocation).replace(" ", " ");
}

private String errorMessage() {
StringBuilder builder = new StringBuilder();
builder.append("Formatter validation detected, see file above that triggered violation. ");
builder.append("To see all violations, use `-Dformatter.failOnFirstValidationError=false`. ");
builder.append("Please format file(s) (for example by invoking `");
builder.append(formatCommand());
builder.append("` and commit before running validation!");
return builder.toString();
}

}