Skip to content

Commit d753e03

Browse files
arnaudsjsCopilot
andauthored
Added support for the skipMarkingStageUnstable config option (#1241)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent cb3245e commit d753e03

7 files changed

Lines changed: 69 additions & 6 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ The JUnit publisher is configured at the job level by adding a Publish JUnit tes
3737
results to corresponding SCM hosting platforms. If not, a default of "Test" will be used.
3838
* **Skip marking build unstable:** If this option is unchecked, then the plugin will mark the build as unstable when it finds at least 1 test failure.
3939
If this option is checked, then the build will still be successful even if there are test failures reported.
40-
In any case, the corresponding pipeline node (and stage) will be marked as unstable in case of test failure.
40+
The corresponding pipeline node (and stage) will still be marked as unstable in case of test failure, unless `skipMarkingStageUnstable` is also set.
4141
In order to enable this, set the property:
4242
`skipMarkingBuildUnstable` to `true`:
4343

4444
junit skipMarkingBuildUnstable: true, testResults: 'test-results.xml'
45+
* **Skip marking stage unstable:** If this option is unchecked, then the plugin will mark the pipeline node (and stage) as unstable when it finds at least 1 test failure.
46+
If this option is checked, then the test failures will still be reported, but no warning is attached to the pipeline node. Neither the stage nor the build will be marked as unstable.
47+
Checking this option effectively implies `skipMarkingBuildUnstable: true`.
48+
In order to enable this, set the property:
49+
`skipMarkingStageUnstable` to `true`:
50+
51+
junit skipMarkingStageUnstable: true, testResults: 'test-results.xml'
4552
4653
### Test result checks (for GitHub projects)
4754

src/main/java/hudson/tasks/junit/pipeline/JUnitResultsStep.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public class JUnitResultsStep extends Step implements JUnitTask {
7070
*/
7171
private boolean skipMarkingBuildUnstable;
7272

73+
/**
74+
* If true, failing tests will not mark the stage (pipeline node) or the overall build as unstable.
75+
*/
76+
private boolean skipMarkingStageUnstable;
77+
7378
private boolean skipOldReports;
7479

7580
@DataBoundConstructor
@@ -227,6 +232,15 @@ public void setSkipMarkingBuildUnstable(boolean skipMarkingBuildUnstable) {
227232
this.skipMarkingBuildUnstable = skipMarkingBuildUnstable;
228233
}
229234

235+
public boolean isSkipMarkingStageUnstable() {
236+
return skipMarkingStageUnstable;
237+
}
238+
239+
@DataBoundSetter
240+
public void setSkipMarkingStageUnstable(boolean skipMarkingStageUnstable) {
241+
this.skipMarkingStageUnstable = skipMarkingStageUnstable;
242+
}
243+
230244
@Override
231245
public boolean isSkipOldReports() {
232246
return this.skipOldReports;

src/main/java/hudson/tasks/junit/pipeline/JUnitResultsStepExecution.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ protected TestResultSummary run() throws Exception {
6565
if (summary.getFailCount() > 0) {
6666
int testFailures = summary.getFailCount();
6767
if (testFailures > 0) {
68-
node.addOrReplaceAction(
69-
new WarningAction(Result.UNSTABLE).withMessage(testFailures + " tests failed"));
70-
if (!step.isSkipMarkingBuildUnstable()) {
71-
run.setResult(Result.UNSTABLE);
68+
if (!step.isSkipMarkingStageUnstable()) {
69+
node.addOrReplaceAction(
70+
new WarningAction(Result.UNSTABLE).withMessage(testFailures + " tests failed"));
71+
if (!step.isSkipMarkingBuildUnstable()) {
72+
run.setResult(Result.UNSTABLE);
73+
}
7274
}
7375
}
7476
}

src/main/resources/hudson/tasks/junit/pipeline/JUnitResultsStep/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ THE SOFTWARE.
2727
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
2828
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
2929
<st:include page="config.jelly" class="hudson.tasks.junit.JUnitResultArchiver" />
30+
<f:entry title="${%Skip marking stage as unstable on test failure}" field="skipMarkingStageUnstable">
31+
<f:checkbox default="false" title="${%If checked, the test failures will still be reported but won't mark the stage nor the build as unstable}"/>
32+
</f:entry>
3033
</j:jelly>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div>
22
If this option is unchecked, then the plugin will mark the build as unstable when it finds at least 1 test failure.
33
If this option is checked, then the build will still be successful even if there are test failures reported.
4-
In any case, the corresponding pipeline node (and stage) will be marked as unstable in case of test failure.
4+
The corresponding pipeline node (and stage) will still be marked as unstable in case of test failure, unless
5+
<code>skipMarkingStageUnstable</code> is also set.
56
</div>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div>
2+
If this option is unchecked, then the plugin will mark the pipeline node (and stage) as unstable when it finds at
3+
least 1 test failure.
4+
If this option is checked, then the test failures will still be reported, but no warning is attached to the
5+
pipeline node, so neither the stage nor the build will be marked as unstable. This effectively implies
6+
<code>skipMarkingBuildUnstable</code> as well.
7+
</div>

src/test/java/hudson/tasks/junit/pipeline/JUnitResultsStepTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ void configRoundTrip() throws Exception {
8383
st.assertRoundTrip(
8484
step,
8585
"junit allowEmptyResults: true, healthScaleFactor: 2.0, skipMarkingBuildUnstable: true, testDataPublishers: [[$class: 'MockTestDataPublisher', name: 'testing']], testResults: '**/target/surefire-reports/TEST-*.xml'");
86+
step.setSkipMarkingStageUnstable(true);
87+
st.assertRoundTrip(
88+
step,
89+
"junit allowEmptyResults: true, healthScaleFactor: 2.0, skipMarkingBuildUnstable: true, skipMarkingStageUnstable: true, testDataPublishers: [[$class: 'MockTestDataPublisher', name: 'testing']], testResults: '**/target/surefire-reports/TEST-*.xml'");
8690
}
8791

8892
@Issue("JENKINS-48250")
@@ -462,6 +466,31 @@ void skipBuildUnstable() throws Exception {
462466
assertStageResults(r, 1, 8, 3, "first");
463467
}
464468

469+
@Test
470+
void skipStageUnstable() throws Exception {
471+
WorkflowJob j = rule.jenkins.createProject(WorkflowJob.class, "skipStageUnstable");
472+
j.setDefinition(new CpsFlowDefinition(
473+
"stage('first') {\n" + " node {\n"
474+
+ " touch 'test-result.xml'\n"
475+
+ " def results = junit(skipMarkingStageUnstable: true, testResults: '*.xml')\n"
476+
+ " assert results.totalCount == 8\n"
477+
+ " assert currentBuild.result == null\n"
478+
+ " }\n"
479+
+ "}\n",
480+
true));
481+
copyToWorkspace(
482+
j, JUnitResultsStepTest.class.getResource("junit-report-testTrends-first-2.xml"), "test-result.xml");
483+
WorkflowRun r = rule.waitForCompletion(j.scheduleBuild2(0).waitForStart());
484+
rule.assertBuildStatus(Result.SUCCESS, r);
485+
486+
// Neither the build nor the stage should be marked unstable: the junit step attaches no WarningAction.
487+
FlowExecution execution = r.getExecution();
488+
BlockStartNode stage =
489+
(BlockStartNode) new DepthFirstScanner().findFirstMatch(execution, stageForName("first"));
490+
assertNotNull(stage);
491+
assertThat(findJUnitSteps(stage), CoreMatchers.not(CoreMatchers.hasItem(hasWarningAction())));
492+
}
493+
465494
@Test
466495
void ageResetSameTestSuiteName() throws Exception {
467496
WorkflowJob j = rule.jenkins.createProject(WorkflowJob.class, "p");

0 commit comments

Comments
 (0)