Skip to content
Merged
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
44 changes: 40 additions & 4 deletions src/main/java/scala_maven_executions/LogProcessorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package scala_maven_executions;

import java.util.regex.Pattern;

public class LogProcessorUtils {

public enum Level {
Expand All @@ -17,16 +19,24 @@ public static class LevelState {
public String untilContains = null;
}

/**
* Matches "warning" or "warnings" as complete words, but not filenames e.g. "Warnings.scala".
*/
private static final Pattern WARNING_WORD = Pattern.compile("\\bwarnings?\\b(?!\\.scala)");

/** Matches "error" or "errors" as complete words, but not filenames e.g. "Errors.scala". */
private static final Pattern ERROR_WORD = Pattern.compile("\\berrors?\\b(?!\\.scala)");

public static LevelState levelStateOf(String line, LevelState previous) {
LevelState back = new LevelState();
String lineLowerCase = line.toLowerCase();
if (lineLowerCase.contains("error")) {
back.level = Level.ERROR;
if (isWarningLine(lineLowerCase)) {
back.level = Level.WARNING;
if (lineLowerCase.contains(".scala")) {
back.untilContains = "^";
}
} else if (lineLowerCase.contains("warn")) {
back.level = Level.WARNING;
} else if (isErrorLine(lineLowerCase)) {
back.level = Level.ERROR;
if (lineLowerCase.contains(".scala")) {
back.untilContains = "^";
}
Expand All @@ -40,4 +50,30 @@ public static LevelState levelStateOf(String line, LevelState previous) {
}
return back;
}

private static boolean isWarningLine(String line) {
return hasSeverityMarker(line, "warning") || WARNING_WORD.matcher(line).find();
}

private static boolean isErrorLine(String line) {
return hasSeverityMarker(line, "error") || ERROR_WORD.matcher(line).find();
}

/**
* Checks whether the line contains a severity marker e.g. "error:" or "warning:".
*
* <p>Matches common compiler and build-tool formats, including:
*
* <ul>
* <li>{@code path:lineNum: error: message}
* <li>{@code error: message} at the start of the line
* </ul>
*
* @param lineLowerCase the lowercase line to check
* @param marker the severity marker to look for e.g. "error" or "warning"
* @return true if the line starts with {@code marker:} or contains {@code " marker:"}
*/
private static boolean hasSeverityMarker(String lineLowerCase, String marker) {
return lineLowerCase.startsWith(marker + ":") || lineLowerCase.contains(" " + marker + ":");
}
}
19 changes: 19 additions & 0 deletions src/test/java/scala_maven/LogProcessorUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ public void jdkSplit() throws Exception {
null);
}

@Test
public void severityDeterminedByMarkerNotFilenameOrTextContent() throws Exception {
LevelState previous = new LevelState();
previous =
assertLevelState(
"/path/to/Errors.scala:88: warning: match may not be exhaustive.",
previous,
Level.WARNING,
"^");
previous =
assertLevelState("/path/to/Warning.scala:12: error: boom", previous, Level.ERROR, "^");
previous =
assertLevelState(
"/path/to/FooBar.scala:3657: warning: a type was inferred to be `Any`; this may indicate a programming error.",
previous,
Level.WARNING,
"^");
}

private LevelState assertLevelState(
String input, LevelState previous, Level expectedLevel, String expectedUntilContains) {
LevelState back = LogProcessorUtils.levelStateOf(input, previous);
Expand Down