Skip to content

Commit f063910

Browse files
committed
fix: correctly classify compiler warnings
1 parent 093b5b8 commit f063910

2 files changed

Lines changed: 59 additions & 4 deletions

File tree

src/main/java/scala_maven_executions/LogProcessorUtils.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package scala_maven_executions;
66

7+
import java.util.regex.Pattern;
8+
79
public class LogProcessorUtils {
810

911
public enum Level {
@@ -17,16 +19,24 @@ public static class LevelState {
1719
public String untilContains = null;
1820
}
1921

22+
/**
23+
* Matches "warning" or "warnings" as complete words, but not filenames e.g. "Warnings.scala".
24+
*/
25+
private static final Pattern WARNING_WORD = Pattern.compile("\\bwarnings?\\b(?!\\.scala)");
26+
27+
/** Matches "error" or "errors" as complete words, but not filenames e.g. "Errors.scala". */
28+
private static final Pattern ERROR_WORD = Pattern.compile("\\berrors?\\b(?!\\.scala)");
29+
2030
public static LevelState levelStateOf(String line, LevelState previous) {
2131
LevelState back = new LevelState();
2232
String lineLowerCase = line.toLowerCase();
23-
if (lineLowerCase.contains("error")) {
24-
back.level = Level.ERROR;
33+
if (isWarningLine(lineLowerCase)) {
34+
back.level = Level.WARNING;
2535
if (lineLowerCase.contains(".scala")) {
2636
back.untilContains = "^";
2737
}
28-
} else if (lineLowerCase.contains("warn")) {
29-
back.level = Level.WARNING;
38+
} else if (isErrorLine(lineLowerCase)) {
39+
back.level = Level.ERROR;
3040
if (lineLowerCase.contains(".scala")) {
3141
back.untilContains = "^";
3242
}
@@ -40,4 +50,30 @@ public static LevelState levelStateOf(String line, LevelState previous) {
4050
}
4151
return back;
4252
}
53+
54+
private static boolean isWarningLine(String line) {
55+
return hasSeverityMarker(line, "warning") || WARNING_WORD.matcher(line).find();
56+
}
57+
58+
private static boolean isErrorLine(String line) {
59+
return hasSeverityMarker(line, "error") || ERROR_WORD.matcher(line).find();
60+
}
61+
62+
/**
63+
* Checks whether the line contains a severity marker e.g. "error:" or "warning:".
64+
*
65+
* <p>Matches common compiler and build-tool formats, including:
66+
*
67+
* <ul>
68+
* <li>{@code path:lineNum: error: message}
69+
* <li>{@code error: message} at the start of the line
70+
* </ul>
71+
*
72+
* @param lineLowerCase the lowercase line to check
73+
* @param marker the severity marker to look for e.g. "error" or "warning"
74+
* @return true if the line starts with {@code marker:} or contains {@code " marker:"}
75+
*/
76+
private static boolean hasSeverityMarker(String lineLowerCase, String marker) {
77+
return lineLowerCase.startsWith(marker + ":") || lineLowerCase.contains(" " + marker + ":");
78+
}
4379
}

src/test/java/scala_maven/LogProcessorUtilsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ public void jdkSplit() throws Exception {
6868
null);
6969
}
7070

71+
@Test
72+
public void severityDeterminedByMarkerNotFilenameOrTextContent() throws Exception {
73+
LevelState previous = new LevelState();
74+
previous =
75+
assertLevelState(
76+
"/path/to/Errors.scala:88: warning: match may not be exhaustive.",
77+
previous,
78+
Level.WARNING,
79+
"^");
80+
previous =
81+
assertLevelState("/path/to/Warning.scala:12: error: boom", previous, Level.ERROR, "^");
82+
previous =
83+
assertLevelState(
84+
"/path/to/FooBar.scala:3657: warning: a type was inferred to be `Any`; this may indicate a programming error.",
85+
previous,
86+
Level.WARNING,
87+
"^");
88+
}
89+
7190
private LevelState assertLevelState(
7291
String input, LevelState previous, Level expectedLevel, String expectedUntilContains) {
7392
LevelState back = LogProcessorUtils.levelStateOf(input, previous);

0 commit comments

Comments
 (0)