Skip to content

Commit

Permalink
Add completed() matcher to check whether a build has completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe committed Nov 26, 2024
1 parent a2c598a commit b46b3de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/main/java/jenkins/test/RunMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ 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;
Expand Down Expand Up @@ -116,4 +123,16 @@ 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");
}
}
}
3 changes: 2 additions & 1 deletion src/test/java/jenkins/test/RunMatchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
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;
Expand All @@ -49,7 +50,7 @@ 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), isSuccessful());
assertThat(j.waitForCompletion(b), allOf(completed(), isSuccessful()));
}

@Test
Expand Down

0 comments on commit b46b3de

Please sign in to comment.