Skip to content

Commit

Permalink
jenkinsci#185 Add integration test for annotating long log file while…
Browse files Browse the repository at this point in the history
… in shortlog
  • Loading branch information
tszmytka committed Jul 30, 2020
1 parent 31b6b09 commit d04809e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/test/java/hudson/plugins/ansicolor/JenkinsTestSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package hudson.plugins.ansicolor;

import hudson.console.AnnotatedLargeText;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.RestartableJenkinsRule;

import java.io.File;
import java.io.StringWriter;
import java.util.Collection;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;

public class JenkinsTestSupport {
@Rule
public RestartableJenkinsRule jenkinsRule = new RestartableJenkinsRule();
private static final int CONSOLE_TAIL_DEFAULT = 150;

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

@Override
public void evaluate() throws Throwable {
final WorkflowJob project = jenkinsRule.j.jenkins.createProject(WorkflowJob.class, "p");
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);
final String html = writer.toString().replaceAll("<!--.+?-->", "");
assertThat(html).contains(expectedOutput);
assertThat(html).doesNotContain(notExpectedOutput);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hudson.plugins.ansicolor.action;

import hudson.plugins.ansicolor.JenkinsTestSupport;
import org.junit.Test;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ShortlogActionCreatorIntegrationTest extends JenkinsTestSupport {

@Test
public void canAnotateLongLogOutputInShortlog() {
final String eol = System.lineSeparator();
final String as1k = IntStream.range(0, 1024).mapToObj(i -> "a").collect(Collectors.joining());
final String as10k = IntStream.range(0, 10 * 1024).mapToObj(i -> as1k + eol).collect(Collectors.joining());
final String bs1k = IntStream.range(0, 1024).mapToObj(i -> "b").collect(Collectors.joining());
final String bs30k = IntStream.range(0, 30).mapToObj(i -> bs1k + eol).collect(Collectors.joining());
final String cs1k = IntStream.range(0, 1024).mapToObj(i -> "c").collect(Collectors.joining());
final String cs50k = IntStream.range(0, 50).mapToObj(i -> cs1k + eol).collect(Collectors.joining());
final String ds1k = IntStream.range(0, 1024).mapToObj(i -> "d").collect(Collectors.joining());
final String ds50k = IntStream.range(0, 50).mapToObj(i -> ds1k + eol).collect(Collectors.joining());

// final String as10k = IntStream.range(0, 10 * 1024).mapToObj(i -> "a").collect(Collectors.joining());
final String script = "echo '\033[32mBeginning\033[0m'\n" +
"ansiColor('vga') {\n" +
" echo '\033[32m" + as10k + "\033[0m'\n" +
"}\n" +
"ansiColor('xterm') {\n" +
" echo '\033[32m" + bs30k + "\033[0m'\n" +
"}\n" +
"ansiColor('css') {\n" +
" echo '\033[32m" + cs50k + "\033[0m'\n" +
" echo '\033[32m" + ds50k + "\033[0m'\n" +
"}\n" +
"echo 'End'";

assertOutputOnRunningPipeline(
Arrays.asList(
// "<span style=\"color: #00CD00;\">" + bs30k + "</span>",
"<span style=\"color: green;\">" + cs50k + "</span>",
"<span style=\"color: green;\">" + ds50k + "</span>",
"End"
),
Arrays.asList(
"Beginning",
"<span style=\"color: #00AA00;\">a",
"\033[32m" + as10k + "\033[0m",
"\033[32m" + bs30k + "\033[0m",
"\033[32m" + cs50k + "\033[0m",
"\033[32m" + ds50k + "\033[0m"
),
script,
true
);
}
}

0 comments on commit d04809e

Please sign in to comment.