Skip to content

Commit ca434c8

Browse files
authored
Merge pull request #217 from tszmytka/master
Adjust tests to reflect code changes
2 parents 6c62fc4 + db5026d commit ca434c8

File tree

4 files changed

+107
-11
lines changed

4 files changed

+107
-11
lines changed

Jenkinsfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
def jenkinsLts = '2.235.5'
1+
final jenkinsLast = '2.235.5'
2+
final jenkinsLts = '2.263.2'
23
buildPlugin(configurations: [
34
[ platform: 'linux', jdk: '8', jenkins: null ],
5+
[ platform: 'linux', jdk: '8', jenkins: jenkinsLast, javaLevel: '8' ],
6+
[ platform: 'windows', jdk: '8', jenkins: jenkinsLast, javaLevel: '8' ],
7+
[ platform: 'linux', jdk: '11', jenkins: jenkinsLast, javaLevel: '8' ],
48
[ platform: 'linux', jdk: '8', jenkins: jenkinsLts, javaLevel: '8' ],
59
[ platform: 'windows', jdk: '8', jenkins: jenkinsLts, javaLevel: '8' ],
610
[ platform: 'linux', jdk: '11', jenkins: jenkinsLts, javaLevel: '8' ],

src/main/java/hudson/plugins/ansicolor/action/ShortlogActionCreator.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class ShortlogActionCreator {
2222
private static final Logger LOGGER = Logger.getLogger(ShortlogActionCreator.class.getName());
2323
private static final int CONSOLE_TAIL_DEFAULT = 150;
2424
private static final int BUFFER_SIZE = 16 * 1024;
25+
public static final VersionNumber LINES_WHOLE_SINCE_VERSION = new VersionNumber("2.260");
26+
static final String PROP_LINES_WHOLE = "jenkins.ansicolor.keepLinesWhole";
2527

2628
private final LineIdentifier lineIdentifier;
2729
private final byte[] eol;
@@ -100,7 +102,7 @@ private int indexOfEol(byte[] buf, int after) {
100102

101103
private int[] calculateBeginLength(byte[] buf, int startInBuff, int eolPos, boolean keepLinesWhole) {
102104
if (keepLinesWhole) {
103-
final int begin = eolPos != -1? eolPos + eol.length: startInBuff;
105+
final int begin = eolPos != -1 ? eolPos + eol.length : startInBuff;
104106
return new int[]{begin, eolPos != -1 ? indexOfEol(buf, eolPos) - begin + eol.length : -1};
105107
}
106108
return new int[]{startInBuff, eolPos != -1 ? eolPos - startInBuff + eol.length : -1};
@@ -112,7 +114,7 @@ public static class Listener extends RunListener<Run<?, ?>> {
112114
public void onFinalized(Run<?, ?> run) {
113115
super.onFinalized(run);
114116
final List<ColorizedAction.Command> commands = Arrays.asList(ColorizedAction.Command.START, ColorizedAction.Command.STOP);
115-
Map<String, ColorizedAction> actions = run.getActions(ColorizedAction.class).stream()
117+
final Map<String, ColorizedAction> actions = run.getActions(ColorizedAction.class).stream()
116118
.filter(a -> commands.contains(a.getCommand()))
117119
.collect(Collectors.toMap(a -> {
118120
try {
@@ -126,13 +128,15 @@ public void onFinalized(Run<?, ?> run) {
126128
final File logFile = new File(run.getRootDir(), "log");
127129
if (logFile.isFile()) {
128130
final ShortlogActionCreator shortlogActionCreator = new ShortlogActionCreator(new LineIdentifier(), System.lineSeparator());
129-
final VersionNumber keepLinesWholeVersion = new VersionNumber("2.260");
130131
final String consoleTail = System.getProperty("hudson.consoleTailKB");
132+
final boolean keepLinesWhole = Optional.ofNullable(System.getProperty(PROP_LINES_WHOLE))
133+
.map(Boolean::parseBoolean)
134+
.orElseGet(() -> Optional.ofNullable(Jenkins.getVersion()).orElse(LINES_WHOLE_SINCE_VERSION).isNewerThan(LINES_WHOLE_SINCE_VERSION));
131135
final ColorizedAction action = shortlogActionCreator.createActionForShortlog(
132136
logFile,
133137
actions,
134138
consoleTail != null ? Integer.parseInt(consoleTail) : CONSOLE_TAIL_DEFAULT,
135-
Optional.ofNullable(Jenkins.getVersion()).orElse(keepLinesWholeVersion).isNewerThan(keepLinesWholeVersion)
139+
keepLinesWhole
136140
);
137141
if (action != null) {
138142
run.addAction(action);

src/test/java/hudson/plugins/ansicolor/JenkinsTestSupport.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
44
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
55
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
6+
import org.junit.Assume;
67
import org.junit.Rule;
78
import org.junit.runners.model.Statement;
89
import org.jvnet.hudson.test.RestartableJenkinsRule;
@@ -11,6 +12,8 @@
1112
import java.io.StringWriter;
1213
import java.util.Collection;
1314
import java.util.Collections;
15+
import java.util.Map;
16+
import java.util.function.BooleanSupplier;
1417
import java.util.stream.Collectors;
1518
import java.util.stream.IntStream;
1619

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

25-
protected void assertOutputOnRunningPipeline(String expectedOutput, String notExpectedOutput, String pipelineScript, boolean useShortLog) {
26-
assertOutputOnRunningPipeline(Collections.singletonList(expectedOutput), Collections.singletonList(notExpectedOutput), pipelineScript, useShortLog);
28+
protected void assertOutputOnRunningPipeline(BooleanSupplier assumption, String expectedOutput, String notExpectedOutput, String pipelineScript, boolean useShortLog, Map<String, String> properties) {
29+
assertOutputOnRunningPipeline(assumption, Collections.singletonList(expectedOutput), Collections.singletonList(notExpectedOutput), pipelineScript, useShortLog, properties);
2730
}
2831

2932
protected void assertOutputOnRunningPipeline(Collection<String> expectedOutput, Collection<String> notExpectedOutput, String pipelineScript, boolean useShortLog) {
33+
assertOutputOnRunningPipeline(() -> true, expectedOutput, notExpectedOutput, pipelineScript, useShortLog, Collections.emptyMap());
34+
}
35+
36+
protected void assertOutputOnRunningPipeline(Collection<String> expectedOutput, Collection<String> notExpectedOutput, String pipelineScript, boolean useShortLog, Map<String, String> properties) {
37+
assertOutputOnRunningPipeline(() -> true, expectedOutput, notExpectedOutput, pipelineScript, useShortLog, properties);
38+
}
39+
40+
protected void assertOutputOnRunningPipeline(
41+
BooleanSupplier assumption,
42+
Collection<String> expectedOutput,
43+
Collection<String> notExpectedOutput,
44+
String pipelineScript,
45+
boolean useShortLog,
46+
Map<String, String> properties
47+
) {
3048
jenkinsRule.addStep(new Statement() {
3149

3250
@Override
3351
public void evaluate() throws Throwable {
52+
Assume.assumeTrue(assumption.getAsBoolean());
53+
properties.forEach(System::setProperty);
3454
final WorkflowJob project = jenkinsRule.j.jenkins.createProject(WorkflowJob.class, "test-project-" + JenkinsTestSupport.this.getClass().getSimpleName());
3555
project.setDefinition(new CpsFlowDefinition(pipelineScript, true));
3656
jenkinsRule.j.assertBuildStatusSuccess(project.scheduleBuild2(0));
3757
StringWriter writer = new StringWriter();
3858
final WorkflowRun lastBuild = project.getLastBuild();
3959
final long start = useShortLog ? new File(lastBuild.getRootDir(), "log").length() - CONSOLE_TAIL_DEFAULT * 1024 : 0;
4060
assertTrue(lastBuild.getLogText().writeHtmlTo(start, writer) > 0);
61+
properties.keySet().forEach(System::clearProperty);
4162
final String html = writer.toString().replaceAll("<!--.+?-->", "");
4263
assertThat(html).contains(expectedOutput);
4364
assertThat(html).doesNotContain(notExpectedOutput);

src/test/java/hudson/plugins/ansicolor/action/ShortlogActionCreatorIntegrationTest.java

+71-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package hudson.plugins.ansicolor.action;
22

33
import hudson.plugins.ansicolor.JenkinsTestSupport;
4+
import jenkins.model.Jenkins;
5+
import org.junit.Ignore;
46
import org.junit.Test;
57

68
import java.util.Arrays;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import java.util.Optional;
12+
import java.util.function.BooleanSupplier;
713

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

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

2233
@Test
23-
public void canAnotateLongLogOutputInShortlogMultipleSteps() {
34+
@Ignore("Needs adjustments for Jenkins > 2.260")
35+
public void canAnnotateLongLogOutputInShortlogLinesWholeTrue() {
36+
final String script = "ansiColor('xterm') {\n" +
37+
repeat("echo '\033[32m" + AS_1K + "\033[0m'\n", 150) +
38+
"echo 'Abc'\n" +
39+
"}";
40+
final Map<String, String> properties = new HashMap<>();
41+
properties.put(ShortlogActionCreator.PROP_LINES_WHOLE, "true");
42+
BooleanSupplier wholeLinesJenkins = () -> Optional.ofNullable(Jenkins.getVersion())
43+
.orElse(ShortlogActionCreator.LINES_WHOLE_SINCE_VERSION)
44+
.isNewerThan(ShortlogActionCreator.LINES_WHOLE_SINCE_VERSION);
45+
assertOutputOnRunningPipeline(wholeLinesJenkins, "<span style=\"color: #00CD00;\">" + AS_1K + "</span>", "\033", script, true, properties);
46+
}
47+
48+
@Test
49+
public void canAnnotateLongLogOutputInShortlogMultipleStepsLinesWholeFalse() {
2450
final String script = "echo '\033[32mBeginning\033[0m'\n" +
2551
"ansiColor('vga') {\n" +
2652
repeat(" echo '\033[32m" + AS_1K + "\033[0m'\n", 10) +
@@ -34,6 +60,8 @@ public void canAnotateLongLogOutputInShortlogMultipleSteps() {
3460
"}\n" +
3561
"echo 'End'";
3662

63+
final Map<String, String> properties = new HashMap<>();
64+
properties.put(ShortlogActionCreator.PROP_LINES_WHOLE, "false");
3765
assertOutputOnRunningPipeline(
3866
Arrays.asList(
3967
"<span style=\"color: #00CD00;\">" + BS_1K + "</span>",
@@ -50,7 +78,46 @@ public void canAnotateLongLogOutputInShortlogMultipleSteps() {
5078
"\033[32m" + DS_1K + "\033[0m"
5179
),
5280
script,
53-
true
81+
true,
82+
properties
83+
);
84+
}
85+
86+
@Test
87+
public void canAnnotateLongLogOutputInShortlogMultipleStepsLinesWholeTrue() {
88+
final String script = "echo '\033[32mBeginning\033[0m'\n" +
89+
"ansiColor('vga') {\n" +
90+
repeat(" echo '\033[32m" + AS_1K + "\033[0m'\n", 10) +
91+
"}\n" +
92+
"ansiColor('xterm') {\n" +
93+
repeat(" echo '\033[32m" + BS_1K + "\033[0m'\n", 30) +
94+
"}\n" +
95+
"ansiColor('css') {\n" +
96+
repeat(" echo '\033[32m" + CS_1K + "\033[0m'\n", 50) +
97+
repeat(" echo '\033[32m" + DS_1K + "\033[0m'\n", 50) +
98+
"}\n" +
99+
"echo 'End'";
100+
101+
final Map<String, String> properties = new HashMap<>();
102+
properties.put(ShortlogActionCreator.PROP_LINES_WHOLE, "true");
103+
assertOutputOnRunningPipeline(
104+
Arrays.asList(
105+
"\033[32m" + BS_1K + "\033[0m",
106+
"<span style=\"color: green;\">" + CS_1K + "</span>",
107+
"<span style=\"color: green;\">" + DS_1K + "</span>",
108+
"End"
109+
),
110+
Arrays.asList(
111+
"Beginning",
112+
"<span style=\"color: #00AA00;\">a",
113+
"\033[32m" + AS_1K + "\033[0m",
114+
"<span style=\"color: #00CD00;\">" + BS_1K + "</span>",
115+
"\033[32m" + CS_1K + "\033[0m",
116+
"\033[32m" + DS_1K + "\033[0m"
117+
),
118+
script,
119+
true,
120+
properties
54121
);
55122
}
56123
}

0 commit comments

Comments
 (0)