Skip to content

Commit

Permalink
Fix JENKINS-49746
Browse files Browse the repository at this point in the history
Added greedy parameter to break out of match checking when numMatches reaches maxMatches
  • Loading branch information
slide committed Apr 10, 2018
1 parent cd58ccc commit 135d698
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
Expand Up @@ -54,6 +54,8 @@ public class BuildLogRegexMacro extends DataBoundTokenMacro {
public boolean addNewline = true;
@Parameter
public String defaultValue = "";
@Parameter
public boolean greedy = true;

@Override
public boolean acceptsMacroName(String macroName) {
Expand Down Expand Up @@ -180,7 +182,7 @@ String getContent(BufferedReader reader)
break;
}
}
if (matched) {
if (matched && (greedy || (numMatches < maxMatches))) {
// The current line matches.
if (showTruncatedLines == true && numLinesTruncated > 0) {
// Append information about truncated lines.
Expand Down
Expand Up @@ -4,9 +4,12 @@
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import java.io.InputStreamReader;
import java.io.StringReader;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -241,5 +244,24 @@ public void testGetContent_defaultValue()

assertEquals("JENKINS", result);
}

@Test @Issue("JENKINS-49746")
public void testOverlappingResults()
throws Exception {
buildLogRegexMacro.regex = "(?m)^(nc\\w+:\\s+\\*E,|.*\\bNG\\b|\\s*Error\\b|E-).*$";
buildLogRegexMacro.linesBefore = 5;
buildLogRegexMacro.greedy = false;
buildLogRegexMacro.linesAfter = 10;
buildLogRegexMacro.maxMatches = 5;
buildLogRegexMacro.showTruncatedLines = false;

when(build.getLogReader()).thenReturn(
new InputStreamReader(getClass().getResourceAsStream("JENKINS-49746-input.txt")));

final String result = buildLogRegexMacro.evaluate(build, listener, BuildLogRegexMacro.MACRO_NAME);

final String expected = IOUtils.toString(getClass().getResourceAsStream("JENKINS-49746-output.txt"));
assertEquals(expected.replaceAll("\r\n", "\n").trim(), result.replaceAll("\r\n", "\n").trim());
}
}

@@ -0,0 +1,95 @@
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
@@ -0,0 +1,31 @@
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion yet_another_location has failed
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *W,ASRTST (some_other_string): (some_time) Assertion some_other_location has failed
A MESSAGE
ncsim: *E,ASRTST (some_string): (some_time) Assertion some_location has failed

0 comments on commit 135d698

Please sign in to comment.