Skip to content

Commit

Permalink
#212 Prepare for case with line extending to next buffer. Cover with …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
tszmytka committed Jan 2, 2021
1 parent 0003e75 commit c326ba5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private ActionContext findLastActionBefore(File logFile, Collection<String> seri
}
if (totalRead + read >= shortlogStart) {
final int eolPos = indexOfEol(buf, startInBuff);
final int[] beginLength = calculateBeginLength(buf, startInBuff, eolPos, keepLinesWhole);
final int[] beginLength = calculateBeginLength(buf, startInBuff, eolPos, partialLine.isEmpty() && keepLinesWhole);
final int begin = beginLength[0];
final int length = beginLength[1];
if (length != -1 && !lastAction.isEmpty()) {
Expand Down Expand Up @@ -100,9 +100,8 @@ private int indexOfEol(byte[] buf, int after) {

private int[] calculateBeginLength(byte[] buf, int startInBuff, int eolPos, boolean keepLinesWhole) {
if (keepLinesWhole) {
final int endEolPos = indexOfEol(buf, eolPos);
final int begin = eolPos + eol.length;
return new int[]{begin, endEolPos != -1 ? endEolPos - begin + eol.length : -1};
final int begin = eolPos != -1? eolPos + eol.length: startInBuff;
return new int[]{begin, eolPos != -1 ? indexOfEol(buf, eolPos) - begin + eol.length : -1};
}
return new int[]{startInBuff, eolPos != -1 ? eolPos - startInBuff + eol.length : -1};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import hudson.console.ConsoleNote;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand All @@ -29,16 +28,39 @@ public void setUp() throws Exception {

@Test
public void canCreateActionForShortlog() {
canCreateActionForShortlog(shortlogActionCreator, "[Pipeline] echo\n", "testlog.log");
final String shortlogLine = "\u001B[3B\u001B[2A\u001B[2K \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold\n";
canCreateActionForShortlog(shortlogActionCreator, shortlogLine, "testlog.log", true);
}

@Test
public void canCreateActionForShortlogBreakLines() {
canCreateActionForShortlog(shortlogActionCreator, "[Pipeline] echo\n", "testlog.log", false);
}

@Test
public void canCreateActionForShortlogForWindows() {
final String shortlogLine = "\u001B[3B\u001B[2A\u001B[2K \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mlightgreen bold " +
"\u001B[92m\u001B[22mlightgreen normal\u001B[0m \u001B[92m\u001B[1mligh";
final String eol = "\r\n";
canCreateActionForShortlog(new ShortlogActionCreator(lineIdentifier, eol), shortlogLine + eol, "testlog-crlf.log", true);
}

@Test
public void canCreateActionForShortlogForWindowsBreakLines() {
final String eol = "\r\n";
canCreateActionForShortlog(new ShortlogActionCreator(lineIdentifier, eol), "[Pipeline] echo" + eol, "testlog-crlf.log");
canCreateActionForShortlog(new ShortlogActionCreator(lineIdentifier, eol), "[Pipeline] echo" + eol, "testlog-crlf.log", false);
}

private void canCreateActionForShortlog(ShortlogActionCreator shortlogActionCreator, String shortlogLine, String logFile) {
private void canCreateActionForShortlog(ShortlogActionCreator shortlogActionCreator, String shortlogLine, String logFile, boolean keepLinesWhole) {
final String lineHash = "mock-line-hash";
final ColorizedAction colorizedAction = new ColorizedAction("xterm", ColorizedAction.Command.START);
final String serializedNote = "<mock-serialized-note-start>";
Expand All @@ -51,7 +73,7 @@ private void canCreateActionForShortlog(ShortlogActionCreator shortlogActionCrea
startActions.put(ConsoleNote.PREAMBLE_STR + serializedNote, colorizedAction);
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-stop>", new ColorizedAction("xterm", ColorizedAction.Command.STOP));
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start1>", new ColorizedAction("gnome-terminal", ColorizedAction.Command.START));
final ColorizedAction shortlogAction = shortlogActionCreator.createActionForShortlog(file, startActions, 3, false);
final ColorizedAction shortlogAction = shortlogActionCreator.createActionForShortlog(file, startActions, 3, keepLinesWhole);
assertEquals(colorizedAction.getColorMapName(), shortlogAction.getColorMapName());
assertEquals(colorizedAction.getCommand(), shortlogAction.getCommand());
assertNotEquals(colorizedAction.getId(), shortlogAction.getId());
Expand All @@ -60,62 +82,77 @@ private void canCreateActionForShortlog(ShortlogActionCreator shortlogActionCrea

@Test
public void wontCreateActionForLogFileShorterThanShortlogLimit() {
final String serializedNote = "<mock-serialized-note-start>";
final ColorizedAction colorizedAction = new ColorizedAction("xterm", ColorizedAction.Command.START);
final File file = new File(getClass().getResource(String.join("/", "", getClass().getName().replace('.', '/'), "testlog.log")).getFile());
final HashMap<String, ColorizedAction> startActions = new HashMap<>();
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start0>", new ColorizedAction("css", ColorizedAction.Command.START));
startActions.put(ConsoleNote.PREAMBLE_STR + serializedNote, colorizedAction);
assertNull(shortlogActionCreator.createActionForShortlog(file, startActions, 256, false));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
final boolean[] keepLinesWholeOptions = {true, false};
for (boolean keepLinesWhole : keepLinesWholeOptions) {
final String serializedNote = "<mock-serialized-note-start>";
final ColorizedAction colorizedAction = new ColorizedAction("xterm", ColorizedAction.Command.START);
final File file = new File(getClass().getResource(String.join("/", "", getClass().getName().replace('.', '/'), "testlog.log")).getFile());
final HashMap<String, ColorizedAction> startActions = new HashMap<>();
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start0>", new ColorizedAction("css", ColorizedAction.Command.START));
startActions.put(ConsoleNote.PREAMBLE_STR + serializedNote, colorizedAction);
assertNull(shortlogActionCreator.createActionForShortlog(file, startActions, 256, keepLinesWhole));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
}
}

@Test
public void wontCreateActionIfBuildHasNoStartActions() {
final File file = new File(getClass().getResource(String.join("/", "", getClass().getName().replace('.', '/'), "testlog.log")).getFile());
assertNull(shortlogActionCreator.createActionForShortlog(file, new HashMap<>(), 3, false));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
final boolean[] keepLinesWholeOptions = {true, false};
for (boolean keepLinesWhole : keepLinesWholeOptions) {
final File file = new File(getClass().getResource(String.join("/", "", getClass().getName().replace('.', '/'), "testlog.log")).getFile());
assertNull(shortlogActionCreator.createActionForShortlog(file, new HashMap<>(), 3, keepLinesWhole));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
}
}

@Test
public void wontCreateActionIfNoCorrespondingNotesArePresent() {
final HashMap<String, ColorizedAction> startActions = new HashMap<>();
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start0>", new ColorizedAction("css", ColorizedAction.Command.START));
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-stop0>", new ColorizedAction("css", ColorizedAction.Command.STOP));
final File file = new File(getClass().getResource(String.join("/", "", getClass().getName().replace('.', '/'), "testlog-no-notes.log")).getFile());
assertNull(shortlogActionCreator.createActionForShortlog(file, startActions, 3, false));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
final boolean[] keepLinesWholeOptions = {true, false};
for (boolean keepLinesWhole : keepLinesWholeOptions) {
final HashMap<String, ColorizedAction> startActions = new HashMap<>();
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start0>", new ColorizedAction("css", ColorizedAction.Command.START));
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-stop0>", new ColorizedAction("css", ColorizedAction.Command.STOP));
final File file = new File(getClass().getResource(String.join("/", "", getClass().getName().replace('.', '/'), "testlog-no-notes.log")).getFile());
assertNull(shortlogActionCreator.createActionForShortlog(file, startActions, 3, keepLinesWhole));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
}
}

@Test
public void wontCreateActionIfNoLogFileIsPresent() {
final HashMap<String, ColorizedAction> startActions = new HashMap<>();
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start0>", new ColorizedAction("css", ColorizedAction.Command.START));
final File file = new File("non-existing.log");
assertNull(shortlogActionCreator.createActionForShortlog(file, startActions, 3, false));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
final boolean[] keepLinesWholeOptions = {true, false};
for (boolean keepLinesWhole : keepLinesWholeOptions) {
final HashMap<String, ColorizedAction> startActions = new HashMap<>();
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start0>", new ColorizedAction("css", ColorizedAction.Command.START));
final File file = new File("non-existing.log");
assertNull(shortlogActionCreator.createActionForShortlog(file, startActions, 3, keepLinesWhole));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
}
}

@Test
public void wontCreateActionIfActionIsNotActiveAtShortlogLimit() {
final File file = new File(getClass().getResource(String.join("/", "", getClass().getName().replace('.', '/'), "testlog-action-not-active.log")).getFile());
final HashMap<String, ColorizedAction> startActions = new HashMap<>();
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start>", new ColorizedAction("css", ColorizedAction.Command.START));
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-stop>", new ColorizedAction("css", ColorizedAction.Command.STOP));

assertNull(shortlogActionCreator.createActionForShortlog(file, startActions, 3, false));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
final boolean[] keepLinesWholeOptions = {true, false};
for (boolean keepLinesWhole : keepLinesWholeOptions) {
final File file = new File(getClass().getResource(String.join("/", "", getClass().getName().replace('.', '/'), "testlog-action-not-active.log")).getFile());
final HashMap<String, ColorizedAction> startActions = new HashMap<>();
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-start>", new ColorizedAction("css", ColorizedAction.Command.START));
startActions.put(ConsoleNote.PREAMBLE_STR + "<mock-serialized-note-stop>", new ColorizedAction("css", ColorizedAction.Command.STOP));

assertNull(shortlogActionCreator.createActionForShortlog(file, startActions, 3, keepLinesWhole));
verify(lineIdentifier, never()).hash(anyString(), anyLong());
}
}

@Test
public void canCreateActionForShortlogOnLogLineExceedingBufferSize() {
final String s = "[Pipeline] echo a very very very long line,a very very very long line,a very very very long line,a very very very long line,a very very very long line";
canCreateActionForShortlog(shortlogActionCreator, s + "\n", "testlog-long.log");
canCreateActionForShortlog(shortlogActionCreator, s + "\n", "testlog-long.log", true);
}

@Test
@Ignore
public void canCreateActionForShortlogPreJenkins2261() {
// todo
public void canCreateActionForShortlogOnLogLineExceedingBufferSizeBreakLines() {
final String s = "[Pipeline] echo a very very very long line,a very very very long line,a very very very long line,a very very very long line,a very very very long line";
canCreateActionForShortlog(shortlogActionCreator, s + "\n", "testlog-long.log", false);
}
}

0 comments on commit c326ba5

Please sign in to comment.