Skip to content

Commit

Permalink
#212 Allow managing 'keeping lines whole' with a property. Test both …
Browse files Browse the repository at this point in the history
…variants
  • Loading branch information
tszmytka committed Jan 18, 2021
1 parent c17793d commit 115da6d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ buildPlugin(configurations: [
[ platform: 'linux', jdk: '8', jenkins: null ],
[ platform: 'linux', jdk: '8', jenkins: jenkinsLast, javaLevel: '8' ],
[ platform: 'windows', jdk: '8', jenkins: jenkinsLast, javaLevel: '8' ],
[ platform: 'linux', jdk: '11', jenkins: jenkinsLast, javaLevel: '8' ],
[ platform: 'linux', jdk: '8', jenkins: jenkinsLts, javaLevel: '8' ],
[ platform: 'windows', jdk: '8', jenkins: jenkinsLts, javaLevel: '8' ],
[ platform: 'linux', jdk: '11', jenkins: jenkinsLts, javaLevel: '8' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class ShortlogActionCreator {
private static final Logger LOGGER = Logger.getLogger(ShortlogActionCreator.class.getName());
private static final int CONSOLE_TAIL_DEFAULT = 150;
private static final int BUFFER_SIZE = 16 * 1024;
private static final VersionNumber LINES_WHOLE_SINCE_VERSION = new VersionNumber("2.260");
static final String PROP_LINES_WHOLE = "jenkins.ansicolor.keepLinesWhole";

private final LineIdentifier lineIdentifier;
private final byte[] eol;
Expand Down Expand Up @@ -100,7 +102,7 @@ private int indexOfEol(byte[] buf, int after) {

private int[] calculateBeginLength(byte[] buf, int startInBuff, int eolPos, boolean keepLinesWhole) {
if (keepLinesWhole) {
final int begin = eolPos != -1? eolPos + eol.length: startInBuff;
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 All @@ -112,7 +114,7 @@ public static class Listener extends RunListener<Run<?, ?>> {
public void onFinalized(Run<?, ?> run) {
super.onFinalized(run);
final List<ColorizedAction.Command> commands = Arrays.asList(ColorizedAction.Command.START, ColorizedAction.Command.STOP);
Map<String, ColorizedAction> actions = run.getActions(ColorizedAction.class).stream()
final Map<String, ColorizedAction> actions = run.getActions(ColorizedAction.class).stream()
.filter(a -> commands.contains(a.getCommand()))
.collect(Collectors.toMap(a -> {
try {
Expand All @@ -126,13 +128,15 @@ public void onFinalized(Run<?, ?> run) {
final File logFile = new File(run.getRootDir(), "log");
if (logFile.isFile()) {
final ShortlogActionCreator shortlogActionCreator = new ShortlogActionCreator(new LineIdentifier(), System.lineSeparator());
final VersionNumber keepLinesWholeVersion = new VersionNumber("2.260");
final String consoleTail = System.getProperty("hudson.consoleTailKB");
final boolean keepLinesWhole = Optional.ofNullable(System.getProperty(PROP_LINES_WHOLE))
.map(Boolean::parseBoolean)
.orElseGet(() -> Optional.ofNullable(Jenkins.getVersion()).orElse(LINES_WHOLE_SINCE_VERSION).isNewerThan(LINES_WHOLE_SINCE_VERSION));
final ColorizedAction action = shortlogActionCreator.createActionForShortlog(
logFile,
actions,
consoleTail != null ? Integer.parseInt(consoleTail) : CONSOLE_TAIL_DEFAULT,
Optional.ofNullable(Jenkins.getVersion()).orElse(keepLinesWholeVersion).isNewerThan(keepLinesWholeVersion)
keepLinesWhole
);
if (action != null) {
run.addAction(action);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.ansicolor;

import hudson.plugins.ansicolor.action.ShortlogActionCreator;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand All @@ -11,6 +12,7 @@
import java.io.StringWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -27,17 +29,23 @@ protected void assertOutputOnRunningPipeline(String expectedOutput, String notEx
}

protected void assertOutputOnRunningPipeline(Collection<String> expectedOutput, Collection<String> notExpectedOutput, String pipelineScript, boolean useShortLog) {
assertOutputOnRunningPipeline(expectedOutput, notExpectedOutput, pipelineScript, useShortLog, Collections.emptyMap());
}

protected void assertOutputOnRunningPipeline(Collection<String> expectedOutput, Collection<String> notExpectedOutput, String pipelineScript, boolean useShortLog, Map<String, String> properties) {
jenkinsRule.addStep(new Statement() {

@Override
public void evaluate() throws Throwable {
properties.forEach(System::setProperty);
final WorkflowJob project = jenkinsRule.j.jenkins.createProject(WorkflowJob.class, "test-project-" + JenkinsTestSupport.this.getClass().getSimpleName());
project.setDefinition(new CpsFlowDefinition(pipelineScript, true));
jenkinsRule.j.assertBuildStatusSuccess(project.scheduleBuild2(0));
StringWriter writer = new StringWriter();
final WorkflowRun lastBuild = project.getLastBuild();
final long start = useShortLog ? new File(lastBuild.getRootDir(), "log").length() - CONSOLE_TAIL_DEFAULT * 1024 : 0;
assertTrue(lastBuild.getLogText().writeHtmlTo(start, writer) > 0);
properties.keySet().forEach(System::clearProperty);
final String html = writer.toString().replaceAll("<!--.+?-->", "");
assertThat(html).contains(expectedOutput);
assertThat(html).doesNotContain(notExpectedOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ShortlogActionCreatorIntegrationTest extends JenkinsTestSupport {
private static final String AS_1K = repeat("a", 1024);
Expand All @@ -20,7 +22,7 @@ public void canAnotateLongLogOutputInShortlog() {
}

@Test
public void canAnotateLongLogOutputInShortlogMultipleSteps() {
public void canAnnotateLongLogOutputInShortlogMultipleStepsLinesWholeFalse() {
final String script = "echo '\033[32mBeginning\033[0m'\n" +
"ansiColor('vga') {\n" +
repeat(" echo '\033[32m" + AS_1K + "\033[0m'\n", 10) +
Expand All @@ -34,6 +36,8 @@ public void canAnotateLongLogOutputInShortlogMultipleSteps() {
"}\n" +
"echo 'End'";

final Map<String, String> properties = new HashMap<>();
properties.put(ShortlogActionCreator.PROP_LINES_WHOLE, "false");
assertOutputOnRunningPipeline(
Arrays.asList(
"<span style=\"color: #00CD00;\">" + BS_1K + "</span>",
Expand All @@ -50,7 +54,46 @@ public void canAnotateLongLogOutputInShortlogMultipleSteps() {
"\033[32m" + DS_1K + "\033[0m"
),
script,
true
true,
properties
);
}

@Test
public void canAnnotateLongLogOutputInShortlogMultipleStepsLinesWholeTrue() {
final String script = "echo '\033[32mBeginning\033[0m'\n" +
"ansiColor('vga') {\n" +
repeat(" echo '\033[32m" + AS_1K + "\033[0m'\n", 10) +
"}\n" +
"ansiColor('xterm') {\n" +
repeat(" echo '\033[32m" + BS_1K + "\033[0m'\n", 30) +
"}\n" +
"ansiColor('css') {\n" +
repeat(" echo '\033[32m" + CS_1K + "\033[0m'\n", 50) +
repeat(" echo '\033[32m" + DS_1K + "\033[0m'\n", 50) +
"}\n" +
"echo 'End'";

final Map<String, String> properties = new HashMap<>();
properties.put(ShortlogActionCreator.PROP_LINES_WHOLE, "true");
assertOutputOnRunningPipeline(
Arrays.asList(
"\033[32m" + BS_1K + "\033[0m",
"<span style=\"color: green;\">" + CS_1K + "</span>",
"<span style=\"color: green;\">" + DS_1K + "</span>",
"End"
),
Arrays.asList(
"Beginning",
"<span style=\"color: #00AA00;\">a",
"\033[32m" + AS_1K + "\033[0m",
"<span style=\"color: #00CD00;\">" + BS_1K + "</span>",
"\033[32m" + CS_1K + "\033[0m",
"\033[32m" + DS_1K + "\033[0m"
),
script,
true,
properties
);
}
}

0 comments on commit 115da6d

Please sign in to comment.