From eba5a69509c470169478b14aba485469c3c49aed Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Wed, 10 Apr 2019 16:15:45 +0200 Subject: [PATCH 01/15] multi finder first somehow working patch --- pom.xml | 8 ++ .../plugins/textfinder/TextFinderModel.java | 74 +++++++++++ .../textfinder/TextFinderPublisher.java | 120 ++++++++---------- .../textfinder/TextFinderModel/config.jelly | 22 ++++ .../TextFinderPublisher/config.jelly | 8 ++ .../TextFinderPublisherFreestyleTest.java | 20 +-- 6 files changed, 177 insertions(+), 75 deletions(-) create mode 100644 src/main/java/hudson/plugins/textfinder/TextFinderModel.java create mode 100644 src/main/resources/hudson/plugins/textfinder/TextFinderModel/config.jelly diff --git a/pom.xml b/pom.xml index 96695ee..f5d1abe 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,14 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + com.coveo fmt-maven-plugin diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java new file mode 100644 index 0000000..83b3d8b --- /dev/null +++ b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java @@ -0,0 +1,74 @@ +package hudson.plugins.textfinder; + +import hudson.Extension; +import hudson.Util; +import hudson.model.AbstractDescribableImpl; +import hudson.model.Descriptor; +import org.kohsuke.stapler.DataBoundConstructor; + +import java.io.Serializable; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +public final class TextFinderModel extends AbstractDescribableImpl implements Serializable { + private static final long serialVersionUID = 7645849785536280615L; + + public final String fileSet; + public final String regexp; + public final boolean succeedIfFound; + public final boolean unstableIfFound; + public final boolean notBuiltIfFound; + /** + * True to also scan the whole console output + */ + public final boolean alsoCheckConsoleOutput; + + @DataBoundConstructor + public TextFinderModel(String fileSet, String regexp, boolean succeedIfFound, boolean unstableIfFound, boolean alsoCheckConsoleOutput, boolean notBuiltIfFound) { + this.fileSet = fileSet != null ? Util.fixEmpty(fileSet.trim()) : null; + this.regexp = regexp; + this.succeedIfFound = succeedIfFound; + this.unstableIfFound = unstableIfFound; + this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; + this.notBuiltIfFound = notBuiltIfFound; + + // Attempt to compile regular expression + try { + Pattern.compile(regexp); + } catch (PatternSyntaxException e) { + // falls through + } + } + + public String getFileSet() { + return fileSet; + } + + public String getRegexp() { + return regexp; + } + + public boolean isSucceedIfFound() { + return succeedIfFound; + } + + public boolean isUnstableIfFound() { + return unstableIfFound; + } + + public boolean isNotBuiltIfFound() { + return notBuiltIfFound; + } + + public boolean isAlsoCheckConsoleOutput() { + return alsoCheckConsoleOutput; + } + + @Extension + public static class DescriptorImpl extends Descriptor { + @Override + public String getDisplayName() { + return "Text finder"; + } + } +} \ No newline at end of file diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index d5c7026..3f13cf7 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -1,7 +1,5 @@ package hudson.plugins.textfinder; -import static hudson.Util.fixEmpty; - import hudson.Extension; import hudson.FilePath; import hudson.Launcher; @@ -17,6 +15,18 @@ import hudson.tasks.Publisher; import hudson.tasks.Recorder; import hudson.util.FormValidation; +import jenkins.MasterToSlaveFileCallable; +import jenkins.model.Jenkins; +import jenkins.tasks.SimpleBuildStep; +import org.apache.commons.io.IOUtils; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.FileSet; +import org.jenkinsci.Symbol; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.QueryParameter; + +import javax.servlet.ServletException; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -25,20 +35,12 @@ import java.io.PrintStream; import java.io.Serializable; import java.nio.charset.Charset; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; -import javax.servlet.ServletException; -import jenkins.MasterToSlaveFileCallable; -import jenkins.tasks.SimpleBuildStep; -import org.apache.commons.io.IOUtils; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.types.FileSet; -import org.jenkinsci.Symbol; -import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.DataBoundSetter; -import org.kohsuke.stapler.QueryParameter; + +import static hudson.Util.fixEmpty; /** * Text Finder plugin for Jenkins. Search in the workspace using a regular expression and determine @@ -48,63 +50,41 @@ */ public class TextFinderPublisher extends Recorder implements Serializable, SimpleBuildStep { - public String fileSet; + /* these are kept here just for backward compatibility and should not be accessed directly */ + public final String fileSet; public final String regexp; - public boolean succeedIfFound; - public boolean unstableIfFound; - public boolean notBuiltIfFound; + public final boolean succeedIfFound; + public final boolean unstableIfFound; + public final boolean notBuiltIfFound; /** True to also scan the whole console output */ - public boolean alsoCheckConsoleOutput; + public final boolean alsoCheckConsoleOutput; - @DataBoundConstructor - public TextFinderPublisher(String regexp) { - this.regexp = regexp; + public final List textFinders; - // Attempt to compile regular expression - try { - Pattern.compile(regexp); - } catch (PatternSyntaxException e) { - // falls through - } - } - - @Deprecated + @DataBoundConstructor public TextFinderPublisher( String fileSet, String regexp, boolean succeedIfFound, boolean unstableIfFound, - boolean alsoCheckConsoleOutput) { - this(regexp); + boolean notBuiltIfFound, + boolean alsoCheckConsoleOutput, + List textFinders) { this.fileSet = Util.fixEmpty(fileSet.trim()); this.succeedIfFound = succeedIfFound; this.unstableIfFound = unstableIfFound; this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; - } - - @DataBoundSetter - public void setFileSet(String fileSet) { - this.fileSet = Util.fixEmpty(fileSet.trim()); - } - - @DataBoundSetter - public void setSucceedIfFound(boolean succeedIfFound) { - this.succeedIfFound = succeedIfFound; - } - - @DataBoundSetter - public void setUnstableIfFound(boolean unstableIfFound) { - this.unstableIfFound = unstableIfFound; - } - - @DataBoundSetter - public void setNotBuiltIfFound(boolean notBuiltIfFound) { this.notBuiltIfFound = notBuiltIfFound; - } + this.textFinders = textFinders; - @DataBoundSetter - public void setAlsoCheckConsoleOutput(boolean alsoCheckConsoleOutput) { - this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; + this.regexp = regexp; + + // Attempt to compile regular expression + try { + Pattern.compile(regexp); + } catch (PatternSyntaxException e) { + // falls through + } } @Override @@ -115,24 +95,30 @@ public BuildStepMonitor getRequiredMonitorService() { @Override public void perform(Run run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException { - findText(run, workspace, listener); + findText(run, workspace, listener, new TextFinderModel(fileSet, regexp, succeedIfFound, + unstableIfFound, alsoCheckConsoleOutput, notBuiltIfFound)); + if (textFinders != null) { + for (TextFinderModel textFinder : textFinders) { + findText(run, workspace, listener, textFinder); + } + } } /** Indicates an orderly abortion of the processing. */ private static final class AbortException extends RuntimeException {} - private void findText(Run run, FilePath workspace, TaskListener listener) + private void findText(Run run, FilePath workspace, TaskListener listener, final TextFinderModel textFinder) throws IOException, InterruptedException { try { PrintStream logger = listener.getLogger(); boolean foundText = false; - if (alsoCheckConsoleOutput) { + if (textFinder.alsoCheckConsoleOutput) { logger.println("Checking console output"); foundText |= checkFile( run.getLogFile(), - compilePattern(logger, regexp), + compilePattern(logger, textFinder.regexp), logger, run.getCharset(), true); @@ -140,21 +126,21 @@ private void findText(Run run, FilePath workspace, TaskListener listener) // 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 - logger.println("Checking " + regexp); + logger.println("Checking " + textFinder.regexp); } final RemoteOutputStream ros = new RemoteOutputStream(logger); - if (fileSet != null) { - foundText |= workspace.act(new FileChecker(ros, fileSet, regexp)); + if (textFinder.fileSet != null) { + foundText |= workspace.act(new FileChecker(ros, textFinder.fileSet, textFinder.regexp)); } - if (foundText != succeedIfFound) { + if (foundText != textFinder.succeedIfFound) { final Result finalResult; - if (notBuiltIfFound) { + if (textFinder.notBuiltIfFound) { finalResult = Result.NOT_BUILT; } else { - finalResult = unstableIfFound ? Result.UNSTABLE : Result.FAILURE; + finalResult = textFinder.unstableIfFound ? Result.UNSTABLE : Result.FAILURE; } run.setResult(finalResult); } @@ -236,6 +222,10 @@ public boolean isApplicable(Class jobType) { return true; } + public List getItemDescriptors() { + return Jenkins.getInstance().getDescriptorList(TextFinderModel.class); + } + /** * Checks the regular expression validity. * diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderModel/config.jelly b/src/main/resources/hudson/plugins/textfinder/TextFinderModel/config.jelly new file mode 100644 index 0000000..a130914 --- /dev/null +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderModel/config.jelly @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly b/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly index 41debca..4c5a754 100644 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly @@ -19,4 +19,12 @@ + + + diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index a24bd95..741ccc9 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -9,6 +9,8 @@ import hudson.tasks.Shell; import java.io.File; import java.io.IOException; +import java.util.Collections; + import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; @@ -42,9 +44,8 @@ public void successIfFoundInConsole() throws Exception { ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); - TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); - textFinder.setSucceedIfFound(true); - textFinder.setAlsoCheckConsoleOutput(true); + TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, true, false, false, true, + Collections.emptyList()); project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); @@ -61,8 +62,8 @@ public void failureIfFoundInConsole() throws Exception { ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); - TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); - textFinder.setAlsoCheckConsoleOutput(true); + TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, false, false, false, true, + Collections.emptyList()); project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); @@ -79,9 +80,8 @@ public void unstableIfFoundInConsole() throws Exception { ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); - TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); - textFinder.setUnstableIfFound(true); - textFinder.setAlsoCheckConsoleOutput(true); + TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, false, true, false, true, + Collections.emptyList()); project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); @@ -93,8 +93,8 @@ public void unstableIfFoundInConsole() throws Exception { @Test public void notFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject("freestyle"); - TextFinderPublisher textFinder = new TextFinderPublisher(UNIQUE_TEXT); - textFinder.setAlsoCheckConsoleOutput(true); + TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, false, false, false, true, + Collections.emptyList()); project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); From f4ca6f7349ef96308d70fbf1268013674a7c2b38 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 11 Apr 2019 11:38:59 +0200 Subject: [PATCH 02/15] base config fields moved to first item in text finders list --- .../textfinder/TextFinderPublisher.java | 106 ++++++++++++------ 1 file changed, 73 insertions(+), 33 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 3f13cf7..3865bfb 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -35,6 +35,7 @@ import java.io.PrintStream; import java.io.Serializable; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -50,17 +51,21 @@ */ public class TextFinderPublisher extends Recorder implements Serializable, SimpleBuildStep { - /* these are kept here just for backward compatibility and should not be accessed directly */ - public final String fileSet; - public final String regexp; - public final boolean succeedIfFound; - public final boolean unstableIfFound; - public final boolean notBuiltIfFound; - /** True to also scan the whole console output */ - public final boolean alsoCheckConsoleOutput; - - public final List textFinders; + /** + * All text finders configs stored here. + * Config before multi finders become first field in this list. + */ + private final List textFinders; + /** + * @param fileSet first textFinder. Keep it here for backward compatibility with old configuration. + * @param regexp first textFinder. Keep it here for backward compatibility with old configuration. + * @param succeedIfFound first textFinder. Keep it here for backward compatibility with old configuration. + * @param unstableIfFound first textFinder. Keep it here for backward compatibility with old configuration. + * @param notBuiltIfFound first textFinder. Keep it here for backward compatibility with old configuration. + * @param alsoCheckConsoleOutput first textFinder. Keep it here for backward compatibility with old configuration. + * @param textFinders configuration for additional textFinders + */ @DataBoundConstructor public TextFinderPublisher( String fileSet, @@ -70,18 +75,18 @@ public TextFinderPublisher( boolean notBuiltIfFound, boolean alsoCheckConsoleOutput, List textFinders) { - this.fileSet = Util.fixEmpty(fileSet.trim()); - this.succeedIfFound = succeedIfFound; - this.unstableIfFound = unstableIfFound; - this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; - this.notBuiltIfFound = notBuiltIfFound; - this.textFinders = textFinders; - - this.regexp = regexp; + this.textFinders = new ArrayList<>(); + this.textFinders.add(new TextFinderModel(Util.fixEmpty(fileSet.trim()), regexp, succeedIfFound, + unstableIfFound, alsoCheckConsoleOutput, notBuiltIfFound)); + if (textFinders != null && !textFinders.isEmpty()) { + this.textFinders.addAll(textFinders); + } - // Attempt to compile regular expression + // Attempt to compile regular expressions try { - Pattern.compile(regexp); + for (TextFinderModel textFinder : this.textFinders) { + Pattern.compile(textFinder.regexp); + } } catch (PatternSyntaxException e) { // falls through } @@ -95,12 +100,8 @@ public BuildStepMonitor getRequiredMonitorService() { @Override public void perform(Run run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException { - findText(run, workspace, listener, new TextFinderModel(fileSet, regexp, succeedIfFound, - unstableIfFound, alsoCheckConsoleOutput, notBuiltIfFound)); - if (textFinders != null) { - for (TextFinderModel textFinder : textFinders) { - findText(run, workspace, listener, textFinder); - } + for (TextFinderModel textFinder : textFinders) { + findText(run, workspace, listener, textFinder); } } @@ -115,13 +116,12 @@ private void findText(Run run, FilePath workspace, TaskListener listener, if (textFinder.alsoCheckConsoleOutput) { logger.println("Checking console output"); - foundText |= - checkFile( - run.getLogFile(), - compilePattern(logger, textFinder.regexp), - logger, - run.getCharset(), - true); + foundText = checkFile( + run.getLogFile(), + compilePattern(logger, textFinder.regexp), + logger, + run.getCharset(), + true); } else { // printing this when checking console output will cause the plugin // to find this line, which would be pointless. @@ -204,6 +204,46 @@ private static Pattern compilePattern(PrintStream logger, String regexp) { return pattern; } + /** cut off first item as it is provided with getters */ + @SuppressWarnings("unused") + public List getTextFinders() { + return textFinders.subList(1, textFinders.size()); + } + + // backward compatibility getters below + // all these getters are there for backward compatibility + // get first field from list which is used to store original config values + @SuppressWarnings("unused") + public String getFileSet() { + return this.textFinders.get(0).fileSet; + } + + @SuppressWarnings("unused") + public String getRegexp() { + return this.textFinders.get(0).regexp; + } + + @SuppressWarnings("unused") + public boolean isSucceedIfFound() { + return this.textFinders.get(0).succeedIfFound; + } + + @SuppressWarnings("unused") + public boolean isUnstableIfFound() { + return this.textFinders.get(0).unstableIfFound; + } + + @SuppressWarnings("unused") + public boolean isNotBuiltIfFound() { + return this.textFinders.get(0).notBuiltIfFound; + } + + @SuppressWarnings("unused") + public boolean isAlsoCheckConsoleOutput() { + return this.textFinders.get(0).alsoCheckConsoleOutput; + } + // backward compatibility getters above + @Symbol("findText") @Extension public static final class DescriptorImpl extends BuildStepDescriptor { From 270ce4ce309bdbcf799ba47855599db038fc01d6 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 11 Apr 2019 11:56:54 +0200 Subject: [PATCH 03/15] last finder wins test --- .../TextFinderPublisherFreestyleTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index 741ccc9..85102d5 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -9,7 +9,9 @@ import hudson.tasks.Shell; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import org.junit.Rule; import org.junit.Test; @@ -101,4 +103,27 @@ public void notFoundInConsole() throws Exception { rule.assertLogContains("Checking console output", build); rule.assertBuildStatus(Result.SUCCESS, build); } + + @Test + public void lastFinderWins() throws Exception { + FreeStyleProject project = rule.createFreeStyleProject("freestyle"); + CommandInterpreter command = + Functions.isWindows() + ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) + : new Shell(ECHO_UNIQUE_TEXT); + project.getBuildersList().add(command); + + List finders = new ArrayList<>(); + finders.add(new TextFinderModel("", UNIQUE_TEXT, false, true, false, true)); // 2nd + finders.add(new TextFinderModel("", UNIQUE_TEXT, false, false, false, true)); // 3rd, this one must win + TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, false, true, false, true, + finders); // 1st will be finder with args from constructor + project.getPublishersList().add(textFinder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + rule.assertBuildStatus(Result.UNSTABLE, build); + } } From a82e3f77529c51cc9da034a2da73393662a5c99d Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 11 Apr 2019 13:43:19 +0200 Subject: [PATCH 04/15] remove unnecessary pom compiler changes --- pom.xml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pom.xml b/pom.xml index f5d1abe..96695ee 100644 --- a/pom.xml +++ b/pom.xml @@ -40,14 +40,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - - 1.7 - 1.7 - - com.coveo fmt-maven-plugin From bf5d3b5785bf2550dda3f9c46da0c2d277abcc2f Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 11 Apr 2019 13:43:46 +0200 Subject: [PATCH 05/15] format --- .../plugins/textfinder/TextFinderModel.java | 20 +++-- .../textfinder/TextFinderPublisher.java | 77 +++++++++++-------- .../TextFinderPublisherFreestyleTest.java | 62 +++++++++++---- 3 files changed, 105 insertions(+), 54 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java index 83b3d8b..579f94e 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java @@ -4,13 +4,13 @@ import hudson.Util; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; -import org.kohsuke.stapler.DataBoundConstructor; - import java.io.Serializable; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import org.kohsuke.stapler.DataBoundConstructor; -public final class TextFinderModel extends AbstractDescribableImpl implements Serializable { +public final class TextFinderModel extends AbstractDescribableImpl + implements Serializable { private static final long serialVersionUID = 7645849785536280615L; public final String fileSet; @@ -18,13 +18,17 @@ public final class TextFinderModel extends AbstractDescribableImpl textFinders; /** - * @param fileSet first textFinder. Keep it here for backward compatibility with old configuration. - * @param regexp first textFinder. Keep it here for backward compatibility with old configuration. - * @param succeedIfFound first textFinder. Keep it here for backward compatibility with old configuration. - * @param unstableIfFound first textFinder. Keep it here for backward compatibility with old configuration. - * @param notBuiltIfFound first textFinder. Keep it here for backward compatibility with old configuration. - * @param alsoCheckConsoleOutput first textFinder. Keep it here for backward compatibility with old configuration. - * @param textFinders configuration for additional textFinders + * @param fileSet Kept for backward compatibility with old configuration. + * @param regexp Kept for backward compatibility with old configuration. + * @param succeedIfFound Kept for backward compatibility with old configuration. + * @param unstableIfFound Kept for backward compatibility with old configuration. + * @param notBuiltIfFound Kept for backward compatibility with old configuration. + * @param alsoCheckConsoleOutput Kept for backward compatibility with old configuration. + * @param textFinders configuration for additional textFinders */ @DataBoundConstructor public TextFinderPublisher( @@ -76,8 +75,14 @@ public TextFinderPublisher( boolean alsoCheckConsoleOutput, List textFinders) { this.textFinders = new ArrayList<>(); - this.textFinders.add(new TextFinderModel(Util.fixEmpty(fileSet.trim()), regexp, succeedIfFound, - unstableIfFound, alsoCheckConsoleOutput, notBuiltIfFound)); + this.textFinders.add( + new TextFinderModel( + Util.fixEmpty(fileSet != null ? fileSet.trim() : ""), + regexp, + succeedIfFound, + unstableIfFound, + alsoCheckConsoleOutput, + notBuiltIfFound)); if (textFinders != null && !textFinders.isEmpty()) { this.textFinders.addAll(textFinders); } @@ -108,7 +113,11 @@ public void perform(Run run, FilePath workspace, Launcher launcher, TaskLi /** Indicates an orderly abortion of the processing. */ private static final class AbortException extends RuntimeException {} - private void findText(Run run, FilePath workspace, TaskListener listener, final TextFinderModel textFinder) + private void findText( + Run run, + FilePath workspace, + TaskListener listener, + final TextFinderModel textFinder) throws IOException, InterruptedException { try { PrintStream logger = listener.getLogger(); @@ -116,12 +125,13 @@ private void findText(Run run, FilePath workspace, TaskListener listener, if (textFinder.alsoCheckConsoleOutput) { logger.println("Checking console output"); - foundText = checkFile( - run.getLogFile(), - compilePattern(logger, textFinder.regexp), - logger, - run.getCharset(), - true); + foundText = + checkFile( + run.getLogFile(), + compilePattern(logger, textFinder.regexp), + logger, + run.getCharset(), + true); } else { // printing this when checking console output will cause the plugin // to find this line, which would be pointless. @@ -132,7 +142,8 @@ private void findText(Run run, FilePath workspace, TaskListener listener, final RemoteOutputStream ros = new RemoteOutputStream(logger); if (textFinder.fileSet != null) { - foundText |= workspace.act(new FileChecker(ros, textFinder.fileSet, textFinder.regexp)); + foundText |= + workspace.act(new FileChecker(ros, textFinder.fileSet, textFinder.regexp)); } if (foundText != textFinder.succeedIfFound) { diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index 85102d5..5f59f47 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -12,7 +12,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; - import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; @@ -46,8 +45,15 @@ public void successIfFoundInConsole() throws Exception { ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); - TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, true, false, false, true, - Collections.emptyList()); + TextFinderPublisher textFinder = + new TextFinderPublisher( + "", + UNIQUE_TEXT, + true, + false, + false, + true, + Collections.emptyList()); project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); @@ -64,8 +70,15 @@ public void failureIfFoundInConsole() throws Exception { ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); - TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, false, false, false, true, - Collections.emptyList()); + TextFinderPublisher textFinder = + new TextFinderPublisher( + "", + UNIQUE_TEXT, + false, + false, + false, + true, + Collections.emptyList()); project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); @@ -82,8 +95,15 @@ public void unstableIfFoundInConsole() throws Exception { ? new BatchFile("prompt $G\n" + ECHO_UNIQUE_TEXT) : new Shell(ECHO_UNIQUE_TEXT); project.getBuildersList().add(command); - TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, false, true, false, true, - Collections.emptyList()); + TextFinderPublisher textFinder = + new TextFinderPublisher( + "", + UNIQUE_TEXT, + false, + true, + false, + true, + Collections.emptyList()); project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); @@ -95,8 +115,15 @@ public void unstableIfFoundInConsole() throws Exception { @Test public void notFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject("freestyle"); - TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, false, false, false, true, - Collections.emptyList()); + TextFinderPublisher textFinder = + new TextFinderPublisher( + "", + UNIQUE_TEXT, + false, + false, + false, + true, + Collections.emptyList()); project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); @@ -114,10 +141,19 @@ public void lastFinderWins() throws Exception { project.getBuildersList().add(command); List finders = new ArrayList<>(); - finders.add(new TextFinderModel("", UNIQUE_TEXT, false, true, false, true)); // 2nd - finders.add(new TextFinderModel("", UNIQUE_TEXT, false, false, false, true)); // 3rd, this one must win - TextFinderPublisher textFinder = new TextFinderPublisher("", UNIQUE_TEXT, false, true, false, true, - finders); // 1st will be finder with args from constructor + finders.add(new TextFinderModel("", UNIQUE_TEXT, false, true, false, true)); // 2nd + finders.add( + new TextFinderModel( + "", UNIQUE_TEXT, false, false, false, true)); // 3rd, this one must win + TextFinderPublisher textFinder = + new TextFinderPublisher( + "", + UNIQUE_TEXT, + false, + true, + false, + true, + finders); // 1st will be finder with args from constructor project.getPublishersList().add(textFinder); FreeStyleBuild build = project.scheduleBuild2(0).get(); From aa11fb05eef505549df350d5e3bc1286ccc689d7 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 11 Apr 2019 13:44:09 +0200 Subject: [PATCH 06/15] check Jenkins.getInstance for null (findbugs) --- .../hudson/plugins/textfinder/TextFinderPublisher.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index b067556..31e2ac0 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -274,7 +274,12 @@ public boolean isApplicable(Class jobType) { } public List getItemDescriptors() { - return Jenkins.getInstance().getDescriptorList(TextFinderModel.class); + Jenkins jenkins = Jenkins.getInstance(); + if (jenkins != null) { + return jenkins.getDescriptorList(TextFinderModel.class); + } else { + throw new NullPointerException("not able to get Jenkins instance"); + } } /** From 469d1d51f7c77768f38a331ee357622a8dc46ff0 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 11 Apr 2019 15:08:37 +0200 Subject: [PATCH 07/15] use extra text finder model for base finder config --- .../plugins/textfinder/TextFinderModel.java | 12 ++--- .../textfinder/TextFinderPublisher.java | 49 +++++++++++-------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java index 579f94e..0f1bbb1 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java @@ -13,13 +13,13 @@ public final class TextFinderModel extends AbstractDescribableImpl textFinders; /** @@ -74,23 +76,25 @@ public TextFinderPublisher( boolean notBuiltIfFound, boolean alsoCheckConsoleOutput, List textFinders) { - this.textFinders = new ArrayList<>(); - this.textFinders.add( + this.baseTextFinder = new TextFinderModel( Util.fixEmpty(fileSet != null ? fileSet.trim() : ""), regexp, succeedIfFound, unstableIfFound, alsoCheckConsoleOutput, - notBuiltIfFound)); + notBuiltIfFound); + + this.textFinders = new ArrayList<>(); if (textFinders != null && !textFinders.isEmpty()) { this.textFinders.addAll(textFinders); } // Attempt to compile regular expressions try { + Pattern.compile(baseTextFinder.getRegexp()); for (TextFinderModel textFinder : this.textFinders) { - Pattern.compile(textFinder.regexp); + Pattern.compile(textFinder.getRegexp()); } } catch (PatternSyntaxException e) { // falls through @@ -105,6 +109,7 @@ public BuildStepMonitor getRequiredMonitorService() { @Override public void perform(Run run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException { + findText(run, workspace, listener, baseTextFinder); for (TextFinderModel textFinder : textFinders) { findText(run, workspace, listener, textFinder); } @@ -123,12 +128,12 @@ private void findText( PrintStream logger = listener.getLogger(); boolean foundText = false; - if (textFinder.alsoCheckConsoleOutput) { + if (textFinder.isAlsoCheckConsoleOutput()) { logger.println("Checking console output"); foundText = checkFile( run.getLogFile(), - compilePattern(logger, textFinder.regexp), + compilePattern(logger, textFinder.getRegexp()), logger, run.getCharset(), true); @@ -136,22 +141,24 @@ private void findText( // 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 - logger.println("Checking " + textFinder.regexp); + logger.println("Checking " + textFinder.getRegexp()); } final RemoteOutputStream ros = new RemoteOutputStream(logger); - if (textFinder.fileSet != null) { + if (textFinder.getFileSet() != null) { foundText |= - workspace.act(new FileChecker(ros, textFinder.fileSet, textFinder.regexp)); + workspace.act( + new FileChecker( + ros, textFinder.getFileSet(), textFinder.getRegexp())); } - if (foundText != textFinder.succeedIfFound) { + if (foundText != textFinder.isSucceedIfFound()) { final Result finalResult; - if (textFinder.notBuiltIfFound) { + if (textFinder.isNotBuiltIfFound()) { finalResult = Result.NOT_BUILT; } else { - finalResult = textFinder.unstableIfFound ? Result.UNSTABLE : Result.FAILURE; + finalResult = textFinder.isUnstableIfFound() ? Result.UNSTABLE : Result.FAILURE; } run.setResult(finalResult); } @@ -218,7 +225,7 @@ private static Pattern compilePattern(PrintStream logger, String regexp) { /** cut off first item as it is provided with getters */ @SuppressWarnings("unused") public List getTextFinders() { - return textFinders.subList(1, textFinders.size()); + return textFinders; } // backward compatibility getters below @@ -226,32 +233,32 @@ public List getTextFinders() { // get first field from list which is used to store original config values @SuppressWarnings("unused") public String getFileSet() { - return this.textFinders.get(0).fileSet; + return this.baseTextFinder.getFileSet(); } @SuppressWarnings("unused") public String getRegexp() { - return this.textFinders.get(0).regexp; + return this.baseTextFinder.getRegexp(); } @SuppressWarnings("unused") public boolean isSucceedIfFound() { - return this.textFinders.get(0).succeedIfFound; + return this.baseTextFinder.isSucceedIfFound(); } @SuppressWarnings("unused") public boolean isUnstableIfFound() { - return this.textFinders.get(0).unstableIfFound; + return this.baseTextFinder.isUnstableIfFound(); } @SuppressWarnings("unused") public boolean isNotBuiltIfFound() { - return this.textFinders.get(0).notBuiltIfFound; + return this.baseTextFinder.isNotBuiltIfFound(); } @SuppressWarnings("unused") public boolean isAlsoCheckConsoleOutput() { - return this.textFinders.get(0).alsoCheckConsoleOutput; + return this.baseTextFinder.isAlsoCheckConsoleOutput(); } // backward compatibility getters above From ba2ecf1e0516fa90d0bacc671281c47a2a413266 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 11 Apr 2019 15:41:37 +0200 Subject: [PATCH 08/15] pipeline symbol for TextFinderModel --- src/main/java/hudson/plugins/textfinder/TextFinderModel.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java index 0f1bbb1..4a3f97b 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java @@ -7,6 +7,7 @@ import java.io.Serializable; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; public final class TextFinderModel extends AbstractDescribableImpl @@ -68,6 +69,7 @@ public boolean isAlsoCheckConsoleOutput() { return alsoCheckConsoleOutput; } + @Symbol("finder") @Extension public static class DescriptorImpl extends Descriptor { @Override From abc4cf2515617a9c963ce131960f8f473dea7d95 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 11 Apr 2019 15:42:52 +0200 Subject: [PATCH 09/15] pipeline and pipeline agent tests for multiple finders --- .../TextFinderPublisherAgentTest.java | 21 +++++++++++++++++++ .../TextFinderPublisherPipelineTest.java | 18 ++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java index dc26bc1..7f75925 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -83,4 +83,25 @@ public void failureIfFoundInConsoleOnAgent() throws Exception { assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.FAILURE, build); } + + @Test + public void lastFinderWins() 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(\"prompt \\$G\\r\\necho foobar\")}\n" + + "node('%s') {findText regexp: 'foobar', alsoCheckConsoleOutput: true, " + + "textFinders: [" + + "finder(regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true)," + + "finder(regexp: 'foobar', notBuiltIfFound: true, alsoCheckConsoleOutput: true)" + + "]}", + agent.getNodeName(), agent.getNodeName()))); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + rule.assertBuildStatus(Result.NOT_BUILT, build); + } } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java index 648abf3..fbedf20 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -185,4 +185,22 @@ public void notFoundInConsole() throws Exception { rule.assertLogContains("Checking console output", build); rule.assertBuildStatus(Result.SUCCESS, build); } + + @Test + public void lastFinderWins() 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, " + + "textFinders: [" + + "finder(regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true)," + + "finder(regexp: 'foobar', notBuiltIfFound: true, alsoCheckConsoleOutput: true)" + + "]}")); + WorkflowRun build = project.scheduleBuild2(0).get(); + rule.waitForCompletion(build); + rule.assertLogContains("Checking console output", build); + assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + rule.assertBuildStatus(Result.NOT_BUILT, build); + } } From 0dd8e1bdd970890354abb9b161f50479cb8f81d6 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Wed, 12 Jun 2019 16:00:08 +0200 Subject: [PATCH 10/15] removed pipelines as they can be easily configured by multiple 'findText' invocations, updated tests --- .../hudson/plugins/textfinder/TextFinderModel.java | 2 -- .../textfinder/TextFinderPublisherAgentTest.java | 10 +++++----- .../textfinder/TextFinderPublisherPipelineTest.java | 10 +++++----- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java index 4a3f97b..0f1bbb1 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java @@ -7,7 +7,6 @@ import java.io.Serializable; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; -import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; public final class TextFinderModel extends AbstractDescribableImpl @@ -69,7 +68,6 @@ public boolean isAlsoCheckConsoleOutput() { return alsoCheckConsoleOutput; } - @Symbol("finder") @Extension public static class DescriptorImpl extends Descriptor { @Override diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java index 7f75925..ca02c3a 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -92,11 +92,11 @@ public void lastFinderWins() throws Exception { new CpsFlowDefinition( String.format( "node('%s') {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n" - + "node('%s') {findText regexp: 'foobar', alsoCheckConsoleOutput: true, " - + "textFinders: [" - + "finder(regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true)," - + "finder(regexp: 'foobar', notBuiltIfFound: true, alsoCheckConsoleOutput: true)" - + "]}", + + "node('%s') {" + + "findText regexp: 'foobar', alsoCheckConsoleOutput: true\n" + + "findText regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true\n" + + "findText regexp: 'foobar', notBuiltIfFound: true, alsoCheckConsoleOutput: true\n" + + "}", agent.getNodeName(), agent.getNodeName()))); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java index fbedf20..6f1226a 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -192,11 +192,11 @@ public void lastFinderWins() throws Exception { project.setDefinition( new CpsFlowDefinition( "node {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n" - + "node {findText regexp: 'foobar', alsoCheckConsoleOutput: true, " - + "textFinders: [" - + "finder(regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true)," - + "finder(regexp: 'foobar', notBuiltIfFound: true, alsoCheckConsoleOutput: true)" - + "]}")); + + "node {" + + "findText regexp: 'foobar', alsoCheckConsoleOutput: true\n" + + "findText regexp: 'foobar', unstableIfFound: true, alsoCheckConsoleOutput: true\n" + + "findText regexp: 'foobar', notBuiltIfFound: true, alsoCheckConsoleOutput: true\n" + + "}")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("Checking console output", build); From 8aa5e70b9d82fc6f6d4edef85be5c4ab51b53b36 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 13 Jun 2019 12:40:16 +0200 Subject: [PATCH 11/15] fix tests with new logging --- .../plugins/textfinder/TextFinderPublisherAgentTest.java | 7 ++++++- .../textfinder/TextFinderPublisherFreestyleTest.java | 7 ++++++- .../textfinder/TextFinderPublisherPipelineTest.java | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java index 14b1cc1..efc7108 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -107,7 +107,12 @@ public void lastFinderWins() throws Exception { agent.getNodeName(), agent.getNodeName()))); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); - rule.assertLogContains("Checking console output", build); + rule.assertLogContains("[Text Finder] Scanning console output...", build); + rule.assertLogContains( + "[Text Finder] Finished looking for pattern '" + + UNIQUE_TEXT + + "' in the console output", + build); assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.NOT_BUILT, build); } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index 710299c..845b5ea 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -178,7 +178,12 @@ public void lastFinderWins() throws Exception { FreeStyleBuild build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); - rule.assertLogContains("Checking console output", build); + rule.assertLogContains("[Text Finder] Scanning console output...", build); + rule.assertLogContains( + "[Text Finder] Finished looking for pattern '" + + UNIQUE_TEXT + + "' in the console output", + 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 6e293fa..f359c95 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -324,7 +324,12 @@ public void lastFinderWins() throws Exception { + "}")); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); - rule.assertLogContains("Checking console output", build); + rule.assertLogContains("[Text Finder] Scanning console output...", build); + rule.assertLogContains( + "[Text Finder] Finished looking for pattern '" + + UNIQUE_TEXT + + "' in the console output", + build); assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); rule.assertBuildStatus(Result.NOT_BUILT, build); } From 05f7f531a7720f689f0a37bc765d84e024ac811e Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sat, 15 Jun 2019 09:25:38 -0700 Subject: [PATCH 12/15] Clean up serialVersionUID --- src/main/java/hudson/plugins/textfinder/TextFinderModel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java index 0f1bbb1..664d994 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java @@ -11,7 +11,7 @@ public final class TextFinderModel extends AbstractDescribableImpl implements Serializable { - private static final long serialVersionUID = 7645849785536280615L; + private static final long serialVersionUID = 1L; private final String fileSet; private final String regexp; From 1080ee89b9ccbace9ac3c9b211795e2e7cf8c9f9 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sat, 15 Jun 2019 09:26:14 -0700 Subject: [PATCH 13/15] Make naming consistent --- src/main/java/hudson/plugins/textfinder/TextFinderModel.java | 2 +- .../plugins/textfinder/TextFinderPublisher/config.jelly | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java index 664d994..d23e98f 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderModel.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderModel.java @@ -72,7 +72,7 @@ public boolean isAlsoCheckConsoleOutput() { public static class DescriptorImpl extends Descriptor { @Override public String getDisplayName() { - return "Text finder"; + return "Text Finder"; } } } diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly b/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly index 4c5a754..6c4a423 100644 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly @@ -24,7 +24,7 @@ hasHeader="true" descriptors="${descriptor.itemDescriptors}" items="${instance.textFinders}" - addCaption="Add Text finder" - deleteCaption="Delete Text finder"/> + addCaption="Add Text Finder" + deleteCaption="Delete Text Finder"/> From 68ec81c1ec95eaf297f0c6ef7dc0f193a1e3a4bc Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sat, 15 Jun 2019 09:32:37 -0700 Subject: [PATCH 14/15] Rename fields for clarity --- .../textfinder/TextFinderPublisher.java | 53 +++++++++---------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index d4ecad9..76b86a7 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -50,13 +50,11 @@ */ public class TextFinderPublisher extends Recorder implements Serializable, SimpleBuildStep { - /** - * This is first text finder in configuration. It must be here to keep backward compatibility. * - */ - private final TextFinderModel baseTextFinder; + /** This is the primary text finder in the configuration. */ + private final TextFinderModel primaryTextFinder; - /** Additional text finders configs stored here. */ - private final List textFinders; + /** Additional text finder configurations are stored here. */ + private final List additionalTextFinders; /** * @param fileSet Kept for backward compatibility with old configuration. @@ -65,7 +63,7 @@ public class TextFinderPublisher extends Recorder implements Serializable, Simpl * @param unstableIfFound Kept for backward compatibility with old configuration. * @param notBuiltIfFound Kept for backward compatibility with old configuration. * @param alsoCheckConsoleOutput Kept for backward compatibility with old configuration. - * @param textFinders configuration for additional textFinders + * @param additionalTextFinders configuration for additional textFinders */ @DataBoundConstructor public TextFinderPublisher( @@ -75,8 +73,8 @@ public TextFinderPublisher( boolean unstableIfFound, boolean notBuiltIfFound, boolean alsoCheckConsoleOutput, - List textFinders) { - this.baseTextFinder = + List additionalTextFinders) { + this.primaryTextFinder = new TextFinderModel( Util.fixEmpty(fileSet != null ? fileSet.trim() : ""), regexp, @@ -85,15 +83,15 @@ public TextFinderPublisher( alsoCheckConsoleOutput, notBuiltIfFound); - this.textFinders = new ArrayList<>(); - if (textFinders != null && !textFinders.isEmpty()) { - this.textFinders.addAll(textFinders); + this.additionalTextFinders = new ArrayList<>(); + if (additionalTextFinders != null && !additionalTextFinders.isEmpty()) { + this.additionalTextFinders.addAll(additionalTextFinders); } // Attempt to compile regular expressions try { - Pattern.compile(baseTextFinder.getRegexp()); - for (TextFinderModel textFinder : this.textFinders) { + Pattern.compile(primaryTextFinder.getRegexp()); + for (TextFinderModel textFinder : this.additionalTextFinders) { Pattern.compile(textFinder.getRegexp()); } } catch (PatternSyntaxException e) { @@ -109,9 +107,9 @@ public BuildStepMonitor getRequiredMonitorService() { @Override public void perform(Run run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException { - findText(run, workspace, listener, baseTextFinder); - for (TextFinderModel textFinder : textFinders) { - findText(run, workspace, listener, textFinder); + findText(primaryTextFinder, run, workspace, listener); + for (TextFinderModel additionalTextFinder : additionalTextFinders) { + findText(additionalTextFinder, run, workspace, listener); } } @@ -119,10 +117,7 @@ public void perform(Run run, FilePath workspace, Launcher launcher, TaskLi private static final class AbortException extends RuntimeException {} private void findText( - Run run, - FilePath workspace, - TaskListener listener, - final TextFinderModel textFinder) + TextFinderModel textFinder, Run run, FilePath workspace, TaskListener listener) throws IOException, InterruptedException { try { PrintStream logger = listener.getLogger(); @@ -234,8 +229,8 @@ private static Pattern compilePattern(PrintStream logger, String regexp) { /** cut off first item as it is provided with getters */ @SuppressWarnings("unused") - public List getTextFinders() { - return textFinders; + public List getAdditionalTextFinders() { + return additionalTextFinders; } // backward compatibility getters below @@ -243,32 +238,32 @@ public List getTextFinders() { // get first field from list which is used to store original config values @SuppressWarnings("unused") public String getFileSet() { - return this.baseTextFinder.getFileSet(); + return this.primaryTextFinder.getFileSet(); } @SuppressWarnings("unused") public String getRegexp() { - return this.baseTextFinder.getRegexp(); + return this.primaryTextFinder.getRegexp(); } @SuppressWarnings("unused") public boolean isSucceedIfFound() { - return this.baseTextFinder.isSucceedIfFound(); + return this.primaryTextFinder.isSucceedIfFound(); } @SuppressWarnings("unused") public boolean isUnstableIfFound() { - return this.baseTextFinder.isUnstableIfFound(); + return this.primaryTextFinder.isUnstableIfFound(); } @SuppressWarnings("unused") public boolean isNotBuiltIfFound() { - return this.baseTextFinder.isNotBuiltIfFound(); + return this.primaryTextFinder.isNotBuiltIfFound(); } @SuppressWarnings("unused") public boolean isAlsoCheckConsoleOutput() { - return this.baseTextFinder.isAlsoCheckConsoleOutput(); + return this.primaryTextFinder.isAlsoCheckConsoleOutput(); } // backward compatibility getters above From 231d8b80fe5cab1a004376afe102ede70679ed4f Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sat, 15 Jun 2019 10:00:31 -0700 Subject: [PATCH 15/15] Clarify references to "additional" finders --- .../textfinder/TextFinderPublisher/config.jelly | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly b/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly index 6c4a423..59c555c 100644 --- a/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly +++ b/src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly @@ -19,12 +19,12 @@ - - + + items="${instance.additionalTextFinders}" + addCaption="Add additional Text Finder" + deleteCaption="Delete additional Text Finder"/>