-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #878 from Vlatombe/runMatchers
- Loading branch information
Showing
3 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package jenkins.test; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.model.Result; | ||
import hudson.model.Run; | ||
import java.io.IOException; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
/** | ||
* Matchers for {@link Run} objects. | ||
*/ | ||
public final class RunMatchers { | ||
private RunMatchers() {} | ||
|
||
/** | ||
* Creates a matcher checking whether a build is successful. | ||
*/ | ||
public static Matcher<Run<?,?>> isSuccessful() { | ||
return new RunResultMatcher(Result.SUCCESS); | ||
} | ||
|
||
/** | ||
* Creates a matcher checking whether a build has a specific outcome. | ||
*/ | ||
public static Matcher<Run<?,?>> hasStatus(Result result) { | ||
return new RunResultMatcher(result); | ||
} | ||
|
||
/** | ||
* Creates a matcher checking whether build logs contain a specific message. | ||
* @param message the expected message | ||
*/ | ||
public static Matcher<Run<?,?>> logContains(String message) { | ||
return new RunLogMatcher(message); | ||
} | ||
|
||
/** | ||
* Creates a matcher checking whether a build has completed. | ||
*/ | ||
public static Matcher<Run<?,?>> completed() { | ||
return new CompletedRunMatcher(); | ||
} | ||
|
||
private static class RunResultMatcher extends TypeSafeMatcher<Run<?,?>> { | ||
@NonNull | ||
private final Result expectedResult; | ||
|
||
public RunResultMatcher(@NonNull Result expectedResult) { | ||
this.expectedResult = expectedResult; | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("a build with result " + expectedResult); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(Run run) { | ||
return run.getResult() == expectedResult; | ||
} | ||
|
||
@Override | ||
protected void describeMismatchSafely(Run<?, ?> item, Description mismatchDescription) { | ||
mismatchDescription.appendText("was ").appendValue(item.getResult()); | ||
} | ||
} | ||
|
||
private static class RunLogMatcher extends TypeSafeMatcher<Run<?, ?>> { | ||
@NonNull | ||
private final String message; | ||
|
||
private RunLogMatcher(@NonNull String message) { | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(Run<?, ?> run) { | ||
try { | ||
return JenkinsRule.getLog(run).contains(message); | ||
} catch (IOException x) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
protected void describeMismatchSafely(Run<?, ?> item, Description mismatchDescription) { | ||
mismatchDescription.appendText("was \n"); | ||
try { | ||
mismatchDescription.appendText(JenkinsRule.getLog(item)); | ||
} catch (IOException e) { | ||
mismatchDescription.appendText("<unreadable>"); | ||
} | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("log containing ").appendValue(message); | ||
} | ||
} | ||
|
||
private static class CompletedRunMatcher extends TypeSafeMatcher<Run<?, ?>> { | ||
@Override | ||
protected boolean matchesSafely(Run<?, ?> run) { | ||
return !run.isLogUpdated(); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("a completed build"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package jenkins.test; | ||
|
||
import static jenkins.test.RunMatchers.completed; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.not; | ||
import static jenkins.test.RunMatchers.logContains; | ||
import static jenkins.test.RunMatchers.hasStatus; | ||
import static jenkins.test.RunMatchers.isSuccessful; | ||
|
||
import hudson.Functions; | ||
import hudson.model.Result; | ||
import hudson.tasks.BatchFile; | ||
import hudson.tasks.Shell; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.FailureBuilder; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.SleepBuilder; | ||
|
||
public class RunMatchersTest { | ||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
public void buildSuccessful() throws Exception { | ||
var p = j.createFreeStyleProject(); | ||
p.getBuildersList().add(new SleepBuilder(1000)); | ||
var b = p.scheduleBuild2(0).waitForStart(); | ||
assertThat(j.waitForCompletion(b), allOf(completed(), isSuccessful())); | ||
} | ||
|
||
@Test | ||
public void buildFailure() throws Exception { | ||
var p = j.createFreeStyleProject(); | ||
p.getBuildersList().add(new FailureBuilder()); | ||
var b = p.scheduleBuild2(0).waitForStart(); | ||
assertThat(j.waitForCompletion(b), hasStatus(Result.FAILURE)); | ||
} | ||
|
||
@Test | ||
public void assertThatLogContains() throws Exception { | ||
var p = j.createFreeStyleProject(); | ||
p.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo hello") : new Shell("echo hello")); | ||
var b = p.scheduleBuild2(0).get(); | ||
System.out.println(b.getDisplayName() + " completed"); | ||
assertThat(b, allOf(logContains("echo hello"), not(logContains("echo bye")))); | ||
} | ||
} |