-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add capturing checks publisher, make publishChecks withChecksName aware #55
Changes from 1 commit
1c2a731
8889928
9feee18
60c6dd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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> | ||
* public class ChecksPublishingTest { | ||
* @TestExtension | ||
* public static final CapturingChecksPublisher.Factory PUBLISHER_FACTORY = new CapturingChecksPublisher.Factory(); | ||
* | ||
* @After | ||
* public void clearPublishedChecks() { | ||
* PUBLISHER_FACTORY.getPublishedChecks().clear(); | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
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; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Provides default Findbugs annotations. | ||
*/ | ||
@DefaultAnnotation(NonNull.class) | ||
package io.jenkins.plugins.checks.api.test; | ||
|
||
import edu.umd.cs.findbugs.annotations.DefaultAnnotation; | ||
import edu.umd.cs.findbugs.annotations.NonNull; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,23 +1,37 @@ | ||||||
package io.jenkins.plugins.checks.steps; | ||||||
|
||||||
import io.jenkins.plugins.checks.api.ChecksConclusion; | ||||||
import io.jenkins.plugins.checks.api.ChecksDetails; | ||||||
import io.jenkins.plugins.checks.api.ChecksStatus; | ||||||
import io.jenkins.plugins.checks.api.test.CapturingChecksPublisher; | ||||||
import io.jenkins.plugins.util.IntegrationTestWithJenkinsPerTest; | ||||||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||||||
import org.junit.Test; | ||||||
import org.jvnet.hudson.test.JenkinsRule; | ||||||
import org.jvnet.hudson.test.TestExtension; | ||||||
|
||||||
import java.io.IOException; | ||||||
|
||||||
import static org.assertj.core.api.Assertions.assertThat; | ||||||
import static org.mockito.ArgumentMatchers.eq; | ||||||
|
||||||
/** | ||||||
* Tests the pipeline step to publish checks. | ||||||
*/ | ||||||
public class PublishChecksStepITest extends IntegrationTestWithJenkinsPerTest { | ||||||
|
||||||
/** | ||||||
* Provide a {@link CapturingChecksPublisher} to check published checks on each test. | ||||||
*/ | ||||||
@TestExtension | ||||||
public static final CapturingChecksPublisher.Factory PUBLISHER_FACTORY = new CapturingChecksPublisher.Factory(); | ||||||
|
||||||
/** | ||||||
* Tests that the step "publishChecks" can be used in pipeline script. | ||||||
* | ||||||
* @throws IOException if fails get log from run | ||||||
*/ | ||||||
@SuppressWarnings("OptionalGetWithoutIsPresent") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just extract the output to a variable and chuck and orElseThrow instead of .get There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'll be a lot of |
||||||
@Test | ||||||
public void shouldPublishChecksWhenUsingPipeline() throws IOException { | ||||||
WorkflowJob job = createPipeline(); | ||||||
|
@@ -26,7 +40,17 @@ public void shouldPublishChecksWhenUsingPipeline() throws IOException { | |||||
+ "text: 'Pipeline support for checks', status: 'IN_PROGRESS', conclusion: 'NONE'")); | ||||||
|
||||||
assertThat(JenkinsRule.getLog(buildSuccessfully(job))) | ||||||
.contains("[Pipeline] publishChecks") | ||||||
.contains("[Checks API] No suitable checks publisher found."); | ||||||
.contains("[Pipeline] publishChecks"); | ||||||
|
||||||
assertThat(PUBLISHER_FACTORY.getPublishedChecks().size()).isEqualTo(1); | ||||||
|
||||||
ChecksDetails details = PUBLISHER_FACTORY.getPublishedChecks().get(0); | ||||||
|
||||||
assertThat(details.getName().get()).isEqualTo("customized-check"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typically, one would use in AssertJ:
Suggested change
This will also not require the annotation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, neat! Thanks. |
||||||
assertThat(details.getOutput().get().getTitle().get()).isEqualTo("Publish Checks Step"); | ||||||
assertThat(details.getOutput().get().getSummary().get()).isEqualTo("customized check created in pipeline"); | ||||||
assertThat(details.getOutput().get().getText().get()).isEqualTo("Pipeline support for checks"); | ||||||
assertThat(details.getStatus()).isEqualTo(ChecksStatus.IN_PROGRESS); | ||||||
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.NONE); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better grammar