Skip to content

Commit

Permalink
Fix JENKINS-14320
Browse files Browse the repository at this point in the history
- Added option addNewline (default true to maintain current behavior) which can be set to false to stop outputting a newline.
  • Loading branch information
slide committed Jul 5, 2012
1 parent 594b9c7 commit 8b082b7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 50 deletions.
Expand Up @@ -91,6 +91,10 @@ public class BuildLogRegexContent implements EmailContent {

private static final String MATCHED_LINE_HTML_STYLE_DEFAULT_VALUE = null;

private static final String ADD_NEWLINE_ARG_NAME = "addNewline";

private static final boolean ADD_NEWLINE_DEFAULT_VALUE = true;

public String getToken() {
return TOKEN;
}
Expand All @@ -104,38 +108,42 @@ public List<String> getArguments() {
SHOW_TRUNCATED_LINES_ARG_NAME,
SUBST_TEXT_ARG_NAME,
ESCAPE_HTML_ARG_NAME,
MATCHED_LINE_HTML_STYLE_ARG_NAME);
MATCHED_LINE_HTML_STYLE_ARG_NAME,
ADD_NEWLINE_ARG_NAME);
}

public String getHelpText() {
return "Displays lines from the build log that match the regular expression.\n"
+ "<ul>\n"
+ "<li><i>" + REGEX_ARG_NAME + "</i> - Lines that match this regular expression "
+ "are included. See also <i>java.util.regex.Pattern</i><br>\n"
+ "Defaults to \"" + REGEX_DEFAULT_VALUE + "\".\n"
+ "Defaults to \"" + REGEX_DEFAULT_VALUE + "\"</li>.\n"
+ "<li><i>" + LINES_BEFORE_ARG_NAME + "</i> - The number of lines to include "
+ "before the matching line. Lines that overlap with another "
+ "match or <i>linesAfter</i> are only included once.<br>\n"
+ "Defaults to " + LINES_BEFORE_DEFAULT_VALUE + ".\n"
+ "Defaults to " + LINES_BEFORE_DEFAULT_VALUE + ".</li>\n"
+ "<li><i>" + LINES_AFTER_ARG_NAME + "</i> - The number of lines to include "
+ "after the matching line. Lines that overlap with another "
+ "match or <i>linesBefore</i> are only included once.<br>\n"
+ "Defaults to " + LINES_AFTER_DEFAULT_VALUE + ".\n"
+ "Defaults to " + LINES_AFTER_DEFAULT_VALUE + ".</li>\n"
+ "<li><i>" + MAX_MATCHES_ARG_NAME + "</i> - The maximum number of matches "
+ "to include. If 0, all matches will be included.<br>\n"
+ "Defaults to " + MAX_MATCHES_DEFAULT_VALUE + ".\n"
+ "Defaults to " + MAX_MATCHES_DEFAULT_VALUE + ".</li>\n"
+ "<li><i>" + SHOW_TRUNCATED_LINES_ARG_NAME + "</i> - If <i>true</i>, include "
+ "<tt>[...truncated ### lines...]</tt> lines.<br>\n"
+ "Defaults to " + SHOW_TRUNCATED_LINES_DEFAULT_VALUE + ".\n"
+ "Defaults to " + SHOW_TRUNCATED_LINES_DEFAULT_VALUE + ".</li>\n"
+ "<li><i>" + SUBST_TEXT_ARG_NAME + "</i> - If non-null, insert this text into the email "
+ "rather than the entire line.<br>\n"
+ "Defaults to null.\n"
+ "Defaults to null.</li>\n"
+ "<li><i>" + ESCAPE_HTML_ARG_NAME + "</i> - If true, escape HTML.<br>\n"
+ "Defaults to " + ESCAPE_HTML_DEFAULT_VALUE + ".\n"
+ "Defaults to " + ESCAPE_HTML_DEFAULT_VALUE + ".</li>\n"
+ "<li><i>" + MATCHED_LINE_HTML_STYLE_ARG_NAME + "</i> - If non-null, output HTML. "
+ "matched lines will become <code>&lt;b style=\"your-style-value\"&gt;"
+ "html escaped matched line&lt;/b&gt;</code>.<br>\n"
+ "Defaults to null.\n"
+ "Defaults to null.</li>\n"
+ "<li><i>" + ADD_NEWLINE_ARG_NAME + "</i> - If true, adds a newline after "
+ "subsText.<br>\n"
+ "Defaults to true.</li>\n"
+ "</ul>\n";
}

Expand Down Expand Up @@ -163,7 +171,7 @@ private void appendContextLine(StringBuffer buffer, String line, boolean escapeH
buffer.append('\n');
}

private void appendMatchedLine(StringBuffer buffer, String line, boolean escapeHtml, String style) {
private void appendMatchedLine(StringBuffer buffer, String line, boolean escapeHtml, String style, boolean addNewline) {
if (escapeHtml) {
line = StringEscapeUtils.escapeHtml(line);
}
Expand All @@ -180,7 +188,10 @@ private void appendMatchedLine(StringBuffer buffer, String line, boolean escapeH
if (style != null) {
buffer.append("</b>");
}
buffer.append('\n');

if(addNewline) {
buffer.append('\n');
}
}

private void appendLinesTruncated(StringBuffer buffer, int numLinesTruncated, boolean asHtml) {
Expand Down Expand Up @@ -238,6 +249,9 @@ String getContent(BufferedReader reader, Map<String, ?> args)
|| Args.get(args,
ESCAPE_HTML_ARG_NAME,
ESCAPE_HTML_DEFAULT_VALUE);
final boolean addNewline = Args.get(args,
ADD_NEWLINE_ARG_NAME,
ADD_NEWLINE_DEFAULT_VALUE);

final Pattern pattern = Pattern.compile(regex);
final StringBuffer buffer = new StringBuffer();
Expand Down Expand Up @@ -286,7 +300,7 @@ String getContent(BufferedReader reader, Map<String, ?> args)
matcher.appendTail(sb);
line = sb.toString();
}
appendMatchedLine(buffer, line, escapeHtml, matchedLineHtmlStyle);
appendMatchedLine(buffer, line, escapeHtml, matchedLineHtmlStyle, addNewline);
++numMatches;
// Set up to add numLinesStillNeeded
numLinesStillNeeded = contextLinesAfter;
Expand Down
Expand Up @@ -36,88 +36,88 @@ public void testGetContent_emptyBuildLogShouldStayEmpty()
assertEquals( "", result );
}

@Test
@Test
public void testGetContent_matchedLines()
throws Exception
{
final BufferedReader reader = new BufferedReader( new StringReader(
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n" ) );
args.put( "showTruncatedLines", false );
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n" ) );
args.put( "showTruncatedLines", false );

final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "6 ERROR\n9 ERROR\n18 ERROR\n", result );
}

@Test
@Test
public void testGetContent_truncatedAndMatchedLines()
throws Exception
{
final BufferedReader reader = new BufferedReader(new StringReader(
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));

final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "[...truncated 5 lines...]\n6 ERROR\n[...truncated 2 lines...]\n9 ERROR\n[...truncated 8 lines...]\n18 ERROR\n[...truncated 5 lines...]\n", result );
}

@Test
@Test
public void testGetContent_truncatedMatchedAndContextLines()
throws Exception
{
final BufferedReader reader = new BufferedReader(new StringReader(
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
args.put( "linesBefore", 3 );
args.put( "linesAfter", 3 );
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
args.put( "linesBefore", 3 );
args.put( "linesAfter", 3 );
final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "[...truncated 2 lines...]\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n[...truncated 2 lines...]\n15\n16\n17\n18 ERROR\n19\n20\n21\n[...truncated 2 lines...]\n", result );
}

@Test
@Test
public void testGetContent_matchedAndContextLines()
throws Exception
{
final BufferedReader reader = new BufferedReader(new StringReader(
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
args.put( "showTruncatedLines", false );
args.put( "linesBefore", 3 );
args.put( "linesAfter", 3 );
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
args.put( "showTruncatedLines", false );
args.put( "linesBefore", 3 );
args.put( "linesAfter", 3 );
final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n15\n16\n17\n18 ERROR\n19\n20\n21\n", result );
}

@Test
@Test
public void testGetContent_truncatedMatchedAndContextLinesAsHtml()
throws Exception
{
final BufferedReader reader = new BufferedReader(new StringReader(
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
args.put( "matchedLineHtmlStyle", "color: red" );
args.put( "linesBefore", 3 );
args.put( "linesAfter", 3 );
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
args.put( "matchedLineHtmlStyle", "color: red" );
args.put( "linesBefore", 3 );
args.put( "linesAfter", 3 );
final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "<p>[...truncated 2 lines...]</p>\n<pre>\n3\n4\n5\n<b style=\"color: red\">6 ERROR</b>\n7\n8\n<b style=\"color: red\">9 ERROR</b>\n10\n11\n12\n</pre>\n<p>[...truncated 2 lines...]</p>\n<pre>\n15\n16\n17\n<b style=\"color: red\">18 ERROR</b>\n19\n20\n21\n</pre>\n<p>[...truncated 2 lines...]</p>\n", result );
}

@Test
@Test
public void testGetContent_matchedAndContextLinesAsHtml()
throws Exception
{
final BufferedReader reader = new BufferedReader(new StringReader(
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
args.put( "matchedLineHtmlStyle", "color: red" );
args.put( "linesBefore", 3 );
args.put( "linesAfter", 3 );
args.put( "showTruncatedLines", false );
"1\n2\n3\n4\n5\n6 ERROR\n7\n8\n9 ERROR\n10\n11\n12\n13\n14\n15\n16\n17\n18 ERROR\n19\n20\n21\n22\n23\n"));
args.put( "matchedLineHtmlStyle", "color: red" );
args.put( "linesBefore", 3 );
args.put( "linesAfter", 3 );
args.put( "showTruncatedLines", false );
final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "<pre>\n3\n4\n5\n<b style=\"color: red\">6 ERROR</b>\n7\n8\n<b style=\"color: red\">9 ERROR</b>\n10\n11\n12\n15\n16\n17\n<b style=\"color: red\">18 ERROR</b>\n19\n20\n21\n</pre>\n", result );
}

@Test
@Test
public void testGetContent_errorMatchedAndNothingReplaced()
throws Exception
{
Expand Down Expand Up @@ -158,7 +158,7 @@ public void testGetContent_prefixMatchedTruncatedAndStripped()
throws Exception
{
final BufferedReader reader = new BufferedReader(
new StringReader( "prefix: Yes\nRandom Line\nprefix: No\n" ) );
new StringReader( "prefix: Yes\nRandom Line\nprefix: No\n" ) );
args.put( "regex", "^prefix: (.*)$");
args.put( "showTruncatedLines", false);
args.put( "substText", "$1");
Expand All @@ -173,9 +173,9 @@ public void testGetContent_escapeHtml()
throws Exception
{
final BufferedReader reader = new BufferedReader(
new StringReader( "error <>&\"" ) );
args.put( "showTruncatedLines", false );
args.put( "escapeHtml", true );
new StringReader( "error <>&\"" ) );
args.put( "showTruncatedLines", false );
args.put( "escapeHtml", true );

final String result = buildLogRegexContent.getContent( reader, args );

Expand All @@ -187,23 +187,23 @@ public void testGetContent_matchedLineHtmlStyleEmpty()
throws Exception
{
final BufferedReader reader = new BufferedReader(
new StringReader( "error" ) );
args.put( "showTruncatedLines", false );
args.put( "matchedLineHtmlStyle", "" );
new StringReader( "error" ) );
args.put( "showTruncatedLines", false );
args.put( "matchedLineHtmlStyle", "" );

final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "<pre>\n<b>error</b>\n</pre>\n", result );
}

@Test
@Test
public void testGetContent_matchedLineHtmlStyle()
throws Exception
{
final BufferedReader reader = new BufferedReader(
new StringReader( "error" ) );
args.put( "showTruncatedLines", false );
args.put( "matchedLineHtmlStyle", "color: red");
new StringReader( "error" ) );
args.put( "showTruncatedLines", false );
args.put( "matchedLineHtmlStyle", "color: red");

final String result = buildLogRegexContent.getContent( reader, args );

Expand All @@ -218,10 +218,27 @@ public void testGetContent_shouldStripOutConsoleNotes()
args.put( "regex", ".*");
args.put( "showTruncatedLines", false);
final BufferedReader reader = new BufferedReader(
new StringReader( ConsoleNote.PREAMBLE_STR + "AAAAdB+LCAAAAAAAAABb85aBtbiIQSOjNKU4P0+vIKc0PTOvWK8kMze1uCQxtyC1SC8ExvbLL0llgABGJgZGLwaB3MycnMzi4My85FTXgvzkjIoiBimoScn5ecX5Oal6zhAaVS9DRQGQ1uaZsmc5AAaMIAyBAAAA" + ConsoleNote.POSTAMBLE_STR + "No emails were triggered." ) );
new StringReader( ConsoleNote.PREAMBLE_STR + "AAAAdB+LCAAAAAAAAABb85aBtbiIQSOjNKU4P0+vIKc0PTOvWK8kMze1uCQxtyC1SC8ExvbLL0llgABGJgZGLwaB3MycnMzi4My85FTXgvzkjIoiBimoScn5ecX5Oal6zhAaVS9DRQGQ1uaZsmc5AAaMIAyBAAAA" + ConsoleNote.POSTAMBLE_STR + "No emails were triggered." ) );

final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "No emails were triggered.\n", result);
}

@Test
public void testGetContent_addNewLineFalse()
throws Exception
{
// See JENKINS-14320
args.put( "addNewline", false );
args.put( "regex", "^\\*{3} Application: (.*)$" );
args.put( "maxMatches", 1);
args.put( "showTruncatedLines", false );
args.put( "substText", "$1" );
final BufferedReader reader = new BufferedReader(
new StringReader( "*** Application: Firefox 15.0a2\n*** Platform: Mac OS X 10.7.4 64bit" ));
final String result = buildLogRegexContent.getContent( reader, args );

assertEquals( "Firefox 15.0a2", result);
}
}

0 comments on commit 8b082b7

Please sign in to comment.