Skip to content

Commit

Permalink
Fix pattern appearing in console logs and use global variables in som…
Browse files Browse the repository at this point in the history
…e tests
  • Loading branch information
Jorge Marin committed Jul 13, 2018
1 parent 32661ee commit 5e36af0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 65 deletions.
22 changes: 9 additions & 13 deletions src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,33 +128,29 @@ private void findText(Run<?, ?> run, FilePath workspace, TaskListener listener)
boolean foundText = false;

if (alsoCheckConsoleOutput) {
logger.println(
"Looking for pattern " + "'" + regexp + "'" + " in the console output");
// Do not mention the pattern we are looking for to avoid false positives
logger.println("Looking for a specific pattern in the console output");
foundText |=
checkFile(
run.getLogFile(),
compilePattern(logger, regexp),
logger,
run.getCharset(),
true);
} else {
// printing this when checking console output will cause the plugin
// to find this line, which would be pointless.
// doing this only when fileSet!=null to avoid
}

final RemoteOutputStream ros = new RemoteOutputStream(logger);

if (fileSet != null) {
logger.println(
"Looking for pattern "
+ "'"
+ regexp
+ "'"
+ " in the file "
+ " in the files at "
+ "'"
+ run.getLogFile()
+ fileSet
+ "'");
}

final RemoteOutputStream ros = new RemoteOutputStream(logger);

if (fileSet != null) {
foundText |= workspace.act(new FileChecker(ros, fileSet, regexp));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,7 @@ public void failureIfFoundInFileOnAgent() throws Exception {
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern "
+ "'"
+ UNIQUE_TEXT
+ "'"
+ " in the file "
+ "'"
+ build.getLogFile()
+ "'",
build);
"Looking for pattern " + "'" + UNIQUE_TEXT + "'" + " in the files at", build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.FAILURE, build);
}
Expand All @@ -88,8 +80,7 @@ public void failureIfFoundInConsoleOnAgent() throws Exception {
agent.getNodeName(), agent.getNodeName())));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern " + "'" + UNIQUE_TEXT + "'" + " in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.FAILURE, build);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public void successIfFoundInConsole() throws Exception {
project.getPublishersList().add(textFinder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern " + "'" + UNIQUE_TEXT + "'" + " in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.SUCCESS, build);
}
Expand All @@ -67,8 +66,7 @@ public void failureIfFoundInConsole() throws Exception {
project.getPublishersList().add(textFinder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern " + "'" + UNIQUE_TEXT + "'" + " in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.FAILURE, build);
}
Expand All @@ -87,8 +85,7 @@ public void unstableIfFoundInConsole() throws Exception {
project.getPublishersList().add(textFinder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern " + "'" + UNIQUE_TEXT + "'" + " in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.UNSTABLE, build);
}
Expand All @@ -101,8 +98,7 @@ public void notFoundInConsole() throws Exception {
project.getPublishersList().add(textFinder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern " + "'" + UNIQUE_TEXT + "'" + " in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
rule.assertBuildStatus(Result.SUCCESS, build);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TextFinderPublisherPipelineTest {

private static final String UNIQUE_TEXT = "foobar";
private static final String ECHO_UNIQUE_TEXT = "echo " + UNIQUE_TEXT;
private static final String fileSet = "out.txt";

@Rule public JenkinsRule rule = new JenkinsRule();

Expand Down Expand Up @@ -54,14 +55,22 @@ public void successIfFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobar'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt', succeedIfFound: true}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: '"
+ UNIQUE_TEXT
+ "'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "', succeedIfFound: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern 'foobar' in the file " + "'" + build.getLogFile() + "'",
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.SUCCESS, build);
}

Expand All @@ -70,14 +79,22 @@ public void failureIfFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobar'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt'}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: '"
+ UNIQUE_TEXT
+ "'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "'}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern 'foobar' in the file " + "'" + build.getLogFile() + "'",
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.FAILURE, build);
}

Expand All @@ -86,14 +103,22 @@ public void unstableIfFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobar'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt', unstableIfFound: true}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: '"
+ UNIQUE_TEXT
+ "'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "', unstableIfFound: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern 'foobar' in the file " + "'" + build.getLogFile() + "'",
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.UNSTABLE, build);
}

Expand All @@ -102,14 +127,22 @@ public void notBuiltIfFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobar'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt', notBuiltIfFound: true}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: '"
+ UNIQUE_TEXT
+ "'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "', notBuiltIfFound: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern 'foobar' in the file " + "'" + build.getLogFile() + "'",
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false);
assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false);
rule.assertBuildStatus(Result.NOT_BUILT, build);
}

Expand All @@ -118,12 +151,18 @@ public void notFoundInFile() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {writeFile file: 'out.txt', text: 'foobaz'}\n"
+ "node {findText regexp: 'foobar', fileSet: 'out.txt'}\n"));
"node {writeFile file: '"
+ fileSet
+ "', text: 'foobaz'}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', fileSet: '"
+ fileSet
+ "'}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains(
"Looking for pattern 'foobar' in the file " + "'" + build.getLogFile() + "'",
"Looking for pattern '" + UNIQUE_TEXT + "' in the files at " + "'" + fileSet + "'",
build);
rule.assertBuildStatus(Result.SUCCESS, build);
}
Expand All @@ -133,11 +172,17 @@ public void successIfFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n"
+ "node {findText regexp: 'foobar', succeedIfFound: true, alsoCheckConsoleOutput: true}\n"));
"node {isUnix() ? sh('"
+ ECHO_UNIQUE_TEXT
+ "') : bat(\"prompt \\$G\\r\\n"
+ ECHO_UNIQUE_TEXT
+ "\")}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', succeedIfFound: true, alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Looking for pattern 'foobar' in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.SUCCESS, build);
}
Expand All @@ -147,11 +192,17 @@ public void failureIfFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n"
+ "node {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n"));
"node {isUnix() ? sh('"
+ ECHO_UNIQUE_TEXT
+ "') : bat(\"prompt \\$G\\r\\n"
+ ECHO_UNIQUE_TEXT
+ "\")}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Looking for pattern 'foobar' in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.FAILURE, build);
}
Expand All @@ -161,11 +212,17 @@ public void unstableIfFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n"
+ "node {findText regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true}\n"));
"node {isUnix() ? sh('"
+ ECHO_UNIQUE_TEXT
+ "') : bat(\"prompt \\$G\\r\\n"
+ ECHO_UNIQUE_TEXT
+ "\")}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', unstableIfFound: true, alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Looking for pattern 'foobar' in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.UNSTABLE, build);
}
Expand All @@ -175,11 +232,17 @@ public void notBuiltIfFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n"
+ "node {findText regexp: 'foobar', notBuiltIfFound: true, alsoCheckConsoleOutput: true}\n"));
"node {isUnix() ? sh('"
+ ECHO_UNIQUE_TEXT
+ "') : bat(\"prompt \\$G\\r\\n"
+ ECHO_UNIQUE_TEXT
+ "\")}\n"
+ "node {findText regexp: '"
+ UNIQUE_TEXT
+ "', notBuiltIfFound: true, alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Looking for pattern 'foobar' in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true);
rule.assertBuildStatus(Result.NOT_BUILT, build);
}
Expand All @@ -189,10 +252,12 @@ public void notFoundInConsole() throws Exception {
WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline");
project.setDefinition(
new CpsFlowDefinition(
"node {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n"));
"node {findText regexp: '"
+ UNIQUE_TEXT
+ "', alsoCheckConsoleOutput: true}\n"));
WorkflowRun build = project.scheduleBuild2(0).get();
rule.waitForCompletion(build);
rule.assertLogContains("Looking for pattern 'foobar' in the console output", build);
rule.assertLogContains("Looking for a specific pattern in the console output", build);
rule.assertBuildStatus(Result.SUCCESS, build);
}
}

0 comments on commit 5e36af0

Please sign in to comment.