Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The fast-forwarding logic had a problem dealing with empty lines
- Loading branch information
Showing
3 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/hudson/plugins/timestamper/annotator/Jenkins17779Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package hudson.plugins.timestamper.annotator; | ||
|
||
import com.google.common.io.Files; | ||
import hudson.MarkupText; | ||
import hudson.console.ConsoleAnnotator; | ||
import hudson.model.Run; | ||
import hudson.plugins.timestamper.TimestampFormatter; | ||
import hudson.plugins.timestamper.TimestamperTestAssistant; | ||
import hudson.plugins.timestamper.TimestampsIO; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.jvnet.hudson.test.Bug; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
public class Jenkins17779Test { | ||
@Rule | ||
public TemporaryFolder folder = new TemporaryFolder(); | ||
|
||
private Run<?, ?> build; | ||
|
||
/** | ||
* Creates a dummy timestamp record that says each line took 1ms to render. | ||
*/ | ||
@Before | ||
public void setUp() throws Exception { | ||
final byte[] consoleLogContents = "AB\n\n\n\nCD\n".getBytes("US-ASCII"); | ||
File consoleLog = folder.newFile(); | ||
Files.write(consoleLogContents, consoleLog); | ||
|
||
build = mock(Run.class); | ||
when(build.getRootDir()).thenReturn(folder.getRoot()); | ||
when(build.getLogInputStream()).thenAnswer(new Answer<InputStream>() { | ||
public InputStream answer(InvocationOnMock invocation) throws Throwable { | ||
return new ByteArrayInputStream(consoleLogContents); | ||
} | ||
}); | ||
when(build.getLogFile()).thenReturn(consoleLog); | ||
|
||
TimestampsIO.Writer writer = new TimestampsIO.Writer(build); | ||
try { | ||
for (int i = 0; i < 10; i++) { | ||
writer.write(TimeUnit.MILLISECONDS.toNanos(i), i, 1); | ||
} | ||
} finally { | ||
writer.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Regression test for JENKINS-17779. | ||
*/ | ||
@Test @Bug(17779) | ||
public void fastForwardShouldHandleDoubleEmptyLines() throws Exception { | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
TimestampFormatter f = new TimestampFormatter("S", "", request); | ||
|
||
// fast-forward to 'CD' which is line 5 | ||
ConsoleAnnotator a = new TimestampAnnotator(f,6); | ||
MarkupText text = new MarkupText("CD"); | ||
a.annotate(build, text); | ||
assertThat(text.toString(true),is(TimestamperTestAssistant.span("4")+"CD")); | ||
|
||
// should get the same result if we go line by line | ||
a = new TimestampAnnotator(f,0); | ||
for (int i=0; i<5; i++) { | ||
text = new MarkupText("CD"); | ||
a.annotate(build, text); | ||
} | ||
assertThat(text.toString(true),is(TimestamperTestAssistant.span("4")+"CD")); | ||
} | ||
} |