diff --git a/src/main/java/org/jenkinsci/plugins/tokenmacro/impl/BuildLogExcerptMacro.java b/src/main/java/org/jenkinsci/plugins/tokenmacro/impl/BuildLogExcerptMacro.java index b8151a3..1bab9a7 100644 --- a/src/main/java/org/jenkinsci/plugins/tokenmacro/impl/BuildLogExcerptMacro.java +++ b/src/main/java/org/jenkinsci/plugins/tokenmacro/impl/BuildLogExcerptMacro.java @@ -76,9 +76,12 @@ String getContent(BufferedReader reader) throws IOException { started = true; continue; } - if (endPattern.matcher(line).matches()) break; - if (started) buffer.append(line).append('\n'); + if (started) { + if (endPattern.matcher(line).matches()) break; + + buffer.append(line).append('\n'); + } } return buffer.toString(); } diff --git a/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/BuildLogExcerptMacroTest.java b/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/BuildLogExcerptMacroTest.java index baec05b..1cc919d 100644 --- a/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/BuildLogExcerptMacroTest.java +++ b/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/BuildLogExcerptMacroTest.java @@ -67,5 +67,19 @@ public void testGetContent_regexpStartEndTags() assertEquals("7\n8\n9\n", result); } + + @Test + public void testGetContent_regexpStartEndTagsEndBeforeStart() + throws Exception { + AbstractBuild build = mock(AbstractBuild.class); + when(build.getLogReader()).thenReturn(new StringReader("1\n2\nSTOP3\n4\n5\nTEST STARTED\n7\n8\n9\nTEST STOPED\n10\n11\n12\n")); + + buildLogExcerptMacro.start = ".*START.*"; + buildLogExcerptMacro.end = ".*STOP.*"; + + final String result = buildLogExcerptMacro.evaluate(build, listener, BuildLogExcerptMacro.MACRO_NAME); + + assertEquals("7\n8\n9\n", result); + } }