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

Parameterize the max line length for the report table. #30

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
49 changes: 45 additions & 4 deletions src/main/java/se/bjurr/violations/lib/ViolationsReporterApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
import se.bjurr.violations.lib.model.Violation;

public class ViolationsReporterApi {
private static final int NO_MAX_LINE_LENGTH = -1;
/** Allow up to 100 characters for the error message if no max line length is specified. */
private static final int DEFAULT_MAX_MESSAGE_LENGTH = 100;

private Iterable<Violation> violations;
private int maxLineLength = NO_MAX_LINE_LENGTH;

private ViolationsReporterApi() {}

Expand Down Expand Up @@ -72,6 +76,25 @@ private StringBuilder toPerFile(Iterable<Violation> violations) {
return sb;
}

/**
* Adds line breaks to the Message column of the report to ensure the table's lines are less than
* or equal to the given limit.
*
* <p>If the other columns and whitespace are are greater than or equal to the requested maximum
* line length, the Message column will always print at least a single character per line, even if
* doing so causes the table to exceed the requested length.
*
* @param maxLineLength The requested maximum line length.
* @return This object.
*/
public ViolationsReporterApi withMaxLineLength(int maxLineLength) {
if (maxLineLength <= 0) {
throw new IllegalArgumentException("maxLineLength must be > 0, but given: " + maxLineLength);
}
this.maxLineLength = maxLineLength;
return this;
}

public ViolationsReporterApi withViolations(List<Violation> violations) {
this.violations = violations;
return this;
Expand All @@ -80,32 +103,50 @@ public ViolationsReporterApi withViolations(List<Violation> violations) {
private StringBuilder toDetailed(Iterable<Violation> violations, String summarySubject) {
final StringBuilder sb = new StringBuilder();
final List<String[]> rows = new ArrayList<>();

int longestMessageLineLengthLength = -1;
for (final Violation violation : violations) {
final String[] row = {
violation.getReporter(),
violation.getRule().or(""),
violation.getSeverity().name(),
violation.getStartLine().toString(),
addNewlines(violation.getMessage())
violation.getMessage(),
};
for (String substring : violation.getMessage().split("\n")) {
longestMessageLineLengthLength =
Math.max(longestMessageLineLengthLength, substring.length());
}
rows.add(row);
}

final String[] headers = {"Reporter", "Rule", "Severity", "Line", "Message"};

final String[][] data = rows.toArray(new String[][] {});

final int maximumMessageLength;
if (maxLineLength == NO_MAX_LINE_LENGTH) {
maximumMessageLength = DEFAULT_MAX_MESSAGE_LENGTH;
} else {
final String infiniteLengthTable = FlipTable.of(headers, data);
final int tableLineLength = infiniteLengthTable.indexOf("\n");
final int nonMessageLength = tableLineLength - longestMessageLineLengthLength;
maximumMessageLength = Math.max(1, maxLineLength - nonMessageLength);
}
for (String[] row : data) {
row[4] = addNewlines(row[4], maximumMessageLength);
}

sb.append(FlipTable.of(headers, data));
sb.append("\n");
sb.append(toCompact(violations, summarySubject));
sb.append("\n");
return sb;
}

private String addNewlines(String message) {
private String addNewlines(String message, int maxLineLength) {
if (message == null) {
return "";
}
final int maxLineLength = 100;
int noLineCounter = 0;
final StringBuilder withNewlines = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package se.bjurr.violations.lib;

import static org.junit.Assert.assertTrue;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi;
Expand Down Expand Up @@ -127,4 +128,46 @@ public void testVerboseWithZeroViolations() {

LOG.info("\n" + report);
}

@Test
public void testCompactWithLineLength() {
final String report =
violationsReporterApi() //
.withViolations(accumulatedViolations) //
.withMaxLineLength(100) //
.getReport(COMPACT);

LOG.info("\n" + report);
for (String line : report.split("\n")) {
assertTrue("Got: " + line.length(), line.length() <= 100);
}
}

@Test
public void testPerFileCompactWithLineLength() {
final String report =
violationsReporterApi() //
.withViolations(accumulatedViolations) //
.withMaxLineLength(100) //
.getReport(PER_FILE_COMPACT);

LOG.info("\n" + report);
for (String line : report.split("\n")) {
assertTrue("Got: " + line.length(), line.length() <= 100);
}
}

@Test
public void testVerboseWithLineLength() {
final String report =
violationsReporterApi() //
.withViolations(accumulatedViolations) //
.withMaxLineLength(100) //
.getReport(VERBOSE);

LOG.info("\n" + report);
for (String line : report.split("\n")) {
assertTrue("Got: " + line.length(), line.length() <= 100);
}
}
}