├── .github ├── CODEOWNERS ├── release-drafter.yml ├── dependabot.yml └── workflows │ ├── release-drafter.yml │ └── jenkins-security-scan.yml ├── .mvn ├── maven.config └── extensions.xml ├── .git-blame-ignore-revs ├── src ├── main │ ├── resources │ │ ├── hudson │ │ │ └── plugins │ │ │ │ └── textfinder │ │ │ │ ├── Messages_ja.properties │ │ │ │ ├── Messages.properties │ │ │ │ ├── Messages_fr.properties │ │ │ │ ├── TextFinder │ │ │ │ ├── config_fr.properties │ │ │ │ ├── help-regexp_ja.html │ │ │ │ ├── help-regexp.html │ │ │ │ ├── help-regexp_fr.html │ │ │ │ ├── help-changeCondition.html │ │ │ │ ├── help-fileSet_ja.html │ │ │ │ ├── help-changeCondition_fr.html │ │ │ │ ├── help-buildResult.html │ │ │ │ ├── help-buildResult_fr.html │ │ │ │ ├── help-fileSet.html │ │ │ │ ├── help-fileSet_fr.html │ │ │ │ ├── config.jelly │ │ │ │ ├── config.properties │ │ │ │ └── config_ja.properties │ │ │ │ └── TextFinderPublisher │ │ │ │ └── config.jelly │ │ └── index.jelly │ ├── webapp │ │ ├── help_ja.html │ │ ├── help.html │ │ └── help_fr.html │ └── java │ │ └── hudson │ │ └── plugins │ │ └── textfinder │ │ ├── TextFinderChangeCondition.java │ │ ├── TextFinder.java │ │ └── TextFinderPublisher.java └── test │ ├── java │ └── hudson │ │ └── plugins │ │ └── textfinder │ │ ├── test │ │ ├── TestEchoStep.java │ │ ├── TestEchoBuilder.java │ │ └── TestWriteFileBuilder.java │ │ ├── TestUtils.java │ │ ├── TextFinderPublisherAgentTest.java │ │ ├── TextFinderPublisherPipelineCompatibilityTest.java │ │ ├── TextFinderPublisherFreestyleCompatibilityTest.java │ │ ├── TextFinderPublisherPipelineTest.java │ │ ├── TextFinderPublisherFreestyleTest.java │ │ └── TextFinderPublisherFreestyleJobDslTest.java │ └── resources │ └── hudson │ └── plugins │ └── textfinder │ └── TextFinderPublisherFreestyleCompatibilityTest │ ├── persistedConfigurationBeforeMultipleFinders │ └── jobs │ │ └── test0 │ │ └── config.xml │ └── persistedConfigurationBeforeChangeCondition │ └── jobs │ └── test0 │ └── config.xml ├── Jenkinsfile ├── .gitignore ├── CHANGELOG.adoc ├── pom.xml └── README.adoc /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @jenkinsci/text-finder-plugin-developers 2 | -------------------------------------------------------------------------------- /.mvn/maven.config: -------------------------------------------------------------------------------- 1 | -Pconsume-incrementals 2 | -Pmight-produce-incrementals 3 | -------------------------------------------------------------------------------- /.git-blame-ignore-revs: -------------------------------------------------------------------------------- 1 | # .git-blame-ignore-revs 2 | # Enable Spotless (#164) 3 | 15ec06fc0695484ded4d8a3a13f60bbeb7a25cf0 4 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/Messages_ja.properties: -------------------------------------------------------------------------------- 1 | TextFinderPublisher.DisplayName=\u6587\u5b57\u5217\u691c\u7d22 2 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/Messages.properties: -------------------------------------------------------------------------------- 1 | TextFinderPublisher.DisplayName=Search files or the console log for regular expression(s) 2 | -------------------------------------------------------------------------------- /src/main/webapp/help_ja.html: -------------------------------------------------------------------------------- 1 |
2 |

3 | このプラグインは、ファイルセットを対象にして、正規表現を用いて文字列を検索する際に使用します。 例えば、ログのファイルセット内の文字列"failure"を検索することができます。 4 |

5 |
-------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | buildPlugin(useContainerAgent: true, forkCount: '1C', configurations: [ 2 | [platform: 'linux', jdk: 21], 3 | [platform: 'windows', jdk: 17], 4 | ]) 5 | -------------------------------------------------------------------------------- /.github/release-drafter.yml: -------------------------------------------------------------------------------- 1 | # See https://github.com/jenkinsci/.github/blob/master/.github/release-drafter.adoc 2 | --- 3 | _extends: .github 4 | tag-template: text-finder-$NEXT_MINOR_VERSION 5 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/Messages_fr.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/judovana/text-finder-plugin/master/src/main/resources/hudson/plugins/textfinder/Messages_fr.properties -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/config_fr.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/judovana/text-finder-plugin/master/src/main/resources/hudson/plugins/textfinder/TextFinder/config_fr.properties -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | 3 | # mvn hpi:run 4 | work 5 | 6 | # IntelliJ IDEA project files 7 | *.iml 8 | *.iws 9 | *.ipr 10 | .idea 11 | 12 | # Eclipse project files 13 | .settings 14 | .classpath 15 | .project 16 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-regexp_ja.html: -------------------------------------------------------------------------------- 1 |
2 | JavaのPatternクラスがサポートしている文法を用いて正規表現を記述してください。 3 |
4 | -------------------------------------------------------------------------------- /src/main/resources/index.jelly: -------------------------------------------------------------------------------- 1 | 2 |
3 | This plugin is used to search for strings in workspace files. The outcome 4 | of this search can be used to downgrade the build status. 5 |
6 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-regexp.html: -------------------------------------------------------------------------------- 1 |
2 | Specify a regular expression using the syntax supported by the Java 3 | Pattern 4 | class. 5 |
6 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-regexp_fr.html: -------------------------------------------------------------------------------- 1 |
2 | Spécifie une expression régulière utilisant la syntaxe supportée par la classe Java 3 | Pattern. 4 |
5 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-changeCondition.html: -------------------------------------------------------------------------------- 1 |
2 | If the change condition is MATCH_FOUND, then the build result 3 | will be set if a match is found. If the change condition is 4 | MATCH_NOT_FOUND, then the build result will be set if a match 5 | is not found. 6 |
7 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-fileSet_ja.html: -------------------------------------------------------------------------------- 1 |
2 | ワークスペースのルートからの相対パスで検索対象ファイルのパスを記述してください。 logs/**/*/*.txtのようにワイルドカードを用いることもできます。 正しい書式は、Ant FileSetのincludes属性を参照してください。 ファイルを検索したくない場合は、このフィールドを空欄にしてください(このとき、通常はコンソール出力も検索するをチェックします)。 3 |
4 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuring-dependabot-version-updates 2 | --- 3 | version: 2 4 | updates: 5 | - package-ecosystem: maven 6 | directory: / 7 | schedule: 8 | interval: weekly 9 | - package-ecosystem: github-actions 10 | directory: / 11 | schedule: 12 | interval: weekly 13 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-changeCondition_fr.html: -------------------------------------------------------------------------------- 1 |
2 | Si la condition de changement est MATCH_FOUND, 3 | alors le résultat du build sera changé si une chaîne correspondante est trouvée. 4 | Si la condition de changement est MATCH_NOT_FOUND, 5 | alors le résultat du build sera changé si aucune correspondance n'est trouvée. 6 |
7 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-buildResult.html: -------------------------------------------------------------------------------- 1 |
2 | Based on the change condition, the build result will be set to this value. 3 | Note that the build result can only get worse, so you cannot change the 4 | result to SUCCESS if the current result is 5 | UNSTABLE or worse. Use SUCCESS to keep the build 6 | result from being set. 7 |
8 | -------------------------------------------------------------------------------- /.mvn/extensions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | io.jenkins.tools.incrementals 4 | git-changelist-maven-extension 5 | 1.8 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-buildResult_fr.html: -------------------------------------------------------------------------------- 1 |
2 | Selon la condition de changement, le résultat du build prendra cette valeur. 3 | Veuillez noter que le résultat du build ne peut que se détériorer, vous ne 4 | pouvez donc pas passer en SUCCESS (succés) si le résultat actuel 5 | est UNSTABLE (unstable) ou pire. Le résultat SUCCESS (succés) 6 | permet uniquement d'éviter de changer l'état du build. 7 |
8 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-fileSet.html: -------------------------------------------------------------------------------- 1 |
2 | Specify the path to the files to search, relative to the workspace root. 3 | This can use wildcards like logs/**/*/*.txt. See the 4 | documentation for the @includes attribute of the Ant 5 | FileSet 6 | type for details. Leave this empty if you do not want to scan any files 7 | (usually combined with checking Also search the console output). 8 |
9 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinderPublisher/config.jelly: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 |
9 |
10 |
11 |
12 |
13 | -------------------------------------------------------------------------------- /.github/workflows/release-drafter.yml: -------------------------------------------------------------------------------- 1 | # Automates creation of Release Drafts using Release Drafter 2 | # More Info: https://github.com/jenkinsci/.github/blob/master/.github/release-drafter.adoc 3 | --- 4 | name: Release Drafter 5 | 6 | on: 7 | push: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | update_release_draft: 13 | runs-on: ubuntu-latest 14 | steps: 15 | # Drafts your next Release notes as Pull Requests are merged into the default branch 16 | - uses: release-drafter/release-drafter@v6 17 | env: 18 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 19 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/help-fileSet_fr.html: -------------------------------------------------------------------------------- 1 |
2 | Spécifie le chemin des fichiers a rechercher, chemin relatif à la racine du répertoire de travail. 3 | Des caractères génériques "wildcards" tels que logs/**/*/*.txt peuvent être utilisés. 4 | Veuillez consulter la documentation concernant l'attribut Ant @includes 5 | FileSet. 6 | Laisser ce champ vide si vous ne souhaitez pas scanner de fichiers (généralement combiné avec la vérification 7 | de la sortie console : "Also search the console output"). 8 |
9 | -------------------------------------------------------------------------------- /src/main/webapp/help.html: -------------------------------------------------------------------------------- 1 |
2 |

3 | This plugin lets you search for some text using regular expressions in a set 4 | of files or the console log. Based on the outcome, you can downgrade the 5 | build result to UNSTABLE, FAILURE, 6 | NOT_BUILT, or ABORTED. 7 |

8 |

9 | For example, you can search for the string failure in a set of 10 | log files. If a match is found, you can downgrade the build result from 11 | SUCCESS to FAILURE. This is handy when you have 12 | some tools in your build chain that do not properly set the exit code. 13 |

14 |
15 | -------------------------------------------------------------------------------- /src/main/webapp/help_fr.html: -------------------------------------------------------------------------------- 1 |
2 |

3 | Ce plugin permet de rechercher du texte dans des fichiers ou dans une sortie console en utilisant des expressions régulières. 4 | Selon le résultat de la recherche, vous pouvez alors changer le statut du build en UNSTABLE (instable), FAILURE (échec), 5 | NOT_BUILT (no construit), ou ABORTED (Suspendu). 6 |

7 |

8 | Par exemple, vous pouvez rechercher la chaîne de caractères "erreur" dans un ensemble de fichiers de log. 9 | Si un résultat correspond, vous pouvez changer l'état du build de SUCCESS à FAILURE (échec). 10 | Cela est utile lorsque les outils utilisés durant le build ne définissent pas correctement le code de sortie. 11 |

12 |
13 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/config.jelly: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | ${it.description} 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/hudson/plugins/textfinder/TextFinderChangeCondition.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import hudson.util.EnumConverter; 4 | import org.kohsuke.stapler.Stapler; 5 | 6 | public enum TextFinderChangeCondition { 7 | MATCH_FOUND("Change the build result if a match is found"), 8 | MATCH_NOT_FOUND("Change the build result if a match is not found"); 9 | 10 | private final String description; 11 | 12 | TextFinderChangeCondition(String description) { 13 | this.description = description; 14 | } 15 | 16 | public String getDescription() { 17 | return description; 18 | } 19 | 20 | static { 21 | // Allow conversion from a string to an enumeration in the databinding process. 22 | Stapler.CONVERT_UTILS.register(new EnumConverter(), TextFinderChangeCondition.class); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.github/workflows/jenkins-security-scan.yml: -------------------------------------------------------------------------------- 1 | # More information about the Jenkins security scan can be found at the developer docs: https://www.jenkins.io/redirect/jenkins-security-scan/ 2 | --- 3 | name: Jenkins Security Scan 4 | on: 5 | push: 6 | branches: 7 | - master 8 | pull_request: 9 | types: [opened, synchronize, reopened] 10 | workflow_dispatch: 11 | 12 | permissions: 13 | security-events: write 14 | contents: read 15 | actions: read 16 | 17 | jobs: 18 | security-scan: 19 | uses: jenkins-infra/jenkins-security-scan/.github/workflows/jenkins-security-scan.yaml@v2 20 | with: 21 | java-cache: 'maven' # Optionally enable use of a build dependency cache. Specify 'maven' or 'gradle' as appropriate. 22 | # java-version: 21 # Optionally specify what version of Java to set up for the build, or remove to use a recent default. 23 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/test/TestEchoStep.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder.test; 2 | 3 | import hudson.Extension; 4 | import hudson.plugins.textfinder.TestUtils; 5 | import org.jenkinsci.plugins.workflow.steps.EchoStep; 6 | import org.jenkinsci.plugins.workflow.steps.Step; 7 | import org.kohsuke.stapler.DataBoundConstructor; 8 | 9 | /** A test {@link Step} that merely prints the message given to it with a prefix. */ 10 | public class TestEchoStep extends EchoStep { 11 | 12 | @DataBoundConstructor 13 | public TestEchoStep(String message) { 14 | super(TestUtils.PREFIX + message); 15 | } 16 | 17 | @Extension 18 | public static final class DescriptorImpl extends EchoStep.DescriptorImpl { 19 | 20 | @Override 21 | public String getFunctionName() { 22 | return "testEcho"; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/resources/hudson/plugins/textfinder/TextFinderPublisherFreestyleCompatibilityTest/persistedConfigurationBeforeMultipleFinders/jobs/test0/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | false 4 | 5 | 6 | false 7 | false 8 | false 9 | false 10 | 11 | false 12 | 13 | 14 | 15 | out.txt 16 | foobar 17 | false 18 | true 19 | false 20 | true 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/test/resources/hudson/plugins/textfinder/TextFinderPublisherFreestyleCompatibilityTest/persistedConfigurationBeforeChangeCondition/jobs/test0/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | false 4 | 5 | 6 | false 7 | false 8 | false 9 | false 10 | 11 | false 12 | 13 | 14 | 15 | 16 | 17 | foobar 18 | out.txt 19 | FAILURE 20 | false 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/config.properties: -------------------------------------------------------------------------------- 1 | # The MIT License 2 | # 3 | # Copyright (c) 2004-2010, Sun Microsystems, Inc. 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in 13 | # all copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | # THE SOFTWARE. 22 | 23 | Files=Files 24 | Regular\ expression=Regular expression 25 | Succeed\ if\ found=Succeed if found 26 | Unstable\ if\ found=Unstable if found 27 | Also\ search\ the\ console\ output=Also search the console output 28 | Build\ result=Build result 29 | Change\ condition=Change condition -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/test/TestEchoBuilder.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder.test; 2 | 3 | import edu.umd.cs.findbugs.annotations.NonNull; 4 | import hudson.Extension; 5 | import hudson.FilePath; 6 | import hudson.Launcher; 7 | import hudson.model.AbstractProject; 8 | import hudson.model.Run; 9 | import hudson.model.TaskListener; 10 | import hudson.plugins.textfinder.TestUtils; 11 | import hudson.tasks.BuildStepDescriptor; 12 | import hudson.tasks.Builder; 13 | import jenkins.tasks.SimpleBuildStep; 14 | import org.jenkinsci.Symbol; 15 | import org.kohsuke.stapler.DataBoundConstructor; 16 | 17 | /** A test {@link Builder} that merely prints the message given to it with a prefix. */ 18 | public class TestEchoBuilder extends Builder implements SimpleBuildStep { 19 | 20 | private final String message; 21 | 22 | @DataBoundConstructor 23 | public TestEchoBuilder(String message) { 24 | this.message = TestUtils.PREFIX + message; 25 | } 26 | 27 | @Override 28 | public void perform( 29 | @NonNull Run run, 30 | @NonNull FilePath workspace, 31 | @NonNull Launcher launcher, 32 | @NonNull TaskListener listener) { 33 | listener.getLogger().println(message); 34 | } 35 | 36 | @Symbol("testEchoBuilder") 37 | @Extension 38 | public static class DescriptorImpl extends BuildStepDescriptor { 39 | 40 | @Override 41 | public boolean isApplicable(Class jobType) { 42 | return true; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/test/TestWriteFileBuilder.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder.test; 2 | 3 | import edu.umd.cs.findbugs.annotations.NonNull; 4 | import hudson.Extension; 5 | import hudson.FilePath; 6 | import hudson.Launcher; 7 | import hudson.model.AbstractProject; 8 | import hudson.model.Run; 9 | import hudson.model.TaskListener; 10 | import hudson.tasks.BuildStepDescriptor; 11 | import hudson.tasks.Builder; 12 | import java.io.IOException; 13 | import jenkins.tasks.SimpleBuildStep; 14 | import org.jenkinsci.Symbol; 15 | import org.kohsuke.stapler.DataBoundConstructor; 16 | 17 | public class TestWriteFileBuilder extends Builder implements SimpleBuildStep { 18 | 19 | private final String file; 20 | private final String text; 21 | 22 | @DataBoundConstructor 23 | public TestWriteFileBuilder(String file, String text) { 24 | this.file = file; 25 | this.text = text; 26 | } 27 | 28 | @Override 29 | public void perform( 30 | @NonNull Run run, 31 | @NonNull FilePath workspace, 32 | @NonNull Launcher launcher, 33 | @NonNull TaskListener listener) 34 | throws InterruptedException, IOException { 35 | FilePath filePath = workspace.child(file); 36 | filePath.write(text, null); 37 | } 38 | 39 | @Symbol("testWriteFileBuilder") 40 | @Extension 41 | public static class DescriptorImpl extends BuildStepDescriptor { 42 | 43 | @Override 44 | public boolean isApplicable(Class jobType) { 45 | return true; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/resources/hudson/plugins/textfinder/TextFinder/config_ja.properties: -------------------------------------------------------------------------------- 1 | # The MIT License 2 | # 3 | # Copyright (c) 2004-2010, Sun Microsystems, Inc. 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in 13 | # all copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | # THE SOFTWARE. 22 | 23 | Files=\u691c\u7d22\u3059\u308b\u30d5\u30a1\u30a4\u30eb 24 | Regular\ expression=\u6b63\u898f\u8868\u73fe 25 | Succeed\ if\ found=\u691c\u51fa\u6642\u306b\u6210\u529f\u6271\u3044\u306b\u3059\u308b 26 | Unstable\ if\ found=\u691c\u51fa\u6642\u306b\u4e0d\u5b89\u5b9a\u6271\u3044\u306b\u3059\u308b 27 | Also\ search\ the\ console\ output=\u30b3\u30f3\u30bd\u30fc\u30eb\u51fa\u529b\u3082\u691c\u7d22\u3059\u308b 28 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/TestUtils.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import static org.junit.Assert.assertNotNull; 4 | 5 | import hudson.model.Run; 6 | import java.io.File; 7 | import java.io.IOException; 8 | import org.jenkinsci.plugins.workflow.actions.WorkspaceAction; 9 | import org.jenkinsci.plugins.workflow.flow.FlowExecution; 10 | import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; 11 | import org.jenkinsci.plugins.workflow.graph.FlowNode; 12 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; 13 | import org.jvnet.hudson.test.JenkinsRule; 14 | 15 | /** Utilities for testing Text Finder */ 16 | public class TestUtils { 17 | 18 | public static final String FILE_SET = "out.txt"; 19 | public static final String PREFIX = ">>> "; 20 | public static final String UNIQUE_TEXT = "foobar"; 21 | 22 | public static void assertFileContainsMatch(File file, String text, JenkinsRule rule, Run build) 23 | throws IOException { 24 | rule.assertLogContains(String.format("%s:%s%s", file, System.getProperty("line.separator"), text), build); 25 | } 26 | 27 | public static File getWorkspace(WorkflowRun build) { 28 | FlowExecution execution = build.getExecution(); 29 | assertNotNull(execution); 30 | FlowGraphWalker walker = new FlowGraphWalker(execution); 31 | for (FlowNode node : walker) { 32 | WorkspaceAction action = node.getAction(WorkspaceAction.class); 33 | if (action != null) { 34 | return new File(action.getWorkspace().getRemote()); 35 | } 36 | } 37 | throw new IllegalStateException("Failed to find workspace"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /CHANGELOG.adoc: -------------------------------------------------------------------------------- 1 | = Changelog 2 | 3 | * All notable changes prior to 1.13 are documented in this changelog. 4 | * Release notes for versions >=1.13 can be found on the https://github.com/jenkinsci/text-finder-plugin/releases[GitHub releases page]. 5 | 6 | == Release History 7 | 8 | === 1.12 9 | 10 | Release date: 2019-08-01 11 | 12 | * Documentation updates: Improve writing of plugin description (https://github.com/jenkinsci/text-finder-plugin/pull/28[#28]) 13 | * Internal: Text Finder now fully supports https://github.com/jenkinsci/jep/tree/master/jep/210[JEP-210] and requires Jenkins 2.121 LTS or greater and JEP 210-compatible versions of Pipeline plugins 14 | * [https://issues.jenkins.io/browse/JENKINS-54128[JENKINS-54128]] Avoid calling `Run#getLogFile` (https://github.com/jenkinsci/text-finder-plugin/pull/46[#46]) 15 | * Make naming consistent (https://github.com/jenkinsci/text-finder-plugin/pull/29[#29]) 16 | * Add UI testing (https://github.com/jenkinsci/text-finder-plugin/pull/41[#41]) 17 | 18 | === 1.11 19 | 20 | Release date: 2019-06-01 21 | 22 | * Add Pipeline support (https://github.com/jenkinsci/text-finder-plugin/pull/12[#12]) 23 | * Add option to set build as not built (https://github.com/jenkinsci/text-finder-plugin/pull/16[#16]) 24 | * Better log output to console (https://github.com/jenkinsci/text-finder-plugin/pull/21[#21]) 25 | 26 | === 1.10 27 | 28 | Release date: 2014-01-31 29 | 30 | * [https://issues.jenkins.io/browse/JENKINS-17507[JENKINS-17507]] Do not block concurrent builds 31 | * Cleaner configuration UI using help buttons 32 | * Japanese translation 33 | 34 | === 1.9 35 | 36 | Release date: 2011-02-13 37 | 38 | * Update link in help 39 | 40 | === 1.8 41 | 42 | Release date: 2010-02-06 43 | 44 | * Update code for more recent Hudson 45 | 46 | === 1.7 47 | 48 | Release date: 2009-05-04 49 | 50 | * [https://issues.jenkins.io/browse/JENKINS-3613[JENKINS-3613]] Fix a file handle leak 51 | 52 | === 1.6 53 | 54 | Release date: 2008-11-06 55 | 56 | * Modify to work with all job types, including Maven 2 jobs 57 | 58 | === 1.5 59 | 60 | * Add *Unstable if found* configuration option. Use this option to set build unstable instead of failing the build. Default is off (previous behavior). 61 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import hudson.model.Result; 4 | import hudson.slaves.DumbSlave; 5 | import java.io.File; 6 | import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; 7 | import org.jenkinsci.plugins.workflow.job.WorkflowJob; 8 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; 9 | import org.junit.Rule; 10 | import org.junit.Test; 11 | import org.jvnet.hudson.test.JenkinsRule; 12 | 13 | public class TextFinderPublisherAgentTest { 14 | 15 | @Rule 16 | public JenkinsRule rule = new JenkinsRule(); 17 | 18 | @Test 19 | public void failureIfFoundInFileOnAgent() throws Exception { 20 | DumbSlave agent = rule.createOnlineSlave(); 21 | WorkflowJob project = rule.createProject(WorkflowJob.class); 22 | project.setDefinition(new CpsFlowDefinition( 23 | "node('" 24 | + agent.getNodeName() 25 | + "') {\n" 26 | + " writeFile file: '" 27 | + TestUtils.FILE_SET 28 | + "', text: 'foobar'\n" 29 | + " findText(textFinders: [textFinder(regexp: 'foobar', fileSet: '" 30 | + TestUtils.FILE_SET 31 | + "')])\n" 32 | + "}\n", 33 | true)); 34 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 35 | rule.assertLogContains( 36 | "[Text Finder] Searching for pattern '" 37 | + TestUtils.UNIQUE_TEXT 38 | + "' in file set '" 39 | + TestUtils.FILE_SET 40 | + "'.", 41 | build); 42 | TestUtils.assertFileContainsMatch( 43 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 44 | rule.assertLogContains( 45 | "[Text Finder] Finished searching for pattern '" 46 | + TestUtils.UNIQUE_TEXT 47 | + "' in file set '" 48 | + TestUtils.FILE_SET 49 | + "'.", 50 | build); 51 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 52 | } 53 | 54 | @Test 55 | public void failureIfFoundInConsoleOnAgent() throws Exception { 56 | DumbSlave agent = rule.createOnlineSlave(); 57 | WorkflowJob project = rule.createProject(WorkflowJob.class); 58 | project.setDefinition(new CpsFlowDefinition( 59 | "node('" 60 | + agent.getNodeName() 61 | + "') {\n" 62 | + " testEcho '" 63 | + TestUtils.UNIQUE_TEXT 64 | + "'\n" 65 | + " findText(textFinders: [textFinder(regexp: 'foobar'," 66 | + " alsoCheckConsoleOutput: true)])\n" 67 | + "}\n", 68 | true)); 69 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 70 | rule.assertLogContains("[Text Finder] Searching console output...", build); 71 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 72 | rule.assertLogContains( 73 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 74 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | org.jenkins-ci.plugins 6 | plugin 7 | 5.9 8 | 9 | 10 | 11 | text-finder 12 | ${revision}${changelist} 13 | hpi 14 | Text Finder 15 | https://github.com/jenkinsci/${project.artifactId}-plugin 16 | 17 | 18 | The MIT License (MIT) 19 | http://opensource.org/licenses/MIT 20 | repo 21 | 22 | 23 | 24 | 25 | 26 | kohsuke 27 | Kohsuke Kawaguchi 28 | 29 | 30 | Santiago Pericas-Geertsen 31 | 32 | 33 | basil 34 | Basil Crow 35 | 36 | 37 | 38 | 39 | scm:git:https://github.com/${gitHubRepo}.git 40 | scm:git:git@github.com:${gitHubRepo}.git 41 | ${scmTag} 42 | https://github.com/${gitHubRepo} 43 | 44 | 45 | 46 | 1.32 47 | -SNAPSHOT 48 | 49 | 2.479 50 | ${jenkins.baseline}.1 51 | jenkinsci/${project.artifactId}-plugin 52 | 2024-10-30T18:43:10Z 53 | false 54 | 55 | 56 | 57 | 58 | 59 | io.jenkins.tools.bom 60 | bom-${jenkins.baseline}.x 61 | 4136.vca_c3202a_7fd1 62 | pom 63 | import 64 | 65 | 66 | 67 | 68 | 69 | 70 | io.jenkins 71 | configuration-as-code 72 | test 73 | 74 | 75 | org.jenkins-ci.plugins 76 | config-file-provider 77 | test 78 | 79 | 80 | org.jenkins-ci.plugins 81 | job-dsl 82 | test 83 | 84 | 85 | org.jenkins-ci.plugins.workflow 86 | workflow-api 87 | test 88 | 89 | 90 | org.jenkins-ci.plugins.workflow 91 | workflow-basic-steps 92 | test 93 | 94 | 95 | org.jenkins-ci.plugins.workflow 96 | workflow-cps 97 | test 98 | 99 | 100 | org.jenkins-ci.plugins.workflow 101 | workflow-durable-task-step 102 | test 103 | 104 | 105 | org.jenkins-ci.plugins.workflow 106 | workflow-job 107 | test 108 | 109 | 110 | org.jenkins-ci.plugins.workflow 111 | workflow-support 112 | test 113 | 114 | 115 | 116 | 117 | 118 | repo.jenkins-ci.org 119 | https://repo.jenkins-ci.org/public/ 120 | 121 | 122 | 123 | 124 | 125 | repo.jenkins-ci.org 126 | https://repo.jenkins-ci.org/public/ 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = Text Finder Plugin 2 | :toc: 3 | :toc-placement!: 4 | :toc-title: 5 | ifdef::env-github[] 6 | :tip-caption: :bulb: 7 | :note-caption: :information_source: 8 | :important-caption: :heavy_exclamation_mark: 9 | :caution-caption: :fire: 10 | :warning-caption: :warning: 11 | endif::[] 12 | 13 | https://ci.jenkins.io/job/Plugins/job/text-finder-plugin/job/master/[image:https://ci.jenkins.io/job/Plugins/job/text-finder-plugin/job/master/badge/icon[Build Status]] 14 | https://github.com/jenkinsci/text-finder-plugin/graphs/contributors[image:https://img.shields.io/github/contributors/jenkinsci/text-finder-plugin.svg[Contributors]] 15 | https://plugins.jenkins.io/text-finder[image:https://img.shields.io/jenkins/plugin/v/text-finder.svg[Jenkins Plugin]] 16 | https://github.com/jenkinsci/text-finder-plugin/releases/latest[image:https://img.shields.io/github/release/jenkinsci/text-finder-plugin.svg?label=changelog[GitHub release]] 17 | https://plugins.jenkins.io/text-finder[image:https://img.shields.io/jenkins/plugin/i/text-finder.svg?color=blue[Jenkins Plugin Installs]] 18 | 19 | toc::[] 20 | 21 | == Introduction 22 | 23 | This plugin lets you search for some text using regular expressions in a set of files or the console log. 24 | Based on the outcome, you can downgrade the build result to `UNSTABLE`, `FAILURE`, `NOT_BUILT`, or `ABORTED`. 25 | 26 | For example, you can search for the string `failure` in a set of log files. 27 | If a match is found, you can downgrade the build result from `SUCCESS` to `FAILURE`. 28 | This is handy when you have some tools in your build chain that do not properly set the exit code. 29 | 30 | == Getting started 31 | 32 | === https://jenkins.io/doc/book/pipeline/[Pipeline] jobs 33 | 34 | The basic Pipeline syntax is as follows: 35 | 36 | [source,groovy] 37 | ---- 38 | findText(textFinders: [textFinder(regexp: '', fileSet: '')]) 39 | ---- 40 | 41 | The regular expression uses the syntax supported by the Java https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html[`Pattern`] class. 42 | The file set specifies the path to the files to search, relative to the workspace root. 43 | This can use wildcards, like `logs/**/*/*.txt`. 44 | See the documentation for the `@includes` attribute of the Ant https://ant.apache.org/manual/Types/fileset.html[`FileSet`] type for details. 45 | 46 | To also check the console log, use the following syntax: 47 | 48 | [source,groovy] 49 | ---- 50 | findText(textFinders: [textFinder(regexp: '', fileSet: '', alsoCheckConsoleOutput: true)]) 51 | ---- 52 | 53 | If you just want to check the console log, you can omit the file set: 54 | 55 | [source,groovy] 56 | ---- 57 | findText(textFinders: [textFinder(regexp: '', alsoCheckConsoleOutput: true)]) 58 | ---- 59 | 60 | To downgrade the build result, use the following syntax: 61 | 62 | [source,groovy] 63 | ---- 64 | findText(textFinders: [textFinder([...], buildResult: 'UNSTABLE')]) 65 | ---- 66 | 67 | If a match is found, the build result will be set to this value. 68 | 69 | NOTE: The build result can only get worse, so you cannot change the result to `SUCCESS` if the current result is `UNSTABLE` or worse. 70 | 71 | Whether or not a build is downgraded depends on its change condition. 72 | The default change condition is `MATCH_FOUND`, which downgrades the build result if a match is found. 73 | To downgrade the build result if a match is _not_ found, set the change condition to `MATCH_NOT_FOUND`: 74 | 75 | [source,groovy] 76 | ---- 77 | findText(textFinders: [textFinder([...], changeCondition: 'MATCH_NOT_FOUND', buildResult: 'UNSTABLE')]) 78 | ---- 79 | 80 | To search for multiple regular expressions, use the following syntax: 81 | 82 | [source,groovy] 83 | ---- 84 | findText(textFinders: [ 85 | textFinder(regexp: '', [...]), 86 | textFinder(regexp: '', [...]), 87 | textFinder(regexp: '', [...]) 88 | ]) 89 | ---- 90 | 91 | === Freestyle jobs 92 | 93 | This plugin provides https://plugins.jenkins.io/job-dsl/[Job DSL] support for Freestyle jobs using https://github.com/jenkinsci/job-dsl-plugin/wiki/Dynamic-DSL[the Dynamic DSL]. 94 | For example: 95 | 96 | [source,groovy] 97 | ---- 98 | job('example') { 99 | publishers { 100 | findText { 101 | textFinders { 102 | textFinder { 103 | regexp '' 104 | fileSet '' 105 | changeCondition '' 106 | alsoCheckConsoleOutput true 107 | buildResult 'UNSTABLE' 108 | } 109 | } 110 | } 111 | } 112 | } 113 | ---- 114 | 115 | To search for multiple regular expressions, use the following syntax: 116 | 117 | [source,groovy] 118 | ---- 119 | job('example') { 120 | publishers { 121 | findText { 122 | textFinders { 123 | textFinder { 124 | regexp '' 125 | [...] 126 | } 127 | textFinder { 128 | regexp '' 129 | [...] 130 | } 131 | textFinder { 132 | regexp '' 133 | [...] 134 | } 135 | } 136 | } 137 | } 138 | } 139 | ---- 140 | 141 | == Issues 142 | 143 | Report issues and enhancements in the https://issues.jenkins.io/[Jenkins issue tracker]. 144 | Use the `text-finder-plugin` component in the `JENKINS` project. 145 | 146 | == Contributing 147 | 148 | Refer to our https://github.com/jenkinsci/.github/blob/master/CONTRIBUTING.md[contribution guidelines]. 149 | -------------------------------------------------------------------------------- /src/main/java/hudson/plugins/textfinder/TextFinder.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import edu.umd.cs.findbugs.annotations.CheckForNull; 4 | import edu.umd.cs.findbugs.annotations.NonNull; 5 | import hudson.Extension; 6 | import hudson.Util; 7 | import hudson.model.AbstractDescribableImpl; 8 | import hudson.model.Descriptor; 9 | import hudson.model.Result; 10 | import hudson.util.FormValidation; 11 | import hudson.util.ListBoxModel; 12 | import java.io.Serializable; 13 | import java.util.Arrays; 14 | import java.util.Objects; 15 | import java.util.regex.Pattern; 16 | import java.util.regex.PatternSyntaxException; 17 | import org.jenkinsci.Symbol; 18 | import org.kohsuke.accmod.Restricted; 19 | import org.kohsuke.accmod.restrictions.NoExternalUse; 20 | import org.kohsuke.stapler.DataBoundConstructor; 21 | import org.kohsuke.stapler.DataBoundSetter; 22 | import org.kohsuke.stapler.QueryParameter; 23 | 24 | public class TextFinder extends AbstractDescribableImpl implements Serializable { 25 | 26 | @NonNull 27 | private /* final */ String regexp; 28 | 29 | @CheckForNull 30 | private String fileSet; 31 | 32 | @NonNull 33 | private String buildResult = Result.FAILURE.toString(); 34 | 35 | private TextFinderChangeCondition changeCondition = TextFinderChangeCondition.MATCH_FOUND; 36 | private boolean alsoCheckConsoleOutput; 37 | 38 | @Restricted(NoExternalUse.class) 39 | public String getRegexp() { 40 | return regexp; 41 | } 42 | 43 | @DataBoundConstructor 44 | @Restricted(NoExternalUse.class) 45 | public TextFinder(String regexp) { 46 | this.regexp = Objects.requireNonNull(regexp); 47 | } 48 | 49 | /** This is gross, but fortunately it is only used in a deprecated code path. */ 50 | @Restricted(NoExternalUse.class) 51 | void setRegexp(String regexp) { 52 | this.regexp = Objects.requireNonNull(regexp); 53 | } 54 | 55 | @Restricted(NoExternalUse.class) 56 | public String getFileSet() { 57 | return fileSet; 58 | } 59 | 60 | @DataBoundSetter 61 | @Restricted(NoExternalUse.class) 62 | public void setFileSet(String fileSet) { 63 | this.fileSet = fileSet != null ? Util.fixEmpty(fileSet.trim()) : null; 64 | } 65 | 66 | @Restricted(NoExternalUse.class) 67 | public String getBuildResult() { 68 | return buildResult; 69 | } 70 | 71 | @DataBoundSetter 72 | @Restricted(NoExternalUse.class) 73 | public void setBuildResult(String buildResult) { 74 | if (buildResult == null || Util.fixEmpty(buildResult.trim()) == null) { 75 | buildResult = Result.FAILURE.toString(); 76 | } 77 | 78 | if (!buildResult.equalsIgnoreCase(Result.fromString(buildResult).toString())) { 79 | throw new IllegalArgumentException("buildResult is invalid: " 80 | + buildResult 81 | + ". Valid options are SUCCESS, UNSTABLE, FAILURE, NOT_BUILT and" 82 | + " ABORTED."); 83 | } 84 | 85 | this.buildResult = buildResult; 86 | } 87 | 88 | @Restricted(NoExternalUse.class) 89 | public TextFinderChangeCondition getChangeCondition() { 90 | return changeCondition; 91 | } 92 | 93 | @DataBoundSetter 94 | @Restricted(NoExternalUse.class) 95 | public void setChangeCondition(TextFinderChangeCondition changeCondition) { 96 | this.changeCondition = changeCondition; 97 | } 98 | 99 | @Restricted(NoExternalUse.class) 100 | public boolean isAlsoCheckConsoleOutput() { 101 | return alsoCheckConsoleOutput; 102 | } 103 | 104 | @DataBoundSetter 105 | @Restricted(NoExternalUse.class) 106 | public void setAlsoCheckConsoleOutput(boolean alsoCheckConsoleOutput) { 107 | this.alsoCheckConsoleOutput = alsoCheckConsoleOutput; 108 | } 109 | 110 | /** 111 | * Called by XStream after object construction 112 | * 113 | * @return modified object 114 | */ 115 | protected Object readResolve() { 116 | if (changeCondition == null) { 117 | changeCondition = TextFinderChangeCondition.MATCH_FOUND; 118 | } 119 | 120 | return this; 121 | } 122 | 123 | @Symbol("textFinder") 124 | @Extension 125 | public static class DescriptorImpl extends Descriptor { 126 | @NonNull 127 | public String getDisplayName() { 128 | // This descriptor is not intended to be displayed on its own. 129 | return ""; 130 | } 131 | 132 | /** 133 | * Checks the regular expression validity. 134 | * 135 | * @param value The expression to check 136 | * @return The form validation result 137 | */ 138 | @SuppressWarnings({"lgtm[jenkins/csrf]", "lgtm[jenkins/no-permission-check]"}) 139 | public FormValidation doCheckRegexp(@QueryParameter String value) { 140 | if (Util.fixEmptyAndTrim(value) == null) { 141 | // not entered yet 142 | return FormValidation.ok(); 143 | } 144 | 145 | try { 146 | Pattern.compile(value); 147 | return FormValidation.ok(); 148 | } catch (PatternSyntaxException e) { 149 | return FormValidation.error(e.getMessage()); 150 | } 151 | } 152 | 153 | @SuppressWarnings({"lgtm[jenkins/csrf]", "lgtm[jenkins/no-permission-check]"}) 154 | public ListBoxModel doFillBuildResultItems() { 155 | ListBoxModel r = new ListBoxModel(); 156 | for (Result result : 157 | Arrays.asList(Result.SUCCESS, Result.UNSTABLE, Result.FAILURE, Result.NOT_BUILT, Result.ABORTED)) { 158 | r.add(result.toString()); 159 | } 160 | return r; 161 | } 162 | } 163 | 164 | private static final long serialVersionUID = 1L; 165 | } 166 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineCompatibilityTest.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import hudson.model.Result; 4 | import java.io.File; 5 | import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; 6 | import org.jenkinsci.plugins.workflow.job.WorkflowJob; 7 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; 8 | import org.junit.Rule; 9 | import org.junit.Test; 10 | import org.jvnet.hudson.test.JenkinsRule; 11 | 12 | public class TextFinderPublisherPipelineCompatibilityTest { 13 | 14 | @Rule 15 | public JenkinsRule rule = new JenkinsRule(); 16 | 17 | @Test 18 | public void successIfFoundInFile() throws Exception { 19 | WorkflowJob project = rule.createProject(WorkflowJob.class); 20 | project.setDefinition(new CpsFlowDefinition( 21 | "node {\n" 22 | + " writeFile file: '" 23 | + TestUtils.FILE_SET 24 | + "', text: '" 25 | + TestUtils.UNIQUE_TEXT 26 | + "'\n" 27 | + " findText regexp: '" 28 | + TestUtils.UNIQUE_TEXT 29 | + "', fileSet: '" 30 | + TestUtils.FILE_SET 31 | + "', succeedIfFound: true\n" 32 | + "}\n", 33 | true)); 34 | WorkflowRun build = rule.buildAndAssertSuccess(project); 35 | rule.assertLogContains( 36 | "[Text Finder] Searching for pattern '" 37 | + TestUtils.UNIQUE_TEXT 38 | + "' in file set '" 39 | + TestUtils.FILE_SET 40 | + "'.", 41 | build); 42 | TestUtils.assertFileContainsMatch( 43 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 44 | rule.assertLogContains( 45 | "[Text Finder] Finished searching for pattern '" 46 | + TestUtils.UNIQUE_TEXT 47 | + "' in file set '" 48 | + TestUtils.FILE_SET 49 | + "'.", 50 | build); 51 | } 52 | 53 | @Test 54 | public void failureIfFoundInFile() throws Exception { 55 | WorkflowJob project = rule.createProject(WorkflowJob.class); 56 | project.setDefinition(new CpsFlowDefinition( 57 | "node {\n" 58 | + " writeFile file: '" 59 | + TestUtils.FILE_SET 60 | + "', text: '" 61 | + TestUtils.UNIQUE_TEXT 62 | + "'\n" 63 | + " findText regexp: '" 64 | + TestUtils.UNIQUE_TEXT 65 | + "', fileSet: '" 66 | + TestUtils.FILE_SET 67 | + "'\n" 68 | + "}\n", 69 | true)); 70 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 71 | rule.assertLogContains( 72 | "[Text Finder] Searching for pattern '" 73 | + TestUtils.UNIQUE_TEXT 74 | + "' in file set '" 75 | + TestUtils.FILE_SET 76 | + "'.", 77 | build); 78 | TestUtils.assertFileContainsMatch( 79 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 80 | rule.assertLogContains( 81 | "[Text Finder] Finished searching for pattern '" 82 | + TestUtils.UNIQUE_TEXT 83 | + "' in file set '" 84 | + TestUtils.FILE_SET 85 | + "'.", 86 | build); 87 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 88 | } 89 | 90 | @Test 91 | public void unstableIfFoundInFile() throws Exception { 92 | WorkflowJob project = rule.createProject(WorkflowJob.class); 93 | project.setDefinition(new CpsFlowDefinition( 94 | "node {\n" 95 | + " writeFile file: '" 96 | + TestUtils.FILE_SET 97 | + "', text: '" 98 | + TestUtils.UNIQUE_TEXT 99 | + "'\n" 100 | + " findText regexp: '" 101 | + TestUtils.UNIQUE_TEXT 102 | + "', fileSet: '" 103 | + TestUtils.FILE_SET 104 | + "', unstableIfFound: true\n" 105 | + "}\n", 106 | true)); 107 | WorkflowRun build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 108 | rule.assertLogContains( 109 | "[Text Finder] Searching for pattern '" 110 | + TestUtils.UNIQUE_TEXT 111 | + "' in file set '" 112 | + TestUtils.FILE_SET 113 | + "'.", 114 | build); 115 | TestUtils.assertFileContainsMatch( 116 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 117 | rule.assertLogContains( 118 | "[Text Finder] Finished searching for pattern '" 119 | + TestUtils.UNIQUE_TEXT 120 | + "' in file set '" 121 | + TestUtils.FILE_SET 122 | + "'.", 123 | build); 124 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 125 | } 126 | 127 | @Test 128 | public void notBuiltIfFoundInFile() throws Exception { 129 | WorkflowJob project = rule.createProject(WorkflowJob.class); 130 | project.setDefinition(new CpsFlowDefinition( 131 | "node {\n" 132 | + " writeFile file: '" 133 | + TestUtils.FILE_SET 134 | + "', text: '" 135 | + TestUtils.UNIQUE_TEXT 136 | + "'\n" 137 | + " findText regexp: '" 138 | + TestUtils.UNIQUE_TEXT 139 | + "', fileSet: '" 140 | + TestUtils.FILE_SET 141 | + "', notBuiltIfFound: true\n" 142 | + "}\n", 143 | true)); 144 | WorkflowRun build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 145 | rule.assertLogContains( 146 | "[Text Finder] Searching for pattern '" 147 | + TestUtils.UNIQUE_TEXT 148 | + "' in file set '" 149 | + TestUtils.FILE_SET 150 | + "'.", 151 | build); 152 | TestUtils.assertFileContainsMatch( 153 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 154 | rule.assertLogContains( 155 | "[Text Finder] Finished searching for pattern '" 156 | + TestUtils.UNIQUE_TEXT 157 | + "' in file set '" 158 | + TestUtils.FILE_SET 159 | + "'.", 160 | build); 161 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 162 | } 163 | 164 | @Test 165 | public void successIfNotFoundInFile() throws Exception { 166 | WorkflowJob project = rule.createProject(WorkflowJob.class); 167 | project.setDefinition(new CpsFlowDefinition( 168 | "node {\n" 169 | + " writeFile file: '" 170 | + TestUtils.FILE_SET 171 | + "', text: 'foobaz'\n" 172 | + " findText regexp: '" 173 | + TestUtils.UNIQUE_TEXT 174 | + "', fileSet: '" 175 | + TestUtils.FILE_SET 176 | + "'\n" 177 | + "}\n", 178 | true)); 179 | WorkflowRun build = rule.buildAndAssertSuccess(project); 180 | rule.assertLogContains( 181 | "[Text Finder] Searching for pattern '" 182 | + TestUtils.UNIQUE_TEXT 183 | + "' in file set '" 184 | + TestUtils.FILE_SET 185 | + "'.", 186 | build); 187 | rule.assertLogContains( 188 | "[Text Finder] Finished searching for pattern '" 189 | + TestUtils.UNIQUE_TEXT 190 | + "' in file set '" 191 | + TestUtils.FILE_SET 192 | + "'.", 193 | build); 194 | } 195 | 196 | @Test 197 | public void failureIfNotFoundInFile() throws Exception { 198 | WorkflowJob project = rule.createProject(WorkflowJob.class); 199 | project.setDefinition(new CpsFlowDefinition( 200 | "node {\n" 201 | + " writeFile file: '" 202 | + TestUtils.FILE_SET 203 | + "', text: 'foobaz'\n" 204 | + " findText regexp: '" 205 | + TestUtils.UNIQUE_TEXT 206 | + "', fileSet: '" 207 | + TestUtils.FILE_SET 208 | + "', succeedIfFound: true\n" 209 | + "}\n", 210 | true)); 211 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 212 | rule.assertLogContains( 213 | "[Text Finder] Searching for pattern '" 214 | + TestUtils.UNIQUE_TEXT 215 | + "' in file set '" 216 | + TestUtils.FILE_SET 217 | + "'.", 218 | build); 219 | rule.assertLogContains( 220 | "[Text Finder] Finished searching for pattern '" 221 | + TestUtils.UNIQUE_TEXT 222 | + "' in file set '" 223 | + TestUtils.FILE_SET 224 | + "'.", 225 | build); 226 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 227 | } 228 | 229 | @Test 230 | public void successIfFoundInConsole() throws Exception { 231 | WorkflowJob project = rule.createProject(WorkflowJob.class); 232 | project.setDefinition(new CpsFlowDefinition( 233 | " testEcho '" 234 | + TestUtils.UNIQUE_TEXT 235 | + "'\n" 236 | + "node {\n" 237 | + " findText regexp: '" 238 | + TestUtils.UNIQUE_TEXT 239 | + "', succeedIfFound: true, alsoCheckConsoleOutput: true\n" 240 | + "}\n", 241 | true)); 242 | WorkflowRun build = rule.buildAndAssertSuccess(project); 243 | rule.assertLogContains("[Text Finder] Searching console output...", build); 244 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 245 | rule.assertLogContains( 246 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 247 | } 248 | 249 | @Test 250 | public void failureIfFoundInConsole() throws Exception { 251 | WorkflowJob project = rule.createProject(WorkflowJob.class); 252 | project.setDefinition(new CpsFlowDefinition( 253 | " testEcho '" 254 | + TestUtils.UNIQUE_TEXT 255 | + "'\n" 256 | + "node {\n" 257 | + " findText regexp: '" 258 | + TestUtils.UNIQUE_TEXT 259 | + "', alsoCheckConsoleOutput: true\n" 260 | + "}\n", 261 | true)); 262 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 263 | rule.assertLogContains("[Text Finder] Searching console output...", build); 264 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 265 | rule.assertLogContains( 266 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 267 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 268 | } 269 | 270 | @Test 271 | public void unstableIfFoundInConsole() throws Exception { 272 | WorkflowJob project = rule.createProject(WorkflowJob.class); 273 | project.setDefinition(new CpsFlowDefinition( 274 | " testEcho '" 275 | + TestUtils.UNIQUE_TEXT 276 | + "'\n" 277 | + "node {\n" 278 | + " findText regexp: '" 279 | + TestUtils.UNIQUE_TEXT 280 | + "', unstableIfFound: true, alsoCheckConsoleOutput: true\n" 281 | + "}\n", 282 | true)); 283 | WorkflowRun build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 284 | rule.assertLogContains("[Text Finder] Searching console output...", build); 285 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 286 | rule.assertLogContains( 287 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 288 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 289 | } 290 | 291 | @Test 292 | public void notBuiltIfFoundInConsole() throws Exception { 293 | WorkflowJob project = rule.createProject(WorkflowJob.class); 294 | project.setDefinition(new CpsFlowDefinition( 295 | " testEcho '" 296 | + TestUtils.UNIQUE_TEXT 297 | + "'\n" 298 | + "node {\n" 299 | + " findText regexp: '" 300 | + TestUtils.UNIQUE_TEXT 301 | + "', notBuiltIfFound: true, alsoCheckConsoleOutput: true\n" 302 | + "}\n", 303 | true)); 304 | WorkflowRun build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 305 | rule.assertLogContains("[Text Finder] Searching console output...", build); 306 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 307 | rule.assertLogContains( 308 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 309 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 310 | } 311 | 312 | @Test 313 | public void successIfNotFoundInConsole() throws Exception { 314 | WorkflowJob project = rule.createProject(WorkflowJob.class); 315 | project.setDefinition(new CpsFlowDefinition( 316 | "node {\n" 317 | + " findText regexp: '" 318 | + TestUtils.UNIQUE_TEXT 319 | + "', alsoCheckConsoleOutput: true\n" 320 | + "}\n", 321 | true)); 322 | WorkflowRun build = rule.buildAndAssertSuccess(project); 323 | rule.assertLogContains("[Text Finder] Searching console output...", build); 324 | rule.assertLogContains( 325 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 326 | } 327 | 328 | @Test 329 | public void failureIfNotFoundInConsole() throws Exception { 330 | WorkflowJob project = rule.createProject(WorkflowJob.class); 331 | project.setDefinition(new CpsFlowDefinition( 332 | "node {\n" 333 | + " findText regexp: '" 334 | + TestUtils.UNIQUE_TEXT 335 | + "', alsoCheckConsoleOutput: true, succeedIfFound: true\n" 336 | + "}\n", 337 | true)); 338 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 339 | rule.assertLogContains("[Text Finder] Searching console output...", build); 340 | rule.assertLogContains( 341 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 342 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 343 | } 344 | } 345 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleCompatibilityTest.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertTrue; 6 | 7 | import hudson.model.FreeStyleBuild; 8 | import hudson.model.FreeStyleProject; 9 | import hudson.model.Result; 10 | import hudson.plugins.textfinder.test.TestEchoBuilder; 11 | import hudson.plugins.textfinder.test.TestWriteFileBuilder; 12 | import java.io.File; 13 | import org.junit.Rule; 14 | import org.junit.Test; 15 | import org.jvnet.hudson.test.JenkinsRule; 16 | import org.jvnet.hudson.test.recipes.LocalData; 17 | 18 | @SuppressWarnings("deprecation") 19 | public class TextFinderPublisherFreestyleCompatibilityTest { 20 | 21 | @Rule 22 | public JenkinsRule rule = new JenkinsRule(); 23 | 24 | @Test 25 | public void successIfFoundInFile() throws Exception { 26 | FreeStyleProject project = rule.createFreeStyleProject(); 27 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 28 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 29 | textFinder.setFileSet(TestUtils.FILE_SET); 30 | textFinder.setSucceedIfFound(true); 31 | project.getPublishersList().add(textFinder); 32 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 33 | rule.assertLogContains( 34 | "[Text Finder] Searching for pattern '" 35 | + TestUtils.UNIQUE_TEXT 36 | + "' in file set '" 37 | + TestUtils.FILE_SET 38 | + "'.", 39 | build); 40 | TestUtils.assertFileContainsMatch( 41 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 42 | rule.assertLogContains( 43 | "[Text Finder] Finished searching for pattern '" 44 | + TestUtils.UNIQUE_TEXT 45 | + "' in file set '" 46 | + TestUtils.FILE_SET 47 | + "'.", 48 | build); 49 | } 50 | 51 | @Test 52 | public void failureIfFoundInFile() throws Exception { 53 | FreeStyleProject project = rule.createFreeStyleProject(); 54 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 55 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 56 | textFinder.setFileSet(TestUtils.FILE_SET); 57 | project.getPublishersList().add(textFinder); 58 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 59 | rule.assertLogContains( 60 | "[Text Finder] Searching for pattern '" 61 | + TestUtils.UNIQUE_TEXT 62 | + "' in file set '" 63 | + TestUtils.FILE_SET 64 | + "'.", 65 | build); 66 | TestUtils.assertFileContainsMatch( 67 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 68 | rule.assertLogContains( 69 | "[Text Finder] Finished searching for pattern '" 70 | + TestUtils.UNIQUE_TEXT 71 | + "' in file set '" 72 | + TestUtils.FILE_SET 73 | + "'.", 74 | build); 75 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 76 | } 77 | 78 | @Test 79 | public void unstableIfFoundInFile() throws Exception { 80 | FreeStyleProject project = rule.createFreeStyleProject(); 81 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 82 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 83 | textFinder.setFileSet(TestUtils.FILE_SET); 84 | textFinder.setUnstableIfFound(true); 85 | project.getPublishersList().add(textFinder); 86 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 87 | rule.assertLogContains( 88 | "[Text Finder] Searching for pattern '" 89 | + TestUtils.UNIQUE_TEXT 90 | + "' in file set '" 91 | + TestUtils.FILE_SET 92 | + "'.", 93 | build); 94 | TestUtils.assertFileContainsMatch( 95 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 96 | rule.assertLogContains( 97 | "[Text Finder] Finished searching for pattern '" 98 | + TestUtils.UNIQUE_TEXT 99 | + "' in file set '" 100 | + TestUtils.FILE_SET 101 | + "'.", 102 | build); 103 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 104 | } 105 | 106 | @Test 107 | public void notBuiltIfFoundInFile() throws Exception { 108 | FreeStyleProject project = rule.createFreeStyleProject(); 109 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 110 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 111 | textFinder.setFileSet(TestUtils.FILE_SET); 112 | textFinder.setNotBuiltIfFound(true); 113 | project.getPublishersList().add(textFinder); 114 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 115 | rule.assertLogContains( 116 | "[Text Finder] Searching for pattern '" 117 | + TestUtils.UNIQUE_TEXT 118 | + "' in file set '" 119 | + TestUtils.FILE_SET 120 | + "'.", 121 | build); 122 | TestUtils.assertFileContainsMatch( 123 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 124 | rule.assertLogContains( 125 | "[Text Finder] Finished searching for pattern '" 126 | + TestUtils.UNIQUE_TEXT 127 | + "' in file set '" 128 | + TestUtils.FILE_SET 129 | + "'.", 130 | build); 131 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 132 | } 133 | 134 | @Test 135 | public void successIfNotFoundInFile() throws Exception { 136 | FreeStyleProject project = rule.createFreeStyleProject(); 137 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, "foobaz")); 138 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 139 | textFinder.setFileSet(TestUtils.FILE_SET); 140 | project.getPublishersList().add(textFinder); 141 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 142 | rule.assertLogContains( 143 | "[Text Finder] Searching for pattern '" 144 | + TestUtils.UNIQUE_TEXT 145 | + "' in file set '" 146 | + TestUtils.FILE_SET 147 | + "'.", 148 | build); 149 | rule.assertLogContains( 150 | "[Text Finder] Finished searching for pattern '" 151 | + TestUtils.UNIQUE_TEXT 152 | + "' in file set '" 153 | + TestUtils.FILE_SET 154 | + "'.", 155 | build); 156 | } 157 | 158 | @Test 159 | public void failureIfNotFoundInFile() throws Exception { 160 | FreeStyleProject project = rule.createFreeStyleProject(); 161 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, "foobaz")); 162 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 163 | textFinder.setFileSet(TestUtils.FILE_SET); 164 | textFinder.setSucceedIfFound(true); 165 | project.getPublishersList().add(textFinder); 166 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 167 | rule.assertLogContains( 168 | "[Text Finder] Searching for pattern '" 169 | + TestUtils.UNIQUE_TEXT 170 | + "' in file set '" 171 | + TestUtils.FILE_SET 172 | + "'.", 173 | build); 174 | rule.assertLogContains( 175 | "[Text Finder] Finished searching for pattern '" 176 | + TestUtils.UNIQUE_TEXT 177 | + "' in file set '" 178 | + TestUtils.FILE_SET 179 | + "'.", 180 | build); 181 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 182 | } 183 | 184 | @Test 185 | public void successIfFoundInConsole() throws Exception { 186 | FreeStyleProject project = rule.createFreeStyleProject(); 187 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 188 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 189 | textFinder.setSucceedIfFound(true); 190 | textFinder.setAlsoCheckConsoleOutput(true); 191 | project.getPublishersList().add(textFinder); 192 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 193 | rule.assertLogContains("[Text Finder] Searching console output...", build); 194 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 195 | rule.assertLogContains( 196 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 197 | } 198 | 199 | @Test 200 | public void failureIfFoundInConsole() throws Exception { 201 | FreeStyleProject project = rule.createFreeStyleProject(); 202 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 203 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 204 | textFinder.setAlsoCheckConsoleOutput(true); 205 | project.getPublishersList().add(textFinder); 206 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 207 | rule.assertLogContains("[Text Finder] Searching console output...", build); 208 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 209 | rule.assertLogContains( 210 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 211 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 212 | } 213 | 214 | @Test 215 | public void unstableIfFoundInConsole() throws Exception { 216 | FreeStyleProject project = rule.createFreeStyleProject(); 217 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 218 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 219 | textFinder.setUnstableIfFound(true); 220 | textFinder.setAlsoCheckConsoleOutput(true); 221 | project.getPublishersList().add(textFinder); 222 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 223 | rule.assertLogContains("[Text Finder] Searching console output...", build); 224 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 225 | rule.assertLogContains( 226 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 227 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 228 | } 229 | 230 | @Test 231 | public void notBuiltIfFoundInConsole() throws Exception { 232 | FreeStyleProject project = rule.createFreeStyleProject(); 233 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 234 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 235 | textFinder.setNotBuiltIfFound(true); 236 | textFinder.setAlsoCheckConsoleOutput(true); 237 | project.getPublishersList().add(textFinder); 238 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 239 | rule.assertLogContains("[Text Finder] Searching console output...", build); 240 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 241 | rule.assertLogContains( 242 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 243 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 244 | } 245 | 246 | @Test 247 | public void successIfNotFoundInConsole() throws Exception { 248 | FreeStyleProject project = rule.createFreeStyleProject(); 249 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 250 | textFinder.setAlsoCheckConsoleOutput(true); 251 | project.getPublishersList().add(textFinder); 252 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 253 | rule.assertLogContains("[Text Finder] Searching console output...", build); 254 | rule.assertLogContains( 255 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 256 | } 257 | 258 | @Test 259 | public void failureIfNotFoundInConsole() throws Exception { 260 | FreeStyleProject project = rule.createFreeStyleProject(); 261 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 262 | textFinder.setAlsoCheckConsoleOutput(true); 263 | textFinder.setSucceedIfFound(true); 264 | project.getPublishersList().add(textFinder); 265 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 266 | rule.assertLogContains("[Text Finder] Searching console output...", build); 267 | rule.assertLogContains( 268 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 269 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 270 | } 271 | 272 | @LocalData 273 | @Test 274 | public void persistedConfigurationBeforeMultipleFinders() { 275 | // Local data created using Text Finder 1.12 with the following code: 276 | /* 277 | FreeStyleProject project = rule.createFreeStyleProject(); 278 | TextFinderPublisher textFinder = new TextFinderPublisher("foobar"); 279 | textFinder.setFileSet("out.txt"); 280 | textFinder.setUnstableIfFound(true); 281 | textFinder.setAlsoCheckConsoleOutput(true); 282 | project.getPublishersList().add(textFinder); 283 | */ 284 | FreeStyleProject project = rule.jenkins.getItemByFullName("test0", FreeStyleProject.class); 285 | assertEquals(1, project.getPublishersList().size()); 286 | TextFinderPublisher textFinderPublisher = 287 | (TextFinderPublisher) project.getPublishersList().get(0); 288 | TextFinder textFinder = textFinderPublisher.getTextFinders().get(0); 289 | assertEquals("foobar", textFinder.getRegexp()); 290 | assertEquals("out.txt", textFinder.getFileSet()); 291 | assertEquals(Result.UNSTABLE.toString(), textFinder.getBuildResult()); 292 | assertTrue(textFinder.isAlsoCheckConsoleOutput()); 293 | } 294 | 295 | @LocalData 296 | @Test 297 | public void persistedConfigurationBeforeChangeCondition() { 298 | // Local data created using Text Finder 1.13 with the following code: 299 | /* 300 | FreeStyleProject project = rule.createFreeStyleProject(); 301 | project.getBuildersList() 302 | .add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 303 | TextFinderPublisher textFinder = new TextFinderPublisher(TestUtils.UNIQUE_TEXT); 304 | textFinder.setFileSet(TestUtils.FILE_SET); 305 | project.getPublishersList().add(textFinder); 306 | */ 307 | FreeStyleProject project = rule.jenkins.getItemByFullName("test0", FreeStyleProject.class); 308 | assertEquals(1, project.getPublishersList().size()); 309 | TextFinderPublisher textFinderPublisher = 310 | (TextFinderPublisher) project.getPublishersList().get(0); 311 | TextFinder textFinder = textFinderPublisher.getTextFinders().get(0); 312 | assertEquals(TestUtils.UNIQUE_TEXT, textFinder.getRegexp()); 313 | assertEquals(TestUtils.FILE_SET, textFinder.getFileSet()); 314 | assertEquals(Result.FAILURE.toString(), textFinder.getBuildResult()); 315 | assertEquals(TextFinderChangeCondition.MATCH_FOUND, textFinder.getChangeCondition()); 316 | assertFalse(textFinder.isAlsoCheckConsoleOutput()); 317 | } 318 | } 319 | -------------------------------------------------------------------------------- /src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import edu.umd.cs.findbugs.annotations.NonNull; 4 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 5 | import hudson.Extension; 6 | import hudson.FilePath; 7 | import hudson.Functions; 8 | import hudson.Launcher; 9 | import hudson.console.ConsoleNote; 10 | import hudson.model.AbstractProject; 11 | import hudson.model.Result; 12 | import hudson.model.Run; 13 | import hudson.model.TaskListener; 14 | import hudson.remoting.RemoteOutputStream; 15 | import hudson.remoting.VirtualChannel; 16 | import hudson.tasks.BuildStepDescriptor; 17 | import hudson.tasks.BuildStepMonitor; 18 | import hudson.tasks.Publisher; 19 | import hudson.tasks.Recorder; 20 | import java.io.BufferedReader; 21 | import java.io.File; 22 | import java.io.FileInputStream; 23 | import java.io.IOException; 24 | import java.io.InputStream; 25 | import java.io.InputStreamReader; 26 | import java.io.PrintStream; 27 | import java.io.Reader; 28 | import java.io.Serializable; 29 | import java.nio.charset.Charset; 30 | import java.util.ArrayList; 31 | import java.util.Collections; 32 | import java.util.List; 33 | import java.util.regex.Matcher; 34 | import java.util.regex.Pattern; 35 | import java.util.regex.PatternSyntaxException; 36 | import jenkins.MasterToSlaveFileCallable; 37 | import jenkins.tasks.SimpleBuildStep; 38 | import org.apache.tools.ant.DirectoryScanner; 39 | import org.apache.tools.ant.Project; 40 | import org.apache.tools.ant.types.FileSet; 41 | import org.jenkinsci.Symbol; 42 | import org.kohsuke.accmod.Restricted; 43 | import org.kohsuke.accmod.restrictions.DoNotUse; 44 | import org.kohsuke.accmod.restrictions.NoExternalUse; 45 | import org.kohsuke.stapler.DataBoundConstructor; 46 | import org.kohsuke.stapler.DataBoundSetter; 47 | 48 | /** 49 | * Text Finder plugin for Jenkins. Search in the workspace using a regular expression and determine 50 | * build outcome based on matches. 51 | * 52 | * @author Santiago.PericasGeertsen@sun.com 53 | */ 54 | public class TextFinderPublisher extends Recorder implements Serializable, SimpleBuildStep { 55 | 56 | @NonNull 57 | private List textFinders; 58 | 59 | @Deprecated 60 | @Restricted(NoExternalUse.class) 61 | @SuppressFBWarnings(value = "PA_PUBLIC_PRIMITIVE_ATTRIBUTE", justification = "Preserve API compatibility.") 62 | public transient String fileSet; 63 | 64 | @Deprecated 65 | @Restricted(NoExternalUse.class) 66 | @SuppressFBWarnings(value = "PA_PUBLIC_PRIMITIVE_ATTRIBUTE", justification = "Preserve API compatibility.") 67 | public transient String regexp; 68 | 69 | @Deprecated 70 | @Restricted(NoExternalUse.class) 71 | @SuppressFBWarnings(value = "PA_PUBLIC_PRIMITIVE_ATTRIBUTE", justification = "Preserve API compatibility.") 72 | public transient boolean succeedIfFound; 73 | 74 | @Deprecated 75 | @Restricted(NoExternalUse.class) 76 | @SuppressFBWarnings(value = "PA_PUBLIC_PRIMITIVE_ATTRIBUTE", justification = "Preserve API compatibility.") 77 | public transient boolean unstableIfFound; 78 | 79 | @Deprecated 80 | @Restricted(NoExternalUse.class) 81 | @SuppressFBWarnings(value = "PA_PUBLIC_PRIMITIVE_ATTRIBUTE", justification = "Preserve API compatibility.") 82 | public transient boolean notBuiltIfFound; 83 | 84 | /** True to also scan the whole console output */ 85 | @Deprecated 86 | @Restricted(NoExternalUse.class) 87 | @SuppressFBWarnings(value = "PA_PUBLIC_PRIMITIVE_ATTRIBUTE", justification = "Preserve API compatibility.") 88 | public transient boolean alsoCheckConsoleOutput; 89 | 90 | /** Used only by Stapler in the snippetizer. */ 91 | @Deprecated 92 | @Restricted(DoNotUse.class) 93 | public transient String buildResult; 94 | 95 | @DataBoundConstructor 96 | public TextFinderPublisher() { 97 | textFinders = new ArrayList<>(); 98 | } 99 | 100 | @Deprecated 101 | @Restricted(NoExternalUse.class) 102 | public TextFinderPublisher(String regexp) { 103 | this(); 104 | textFinders.add(new TextFinder(regexp)); 105 | } 106 | 107 | @Deprecated 108 | @Restricted(NoExternalUse.class) 109 | public TextFinderPublisher( 110 | String fileSet, 111 | String regexp, 112 | boolean succeedIfFound, 113 | boolean unstableIfFound, 114 | boolean alsoCheckConsoleOutput) { 115 | this(regexp); 116 | setFileSet(fileSet); 117 | setSucceedIfFound(succeedIfFound); 118 | setUnstableIfFound(unstableIfFound); 119 | setAlsoCheckConsoleOutput(alsoCheckConsoleOutput); 120 | } 121 | 122 | @NonNull 123 | public List getTextFinders() { 124 | return textFinders; 125 | } 126 | 127 | @DataBoundSetter 128 | public void setTextFinders(List textFinders) { 129 | this.textFinders = textFinders != null ? new ArrayList<>(textFinders) : new ArrayList<>(); 130 | } 131 | 132 | @DataBoundSetter 133 | @Deprecated 134 | @Restricted(NoExternalUse.class) 135 | public void setRegexp(String regexp) { 136 | getFirst().setRegexp(regexp); 137 | } 138 | 139 | @DataBoundSetter 140 | @Deprecated 141 | @Restricted(NoExternalUse.class) 142 | public void setFileSet(String fileSet) { 143 | getFirst().setFileSet(fileSet); 144 | } 145 | 146 | @DataBoundSetter 147 | @Deprecated 148 | @Restricted(NoExternalUse.class) 149 | public void setBuildResult(String buildResult) { 150 | getFirst().setBuildResult(buildResult); 151 | } 152 | 153 | @DataBoundSetter 154 | @Deprecated 155 | @Restricted(NoExternalUse.class) 156 | public void setSucceedIfFound(boolean succeedIfFound) { 157 | if (succeedIfFound) { 158 | getFirst().setChangeCondition(TextFinderChangeCondition.MATCH_NOT_FOUND); 159 | } 160 | } 161 | 162 | @DataBoundSetter 163 | @Deprecated 164 | @Restricted(NoExternalUse.class) 165 | public void setUnstableIfFound(boolean unstableIfFound) { 166 | // Versions prior to 1.13 treated NOT_BUILT with higher precedence than UNSTABLE. For 167 | // compatibility, maintain the same behavior when migrating settings from these old 168 | // versions. 169 | if (unstableIfFound && !getFirst().getBuildResult().equals(Result.NOT_BUILT.toString())) { 170 | getFirst().setBuildResult(Result.UNSTABLE.toString()); 171 | } 172 | } 173 | 174 | @DataBoundSetter 175 | @Deprecated 176 | @Restricted(NoExternalUse.class) 177 | public void setNotBuiltIfFound(boolean notBuiltIfFound) { 178 | if (notBuiltIfFound) { 179 | getFirst().setBuildResult(Result.NOT_BUILT.toString()); 180 | } 181 | } 182 | 183 | @DataBoundSetter 184 | @Deprecated 185 | @Restricted(NoExternalUse.class) 186 | public void setAlsoCheckConsoleOutput(boolean alsoCheckConsoleOutput) { 187 | getFirst().setAlsoCheckConsoleOutput(alsoCheckConsoleOutput); 188 | } 189 | 190 | private TextFinder getFirst() { 191 | if (textFinders.isEmpty()) { 192 | // This is gross, but fortunately it is only used in a deprecated code path. 193 | TextFinder first = new TextFinder(""); 194 | textFinders.add(first); 195 | return first; 196 | } else { 197 | return textFinders.get(0); 198 | } 199 | } 200 | 201 | /** 202 | * Called by XStream after object construction 203 | * 204 | * @return modified object 205 | */ 206 | protected Object readResolve() { 207 | if (regexp != null) { 208 | setTextFinders(Collections.singletonList(new TextFinder(regexp))); 209 | regexp = null; 210 | } 211 | 212 | if (fileSet != null) { 213 | setFileSet(fileSet); 214 | fileSet = null; 215 | } 216 | 217 | if (succeedIfFound) { 218 | setSucceedIfFound(succeedIfFound); 219 | succeedIfFound = false; 220 | } 221 | 222 | if (unstableIfFound) { 223 | setUnstableIfFound(unstableIfFound); 224 | unstableIfFound = false; 225 | } 226 | 227 | if (notBuiltIfFound) { 228 | setNotBuiltIfFound(notBuiltIfFound); 229 | notBuiltIfFound = false; 230 | } 231 | 232 | if (alsoCheckConsoleOutput) { 233 | setAlsoCheckConsoleOutput(alsoCheckConsoleOutput); 234 | alsoCheckConsoleOutput = false; 235 | } 236 | 237 | return this; 238 | } 239 | 240 | @Override 241 | public BuildStepMonitor getRequiredMonitorService() { 242 | return BuildStepMonitor.NONE; 243 | } 244 | 245 | @Override 246 | public void perform( 247 | @NonNull Run run, 248 | @NonNull FilePath workspace, 249 | @NonNull Launcher launcher, 250 | @NonNull TaskListener listener) 251 | throws InterruptedException, IOException { 252 | for (TextFinder textFinder : textFinders) { 253 | findText(textFinder, run, workspace, listener); 254 | } 255 | } 256 | 257 | /** Indicates an orderly abortion of the processing. */ 258 | private static final class AbortException extends RuntimeException {} 259 | 260 | private static void findText(TextFinder textFinder, Run run, FilePath workspace, TaskListener listener) 261 | throws IOException, InterruptedException { 262 | try { 263 | PrintStream logger = listener.getLogger(); 264 | boolean foundText = false; 265 | 266 | if (textFinder.isAlsoCheckConsoleOutput()) { 267 | // Do not mention the pattern we are looking for to avoid false positives 268 | logger.println("[Text Finder] Searching console output..."); 269 | foundText = checkConsole(run, compilePattern(logger, textFinder.getRegexp()), logger); 270 | logger.println("[Text Finder] Finished searching for pattern '" 271 | + textFinder.getRegexp() 272 | + "' in console output."); 273 | } 274 | 275 | if (textFinder.getFileSet() != null) { 276 | logger.println("[Text Finder] Searching for pattern '" 277 | + textFinder.getRegexp() 278 | + "' in file set '" 279 | + textFinder.getFileSet() 280 | + "'..."); 281 | RemoteOutputStream ros = new RemoteOutputStream(logger); 282 | foundText |= workspace.act(new FileChecker(ros, textFinder.getFileSet(), textFinder.getRegexp())); 283 | logger.println("[Text Finder] Finished searching for pattern '" 284 | + textFinder.getRegexp() 285 | + "' in file set '" 286 | + textFinder.getFileSet() 287 | + "'."); 288 | } 289 | 290 | Result result = Result.SUCCESS; 291 | switch (textFinder.getChangeCondition()) { 292 | case MATCH_FOUND: 293 | if (foundText) { 294 | result = Result.fromString(textFinder.getBuildResult()); 295 | } 296 | break; 297 | case MATCH_NOT_FOUND: 298 | if (!foundText) { 299 | result = Result.fromString(textFinder.getBuildResult()); 300 | } 301 | break; 302 | default: 303 | throw new IllegalStateException("Unexpected value: " + textFinder.getChangeCondition()); 304 | } 305 | 306 | if (!result.equals(Result.SUCCESS)) { 307 | logger.println("[Text Finder] Setting build result to '" + result + "'."); 308 | run.setResult(result); 309 | } 310 | } catch (AbortException e) { 311 | // no test file found 312 | run.setResult(Result.UNSTABLE); 313 | } 314 | } 315 | 316 | /** 317 | * Search the given regexp pattern. 318 | * 319 | * @param isConsoleLog True if the reader represents a console log (as opposed to a file). 320 | */ 321 | private static boolean checkPattern( 322 | Reader r, Pattern pattern, PrintStream logger, String header, boolean isConsoleLog) throws IOException { 323 | boolean logFilename = true; 324 | boolean foundText = false; 325 | try (BufferedReader reader = new BufferedReader(r)) { 326 | // Assume default encoding and text files 327 | String line; 328 | while ((line = reader.readLine()) != null) { 329 | /* 330 | * Strip console logs of their console notes before searching; otherwise, we might 331 | * accidentally match the search string in the encoded console note. 332 | */ 333 | if (isConsoleLog) { 334 | line = ConsoleNote.removeNotes(line); 335 | } 336 | Matcher matcher = pattern.matcher(line); 337 | if (matcher.find()) { 338 | if (logFilename) { // first occurrence 339 | if (header != null) { 340 | logger.println(header); 341 | } 342 | logFilename = false; 343 | } 344 | logger.println(line); 345 | foundText = true; 346 | /* 347 | * When searching console output, return immediately as soon as the first hit is 348 | * found; otherwise, we'll loop forever. 349 | */ 350 | if (isConsoleLog) { 351 | return true; 352 | } 353 | } 354 | } 355 | } 356 | return foundText; 357 | } 358 | 359 | private static boolean checkConsole(Run build, Pattern pattern, PrintStream logger) { 360 | try (Reader r = build.getLogReader()) { 361 | return checkPattern(r, pattern, logger, null, true); 362 | } catch (IOException e) { 363 | logger.println("[Text Finder] Error reading console output -- ignoring"); 364 | Functions.printStackTrace(e, logger); 365 | } 366 | 367 | return false; 368 | } 369 | 370 | private static boolean checkFile(File f, Pattern pattern, PrintStream logger, Charset charset) { 371 | try (InputStream is = new FileInputStream(f); 372 | Reader r = new InputStreamReader(is, charset)) { 373 | return checkPattern(r, pattern, logger, f + ":", false); 374 | } catch (IOException e) { 375 | logger.println("[Text Finder] Error reading file '" + f + "' -- ignoring"); 376 | Functions.printStackTrace(e, logger); 377 | } 378 | 379 | return false; 380 | } 381 | 382 | private static Pattern compilePattern(PrintStream logger, String regexp) { 383 | Pattern pattern; 384 | try { 385 | pattern = Pattern.compile(regexp); 386 | } catch (PatternSyntaxException e) { 387 | logger.println("[Text Finder] Unable to compile regular expression '" + regexp + "'"); 388 | throw new AbortException(); 389 | } 390 | return pattern; 391 | } 392 | 393 | @Symbol("findText") 394 | @Extension 395 | public static final class DescriptorImpl extends BuildStepDescriptor { 396 | @NonNull 397 | @Override 398 | public String getDisplayName() { 399 | return Messages.TextFinderPublisher_DisplayName(); 400 | } 401 | 402 | @Override 403 | public String getHelpFile() { 404 | return "/plugin/text-finder/help.html"; 405 | } 406 | 407 | @Override 408 | public boolean isApplicable(Class jobType) { 409 | return true; 410 | } 411 | } 412 | 413 | private static class FileChecker extends MasterToSlaveFileCallable { 414 | 415 | private final RemoteOutputStream ros; 416 | private final String fileSet; 417 | private final String regexp; 418 | 419 | public FileChecker(RemoteOutputStream ros, String fileSet, String regexp) { 420 | this.ros = ros; 421 | this.fileSet = fileSet; 422 | this.regexp = regexp; 423 | } 424 | 425 | @Override 426 | public Boolean invoke(File ws, VirtualChannel channel) throws IOException { 427 | PrintStream logger = 428 | new PrintStream(ros, true, Charset.defaultCharset().toString()); 429 | 430 | // Collect list of files for searching 431 | FileSet fs = new FileSet(); 432 | Project p = new Project(); 433 | fs.setProject(p); 434 | fs.setDir(ws); 435 | fs.setIncludes(fileSet); 436 | DirectoryScanner ds = fs.getDirectoryScanner(p); 437 | 438 | // Any files in the final set? 439 | String[] files = ds.getIncludedFiles(); 440 | if (files.length == 0) { 441 | logger.println("[Text Finder] File set '" + fileSet + "' is empty"); 442 | throw new AbortException(); 443 | } 444 | 445 | Pattern pattern = compilePattern(logger, regexp); 446 | 447 | boolean foundText = false; 448 | 449 | for (String file : files) { 450 | File f = new File(ws, file); 451 | 452 | if (!f.exists()) { 453 | logger.println("[Text Finder] Unable to find file '" + f + "'"); 454 | continue; 455 | } 456 | 457 | if (!f.canRead()) { 458 | logger.println("[Text Finder] Unable to read from file '" + f + "'"); 459 | continue; 460 | } 461 | 462 | foundText |= checkFile(f, pattern, logger, Charset.defaultCharset()); 463 | } 464 | 465 | return foundText; 466 | } 467 | } 468 | 469 | private static final long serialVersionUID = 1L; 470 | } 471 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import hudson.model.Result; 4 | import java.io.File; 5 | import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; 6 | import org.jenkinsci.plugins.workflow.job.WorkflowJob; 7 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; 8 | import org.junit.Rule; 9 | import org.junit.Test; 10 | import org.jvnet.hudson.test.JenkinsRule; 11 | 12 | public class TextFinderPublisherPipelineTest { 13 | 14 | @Rule 15 | public JenkinsRule rule = new JenkinsRule(); 16 | 17 | @Test 18 | public void successIfFoundInFile() throws Exception { 19 | WorkflowJob project = rule.createProject(WorkflowJob.class); 20 | project.setDefinition(new CpsFlowDefinition( 21 | "node {\n" 22 | + " writeFile file: '" 23 | + TestUtils.FILE_SET 24 | + "', text: '" 25 | + TestUtils.UNIQUE_TEXT 26 | + "'\n" 27 | + " findText(textFinders: [textFinder(regexp: '" 28 | + TestUtils.UNIQUE_TEXT 29 | + "', fileSet: '" 30 | + TestUtils.FILE_SET 31 | + "', buildResult: 'SUCCESS')])\n" 32 | + "}\n", 33 | true)); 34 | WorkflowRun build = rule.buildAndAssertSuccess(project); 35 | rule.assertLogContains( 36 | "[Text Finder] Searching for pattern '" 37 | + TestUtils.UNIQUE_TEXT 38 | + "' in file set '" 39 | + TestUtils.FILE_SET 40 | + "'.", 41 | build); 42 | TestUtils.assertFileContainsMatch( 43 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 44 | rule.assertLogContains( 45 | "[Text Finder] Finished searching for pattern '" 46 | + TestUtils.UNIQUE_TEXT 47 | + "' in file set '" 48 | + TestUtils.FILE_SET 49 | + "'.", 50 | build); 51 | } 52 | 53 | @Test 54 | public void failureIfFoundInFile() throws Exception { 55 | WorkflowJob project = rule.createProject(WorkflowJob.class); 56 | project.setDefinition(new CpsFlowDefinition( 57 | "node {\n" 58 | + " writeFile file: '" 59 | + TestUtils.FILE_SET 60 | + "', text: '" 61 | + TestUtils.UNIQUE_TEXT 62 | + "'\n" 63 | + " findText(textFinders: [textFinder(regexp: '" 64 | + TestUtils.UNIQUE_TEXT 65 | + "', fileSet: '" 66 | + TestUtils.FILE_SET 67 | + "')])\n" 68 | + "}\n", 69 | true)); 70 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 71 | rule.assertLogContains( 72 | "[Text Finder] Searching for pattern '" 73 | + TestUtils.UNIQUE_TEXT 74 | + "' in file set '" 75 | + TestUtils.FILE_SET 76 | + "'.", 77 | build); 78 | TestUtils.assertFileContainsMatch( 79 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 80 | rule.assertLogContains( 81 | "[Text Finder] Finished searching for pattern '" 82 | + TestUtils.UNIQUE_TEXT 83 | + "' in file set '" 84 | + TestUtils.FILE_SET 85 | + "'.", 86 | build); 87 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 88 | } 89 | 90 | @Test 91 | public void unstableIfFoundInFile() throws Exception { 92 | WorkflowJob project = rule.createProject(WorkflowJob.class); 93 | project.setDefinition(new CpsFlowDefinition( 94 | "node {\n" 95 | + " writeFile file: '" 96 | + TestUtils.FILE_SET 97 | + "', text: '" 98 | + TestUtils.UNIQUE_TEXT 99 | + "'\n" 100 | + " findText(textFinders: [textFinder(regexp: '" 101 | + TestUtils.UNIQUE_TEXT 102 | + "', fileSet: '" 103 | + TestUtils.FILE_SET 104 | + "', buildResult: 'UNSTABLE')])\n" 105 | + "}\n", 106 | true)); 107 | WorkflowRun build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 108 | rule.assertLogContains( 109 | "[Text Finder] Searching for pattern '" 110 | + TestUtils.UNIQUE_TEXT 111 | + "' in file set '" 112 | + TestUtils.FILE_SET 113 | + "'.", 114 | build); 115 | TestUtils.assertFileContainsMatch( 116 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 117 | rule.assertLogContains( 118 | "[Text Finder] Finished searching for pattern '" 119 | + TestUtils.UNIQUE_TEXT 120 | + "' in file set '" 121 | + TestUtils.FILE_SET 122 | + "'.", 123 | build); 124 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 125 | } 126 | 127 | @Test 128 | public void notBuiltIfFoundInFile() throws Exception { 129 | WorkflowJob project = rule.createProject(WorkflowJob.class); 130 | project.setDefinition(new CpsFlowDefinition( 131 | "node {\n" 132 | + " writeFile file: '" 133 | + TestUtils.FILE_SET 134 | + "', text: '" 135 | + TestUtils.UNIQUE_TEXT 136 | + "'\n" 137 | + " findText(textFinders: [textFinder(regexp: '" 138 | + TestUtils.UNIQUE_TEXT 139 | + "', fileSet: '" 140 | + TestUtils.FILE_SET 141 | + "', buildResult: 'NOT_BUILT')])\n" 142 | + "}\n", 143 | true)); 144 | WorkflowRun build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 145 | rule.assertLogContains( 146 | "[Text Finder] Searching for pattern '" 147 | + TestUtils.UNIQUE_TEXT 148 | + "' in file set '" 149 | + TestUtils.FILE_SET 150 | + "'.", 151 | build); 152 | TestUtils.assertFileContainsMatch( 153 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 154 | rule.assertLogContains( 155 | "[Text Finder] Finished searching for pattern '" 156 | + TestUtils.UNIQUE_TEXT 157 | + "' in file set '" 158 | + TestUtils.FILE_SET 159 | + "'.", 160 | build); 161 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 162 | } 163 | 164 | @Test 165 | public void abortedIfFoundInFile() throws Exception { 166 | WorkflowJob project = rule.createProject(WorkflowJob.class); 167 | project.setDefinition(new CpsFlowDefinition( 168 | "node {\n" 169 | + " writeFile file: '" 170 | + TestUtils.FILE_SET 171 | + "', text: '" 172 | + TestUtils.UNIQUE_TEXT 173 | + "'\n" 174 | + " findText(textFinders: [textFinder(regexp: '" 175 | + TestUtils.UNIQUE_TEXT 176 | + "', fileSet: '" 177 | + TestUtils.FILE_SET 178 | + "', buildResult: 'ABORTED')])\n" 179 | + "}\n", 180 | true)); 181 | WorkflowRun build = rule.buildAndAssertStatus(Result.ABORTED, project); 182 | rule.assertLogContains( 183 | "[Text Finder] Searching for pattern '" 184 | + TestUtils.UNIQUE_TEXT 185 | + "' in file set '" 186 | + TestUtils.FILE_SET 187 | + "'.", 188 | build); 189 | TestUtils.assertFileContainsMatch( 190 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 191 | rule.assertLogContains( 192 | "[Text Finder] Finished searching for pattern '" 193 | + TestUtils.UNIQUE_TEXT 194 | + "' in file set '" 195 | + TestUtils.FILE_SET 196 | + "'.", 197 | build); 198 | rule.assertLogContains("Setting build result to 'ABORTED'.", build); 199 | } 200 | 201 | @Test 202 | public void successIfNotFoundInFile() throws Exception { 203 | WorkflowJob project = rule.createProject(WorkflowJob.class); 204 | project.setDefinition(new CpsFlowDefinition( 205 | "node {\n" 206 | + " writeFile file: '" 207 | + TestUtils.FILE_SET 208 | + "', text: 'foobaz'\n" 209 | + " findText(textFinders: [textFinder(regexp: '" 210 | + TestUtils.UNIQUE_TEXT 211 | + "', fileSet: '" 212 | + TestUtils.FILE_SET 213 | + "')])\n" 214 | + "}\n", 215 | true)); 216 | WorkflowRun build = rule.buildAndAssertSuccess(project); 217 | rule.assertLogContains( 218 | "[Text Finder] Searching for pattern '" 219 | + TestUtils.UNIQUE_TEXT 220 | + "' in file set '" 221 | + TestUtils.FILE_SET 222 | + "'.", 223 | build); 224 | rule.assertLogContains( 225 | "[Text Finder] Finished searching for pattern '" 226 | + TestUtils.UNIQUE_TEXT 227 | + "' in file set '" 228 | + TestUtils.FILE_SET 229 | + "'.", 230 | build); 231 | } 232 | 233 | @Test 234 | public void failureIfNotFoundInFile() throws Exception { 235 | WorkflowJob project = rule.createProject(WorkflowJob.class); 236 | project.setDefinition(new CpsFlowDefinition( 237 | "node {\n" 238 | + " writeFile file: '" 239 | + TestUtils.FILE_SET 240 | + "', text: 'foobaz'\n" 241 | + " findText(textFinders: [textFinder(regexp: '" 242 | + TestUtils.UNIQUE_TEXT 243 | + "', fileSet: '" 244 | + TestUtils.FILE_SET 245 | + "', changeCondition: 'MATCH_NOT_FOUND')])\n" 246 | + "}\n", 247 | true)); 248 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 249 | rule.assertLogContains( 250 | "[Text Finder] Searching for pattern '" 251 | + TestUtils.UNIQUE_TEXT 252 | + "' in file set '" 253 | + TestUtils.FILE_SET 254 | + "'.", 255 | build); 256 | rule.assertLogContains( 257 | "[Text Finder] Finished searching for pattern '" 258 | + TestUtils.UNIQUE_TEXT 259 | + "' in file set '" 260 | + TestUtils.FILE_SET 261 | + "'.", 262 | build); 263 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 264 | } 265 | 266 | @Test 267 | public void multipleTextFindersInFile() throws Exception { 268 | WorkflowJob project = rule.createProject(WorkflowJob.class); 269 | project.setDefinition(new CpsFlowDefinition( 270 | "node {\n" 271 | + " writeFile file: '" 272 | + TestUtils.FILE_SET 273 | + "', text: '" 274 | + TestUtils.UNIQUE_TEXT 275 | + "'\n" 276 | + " findText(textFinders: [textFinder(regexp: '" 277 | + TestUtils.UNIQUE_TEXT 278 | + "', fileSet: '" 279 | + TestUtils.FILE_SET 280 | + "', buildResult: 'SUCCESS'),\n" 281 | + " textFinder(regexp: '" 282 | + TestUtils.UNIQUE_TEXT 283 | + "', fileSet: '" 284 | + TestUtils.FILE_SET 285 | + "'),\n" 286 | + " textFinder(regexp: '" 287 | + TestUtils.UNIQUE_TEXT 288 | + "', fileSet: '" 289 | + TestUtils.FILE_SET 290 | + "', buildResult: 'UNSTABLE')])\n" 291 | + "}\n", 292 | true)); 293 | 294 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 295 | rule.assertLogContains( 296 | "[Text Finder] Searching for pattern '" 297 | + TestUtils.UNIQUE_TEXT 298 | + "' in file set '" 299 | + TestUtils.FILE_SET 300 | + "'.", 301 | build); 302 | TestUtils.assertFileContainsMatch( 303 | new File(TestUtils.getWorkspace(build), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 304 | rule.assertLogContains( 305 | "[Text Finder] Finished searching for pattern '" 306 | + TestUtils.UNIQUE_TEXT 307 | + "' in file set '" 308 | + TestUtils.FILE_SET 309 | + "'.", 310 | build); 311 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 312 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 313 | } 314 | 315 | @Test 316 | public void successIfFoundInConsole() throws Exception { 317 | WorkflowJob project = rule.createProject(WorkflowJob.class); 318 | project.setDefinition(new CpsFlowDefinition( 319 | " testEcho '" 320 | + TestUtils.UNIQUE_TEXT 321 | + "'\n" 322 | + "node {\n" 323 | + " findText(textFinders: [textFinder(regexp: '" 324 | + TestUtils.UNIQUE_TEXT 325 | + "', buildResult: 'SUCCESS', alsoCheckConsoleOutput: true)])\n" 326 | + "}\n", 327 | true)); 328 | WorkflowRun build = rule.buildAndAssertSuccess(project); 329 | rule.assertLogContains("[Text Finder] Searching console output...", build); 330 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 331 | rule.assertLogContains( 332 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 333 | } 334 | 335 | @Test 336 | public void failureIfFoundInConsole() throws Exception { 337 | WorkflowJob project = rule.createProject(WorkflowJob.class); 338 | project.setDefinition(new CpsFlowDefinition( 339 | " testEcho '" 340 | + TestUtils.UNIQUE_TEXT 341 | + "'\n" 342 | + "node {\n" 343 | + " findText(textFinders: [textFinder(regexp: '" 344 | + TestUtils.UNIQUE_TEXT 345 | + "', alsoCheckConsoleOutput: true)])\n" 346 | + "}\n", 347 | true)); 348 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 349 | rule.assertLogContains("[Text Finder] Searching console output...", build); 350 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 351 | rule.assertLogContains( 352 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 353 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 354 | } 355 | 356 | @Test 357 | public void unstableIfFoundInConsole() throws Exception { 358 | WorkflowJob project = rule.createProject(WorkflowJob.class); 359 | project.setDefinition(new CpsFlowDefinition( 360 | " testEcho '" 361 | + TestUtils.UNIQUE_TEXT 362 | + "'\n" 363 | + "node {\n" 364 | + " findText(textFinders: [textFinder(regexp: '" 365 | + TestUtils.UNIQUE_TEXT 366 | + "', buildResult: 'UNSTABLE', alsoCheckConsoleOutput: true)])\n" 367 | + "}\n", 368 | true)); 369 | WorkflowRun build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 370 | rule.assertLogContains("[Text Finder] Searching console output...", build); 371 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 372 | rule.assertLogContains( 373 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 374 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 375 | } 376 | 377 | @Test 378 | public void notBuiltIfFoundInConsole() throws Exception { 379 | WorkflowJob project = rule.createProject(WorkflowJob.class); 380 | project.setDefinition(new CpsFlowDefinition( 381 | " testEcho '" 382 | + TestUtils.UNIQUE_TEXT 383 | + "'\n" 384 | + "node {\n" 385 | + " findText(textFinders: [textFinder(regexp: '" 386 | + TestUtils.UNIQUE_TEXT 387 | + "', buildResult: 'NOT_BUILT', alsoCheckConsoleOutput: true)])\n" 388 | + "}\n", 389 | true)); 390 | WorkflowRun build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 391 | rule.assertLogContains("[Text Finder] Searching console output...", build); 392 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 393 | rule.assertLogContains( 394 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 395 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 396 | } 397 | 398 | @Test 399 | public void abortedBuiltIfFoundInConsole() throws Exception { 400 | WorkflowJob project = rule.createProject(WorkflowJob.class); 401 | project.setDefinition(new CpsFlowDefinition( 402 | " testEcho '" 403 | + TestUtils.UNIQUE_TEXT 404 | + "'\n" 405 | + "node {\n" 406 | + " findText(textFinders: [textFinder(regexp: '" 407 | + TestUtils.UNIQUE_TEXT 408 | + "', buildResult: 'ABORTED', alsoCheckConsoleOutput: true)])\n" 409 | + "}\n", 410 | true)); 411 | WorkflowRun build = rule.buildAndAssertStatus(Result.ABORTED, project); 412 | rule.assertLogContains("[Text Finder] Searching console output...", build); 413 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 414 | rule.assertLogContains( 415 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 416 | rule.assertLogContains("Setting build result to 'ABORTED'.", build); 417 | } 418 | 419 | @Test 420 | public void successIfNotFoundInConsole() throws Exception { 421 | WorkflowJob project = rule.createProject(WorkflowJob.class); 422 | project.setDefinition(new CpsFlowDefinition( 423 | "node {\n" 424 | + " findText(textFinders: [textFinder(regexp: '" 425 | + TestUtils.UNIQUE_TEXT 426 | + "', alsoCheckConsoleOutput: true)])\n" 427 | + "}\n", 428 | true)); 429 | WorkflowRun build = rule.buildAndAssertSuccess(project); 430 | rule.assertLogContains("[Text Finder] Searching console output...", build); 431 | rule.assertLogContains( 432 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 433 | } 434 | 435 | @Test 436 | public void failureIfNotFoundInConsole() throws Exception { 437 | WorkflowJob project = rule.createProject(WorkflowJob.class); 438 | project.setDefinition(new CpsFlowDefinition( 439 | "node {\n" 440 | + " findText(textFinders: [textFinder(regexp: '" 441 | + TestUtils.UNIQUE_TEXT 442 | + "', alsoCheckConsoleOutput: true, changeCondition:" 443 | + " 'MATCH_NOT_FOUND')])\n" 444 | + "}\n", 445 | true)); 446 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 447 | rule.assertLogContains("[Text Finder] Searching console output...", build); 448 | rule.assertLogContains( 449 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 450 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 451 | } 452 | 453 | @Test 454 | public void multipleTextFindersInConsole() throws Exception { 455 | WorkflowJob project = rule.createProject(WorkflowJob.class); 456 | project.setDefinition(new CpsFlowDefinition( 457 | " testEcho '" 458 | + TestUtils.UNIQUE_TEXT 459 | + "'\n" 460 | + "node {\n" 461 | + " findText(textFinders: [textFinder(regexp: '" 462 | + TestUtils.UNIQUE_TEXT 463 | + "', buildResult: 'SUCCESS', alsoCheckConsoleOutput: true),\n" 464 | + " textFinder(regexp: '" 465 | + TestUtils.UNIQUE_TEXT 466 | + "', alsoCheckConsoleOutput: true),\n" 467 | + " textFinder(regexp: '" 468 | + TestUtils.UNIQUE_TEXT 469 | + "', buildResult: 'UNSTABLE', alsoCheckConsoleOutput: true)])\n" 470 | + "}\n", 471 | true)); 472 | WorkflowRun build = rule.buildAndAssertStatus(Result.FAILURE, project); 473 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 474 | rule.assertLogContains("[Text Finder] Searching console output...", build); 475 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 476 | rule.assertLogContains( 477 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 478 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 479 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 480 | } 481 | } 482 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import hudson.model.FreeStyleBuild; 7 | import hudson.model.FreeStyleProject; 8 | import hudson.model.Result; 9 | import hudson.plugins.textfinder.test.TestEchoBuilder; 10 | import hudson.plugins.textfinder.test.TestWriteFileBuilder; 11 | import hudson.util.VersionNumber; 12 | import java.io.File; 13 | import java.util.Arrays; 14 | import java.util.Collections; 15 | import jenkins.model.Jenkins; 16 | import org.htmlunit.WebClientUtil; 17 | import org.htmlunit.html.HtmlForm; 18 | import org.htmlunit.html.HtmlPage; 19 | import org.junit.Rule; 20 | import org.junit.Test; 21 | import org.jvnet.hudson.test.JenkinsRule; 22 | 23 | public class TextFinderPublisherFreestyleTest { 24 | 25 | @Rule 26 | public JenkinsRule rule = new JenkinsRule(); 27 | 28 | @Test 29 | public void successIfFoundInFile() throws Exception { 30 | FreeStyleProject project = rule.createFreeStyleProject(); 31 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 32 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 33 | textFinder.setFileSet(TestUtils.FILE_SET); 34 | textFinder.setBuildResult(Result.SUCCESS.toString()); 35 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 36 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 37 | project.getPublishersList().add(textFinderPublisher); 38 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 39 | rule.assertLogContains( 40 | "[Text Finder] Searching for pattern '" 41 | + TestUtils.UNIQUE_TEXT 42 | + "' in file set '" 43 | + TestUtils.FILE_SET 44 | + "'.", 45 | build); 46 | TestUtils.assertFileContainsMatch( 47 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 48 | rule.assertLogContains( 49 | "[Text Finder] Finished searching for pattern '" 50 | + TestUtils.UNIQUE_TEXT 51 | + "' in file set '" 52 | + TestUtils.FILE_SET 53 | + "'.", 54 | build); 55 | } 56 | 57 | @Test 58 | public void failureIfFoundInFile() throws Exception { 59 | FreeStyleProject project = rule.createFreeStyleProject(); 60 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 61 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 62 | textFinder.setFileSet(TestUtils.FILE_SET); 63 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 64 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 65 | project.getPublishersList().add(textFinderPublisher); 66 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 67 | rule.assertLogContains( 68 | "[Text Finder] Searching for pattern '" 69 | + TestUtils.UNIQUE_TEXT 70 | + "' in file set '" 71 | + TestUtils.FILE_SET 72 | + "'.", 73 | build); 74 | TestUtils.assertFileContainsMatch( 75 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 76 | rule.assertLogContains( 77 | "[Text Finder] Finished searching for pattern '" 78 | + TestUtils.UNIQUE_TEXT 79 | + "' in file set '" 80 | + TestUtils.FILE_SET 81 | + "'.", 82 | build); 83 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 84 | } 85 | 86 | @Test 87 | public void unstableIfFoundInFile() throws Exception { 88 | FreeStyleProject project = rule.createFreeStyleProject(); 89 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 90 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 91 | textFinder.setFileSet(TestUtils.FILE_SET); 92 | textFinder.setBuildResult(Result.UNSTABLE.toString()); 93 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 94 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 95 | project.getPublishersList().add(textFinderPublisher); 96 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 97 | rule.assertLogContains( 98 | "[Text Finder] Searching for pattern '" 99 | + TestUtils.UNIQUE_TEXT 100 | + "' in file set '" 101 | + TestUtils.FILE_SET 102 | + "'.", 103 | build); 104 | TestUtils.assertFileContainsMatch( 105 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 106 | rule.assertLogContains( 107 | "[Text Finder] Finished searching for pattern '" 108 | + TestUtils.UNIQUE_TEXT 109 | + "' in file set '" 110 | + TestUtils.FILE_SET 111 | + "'.", 112 | build); 113 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 114 | } 115 | 116 | @Test 117 | public void notBuiltIfFoundInFile() throws Exception { 118 | FreeStyleProject project = rule.createFreeStyleProject(); 119 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 120 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 121 | textFinder.setFileSet(TestUtils.FILE_SET); 122 | textFinder.setBuildResult(Result.NOT_BUILT.toString()); 123 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 124 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 125 | project.getPublishersList().add(textFinderPublisher); 126 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 127 | rule.assertLogContains( 128 | "[Text Finder] Searching for pattern '" 129 | + TestUtils.UNIQUE_TEXT 130 | + "' in file set '" 131 | + TestUtils.FILE_SET 132 | + "'.", 133 | build); 134 | TestUtils.assertFileContainsMatch( 135 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 136 | rule.assertLogContains( 137 | "[Text Finder] Finished searching for pattern '" 138 | + TestUtils.UNIQUE_TEXT 139 | + "' in file set '" 140 | + TestUtils.FILE_SET 141 | + "'.", 142 | build); 143 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 144 | } 145 | 146 | @Test 147 | public void abortedIfFoundInFile() throws Exception { 148 | FreeStyleProject project = rule.createFreeStyleProject(); 149 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 150 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 151 | textFinder.setFileSet(TestUtils.FILE_SET); 152 | textFinder.setBuildResult(Result.ABORTED.toString()); 153 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 154 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 155 | project.getPublishersList().add(textFinderPublisher); 156 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.ABORTED, project); 157 | rule.assertLogContains( 158 | "[Text Finder] Searching for pattern '" 159 | + TestUtils.UNIQUE_TEXT 160 | + "' in file set '" 161 | + TestUtils.FILE_SET 162 | + "'.", 163 | build); 164 | TestUtils.assertFileContainsMatch( 165 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 166 | rule.assertLogContains( 167 | "[Text Finder] Finished searching for pattern '" 168 | + TestUtils.UNIQUE_TEXT 169 | + "' in file set '" 170 | + TestUtils.FILE_SET 171 | + "'.", 172 | build); 173 | rule.assertLogContains("Setting build result to 'ABORTED'.", build); 174 | } 175 | 176 | @Test 177 | public void successIfNotFoundInFile() throws Exception { 178 | FreeStyleProject project = rule.createFreeStyleProject(); 179 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, "foobaz")); 180 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 181 | textFinder.setFileSet(TestUtils.FILE_SET); 182 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 183 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 184 | project.getPublishersList().add(textFinderPublisher); 185 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 186 | rule.assertLogContains( 187 | "[Text Finder] Searching for pattern '" 188 | + TestUtils.UNIQUE_TEXT 189 | + "' in file set '" 190 | + TestUtils.FILE_SET 191 | + "'.", 192 | build); 193 | rule.assertLogContains( 194 | "[Text Finder] Finished searching for pattern '" 195 | + TestUtils.UNIQUE_TEXT 196 | + "' in file set '" 197 | + TestUtils.FILE_SET 198 | + "'.", 199 | build); 200 | } 201 | 202 | @Test 203 | public void failureIfNotFoundInFile() throws Exception { 204 | FreeStyleProject project = rule.createFreeStyleProject(); 205 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, "foobaz")); 206 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 207 | textFinder.setFileSet(TestUtils.FILE_SET); 208 | textFinder.setChangeCondition(TextFinderChangeCondition.MATCH_NOT_FOUND); 209 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 210 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 211 | project.getPublishersList().add(textFinderPublisher); 212 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 213 | rule.assertLogContains( 214 | "[Text Finder] Searching for pattern '" 215 | + TestUtils.UNIQUE_TEXT 216 | + "' in file set '" 217 | + TestUtils.FILE_SET 218 | + "'.", 219 | build); 220 | rule.assertLogContains( 221 | "[Text Finder] Finished searching for pattern '" 222 | + TestUtils.UNIQUE_TEXT 223 | + "' in file set '" 224 | + TestUtils.FILE_SET 225 | + "'.", 226 | build); 227 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 228 | } 229 | 230 | @Test 231 | public void multipleTextFindersInFile() throws Exception { 232 | FreeStyleProject project = rule.createFreeStyleProject(); 233 | project.getBuildersList().add(new TestWriteFileBuilder(TestUtils.FILE_SET, TestUtils.UNIQUE_TEXT)); 234 | TextFinder tf1 = new TextFinder(TestUtils.UNIQUE_TEXT); 235 | tf1.setFileSet(TestUtils.FILE_SET); 236 | tf1.setBuildResult(Result.SUCCESS.toString()); 237 | TextFinder tf2 = new TextFinder(TestUtils.UNIQUE_TEXT); 238 | tf2.setFileSet(TestUtils.FILE_SET); 239 | TextFinder tf3 = new TextFinder(TestUtils.UNIQUE_TEXT); 240 | tf3.setFileSet(TestUtils.FILE_SET); 241 | tf3.setBuildResult(Result.UNSTABLE.toString()); 242 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 243 | textFinderPublisher.setTextFinders(Arrays.asList(tf1, tf2, tf3)); 244 | project.getPublishersList().add(textFinderPublisher); 245 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 246 | rule.assertLogContains( 247 | "[Text Finder] Searching for pattern '" 248 | + TestUtils.UNIQUE_TEXT 249 | + "' in file set '" 250 | + TestUtils.FILE_SET 251 | + "'.", 252 | build); 253 | TestUtils.assertFileContainsMatch( 254 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 255 | rule.assertLogContains( 256 | "[Text Finder] Finished searching for pattern '" 257 | + TestUtils.UNIQUE_TEXT 258 | + "' in file set '" 259 | + TestUtils.FILE_SET 260 | + "'.", 261 | build); 262 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 263 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 264 | } 265 | 266 | @Test 267 | public void successIfFoundInConsole() throws Exception { 268 | FreeStyleProject project = rule.createFreeStyleProject(); 269 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 270 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 271 | textFinder.setBuildResult(Result.SUCCESS.toString()); 272 | textFinder.setAlsoCheckConsoleOutput(true); 273 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 274 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 275 | project.getPublishersList().add(textFinderPublisher); 276 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 277 | rule.assertLogContains("[Text Finder] Searching console output...", build); 278 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 279 | rule.assertLogContains( 280 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 281 | } 282 | 283 | @Test 284 | public void failureIfFoundInConsole() throws Exception { 285 | FreeStyleProject project = rule.createFreeStyleProject(); 286 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 287 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 288 | textFinder.setAlsoCheckConsoleOutput(true); 289 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 290 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 291 | project.getPublishersList().add(textFinderPublisher); 292 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 293 | rule.assertLogContains("[Text Finder] Searching console output...", build); 294 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 295 | rule.assertLogContains( 296 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 297 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 298 | } 299 | 300 | @Test 301 | public void unstableIfFoundInConsole() throws Exception { 302 | FreeStyleProject project = rule.createFreeStyleProject(); 303 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 304 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 305 | textFinder.setBuildResult(Result.UNSTABLE.toString()); 306 | textFinder.setAlsoCheckConsoleOutput(true); 307 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 308 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 309 | project.getPublishersList().add(textFinderPublisher); 310 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 311 | rule.assertLogContains("[Text Finder] Searching console output...", build); 312 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 313 | rule.assertLogContains( 314 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 315 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 316 | } 317 | 318 | @Test 319 | public void notBuiltIfFoundInConsole() throws Exception { 320 | FreeStyleProject project = rule.createFreeStyleProject(); 321 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 322 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 323 | textFinder.setBuildResult(Result.NOT_BUILT.toString()); 324 | textFinder.setAlsoCheckConsoleOutput(true); 325 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 326 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 327 | project.getPublishersList().add(textFinderPublisher); 328 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 329 | rule.assertLogContains("[Text Finder] Searching console output...", build); 330 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 331 | rule.assertLogContains( 332 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 333 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 334 | } 335 | 336 | @Test 337 | public void abortedIfFoundInConsole() throws Exception { 338 | FreeStyleProject project = rule.createFreeStyleProject(); 339 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 340 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 341 | textFinder.setBuildResult(Result.ABORTED.toString()); 342 | textFinder.setAlsoCheckConsoleOutput(true); 343 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 344 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 345 | project.getPublishersList().add(textFinderPublisher); 346 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.ABORTED, project); 347 | rule.assertLogContains("[Text Finder] Searching console output...", build); 348 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 349 | rule.assertLogContains( 350 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 351 | rule.assertLogContains("Setting build result to 'ABORTED'.", build); 352 | } 353 | 354 | @Test 355 | public void successIfNotFoundInConsole() throws Exception { 356 | FreeStyleProject project = rule.createFreeStyleProject(); 357 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 358 | textFinder.setAlsoCheckConsoleOutput(true); 359 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 360 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 361 | project.getPublishersList().add(textFinderPublisher); 362 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 363 | rule.assertLogContains("[Text Finder] Searching console output...", build); 364 | rule.assertLogContains( 365 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 366 | } 367 | 368 | @Test 369 | public void failureIfNotFoundInConsole() throws Exception { 370 | FreeStyleProject project = rule.createFreeStyleProject(); 371 | TextFinder textFinder = new TextFinder(TestUtils.UNIQUE_TEXT); 372 | textFinder.setAlsoCheckConsoleOutput(true); 373 | textFinder.setChangeCondition(TextFinderChangeCondition.MATCH_NOT_FOUND); 374 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 375 | textFinderPublisher.setTextFinders(Collections.singletonList(textFinder)); 376 | project.getPublishersList().add(textFinderPublisher); 377 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 378 | rule.assertLogContains("[Text Finder] Searching console output...", build); 379 | rule.assertLogContains( 380 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 381 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 382 | } 383 | 384 | @Test 385 | public void multipleTextFindersInConsole() throws Exception { 386 | FreeStyleProject project = rule.createFreeStyleProject(); 387 | project.getBuildersList().add(new TestEchoBuilder(TestUtils.UNIQUE_TEXT)); 388 | TextFinder tf1 = new TextFinder(TestUtils.UNIQUE_TEXT); 389 | tf1.setAlsoCheckConsoleOutput(true); 390 | tf1.setBuildResult(Result.SUCCESS.toString()); 391 | TextFinder tf2 = new TextFinder(TestUtils.UNIQUE_TEXT); 392 | tf2.setAlsoCheckConsoleOutput(true); 393 | TextFinder tf3 = new TextFinder(TestUtils.UNIQUE_TEXT); 394 | tf3.setAlsoCheckConsoleOutput(true); 395 | tf3.setBuildResult(Result.UNSTABLE.toString()); 396 | TextFinderPublisher textFinderPublisher = new TextFinderPublisher(); 397 | textFinderPublisher.setTextFinders(Arrays.asList(tf1, tf2, tf3)); 398 | project.getPublishersList().add(textFinderPublisher); 399 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 400 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 401 | rule.assertLogContains("[Text Finder] Searching console output...", build); 402 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 403 | rule.assertLogContains( 404 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 405 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 406 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 407 | } 408 | 409 | @Test 410 | public void createTextFinderViaWebClient() throws Exception { 411 | FreeStyleProject project = rule.createFreeStyleProject(); 412 | assertEquals(0, project.getPublishersList().size()); 413 | 414 | // Go to the "Configure" page. 415 | JenkinsRule.WebClient webClient = rule.createWebClient(); 416 | HtmlPage page = webClient.goTo(project.getUrl() + "/configure"); 417 | 418 | // Add a Text Finder. 419 | HtmlForm config = page.getFormByName("config"); 420 | rule.getButtonByCaption(config, "Add post-build action").click(); 421 | if (Jenkins.getVersion().isOlderThan(new VersionNumber("2.422"))) { 422 | page.getAnchorByText(Messages.TextFinderPublisher_DisplayName()).click(); 423 | } else { 424 | rule.getButtonByCaption(config, Messages.TextFinderPublisher_DisplayName()) 425 | .click(); 426 | } 427 | 428 | // Wait for the YUI JavaScript to load. 429 | WebClientUtil.waitForJSExec(page.getWebClient()); 430 | 431 | // Configure the Text Finder. 432 | config.getInputByName("_.fileSet").setValue("file1"); 433 | config.getInputByName("_.regexp").setValue(TestUtils.UNIQUE_TEXT); 434 | config.getSelectByName("_.buildResult").setSelectedAttribute(Result.UNSTABLE.toString(), true); 435 | config.getInputByName("_.alsoCheckConsoleOutput").click(); 436 | 437 | // Submit the page. 438 | rule.submit(config); 439 | 440 | // Ensure that the Text Finder was configured correctly. 441 | assertEquals(1, project.getPublishersList().size()); 442 | TextFinderPublisher textFinderPublisher = 443 | (TextFinderPublisher) project.getPublishersList().get(0); 444 | TextFinder textFinder = textFinderPublisher.getTextFinders().get(0); 445 | assertEquals("file1", textFinder.getFileSet()); 446 | assertEquals(TestUtils.UNIQUE_TEXT, textFinder.getRegexp()); 447 | assertEquals(Result.UNSTABLE.toString(), textFinder.getBuildResult()); 448 | assertTrue(textFinder.isAlsoCheckConsoleOutput()); 449 | } 450 | } 451 | -------------------------------------------------------------------------------- /src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleJobDslTest.java: -------------------------------------------------------------------------------- 1 | package hudson.plugins.textfinder; 2 | 3 | import hudson.model.FreeStyleBuild; 4 | import hudson.model.FreeStyleProject; 5 | import hudson.model.Result; 6 | import java.io.File; 7 | import javaposse.jobdsl.plugin.ExecuteDslScripts; 8 | import org.junit.Rule; 9 | import org.junit.Test; 10 | import org.jvnet.hudson.test.JenkinsRule; 11 | 12 | public class TextFinderPublisherFreestyleJobDslTest { 13 | 14 | @Rule 15 | public JenkinsRule rule = new JenkinsRule(); 16 | 17 | @Test 18 | public void successIfFoundInFile() throws Exception { 19 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 20 | + " testWriteFileBuilder {\n" 21 | + " file '" 22 | + TestUtils.FILE_SET 23 | + "'\n" 24 | + " text '" 25 | + TestUtils.UNIQUE_TEXT 26 | + "'\n" 27 | + " }\n" 28 | + " }\n" 29 | + " publishers {\n" 30 | + " findText {\n" 31 | + " textFinders {\n" 32 | + " textFinder {\n" 33 | + " regexp '" 34 | + TestUtils.UNIQUE_TEXT 35 | + "'\n" 36 | + " fileSet '" 37 | + TestUtils.FILE_SET 38 | + "'\n" 39 | + " buildResult 'SUCCESS'\n" 40 | + " }\n" 41 | + " }\n" 42 | + " }\n" 43 | + " }\n"); 44 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 45 | rule.assertLogContains( 46 | "[Text Finder] Searching for pattern '" 47 | + TestUtils.UNIQUE_TEXT 48 | + "' in file set '" 49 | + TestUtils.FILE_SET 50 | + "'.", 51 | build); 52 | TestUtils.assertFileContainsMatch( 53 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 54 | rule.assertLogContains( 55 | "[Text Finder] Finished searching for pattern '" 56 | + TestUtils.UNIQUE_TEXT 57 | + "' in file set '" 58 | + TestUtils.FILE_SET 59 | + "'.", 60 | build); 61 | } 62 | 63 | @Test 64 | public void failureIfFoundInFile() throws Exception { 65 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 66 | + " testWriteFileBuilder {\n" 67 | + " file '" 68 | + TestUtils.FILE_SET 69 | + "'\n" 70 | + " text '" 71 | + TestUtils.UNIQUE_TEXT 72 | + "'\n" 73 | + " }\n" 74 | + " }\n" 75 | + " publishers {\n" 76 | + " findText {\n" 77 | + " textFinders {\n" 78 | + " textFinder {\n" 79 | + " regexp '" 80 | + TestUtils.UNIQUE_TEXT 81 | + "'\n" 82 | + " fileSet '" 83 | + TestUtils.FILE_SET 84 | + "'\n" 85 | + " }\n" 86 | + " }\n" 87 | + " }\n" 88 | + " }\n"); 89 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 90 | rule.assertLogContains( 91 | "[Text Finder] Searching for pattern '" 92 | + TestUtils.UNIQUE_TEXT 93 | + "' in file set '" 94 | + TestUtils.FILE_SET 95 | + "'.", 96 | build); 97 | TestUtils.assertFileContainsMatch( 98 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 99 | rule.assertLogContains( 100 | "[Text Finder] Finished searching for pattern '" 101 | + TestUtils.UNIQUE_TEXT 102 | + "' in file set '" 103 | + TestUtils.FILE_SET 104 | + "'.", 105 | build); 106 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 107 | } 108 | 109 | @Test 110 | public void unstableIfFoundInFile() throws Exception { 111 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 112 | + " testWriteFileBuilder {\n" 113 | + " file '" 114 | + TestUtils.FILE_SET 115 | + "'\n" 116 | + " text '" 117 | + TestUtils.UNIQUE_TEXT 118 | + "'\n" 119 | + " }\n" 120 | + " }\n" 121 | + " publishers {\n" 122 | + " findText {\n" 123 | + " textFinders {\n" 124 | + " textFinder {\n" 125 | + " regexp '" 126 | + TestUtils.UNIQUE_TEXT 127 | + "'\n" 128 | + " fileSet '" 129 | + TestUtils.FILE_SET 130 | + "'\n" 131 | + " buildResult 'UNSTABLE'\n" 132 | + " }\n" 133 | + " }\n" 134 | + " }\n" 135 | + " }\n"); 136 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 137 | rule.assertLogContains( 138 | "[Text Finder] Searching for pattern '" 139 | + TestUtils.UNIQUE_TEXT 140 | + "' in file set '" 141 | + TestUtils.FILE_SET 142 | + "'.", 143 | build); 144 | TestUtils.assertFileContainsMatch( 145 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 146 | rule.assertLogContains( 147 | "[Text Finder] Finished searching for pattern '" 148 | + TestUtils.UNIQUE_TEXT 149 | + "' in file set '" 150 | + TestUtils.FILE_SET 151 | + "'.", 152 | build); 153 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 154 | } 155 | 156 | @Test 157 | public void notBuiltIfFoundInFile() throws Exception { 158 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 159 | + " testWriteFileBuilder {\n" 160 | + " file '" 161 | + TestUtils.FILE_SET 162 | + "'\n" 163 | + " text '" 164 | + TestUtils.UNIQUE_TEXT 165 | + "'\n" 166 | + " }\n" 167 | + " }\n" 168 | + " publishers {\n" 169 | + " findText {\n" 170 | + " textFinders {\n" 171 | + " textFinder {\n" 172 | + " regexp '" 173 | + TestUtils.UNIQUE_TEXT 174 | + "'\n" 175 | + " fileSet '" 176 | + TestUtils.FILE_SET 177 | + "'\n" 178 | + " buildResult 'NOT_BUILT'\n" 179 | + " }\n" 180 | + " }\n" 181 | + " }\n" 182 | + " }\n"); 183 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 184 | rule.assertLogContains( 185 | "[Text Finder] Searching for pattern '" 186 | + TestUtils.UNIQUE_TEXT 187 | + "' in file set '" 188 | + TestUtils.FILE_SET 189 | + "'.", 190 | build); 191 | TestUtils.assertFileContainsMatch( 192 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 193 | rule.assertLogContains( 194 | "[Text Finder] Finished searching for pattern '" 195 | + TestUtils.UNIQUE_TEXT 196 | + "' in file set '" 197 | + TestUtils.FILE_SET 198 | + "'.", 199 | build); 200 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 201 | } 202 | 203 | @Test 204 | public void abortedIfFoundInFile() throws Exception { 205 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 206 | + " testWriteFileBuilder {\n" 207 | + " file '" 208 | + TestUtils.FILE_SET 209 | + "'\n" 210 | + " text '" 211 | + TestUtils.UNIQUE_TEXT 212 | + "'\n" 213 | + " }\n" 214 | + " }\n" 215 | + " publishers {\n" 216 | + " findText {\n" 217 | + " textFinders {\n" 218 | + " textFinder {\n" 219 | + " regexp '" 220 | + TestUtils.UNIQUE_TEXT 221 | + "'\n" 222 | + " fileSet '" 223 | + TestUtils.FILE_SET 224 | + "'\n" 225 | + " buildResult 'ABORTED'\n" 226 | + " }\n" 227 | + " }\n" 228 | + " }\n" 229 | + " }\n"); 230 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.ABORTED, project); 231 | rule.assertLogContains( 232 | "[Text Finder] Searching for pattern '" 233 | + TestUtils.UNIQUE_TEXT 234 | + "' in file set '" 235 | + TestUtils.FILE_SET 236 | + "'.", 237 | build); 238 | TestUtils.assertFileContainsMatch( 239 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 240 | rule.assertLogContains( 241 | "[Text Finder] Finished searching for pattern '" 242 | + TestUtils.UNIQUE_TEXT 243 | + "' in file set '" 244 | + TestUtils.FILE_SET 245 | + "'.", 246 | build); 247 | rule.assertLogContains("Setting build result to 'ABORTED'.", build); 248 | } 249 | 250 | @Test 251 | public void multipleTextFindersInFile() throws Exception { 252 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 253 | + " testWriteFileBuilder {\n" 254 | + " file '" 255 | + TestUtils.FILE_SET 256 | + "'\n" 257 | + " text '" 258 | + TestUtils.UNIQUE_TEXT 259 | + "'\n" 260 | + " }\n" 261 | + " }\n" 262 | + " publishers {\n" 263 | + " findText {\n" 264 | + " textFinders {\n" 265 | + " textFinder {\n" 266 | + " regexp '" 267 | + TestUtils.UNIQUE_TEXT 268 | + "'\n" 269 | + " fileSet '" 270 | + TestUtils.FILE_SET 271 | + "'\n" 272 | + " buildResult 'SUCCESS'\n" 273 | + " }\n" 274 | + " textFinder {\n" 275 | + " regexp '" 276 | + TestUtils.UNIQUE_TEXT 277 | + "'\n" 278 | + " fileSet '" 279 | + TestUtils.FILE_SET 280 | + "'\n" 281 | + " }\n" 282 | + " textFinder {\n" 283 | + " regexp '" 284 | + TestUtils.UNIQUE_TEXT 285 | + "'\n" 286 | + " fileSet '" 287 | + TestUtils.FILE_SET 288 | + "'\n" 289 | + " buildResult 'UNSTABLE'\n" 290 | + " }\n" 291 | + " }\n" 292 | + " }\n" 293 | + " }\n"); 294 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 295 | rule.assertLogContains( 296 | "[Text Finder] Searching for pattern '" 297 | + TestUtils.UNIQUE_TEXT 298 | + "' in file set '" 299 | + TestUtils.FILE_SET 300 | + "'.", 301 | build); 302 | TestUtils.assertFileContainsMatch( 303 | new File(build.getWorkspace().getRemote(), TestUtils.FILE_SET), TestUtils.UNIQUE_TEXT, rule, build); 304 | rule.assertLogContains( 305 | "[Text Finder] Finished searching for pattern '" 306 | + TestUtils.UNIQUE_TEXT 307 | + "' in file set '" 308 | + TestUtils.FILE_SET 309 | + "'.", 310 | build); 311 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 312 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 313 | } 314 | 315 | @Test 316 | public void successIfNotFoundInFile() throws Exception { 317 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 318 | + " testWriteFileBuilder {\n" 319 | + " file '" 320 | + TestUtils.FILE_SET 321 | + "'\n" 322 | + " text 'foobaz'\n" 323 | + " }\n" 324 | + " }\n" 325 | + " publishers {\n" 326 | + " findText {\n" 327 | + " textFinders {\n" 328 | + " textFinder {\n" 329 | + " regexp '" 330 | + TestUtils.UNIQUE_TEXT 331 | + "'\n" 332 | + " fileSet '" 333 | + TestUtils.FILE_SET 334 | + "'\n" 335 | + " }\n" 336 | + " }\n" 337 | + " }\n" 338 | + " }\n"); 339 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 340 | rule.assertLogContains( 341 | "[Text Finder] Searching for pattern '" 342 | + TestUtils.UNIQUE_TEXT 343 | + "' in file set '" 344 | + TestUtils.FILE_SET 345 | + "'.", 346 | build); 347 | rule.assertLogContains( 348 | "[Text Finder] Finished searching for pattern '" 349 | + TestUtils.UNIQUE_TEXT 350 | + "' in file set '" 351 | + TestUtils.FILE_SET 352 | + "'.", 353 | build); 354 | } 355 | 356 | @Test 357 | public void failureIfNotFoundInFile() throws Exception { 358 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 359 | + " testWriteFileBuilder {\n" 360 | + " file '" 361 | + TestUtils.FILE_SET 362 | + "'\n" 363 | + " text 'foobaz'\n" 364 | + " }\n" 365 | + " }\n" 366 | + " publishers {\n" 367 | + " findText {\n" 368 | + " textFinders {\n" 369 | + " textFinder {\n" 370 | + " regexp '" 371 | + TestUtils.UNIQUE_TEXT 372 | + "'\n" 373 | + " fileSet '" 374 | + TestUtils.FILE_SET 375 | + "'\n" 376 | + " changeCondition 'MATCH_NOT_FOUND'\n" 377 | + " }\n" 378 | + " }\n" 379 | + " }\n" 380 | + " }\n"); 381 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 382 | rule.assertLogContains( 383 | "[Text Finder] Searching for pattern '" 384 | + TestUtils.UNIQUE_TEXT 385 | + "' in file set '" 386 | + TestUtils.FILE_SET 387 | + "'.", 388 | build); 389 | rule.assertLogContains( 390 | "[Text Finder] Finished searching for pattern '" 391 | + TestUtils.UNIQUE_TEXT 392 | + "' in file set '" 393 | + TestUtils.FILE_SET 394 | + "'.", 395 | build); 396 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 397 | } 398 | 399 | @Test 400 | public void successIfFoundInConsole() throws Exception { 401 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 402 | + " testEchoBuilder {\n" 403 | + " message '" 404 | + TestUtils.UNIQUE_TEXT 405 | + "'\n" 406 | + " }\n" 407 | + " }\n" 408 | + " publishers {\n" 409 | + " findText {\n" 410 | + " textFinders {\n" 411 | + " textFinder {\n" 412 | + " regexp '" 413 | + TestUtils.UNIQUE_TEXT 414 | + "'\n" 415 | + " buildResult 'SUCCESS'\n" 416 | + " alsoCheckConsoleOutput true\n" 417 | + " }\n" 418 | + " }\n" 419 | + " }\n" 420 | + " }\n"); 421 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 422 | rule.assertLogContains("[Text Finder] Searching console output...", build); 423 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 424 | rule.assertLogContains( 425 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 426 | } 427 | 428 | @Test 429 | public void failureIfFoundInConsole() throws Exception { 430 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 431 | + " testEchoBuilder {\n" 432 | + " message '" 433 | + TestUtils.UNIQUE_TEXT 434 | + "'\n" 435 | + " }\n" 436 | + " }\n" 437 | + " publishers {\n" 438 | + " findText {\n" 439 | + " textFinders {\n" 440 | + " textFinder {\n" 441 | + " regexp '" 442 | + TestUtils.UNIQUE_TEXT 443 | + "'\n" 444 | + " alsoCheckConsoleOutput true\n" 445 | + " }\n" 446 | + " }\n" 447 | + " }\n" 448 | + " }\n"); 449 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 450 | rule.assertLogContains("[Text Finder] Searching console output...", build); 451 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 452 | rule.assertLogContains( 453 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 454 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 455 | } 456 | 457 | @Test 458 | public void unstableIfFoundInConsole() throws Exception { 459 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 460 | + " testEchoBuilder {\n" 461 | + " message '" 462 | + TestUtils.UNIQUE_TEXT 463 | + "'\n" 464 | + " }\n" 465 | + " }\n" 466 | + " publishers {\n" 467 | + " findText {\n" 468 | + " textFinders {\n" 469 | + " textFinder {\n" 470 | + " regexp '" 471 | + TestUtils.UNIQUE_TEXT 472 | + "'\n" 473 | + " buildResult 'UNSTABLE'\n" 474 | + " alsoCheckConsoleOutput true\n" 475 | + " }\n" 476 | + " }\n" 477 | + " }\n" 478 | + " }\n"); 479 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.UNSTABLE, project); 480 | rule.assertLogContains("[Text Finder] Searching console output...", build); 481 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 482 | rule.assertLogContains( 483 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 484 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 485 | } 486 | 487 | @Test 488 | public void notBuiltIfFoundInConsole() throws Exception { 489 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 490 | + " testEchoBuilder {\n" 491 | + " message '" 492 | + TestUtils.UNIQUE_TEXT 493 | + "'\n" 494 | + " }\n" 495 | + " }\n" 496 | + " publishers {\n" 497 | + " findText {\n" 498 | + " textFinders {\n" 499 | + " textFinder {\n" 500 | + " regexp '" 501 | + TestUtils.UNIQUE_TEXT 502 | + "'\n" 503 | + " buildResult 'NOT_BUILT'\n" 504 | + " alsoCheckConsoleOutput true\n" 505 | + " }\n" 506 | + " }\n" 507 | + " }\n" 508 | + " }\n"); 509 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.NOT_BUILT, project); 510 | rule.assertLogContains("[Text Finder] Searching console output...", build); 511 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 512 | rule.assertLogContains( 513 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 514 | rule.assertLogContains("Setting build result to 'NOT_BUILT'.", build); 515 | } 516 | 517 | @Test 518 | public void abortedIfFoundInConsole() throws Exception { 519 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 520 | + " testEchoBuilder {\n" 521 | + " message '" 522 | + TestUtils.UNIQUE_TEXT 523 | + "'\n" 524 | + " }\n" 525 | + " }\n" 526 | + " publishers {\n" 527 | + " findText {\n" 528 | + " textFinders {\n" 529 | + " textFinder {\n" 530 | + " regexp '" 531 | + TestUtils.UNIQUE_TEXT 532 | + "'\n" 533 | + " buildResult 'ABORTED'\n" 534 | + " alsoCheckConsoleOutput true\n" 535 | + " }\n" 536 | + " }\n" 537 | + " }\n" 538 | + " }\n"); 539 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.ABORTED, project); 540 | rule.assertLogContains("[Text Finder] Searching console output...", build); 541 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 542 | rule.assertLogContains( 543 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 544 | rule.assertLogContains("Setting build result to 'ABORTED'.", build); 545 | } 546 | 547 | @Test 548 | public void successIfNotFoundInConsole() throws Exception { 549 | FreeStyleProject project = createProjectFromDsl(" publishers {\n" 550 | + " findText {\n" 551 | + " textFinders {\n" 552 | + " textFinder {\n" 553 | + " regexp '" 554 | + TestUtils.UNIQUE_TEXT 555 | + "'\n" 556 | + " alsoCheckConsoleOutput true\n" 557 | + " }\n" 558 | + " }\n" 559 | + " }\n" 560 | + " }\n"); 561 | FreeStyleBuild build = rule.buildAndAssertSuccess(project); 562 | rule.assertLogContains("[Text Finder] Searching console output...", build); 563 | rule.assertLogContains( 564 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 565 | } 566 | 567 | @Test 568 | public void failureIfNotFoundInConsole() throws Exception { 569 | FreeStyleProject project = createProjectFromDsl(" publishers {\n" 570 | + " findText {\n" 571 | + " textFinders {\n" 572 | + " textFinder {\n" 573 | + " regexp '" 574 | + TestUtils.UNIQUE_TEXT 575 | + "'\n" 576 | + " alsoCheckConsoleOutput true\n" 577 | + " changeCondition 'MATCH_NOT_FOUND'\n" 578 | + " }\n" 579 | + " }\n" 580 | + " }\n" 581 | + " }\n"); 582 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 583 | rule.assertLogContains("[Text Finder] Searching console output...", build); 584 | rule.assertLogContains( 585 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 586 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 587 | } 588 | 589 | @Test 590 | public void multipleTextFindersInConsole() throws Exception { 591 | FreeStyleProject project = createProjectFromDsl(" steps {\n" 592 | + " testEchoBuilder {\n" 593 | + " message '" 594 | + TestUtils.UNIQUE_TEXT 595 | + "'\n" 596 | + " }\n" 597 | + " }\n" 598 | + " publishers {\n" 599 | + " findText {\n" 600 | + " textFinders {\n" 601 | + " textFinder {\n" 602 | + " regexp '" 603 | + TestUtils.UNIQUE_TEXT 604 | + "'\n" 605 | + " buildResult 'SUCCESS'\n" 606 | + " alsoCheckConsoleOutput true\n" 607 | + " }\n" 608 | + " textFinder {\n" 609 | + " regexp '" 610 | + TestUtils.UNIQUE_TEXT 611 | + "'\n" 612 | + " alsoCheckConsoleOutput true\n" 613 | + " }\n" 614 | + " textFinder {\n" 615 | + " regexp '" 616 | + TestUtils.UNIQUE_TEXT 617 | + "'\n" 618 | + " buildResult 'UNSTABLE'\n" 619 | + " alsoCheckConsoleOutput true\n" 620 | + " }\n" 621 | + " }\n" 622 | + " }\n" 623 | + " }\n"); 624 | FreeStyleBuild build = rule.buildAndAssertStatus(Result.FAILURE, project); 625 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 626 | rule.assertLogContains("[Text Finder] Searching console output...", build); 627 | rule.assertLogContains(TestUtils.PREFIX + TestUtils.UNIQUE_TEXT, build); 628 | rule.assertLogContains( 629 | "Finished searching for pattern '" + TestUtils.UNIQUE_TEXT + "' in console output.", build); 630 | rule.assertLogContains("Setting build result to 'FAILURE'.", build); 631 | rule.assertLogContains("Setting build result to 'UNSTABLE'.", build); 632 | } 633 | 634 | private FreeStyleProject createProjectFromDsl(String dsl) throws Exception { 635 | FreeStyleProject seed = rule.createFreeStyleProject(); 636 | ExecuteDslScripts executeDslScripts = new ExecuteDslScripts(); 637 | String generatedJobName = "test" + rule.jenkins.getItems().size(); 638 | executeDslScripts.setScriptText("job('" + generatedJobName + "') {\n" + dsl + "}\n"); 639 | executeDslScripts.setUseScriptText(true); 640 | seed.getBuildersList().add(executeDslScripts); 641 | rule.buildAndAssertSuccess(seed); 642 | return rule.jenkins.getItemByFullName(generatedJobName, FreeStyleProject.class); 643 | } 644 | } 645 | --------------------------------------------------------------------------------