From 5e36af01b5c16268cb2802957a38c2b67b2139f1 Mon Sep 17 00:00:00 2001 From: Jorge Marin Date: Fri, 13 Jul 2018 16:23:38 +0000 Subject: [PATCH] Fix pattern appearing in console logs and use global variables in some tests --- .../textfinder/TextFinderPublisher.java | 22 ++- .../TextFinderPublisherAgentTest.java | 13 +- .../TextFinderPublisherFreestyleTest.java | 12 +- .../TextFinderPublisherPipelineTest.java | 131 +++++++++++++----- 4 files changed, 113 insertions(+), 65 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 7266416..328b8b8 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -128,8 +128,8 @@ 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(), @@ -137,24 +137,20 @@ private void findText(Run run, FilePath workspace, TaskListener listener) 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)); } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java index 452d71e..d8b259f 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -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); } @@ -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); } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index 8b5d11c..44de798 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -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); } @@ -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); } @@ -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); } @@ -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); } } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java index b2baf4b..e331304 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -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(); @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } }