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

Add failFast option to allow displaying all errors in build #1032

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.asciidoctor.maven.log;

import java.io.File;
import java.util.Optional;

import org.asciidoctor.ast.Cursor;
import org.asciidoctor.log.LogRecord;

/**
* {@link LogRecord} proxy that allows capturing the source file being
* processed.
* Important: the {@link #sourceFile} and the actual source where an error is present
* may not be the same. For example if the source is being included.
*
* @since 3.1.2
*/
final class CapturedLogRecord extends LogRecord {

private final File sourceFile;

CapturedLogRecord(LogRecord record, File sourceFile) {
super(record.getSeverity(), record.getCursor(), record.getMessage(), record.getSourceFileName(), record.getSourceMethodName());
this.sourceFile = sourceFile;
}

public Cursor getCursor() {
if (sourceFile == null)
return null;

return Optional.ofNullable(super.getCursor())
.orElse(new FileCursor(sourceFile));
}

public File getSourceFile() {
return sourceFile;
}

class FileCursor implements Cursor {

private final File file;

public FileCursor(File file) {
this.file = file;
}

@Override
public int getLineNumber() {
return 0;
}

@Override
public String getPath() {
return file.getName();
}

@Override
public String getDir() {
return file.getParent();
}

@Override
public String getFile() {
return file.getAbsolutePath();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
* POJO for Maven XML mapping.
*
* @author abelsromero
* @since 1.5.7
*/
public class LogHandler {

private Boolean outputToConsole;
private FailIf failIf;
private Boolean failFast;

public LogHandler() {
outputToConsole = Boolean.TRUE;
failFast = Boolean.TRUE;
}

public Boolean getOutputToConsole() {
return outputToConsole;
Expand All @@ -37,4 +44,11 @@ public boolean isContainsTextNotBlank() {
return failIf != null && isNotBlank(failIf.getContainsText());
}

public Boolean getFailFast() {
return failFast;
}

public void setFailFast(Boolean failFast) {
this.failFast = failFast;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import org.asciidoctor.log.LogRecord;
Expand All @@ -23,35 +24,27 @@ public LogRecordsProcessors(LogHandler logHandler,
}

public void processLogRecords(MemoryLogHandler memoryLogHandler) throws Exception {
if (logHandler.isSeveritySet() && logHandler.isContainsTextNotBlank()) {
final Severity severity = logHandler.getFailIf().getSeverity();
final String textToSearch = logHandler.getFailIf().getContainsText();
if (logHandler.isSeveritySet() || logHandler.isContainsTextNotBlank()) {
final Severity severity = Optional.ofNullable(logHandler.getFailIf()).map(FailIf::getSeverity).orElse(null);
final String textToSearch = Optional.ofNullable(logHandler.getFailIf()).map(FailIf::getContainsText).orElse(null);

final List<LogRecord> records = memoryLogHandler.filter(severity, textToSearch);
if (records.size() > 0) {
for (LogRecord record : records) {
errorMessageConsumer.accept(LogRecordFormatter.format(record, sourceDirectory));
}
throw new Exception(String.format("Found %s issue(s) matching severity %s or higher and text '%s'", records.size(), severity, textToSearch));
throw new Exception(getMessage(records, severity, textToSearch));
}
}
}

private String getMessage(List<LogRecord> records, Severity severity, String textToSearch) {
if (logHandler.isSeveritySet() && logHandler.isContainsTextNotBlank()) {
return String.format("Found %s issue(s) matching severity %s or higher and text '%s'", records.size(), severity, textToSearch);
} else if (logHandler.isSeveritySet()) {
final Severity severity = logHandler.getFailIf().getSeverity();
final List<LogRecord> records = memoryLogHandler.filter(severity);
if (records.size() > 0) {
for (LogRecord record : records) {
errorMessageConsumer.accept(LogRecordFormatter.format(record, sourceDirectory));
}
throw new Exception(String.format("Found %s issue(s) of severity %s or higher during conversion", records.size(), severity));
}
} else if (logHandler.isContainsTextNotBlank()) {
final String textToSearch = logHandler.getFailIf().getContainsText();
final List<LogRecord> records = memoryLogHandler.filter(textToSearch);
if (records.size() > 0) {
for (LogRecord record : records) {
errorMessageConsumer.accept(LogRecordFormatter.format(record, sourceDirectory));
}
throw new Exception(String.format("Found %s issue(s) containing '%s'", records.size(), textToSearch));
}
return String.format("Found %s issue(s) of severity %s or higher during conversion", records.size(), severity);
} else {
return String.format("Found %s issue(s) containing '%s'", records.size(), textToSearch);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.asciidoctor.maven.log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -8,6 +9,7 @@
import org.asciidoctor.log.LogHandler;
import org.asciidoctor.log.LogRecord;
import org.asciidoctor.log.Severity;
import org.asciidoctor.maven.commons.StringUtils;


/**
Expand All @@ -23,16 +25,26 @@ public class MemoryLogHandler implements LogHandler {
private final Boolean outputToConsole;
private final Consumer<LogRecord> recordConsumer;

/**
* Provides simple way to inject the current file being processes.
* Will need re-work in concurrent scenarios.
*
* @since 3.1.2
*/
private File currentFile;

public MemoryLogHandler(Boolean outputToConsole, Consumer<LogRecord> recordConsumer) {
this.outputToConsole = outputToConsole == null ? Boolean.FALSE : outputToConsole;
this.recordConsumer = recordConsumer;
}

@Override
public void log(LogRecord logRecord) {
records.add(logRecord);
final CapturedLogRecord record = new CapturedLogRecord(logRecord, currentFile);

records.add(record);
if (outputToConsole)
recordConsumer.accept(logRecord);
recordConsumer.accept(record);
}

public void clear() {
Expand All @@ -46,9 +58,7 @@ public void clear() {
* @return list of filtered logRecords
*/
public List<LogRecord> filter(Severity severity) {
return this.records.stream()
.filter(record -> severityIsHigher(record, severity))
.collect(Collectors.toList());
return filter(severity, null);
}

/**
Expand All @@ -58,16 +68,14 @@ public List<LogRecord> filter(Severity severity) {
* @return list of filtered logRecords
*/
public List<LogRecord> filter(String text) {
return this.records.stream()
.filter(record -> messageContains(record, text))
.collect(Collectors.toList());
return filter(null, text);
}

/**
* Returns LogRecords that are equal or above the severity level and whose message contains text.
*
* @param severity Asciidoctor's severity level
* @param text text to search for in the LogRecords
* @param severity Asciidoctor's severity level (no filter applied when null)
* @param text text to search for in the LogRecords (no filter applied when null)
* @return list of filtered logRecords
*/
public List<LogRecord> filter(Severity severity, String text) {
Expand Down Expand Up @@ -96,11 +104,23 @@ public void processAll() {
}

private static boolean severityIsHigher(LogRecord record, Severity severity) {
return record.getSeverity().ordinal() >= severity.ordinal();
if (severity == null) {
return true;
} else {
return record.getSeverity().ordinal() >= severity.ordinal();
}
}

private static boolean messageContains(LogRecord record, String text) {
return record.getMessage().contains(text);
if (StringUtils.isBlank(text)) {
return true;
} else {
return record.getMessage().contains(text);
}
}

public void setCurrentFile(File currentFile) {
this.currentFile = currentFile;
}

}
Loading