-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add capturing checks publisher, make publishChecks withChecksName awa…
…re (#55) * Add capturing checks publisher, make publishChecks withChecksName aware * Move CapturingChecksPublisher to test root, add to test-jar, add docs * Add link to example usage of capturing publisher. * Improve test assertions, name logic
- Loading branch information
1 parent
3ca4e19
commit 3a298a6
Showing
6 changed files
with
203 additions
and
4 deletions.
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
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
75 changes: 75 additions & 0 deletions
75
src/test/java/io/jenkins/plugins/checks/api/test/CapturingChecksPublisher.java
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,75 @@ | ||
package io.jenkins.plugins.checks.api.test; | ||
|
||
import hudson.ExtensionList; | ||
import hudson.model.Job; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import io.jenkins.plugins.checks.api.ChecksDetails; | ||
import io.jenkins.plugins.checks.api.ChecksPublisher; | ||
import io.jenkins.plugins.checks.api.ChecksPublisherFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Implementation of {@link ChecksPublisher} for use in testing, that records each captured checks in a simple list. | ||
* | ||
* For example: | ||
* | ||
* <pre> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mrginglymus
Author
Contributor
|
||
* public class ChecksPublishingTest extends IntegrationTestWithJenkinsPerTest { | ||
* | ||
* @TestExtension | ||
* public static final CapturingChecksPublisher.Factory PUBLISHER_FACTORY = new CapturingChecksPublisher.Factory(); | ||
* | ||
* @After | ||
* public void clearPublishedChecks() { | ||
* PUBLISHER_FACTORY.getPublishedChecks().clear(); | ||
* } | ||
* | ||
* @Test | ||
* public void testChecksPublishing() { | ||
* | ||
* // ...Run a test job... | ||
* | ||
* List<ChecksDetails> publishedDetails = PUBLISHER_FACTORY.getPublishedChecks(); | ||
* | ||
* // ...Inspect published checks... | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* An example of this can be found in {@link io.jenkins.plugins.checks.steps.PublishChecksStepITest} | ||
*/ | ||
public class CapturingChecksPublisher extends ChecksPublisher { | ||
|
||
private final List<ChecksDetails> publishedChecks = new ArrayList<>(); | ||
|
||
@Override | ||
public void publish(final ChecksDetails details) { | ||
publishedChecks.add(details); | ||
} | ||
|
||
/** | ||
* Implementation of {@link ChecksPublisherFactory} that returns a {@link CapturingChecksPublisher}. | ||
*/ | ||
public static class Factory extends ChecksPublisherFactory { | ||
|
||
private final CapturingChecksPublisher publisher = new CapturingChecksPublisher(); | ||
|
||
@Override | ||
protected Optional<ChecksPublisher> createPublisher(final Run<?, ?> run, final TaskListener listener) { | ||
return Optional.of(publisher); | ||
} | ||
|
||
@Override | ||
protected Optional<ChecksPublisher> createPublisher(final Job<?, ?> job, final TaskListener listener) { | ||
return Optional.of(publisher); | ||
} | ||
|
||
public List<ChecksDetails> getPublishedChecks() { | ||
return ExtensionList.lookup(Factory.class).get(0).publisher.publishedChecks; | ||
} | ||
} | ||
} |
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
Maybe this is a little bit late, but if you would use
<pre>{@code
(see https://reflectoring.io/howto-format-code-snippets-in-javadoc/#pre--code) then GitHub will render those comments correctly.