From 118ceb79e49d89cb64b9498bf3c4bd5ba45978cf Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sat, 9 Jun 2018 00:16:38 -0700 Subject: [PATCH 01/15] Add pipeline support --- pom.xml | 43 +++- .../textfinder/TextFinderPublisher.java | 210 ++++++++++++------ .../TextFinderPublisher/config.jelly | 1 + src/main/resources/index.jelly | 1 + .../TextFinderPublisherFreestyleTest.java | 96 ++++++++ .../TextFinderPublisherPipelineTest.java | 151 +++++++++++++ 6 files changed, 430 insertions(+), 72 deletions(-) create mode 100644 src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java create mode 100644 src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java diff --git a/pom.xml b/pom.xml index 44b4a7f..d18273d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,8 @@ org.jenkins-ci.plugins plugin - 1.480 + 3.4 + text-finder @@ -12,6 +13,10 @@ 1.11-SNAPSHOT Jenkins TextFinder plugin http://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin + + 1.642.1 + 7 + @@ -28,17 +33,49 @@ https://github.com/jenkinsci/${project.artifactId}-plugin + + + org.jenkins-ci.plugins + structs + 1.3 + + + org.jenkins-ci.plugins.workflow + workflow-basic-steps + 2.1 + test + + + org.jenkins-ci.plugins.workflow + workflow-cps + 2.10 + test + + + org.jenkins-ci.plugins.workflow + workflow-durable-task-step + 2.4 + test + + + org.jenkins-ci.plugins.workflow + workflow-job + 2.4 + test + + + repo.jenkins-ci.org - http://repo.jenkins-ci.org/public/ + https://repo.jenkins-ci.org/public/ repo.jenkins-ci.org - http://repo.jenkins-ci.org/public/ + https://repo.jenkins-ci.org/public/ diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 9d0f0a4..2117b52 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -1,25 +1,28 @@ package hudson.plugins.textfinder; -import hudson.FilePath.FileCallable; +import hudson.FilePath; import hudson.Launcher; import hudson.Util; import hudson.Extension; import static hudson.Util.fixEmpty; -import hudson.model.AbstractBuild; import hudson.model.AbstractProject; -import hudson.model.BuildListener; import hudson.model.Result; -import hudson.remoting.RemoteOutputStream; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.remoting.VirtualChannel; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepMonitor; import hudson.tasks.Publisher; import hudson.tasks.Recorder; import hudson.util.FormValidation; +import jenkins.MasterToSlaveFileCallable; +import jenkins.tasks.SimpleBuildStep; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.types.FileSet; import org.apache.commons.io.IOUtils; +import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.QueryParameter; import javax.servlet.ServletException; @@ -39,24 +42,20 @@ * * @author Santiago.PericasGeertsen@sun.com */ -public class TextFinderPublisher extends Recorder implements Serializable { +public class TextFinderPublisher extends Recorder implements Serializable, SimpleBuildStep { - public final String fileSet; + public String fileSet; public final String regexp; - public final boolean succeedIfFound; - public final boolean unstableIfFound; + public boolean succeedIfFound; + public boolean unstableIfFound; /** * True to also scan the whole console output */ - public final boolean alsoCheckConsoleOutput; + public boolean alsoCheckConsoleOutput; @DataBoundConstructor - public TextFinderPublisher(String fileSet, String regexp, boolean succeedIfFound, boolean unstableIfFound, boolean alsoCheckConsoleOutput) { - this.fileSet = Util.fixEmpty(fileSet.trim()); + public TextFinderPublisher(String regexp) { this.regexp = regexp; - this.succeedIfFound = succeedIfFound; - this.unstableIfFound = unstableIfFound; - this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; // Attempt to compile regular expression try { @@ -66,13 +65,69 @@ public TextFinderPublisher(String fileSet, String regexp, boolean succeedIfFound } } + @Deprecated + public TextFinderPublisher( + String fileSet, + String regexp, + boolean succeedIfFound, + boolean unstableIfFound, + boolean alsoCheckConsoleOutput) { + this(regexp); + this.fileSet = Util.fixEmpty(fileSet.trim()); + this.succeedIfFound = succeedIfFound; + this.unstableIfFound = unstableIfFound; + this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; + } + + public String getRegexp() { + return regexp; + } + + public String getFileSet() { + return fileSet; + } + + @DataBoundSetter + public void setFileSet(String fileSet) { + this.fileSet = Util.fixEmpty(fileSet.trim()); + } + + public boolean isSucceedIfFound() { + return succeedIfFound; + } + + @DataBoundSetter + public void setSucceedIfFound(boolean succeedIfFound) { + this.succeedIfFound = succeedIfFound; + } + + public boolean isUnstableIfFound() { + return unstableIfFound; + } + + @DataBoundSetter + public void setUnstableIfFound(boolean unstableIfFound) { + this.unstableIfFound = unstableIfFound; + } + + public boolean isAlsoCheckConsoleOutput() { + return alsoCheckConsoleOutput; + } + + @DataBoundSetter + public void setAlsoCheckConsoleOutput(boolean alsoCheckConsoleOutput) { + this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; + } + + @Override public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; } - public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { - findText(build, listener.getLogger()); - return true; + @Override + public void perform(Run run, FilePath workspace, Launcher launcher, TaskListener listener) + throws InterruptedException, IOException { + findText(run, workspace, listener); } /** @@ -81,13 +136,16 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen private static final class AbortException extends RuntimeException { } - private void findText(AbstractBuild build, PrintStream logger) throws IOException, InterruptedException { + private void findText(Run run, FilePath workspace, TaskListener listener) + throws IOException, InterruptedException { try { + PrintStream logger = listener.getLogger(); boolean foundText = false; if(alsoCheckConsoleOutput) { logger.println("Checking console output"); - foundText |= checkFile(build.getLogFile(), compilePattern(logger), logger, true); + foundText |= + checkFile(run.getLogFile(), compilePattern(logger, regexp), logger, true); } else { // printing this when checking console output will cause the plugin // to find this line, which would be pointless. @@ -95,60 +153,15 @@ private void findText(AbstractBuild build, PrintStream logger) throws IOExceptio logger.println("Checking " + regexp); } - final RemoteOutputStream ros = new RemoteOutputStream(logger); - if(fileSet!=null) { - foundText |= build.getWorkspace().act(new FileCallable() { - public Boolean invoke(File ws, VirtualChannel channel) throws IOException { - PrintStream logger = new PrintStream(ros); - - // Collect list of files for searching - FileSet fs = new FileSet(); - org.apache.tools.ant.Project p = new org.apache.tools.ant.Project(); - fs.setProject(p); - fs.setDir(ws); - fs.setIncludes(fileSet); - DirectoryScanner ds = fs.getDirectoryScanner(p); - - // Any files in the final set? - String[] files = ds.getIncludedFiles(); - if (files.length == 0) { - logger.println("Jenkins Text Finder: File set '" + - fileSet + "' is empty"); - throw new AbortException(); - } - - Pattern pattern = compilePattern(logger); - - boolean foundText = false; - - for (String file : files) { - File f = new File(ws, file); - - if (!f.exists()) { - logger.println("Jenkins Text Finder: Unable to" + - " find file '" + f + "'"); - continue; - } - if (!f.canRead()) { - logger.println("Jenkins Text Finder: Unable to" + - " read from file '" + f + "'"); - continue; - } - - foundText |= checkFile(f, pattern, logger, false); - } - - return foundText; - } - }); + foundText |= workspace.act(new FileChecker(listener, fileSet, regexp)); } if (foundText != succeedIfFound) - build.setResult(unstableIfFound ? Result.UNSTABLE : Result.FAILURE); + run.setResult(unstableIfFound ? Result.UNSTABLE : Result.FAILURE); } catch (AbortException e) { // no test file found - build.setResult(Result.UNSTABLE); + run.setResult(Result.UNSTABLE); } } @@ -159,7 +172,8 @@ public Boolean invoke(File ws, VirtualChannel channel) throws IOException { * true to return immediately as soon as the first hit is found. this is necessary * when we are scanning the console output, because otherwise we'll loop forever. */ - private boolean checkFile(File f, Pattern pattern, PrintStream logger, boolean abortAfterFirstHit) { + private static boolean checkFile( + File f, Pattern pattern, PrintStream logger, boolean abortAfterFirstHit) { boolean logFilename = true; boolean foundText = false; BufferedReader reader=null; @@ -189,7 +203,7 @@ private boolean checkFile(File f, Pattern pattern, PrintStream logger, boolean a return foundText; } - private Pattern compilePattern(PrintStream logger) { + private static Pattern compilePattern(PrintStream logger, String regexp) { Pattern pattern; try { pattern = Pattern.compile(regexp); @@ -201,8 +215,10 @@ private Pattern compilePattern(PrintStream logger) { return pattern; } + @Symbol("textFinder") @Extension public static final class DescriptorImpl extends BuildStepDescriptor { + @Override public String getDisplayName() { return Messages.DisplayName(); } @@ -212,6 +228,7 @@ public String getHelpFile() { return "/plugin/text-finder/help.html"; } + @Override public boolean isApplicable(Class jobType) { return true; } @@ -233,6 +250,61 @@ public FormValidation doCheckRegexp(@QueryParameter String value) throws IOExcep } } + private static class FileChecker extends MasterToSlaveFileCallable { + + private final TaskListener listener; + private final String fileSet; + private final String regexp; + + public FileChecker(TaskListener listener, String fileSet, String regexp) { + this.listener = listener; + this.fileSet = fileSet; + this.regexp = regexp; + } + + @Override + public Boolean invoke(File ws, VirtualChannel channel) throws IOException { + PrintStream logger = listener.getLogger(); + + // Collect list of files for searching + FileSet fs = new FileSet(); + org.apache.tools.ant.Project p = new org.apache.tools.ant.Project(); + fs.setProject(p); + fs.setDir(ws); + fs.setIncludes(fileSet); + DirectoryScanner ds = fs.getDirectoryScanner(p); + + // Any files in the final set? + String[] files = ds.getIncludedFiles(); + if (files.length == 0) { + logger.println("Jenkins Text Finder: File set '" + fileSet + "' is empty"); + throw new AbortException(); + } + + Pattern pattern = compilePattern(logger, regexp); + + boolean foundText = false; + + for (String file : files) { + File f = new File(ws, file); + + if (!f.exists()) { + logger.println("Jenkins Text Finder: Unable to" + " find file '" + f + "'"); + continue; + } + if (!f.canRead()) { + logger.println( + "Jenkins Text Finder: Unable to" + " read from file '" + f + "'"); + continue; + } + + foundText |= checkFile(f, pattern, logger, false); + } + + return foundText; + } + } + private static final long serialVersionUID = 1L; } diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly b/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly index fe51541..2c79b0d 100644 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly @@ -1,3 +1,4 @@ + diff --git a/src/main/resources/index.jelly b/src/main/resources/index.jelly index 943cac6..1bc97d3 100644 --- a/src/main/resources/index.jelly +++ b/src/main/resources/index.jelly @@ -1,3 +1,4 @@ +
This plugin is used to search for strings in workspace files. The outcome of this search can be used to mark the build as normal or failed. diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java new file mode 100644 index 0000000..6b25c04 --- /dev/null +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -0,0 +1,96 @@ +package hudson.plugins.textfinder; + +import hudson.Functions; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import hudson.tasks.BatchFile; +import hudson.tasks.CommandInterpreter; +import hudson.tasks.Shell; +import java.io.File; +import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +public class TextFinderPublisherFreestyleTest { + + private static final String UNIQUE_TEXT = "foobar"; + private static final String ECHO_UNIQUE_TEXT = "echo " + UNIQUE_TEXT; + private static final String LOG_UNIQUE_TEXT = "+ " + ECHO_UNIQUE_TEXT; + + @Rule public JenkinsRule rule = new JenkinsRule(); + + private void assertLogContainsMatch(File file, String text, FreeStyleBuild build) + throws IOException { + rule.assertLogContains(String.format("%s:\n%s", file, text), build); + } + + @Test + public void successIfFoundInConsole() throws Exception { + FreeStyleProject project = rule.createFreeStyleProject("freestyle"); + CommandInterpreter command = + Functions.isWindows() + ? new BatchFile(ECHO_UNIQUE_TEXT) + : new Shell(ECHO_UNIQUE_TEXT); + project.getBuildersList().add(command); + TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); + textFinder.setSucceedIfFound(true); + textFinder.setAlsoCheckConsoleOutput(true); + project.getPublishersList().add(textFinder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.SUCCESS, build); + } + + @Test + public void failureIfFoundInConsole() throws Exception { + FreeStyleProject project = rule.createFreeStyleProject("freestyle"); + CommandInterpreter command = + Functions.isWindows() + ? new BatchFile(ECHO_UNIQUE_TEXT) + : new Shell(ECHO_UNIQUE_TEXT); + project.getBuildersList().add(command); + TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); + textFinder.setAlsoCheckConsoleOutput(true); + project.getPublishersList().add(textFinder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.FAILURE, build); + } + + @Test + public void unstableIfFoundInConsole() throws Exception { + FreeStyleProject project = rule.createFreeStyleProject("freestyle"); + CommandInterpreter command = + Functions.isWindows() + ? new BatchFile(ECHO_UNIQUE_TEXT) + : new Shell(ECHO_UNIQUE_TEXT); + project.getBuildersList().add(command); + TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); + textFinder.setUnstableIfFound(true); + textFinder.setAlsoCheckConsoleOutput(true); + project.getPublishersList().add(textFinder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.UNSTABLE, build); + } + + @Test + public void notFoundInConsole() throws Exception { + FreeStyleProject project = rule.createFreeStyleProject("freestyle"); + TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); + textFinder.setAlsoCheckConsoleOutput(true); + project.getPublishersList().add(textFinder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking 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 new file mode 100644 index 0000000..76d398f --- /dev/null +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -0,0 +1,151 @@ +package hudson.plugins.textfinder; + +import hudson.model.Result; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.jenkinsci.plugins.workflow.actions.WorkspaceAction; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; +import org.jenkinsci.plugins.workflow.graph.FlowNode; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +public class TextFinderPublisherPipelineTest { + + private static final String UNIQUE_TEXT = "foobar"; + private static final String ECHO_UNIQUE_TEXT = "echo " + UNIQUE_TEXT; + private static final String LOG_UNIQUE_TEXT = "+ " + ECHO_UNIQUE_TEXT; + + @Rule public JenkinsRule rule = new JenkinsRule(); + + private void assertLogContainsMatch(File file, String text, WorkflowRun build) + throws IOException { + rule.assertLogContains(String.format("%s:\n%s", file, text), build); + } + + private File getWorkspace(WorkflowRun build) { + FlowGraphWalker walker = new FlowGraphWalker(build.getExecution()); + List actions = new ArrayList<>(); + for (FlowNode node : walker) { + WorkspaceAction action = node.getAction(WorkspaceAction.class); + if (action != null) { + return new File(action.getWorkspace().getRemote()); + } + } + throw new IllegalStateException("Failed to find workspace"); + } + + @Test + 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 {textFinder regexp: 'foobar', fileSet: 'out.txt', succeedIfFound: true}\n")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking foobar", build); + assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.SUCCESS, build); + } + + @Test + 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 {textFinder regexp: 'foobar', fileSet: 'out.txt'}\n")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking foobar", build); + assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.FAILURE, build); + } + + @Test + 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 {textFinder regexp: 'foobar', fileSet: 'out.txt', unstableIfFound: true}\n")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking foobar", build); + assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.UNSTABLE, build); + } + + @Test + 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 {textFinder regexp: 'foobar', fileSet: 'out.txt'}\n")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking foobar", build); + rule.assertBuildStatus(Result.SUCCESS, build); + } + + @Test + public void successIfFoundInConsole() throws Exception { + WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + project.setDefinition( + new CpsFlowDefinition( + "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" + + "node {textFinder regexp: 'foobar', succeedIfFound: true, alsoCheckConsoleOutput: true}\n")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.SUCCESS, build); + } + + @Test + public void failureIfFoundInConsole() throws Exception { + WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + project.setDefinition( + new CpsFlowDefinition( + "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" + + "node {textFinder regexp: 'foobar', alsoCheckConsoleOutput: true}\n")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.FAILURE, build); + } + + @Test + public void unstableIfFoundInConsole() throws Exception { + WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + project.setDefinition( + new CpsFlowDefinition( + "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" + + "node {textFinder regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true}\n")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.UNSTABLE, build); + } + + @Test + public void notFoundInConsole() throws Exception { + WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + project.setDefinition( + new CpsFlowDefinition( + "node {textFinder regexp: 'foobar', alsoCheckConsoleOutput: true}\n")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + rule.assertBuildStatus(Result.SUCCESS, build); + } +} From 8a3753a8f9605dad0eb4f294830230bc9d476345 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 12 Jun 2018 17:29:59 -0700 Subject: [PATCH 02/15] Update POM version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d18273d..33a82a3 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 3.4 + 3.15 From dcaeee1dab7000879b6ac1c605cec2d1b9b51c88 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 12 Jun 2018 17:30:54 -0700 Subject: [PATCH 03/15] textFinder => findText (per review feedback) --- .../plugins/textfinder/TextFinderPublisher.java | 2 +- .../TextFinderPublisherPipelineTest.java | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 2117b52..8e79735 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -215,7 +215,7 @@ private static Pattern compilePattern(PrintStream logger, String regexp) { return pattern; } - @Symbol("textFinder") + @Symbol("findText") @Extension public static final class DescriptorImpl extends BuildStepDescriptor { @Override diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java index 76d398f..c103a97 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -46,7 +46,7 @@ public void successIfFoundInFile() throws Exception { project.setDefinition( new CpsFlowDefinition( "node {writeFile file: 'out.txt', text: 'foobar'}\n" - + "node {textFinder regexp: 'foobar', fileSet: 'out.txt', succeedIfFound: true}\n")); + + "node {findText regexp: 'foobar', fileSet: 'out.txt', succeedIfFound: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking foobar", build); @@ -60,7 +60,7 @@ public void failureIfFoundInFile() throws Exception { project.setDefinition( new CpsFlowDefinition( "node {writeFile file: 'out.txt', text: 'foobar'}\n" - + "node {textFinder regexp: 'foobar', fileSet: 'out.txt'}\n")); + + "node {findText regexp: 'foobar', fileSet: 'out.txt'}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking foobar", build); @@ -74,7 +74,7 @@ public void unstableIfFoundInFile() throws Exception { project.setDefinition( new CpsFlowDefinition( "node {writeFile file: 'out.txt', text: 'foobar'}\n" - + "node {textFinder regexp: 'foobar', fileSet: 'out.txt', unstableIfFound: true}\n")); + + "node {findText regexp: 'foobar', fileSet: 'out.txt', unstableIfFound: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking foobar", build); @@ -88,7 +88,7 @@ public void notFoundInFile() throws Exception { project.setDefinition( new CpsFlowDefinition( "node {writeFile file: 'out.txt', text: 'foobaz'}\n" - + "node {textFinder regexp: 'foobar', fileSet: 'out.txt'}\n")); + + "node {findText regexp: 'foobar', fileSet: 'out.txt'}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking foobar", build); @@ -101,7 +101,7 @@ public void successIfFoundInConsole() throws Exception { project.setDefinition( new CpsFlowDefinition( "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" - + "node {textFinder regexp: 'foobar', succeedIfFound: true, alsoCheckConsoleOutput: true}\n")); + + "node {findText regexp: 'foobar', succeedIfFound: true, alsoCheckConsoleOutput: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); @@ -115,7 +115,7 @@ public void failureIfFoundInConsole() throws Exception { project.setDefinition( new CpsFlowDefinition( "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" - + "node {textFinder regexp: 'foobar', alsoCheckConsoleOutput: true}\n")); + + "node {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); @@ -129,7 +129,7 @@ public void unstableIfFoundInConsole() throws Exception { project.setDefinition( new CpsFlowDefinition( "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" - + "node {textFinder regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true}\n")); + + "node {findText regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); @@ -142,7 +142,7 @@ public void notFoundInConsole() throws Exception { WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); project.setDefinition( new CpsFlowDefinition( - "node {textFinder regexp: 'foobar', alsoCheckConsoleOutput: true}\n")); + "node {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); From ad508d1b9ee3f6c58ca0ad90f7edf8dd1a21e9a1 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 12 Jun 2018 17:47:31 -0700 Subject: [PATCH 04/15] Add automated test on agent --- .../TextFinderPublisherAgentTest.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java new file mode 100644 index 0000000..720b51a --- /dev/null +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -0,0 +1,77 @@ +package hudson.plugins.textfinder; + +import hudson.model.Result; +import hudson.slaves.DumbSlave; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.jenkinsci.plugins.workflow.actions.WorkspaceAction; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; +import org.jenkinsci.plugins.workflow.graph.FlowNode; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +public class TextFinderPublisherAgentTest { + + private static final String UNIQUE_TEXT = "foobar"; + private static final String ECHO_UNIQUE_TEXT = "echo " + UNIQUE_TEXT; + private static final String LOG_UNIQUE_TEXT = "+ " + ECHO_UNIQUE_TEXT; + + @Rule public JenkinsRule rule = new JenkinsRule(); + + private void assertLogContainsMatch(File file, String text, WorkflowRun build) + throws IOException { + rule.assertLogContains(String.format("%s:\n%s", file, text), build); + } + + private File getWorkspace(WorkflowRun build) { + FlowGraphWalker walker = new FlowGraphWalker(build.getExecution()); + List actions = new ArrayList<>(); + for (FlowNode node : walker) { + WorkspaceAction action = node.getAction(WorkspaceAction.class); + if (action != null) { + return new File(action.getWorkspace().getRemote()); + } + } + throw new IllegalStateException("Failed to find workspace"); + } + + @Test + public void failureIfFoundInFileOnAgent() throws Exception { + DumbSlave agent = rule.createOnlineSlave(); + WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + project.setDefinition( + new CpsFlowDefinition( + String.format( + "node('%s') {writeFile file: 'out.txt', text: 'foobar'}\n" + + "node('%s') {findText regexp: 'foobar', fileSet: 'out.txt'}\n", + agent.getNodeName(), agent.getNodeName()))); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking foobar", build); + assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.FAILURE, build); + } + + @Test + public void failureIfFoundInConsoleOnAgent() throws Exception { + DumbSlave agent = rule.createOnlineSlave(); + WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + project.setDefinition( + new CpsFlowDefinition( + String.format( + "node('%s') {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" + + "node('%s') {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n", + agent.getNodeName(), agent.getNodeName()))); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + rule.assertBuildStatus(Result.FAILURE, build); + } +} From fc58b298e6a22127121a611d507e7e3868b51d15 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 12 Jun 2018 17:51:45 -0700 Subject: [PATCH 05/15] TaskListener => RemoteOutputStream (per review feedback) --- .../plugins/textfinder/TextFinderPublisher.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 8e79735..6dc4047 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -9,6 +9,7 @@ import hudson.model.Result; import hudson.model.Run; import hudson.model.TaskListener; +import hudson.remoting.RemoteOutputStream; import hudson.remoting.VirtualChannel; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepMonitor; @@ -153,8 +154,10 @@ private void findText(Run run, FilePath workspace, TaskListener listener) logger.println("Checking " + regexp); } + final RemoteOutputStream ros = new RemoteOutputStream(logger); + if(fileSet!=null) { - foundText |= workspace.act(new FileChecker(listener, fileSet, regexp)); + foundText |= workspace.act(new FileChecker(ros, fileSet, regexp)); } if (foundText != succeedIfFound) @@ -252,19 +255,19 @@ public FormValidation doCheckRegexp(@QueryParameter String value) throws IOExcep private static class FileChecker extends MasterToSlaveFileCallable { - private final TaskListener listener; + private final RemoteOutputStream ros; private final String fileSet; private final String regexp; - public FileChecker(TaskListener listener, String fileSet, String regexp) { - this.listener = listener; + public FileChecker(RemoteOutputStream ros, String fileSet, String regexp) { + this.ros = ros; this.fileSet = fileSet; this.regexp = regexp; } @Override public Boolean invoke(File ws, VirtualChannel channel) throws IOException { - PrintStream logger = listener.getLogger(); + PrintStream logger = new PrintStream(ros); // Collect list of files for searching FileSet fs = new FileSet(); From 563aaa3e6a2718c8e6a1b9e02b787ba5dbc713e5 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 12 Jun 2018 18:54:13 -0700 Subject: [PATCH 06/15] Add Jenkinsfile for https://ci.jenkins.io/job/Plugins/ --- Jenkinsfile | 1 + 1 file changed, 1 insertion(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..a229fa5 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1 @@ +buildPlugin() From 5906137af21afc2c3f163077bad23dae601f9221 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 13 Jun 2018 09:37:23 -0700 Subject: [PATCH 07/15] Set up testing against the 1.121.x branch for discovering compatibility issues --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index a229fa5..ed021b6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1 +1 @@ -buildPlugin() +buildPlugin(jenkinsVersions: [null, '2.121.1']) From 73c6bb1f59fe0b9c2d823f1e799247a52fba767d Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Jun 2018 07:59:56 -0700 Subject: [PATCH 08/15] Bump minimum Jenkins version to 1.651 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 33a82a3..5d53306 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ Jenkins TextFinder plugin http://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin - 1.642.1 + 1.651 7 From 1bf70f41e2fef9195489b6e3f6c495f65f4b6876 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Jun 2018 08:18:22 -0700 Subject: [PATCH 09/15] Downgrade minimum Jenkins version to 1.650 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5d53306..fc894b7 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ Jenkins TextFinder plugin http://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin - 1.651 + 1.650 7 From 4f4eb26b69cfceb207809d8bfd65b0b21d78b771 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Jun 2018 09:17:20 -0700 Subject: [PATCH 10/15] Fix FindBugs warnings --- pom.xml | 2 +- .../hudson/plugins/textfinder/TextFinderPublisher.java | 9 +++++---- .../hudson/plugins/textfinder/Messages.properties | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index fc894b7..5d53306 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ Jenkins TextFinder plugin http://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin - 1.650 + 1.651 7 diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 6dc4047..6e08cca 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -29,8 +29,9 @@ import javax.servlet.ServletException; import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.io.PrintStream; import java.io.Serializable; import java.util.regex.Matcher; @@ -183,7 +184,7 @@ private static boolean checkFile( try { // Assume default encoding and text files String line; - reader = new BufferedReader(new FileReader(f)); + reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8")); while ((line = reader.readLine()) != null) { Matcher matcher = pattern.matcher(line); if (matcher.find()) { @@ -223,7 +224,7 @@ private static Pattern compilePattern(PrintStream logger, String regexp) { public static final class DescriptorImpl extends BuildStepDescriptor { @Override public String getDisplayName() { - return Messages.DisplayName(); + return Messages.TextFinderPublisher_DisplayName(); } @Override @@ -267,7 +268,7 @@ public FileChecker(RemoteOutputStream ros, String fileSet, String regexp) { @Override public Boolean invoke(File ws, VirtualChannel channel) throws IOException { - PrintStream logger = new PrintStream(ros); + PrintStream logger = new PrintStream(ros, false, "UTF-8"); // Collect list of files for searching FileSet fs = new FileSet(); diff --git a/src/main/resources/hudson/plugins/textfinder/Messages.properties b/src/main/resources/hudson/plugins/textfinder/Messages.properties index c786f57..af5ebc7 100755 --- a/src/main/resources/hudson/plugins/textfinder/Messages.properties +++ b/src/main/resources/hudson/plugins/textfinder/Messages.properties @@ -1 +1 @@ -DisplayName=Jenkins Text Finder +TextFinderPublisher.DisplayName=Jenkins Text Finder From 1ff8ffd4902f00a760485b8ddf22d67d329392b9 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Jun 2018 09:34:16 -0700 Subject: [PATCH 11/15] Attempt to fix tests on Windows --- .../plugins/textfinder/TextFinderPublisherAgentTest.java | 3 ++- .../plugins/textfinder/TextFinderPublisherFreestyleTest.java | 3 ++- .../plugins/textfinder/TextFinderPublisherPipelineTest.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java index 720b51a..f8a6e34 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -26,7 +26,8 @@ public class TextFinderPublisherAgentTest { private void assertLogContainsMatch(File file, String text, WorkflowRun build) throws IOException { - rule.assertLogContains(String.format("%s:\n%s", file, text), build); + rule.assertLogContains( + String.format("%s:%s%s", file, System.getProperty("line.separator"), text), build); } private File getWorkspace(WorkflowRun build) { diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index 6b25c04..edaf14f 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -23,7 +23,8 @@ public class TextFinderPublisherFreestyleTest { private void assertLogContainsMatch(File file, String text, FreeStyleBuild build) throws IOException { - rule.assertLogContains(String.format("%s:\n%s", file, text), build); + rule.assertLogContains( + String.format("%s:%s%s", file, System.getProperty("line.separator"), text), build); } @Test diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java index c103a97..6e768c0 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -25,7 +25,8 @@ public class TextFinderPublisherPipelineTest { private void assertLogContainsMatch(File file, String text, WorkflowRun build) throws IOException { - rule.assertLogContains(String.format("%s:\n%s", file, text), build); + rule.assertLogContains( + String.format("%s:%s%s", file, System.getProperty("line.separator"), text), build); } private File getWorkspace(WorkflowRun build) { From 3b31863e1259ad5e99f75617b1381f5069dbc62d Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Jun 2018 09:34:21 -0700 Subject: [PATCH 12/15] Remove getters --- .../textfinder/TextFinderPublisher.java | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 6e08cca..fec36f4 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -81,41 +81,21 @@ public TextFinderPublisher( this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; } - public String getRegexp() { - return regexp; - } - - public String getFileSet() { - return fileSet; - } - @DataBoundSetter public void setFileSet(String fileSet) { this.fileSet = Util.fixEmpty(fileSet.trim()); } - public boolean isSucceedIfFound() { - return succeedIfFound; - } - @DataBoundSetter public void setSucceedIfFound(boolean succeedIfFound) { this.succeedIfFound = succeedIfFound; } - public boolean isUnstableIfFound() { - return unstableIfFound; - } - @DataBoundSetter public void setUnstableIfFound(boolean unstableIfFound) { this.unstableIfFound = unstableIfFound; } - public boolean isAlsoCheckConsoleOutput() { - return alsoCheckConsoleOutput; - } - @DataBoundSetter public void setAlsoCheckConsoleOutput(boolean alsoCheckConsoleOutput) { this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; From 4c89b0ce1b2ceb5805531ca5220ae06956a9f54f Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Jun 2018 10:26:07 -0700 Subject: [PATCH 13/15] Attempt to fix tests on Windows (again) --- .../TextFinderPublisherAgentTest.java | 20 ++++++++---- .../TextFinderPublisherFreestyleTest.java | 27 ++++++++++------ .../TextFinderPublisherPipelineTest.java | 32 ++++++++++++------- 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java index f8a6e34..f73d714 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -1,5 +1,6 @@ package hudson.plugins.textfinder; +import hudson.Functions; import hudson.model.Result; import hudson.slaves.DumbSlave; import java.io.File; @@ -20,14 +21,21 @@ public class TextFinderPublisherAgentTest { private static final String UNIQUE_TEXT = "foobar"; private static final String ECHO_UNIQUE_TEXT = "echo " + UNIQUE_TEXT; - private static final String LOG_UNIQUE_TEXT = "+ " + ECHO_UNIQUE_TEXT; @Rule public JenkinsRule rule = new JenkinsRule(); - private void assertLogContainsMatch(File file, String text, WorkflowRun build) + private void assertLogContainsMatch(File file, String text, WorkflowRun build, boolean isShell) throws IOException { + String prompt; + if (isShell) { + prompt = Functions.isWindows() ? "> " : "+ "; + } else { + prompt = ""; + } rule.assertLogContains( - String.format("%s:%s%s", file, System.getProperty("line.separator"), text), build); + String.format( + "%s:%s%s%s", file, System.getProperty("line.separator"), prompt, text), + build); } private File getWorkspace(WorkflowRun build) { @@ -55,7 +63,7 @@ public void failureIfFoundInFileOnAgent() throws Exception { WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking foobar", build); - assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build); + assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false); rule.assertBuildStatus(Result.FAILURE, build); } @@ -66,13 +74,13 @@ public void failureIfFoundInConsoleOnAgent() throws Exception { project.setDefinition( new CpsFlowDefinition( String.format( - "node('%s') {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" + "node('%s') {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n" + "node('%s') {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n", agent.getNodeName(), agent.getNodeName()))); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); - assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, 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 edaf14f..3e54d71 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -17,14 +17,21 @@ public class TextFinderPublisherFreestyleTest { private static final String UNIQUE_TEXT = "foobar"; private static final String ECHO_UNIQUE_TEXT = "echo " + UNIQUE_TEXT; - private static final String LOG_UNIQUE_TEXT = "+ " + ECHO_UNIQUE_TEXT; @Rule public JenkinsRule rule = new JenkinsRule(); - private void assertLogContainsMatch(File file, String text, FreeStyleBuild build) - throws IOException { + private void assertLogContainsMatch( + File file, String text, FreeStyleBuild build, boolean isShell) throws IOException { + String prompt; + if (isShell) { + prompt = Functions.isWindows() ? "> " : "+ "; + } else { + prompt = ""; + } rule.assertLogContains( - String.format("%s:%s%s", file, System.getProperty("line.separator"), text), build); + String.format( + "%s:%s%s%s", file, System.getProperty("line.separator"), prompt, text), + build); } @Test @@ -32,7 +39,7 @@ public void successIfFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject("freestyle"); CommandInterpreter command = Functions.isWindows() - ? new BatchFile(ECHO_UNIQUE_TEXT) + ? new BatchFile("prompt $G\\r\\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); @@ -42,7 +49,7 @@ public void successIfFoundInConsole() throws Exception { FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); - assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.SUCCESS, build); } @@ -51,7 +58,7 @@ public void failureIfFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject("freestyle"); CommandInterpreter command = Functions.isWindows() - ? new BatchFile(ECHO_UNIQUE_TEXT) + ? new BatchFile("prompt $G\\r\\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); @@ -60,7 +67,7 @@ public void failureIfFoundInConsole() throws Exception { FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); - assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.FAILURE, build); } @@ -69,7 +76,7 @@ public void unstableIfFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject("freestyle"); CommandInterpreter command = Functions.isWindows() - ? new BatchFile(ECHO_UNIQUE_TEXT) + ? new BatchFile("prompt $G\\r\\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); @@ -79,7 +86,7 @@ public void unstableIfFoundInConsole() throws Exception { FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); - assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.UNSTABLE, build); } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java index 6e768c0..c7eefdc 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -1,5 +1,6 @@ package hudson.plugins.textfinder; +import hudson.Functions; import hudson.model.Result; import java.io.File; import java.io.IOException; @@ -19,14 +20,21 @@ public class TextFinderPublisherPipelineTest { private static final String UNIQUE_TEXT = "foobar"; private static final String ECHO_UNIQUE_TEXT = "echo " + UNIQUE_TEXT; - private static final String LOG_UNIQUE_TEXT = "+ " + ECHO_UNIQUE_TEXT; @Rule public JenkinsRule rule = new JenkinsRule(); - private void assertLogContainsMatch(File file, String text, WorkflowRun build) + private void assertLogContainsMatch(File file, String text, WorkflowRun build, boolean isShell) throws IOException { + String prompt; + if (isShell) { + prompt = Functions.isWindows() ? "> " : "+ "; + } else { + prompt = ""; + } rule.assertLogContains( - String.format("%s:%s%s", file, System.getProperty("line.separator"), text), build); + String.format( + "%s:%s%s%s", file, System.getProperty("line.separator"), prompt, text), + build); } private File getWorkspace(WorkflowRun build) { @@ -51,7 +59,7 @@ public void successIfFoundInFile() throws Exception { WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking foobar", build); - assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build); + assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false); rule.assertBuildStatus(Result.SUCCESS, build); } @@ -65,7 +73,7 @@ public void failureIfFoundInFile() throws Exception { WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking foobar", build); - assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build); + assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false); rule.assertBuildStatus(Result.FAILURE, build); } @@ -79,7 +87,7 @@ public void unstableIfFoundInFile() throws Exception { WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking foobar", build); - assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build); + assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false); rule.assertBuildStatus(Result.UNSTABLE, build); } @@ -101,12 +109,12 @@ public void successIfFoundInConsole() throws Exception { WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); project.setDefinition( new CpsFlowDefinition( - "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" + "node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n" + "node {findText regexp: 'foobar', succeedIfFound: true, alsoCheckConsoleOutput: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); - assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.SUCCESS, build); } @@ -115,12 +123,12 @@ public void failureIfFoundInConsole() throws Exception { WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); project.setDefinition( new CpsFlowDefinition( - "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" + "node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n" + "node {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); - assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.FAILURE, build); } @@ -129,12 +137,12 @@ public void unstableIfFoundInConsole() throws Exception { WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); project.setDefinition( new CpsFlowDefinition( - "node {isUnix() ? sh('echo foobar') : bat('echo foobar')}\n" + "node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n" + "node {findText regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true}\n")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); - assertLogContainsMatch(build.getLogFile(), LOG_UNIQUE_TEXT, build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.UNSTABLE, build); } From 410dc7ebf58896054cbcf0035c4c50aa885656cf Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Jun 2018 10:32:38 -0700 Subject: [PATCH 14/15] Another try at fixing tests on Windows --- .../hudson/plugins/textfinder/TextFinderPublisherAgentTest.java | 2 +- .../plugins/textfinder/TextFinderPublisherFreestyleTest.java | 2 +- .../plugins/textfinder/TextFinderPublisherPipelineTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java index f73d714..dc26bc1 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -28,7 +28,7 @@ private void assertLogContainsMatch(File file, String text, WorkflowRun build, b throws IOException { String prompt; if (isShell) { - prompt = Functions.isWindows() ? "> " : "+ "; + prompt = Functions.isWindows() ? ">" : "+ "; } else { prompt = ""; } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index 3e54d71..d4e8b09 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -24,7 +24,7 @@ private void assertLogContainsMatch( File file, String text, FreeStyleBuild build, boolean isShell) throws IOException { String prompt; if (isShell) { - prompt = Functions.isWindows() ? "> " : "+ "; + prompt = Functions.isWindows() ? ">" : "+ "; } else { prompt = ""; } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java index c7eefdc..ad14937 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -27,7 +27,7 @@ private void assertLogContainsMatch(File file, String text, WorkflowRun build, b throws IOException { String prompt; if (isShell) { - prompt = Functions.isWindows() ? "> " : "+ "; + prompt = Functions.isWindows() ? ">" : "+ "; } else { prompt = ""; } From 54f6a0cd2dc6484086127d94072dc0095bafe38f Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Jun 2018 10:37:41 -0700 Subject: [PATCH 15/15] Another try at fixing tests on Windows --- .../textfinder/TextFinderPublisherFreestyleTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index d4e8b09..a24bd95 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -39,7 +39,7 @@ public void successIfFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject("freestyle"); CommandInterpreter command = Functions.isWindows() - ? new BatchFile("prompt $G\\r\\n" + ECHO_UNIQUE_TEXT) + ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); @@ -58,7 +58,7 @@ public void failureIfFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject("freestyle"); CommandInterpreter command = Functions.isWindows() - ? new BatchFile("prompt $G\\r\\n" + ECHO_UNIQUE_TEXT) + ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); @@ -76,7 +76,7 @@ public void unstableIfFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject("freestyle"); CommandInterpreter command = Functions.isWindows() - ? new BatchFile("prompt $G\\r\\n" + ECHO_UNIQUE_TEXT) + ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT);