Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust tests to reflect code changes #217

Merged
merged 4 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
def jenkinsLts = '2.235.5'
final jenkinsLast = '2.235.5'
final jenkinsLts = '2.263.2'
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;
public 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
25 changes: 23 additions & 2 deletions src/test/java/hudson/plugins/ansicolor/JenkinsTestSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.RestartableJenkinsRule;
Expand All @@ -11,6 +12,8 @@
import java.io.StringWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.function.BooleanSupplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -22,22 +25,40 @@ public class JenkinsTestSupport {
public RestartableJenkinsRule jenkinsRule = new RestartableJenkinsRule();
private static final int CONSOLE_TAIL_DEFAULT = 150;

protected void assertOutputOnRunningPipeline(String expectedOutput, String notExpectedOutput, String pipelineScript, boolean useShortLog) {
assertOutputOnRunningPipeline(Collections.singletonList(expectedOutput), Collections.singletonList(notExpectedOutput), pipelineScript, useShortLog);
protected void assertOutputOnRunningPipeline(BooleanSupplier assumption, String expectedOutput, String notExpectedOutput, String pipelineScript, boolean useShortLog, Map<String, String> properties) {
assertOutputOnRunningPipeline(assumption, Collections.singletonList(expectedOutput), Collections.singletonList(notExpectedOutput), pipelineScript, useShortLog, properties);
}

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

protected void assertOutputOnRunningPipeline(Collection<String> expectedOutput, Collection<String> notExpectedOutput, String pipelineScript, boolean useShortLog, Map<String, String> properties) {
assertOutputOnRunningPipeline(() -> true, expectedOutput, notExpectedOutput, pipelineScript, useShortLog, properties);
}

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

@Override
public void evaluate() throws Throwable {
Assume.assumeTrue(assumption.getAsBoolean());
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
@@ -1,9 +1,15 @@
package hudson.plugins.ansicolor.action;

import hudson.plugins.ansicolor.JenkinsTestSupport;
import jenkins.model.Jenkins;
import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.BooleanSupplier;

public class ShortlogActionCreatorIntegrationTest extends JenkinsTestSupport {
private static final String AS_1K = repeat("a", 1024);
Expand All @@ -12,15 +18,35 @@ public class ShortlogActionCreatorIntegrationTest extends JenkinsTestSupport {
private static final String DS_1K = repeat("d", 1024);

@Test
public void canAnotateLongLogOutputInShortlog() {
public void canAnnotateLongLogOutputInShortlogLinesWholeFalse() {
final String script = "ansiColor('xterm') {\n" +
repeat("echo '\033[32m" + AS_1K + "\033[0m'\n", 150) +
"}";
assertOutputOnRunningPipeline("<span style=\"color: #00CD00;\">" + AS_1K + "</span>", "\033", script, true);
final Map<String, String> properties = new HashMap<>();
properties.put(ShortlogActionCreator.PROP_LINES_WHOLE, "false");
BooleanSupplier brokenLinesJenkins = () -> Optional.ofNullable(Jenkins.getVersion())
.orElse(ShortlogActionCreator.LINES_WHOLE_SINCE_VERSION)
.isOlderThan(ShortlogActionCreator.LINES_WHOLE_SINCE_VERSION);
assertOutputOnRunningPipeline(brokenLinesJenkins, "<span style=\"color: #00CD00;\">" + AS_1K + "</span>", "\033", script, true, properties);
}

@Test
public void canAnotateLongLogOutputInShortlogMultipleSteps() {
@Ignore("Needs adjustments for Jenkins > 2.260")
public void canAnnotateLongLogOutputInShortlogLinesWholeTrue() {
final String script = "ansiColor('xterm') {\n" +
repeat("echo '\033[32m" + AS_1K + "\033[0m'\n", 150) +
"echo 'Abc'\n" +
"}";
final Map<String, String> properties = new HashMap<>();
properties.put(ShortlogActionCreator.PROP_LINES_WHOLE, "true");
BooleanSupplier wholeLinesJenkins = () -> Optional.ofNullable(Jenkins.getVersion())
.orElse(ShortlogActionCreator.LINES_WHOLE_SINCE_VERSION)
.isNewerThan(ShortlogActionCreator.LINES_WHOLE_SINCE_VERSION);
assertOutputOnRunningPipeline(wholeLinesJenkins, "<span style=\"color: #00CD00;\">" + AS_1K + "</span>", "\033", script, true, properties);
}

@Test
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 +60,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 +78,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
);
}
}