Skip to content

Commit 625a45c

Browse files
Remove test dependency to junit plugin test artifact (#905)
* see jenkinsci/bom#5041
1 parent 00f8262 commit 625a45c

File tree

2 files changed

+136
-13
lines changed

2 files changed

+136
-13
lines changed

pipeline-maven/pom.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,7 @@
320320
<classifier>tests</classifier>
321321
<scope>test</scope>
322322
</dependency>
323-
<dependency>
324-
<groupId>org.jenkins-ci.plugins</groupId>
325-
<artifactId>junit</artifactId>
326-
<version>1322.v1556dc1c59a_f</version>
327-
<classifier>tests</classifier>
328-
<scope>test</scope>
329-
</dependency>
330-
<!-- move the related test to pipeline-maven-database and remove this dependency
331-
-->
323+
<!-- move the related test to pipeline-maven-database and remove this dependency -->
332324
<dependency>
333325
<groupId>org.jenkins-ci.plugins</groupId>
334326
<artifactId>pipeline-maven-database</artifactId>

pipeline-maven/src/test/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepOnMasterTest.java

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,37 @@
2424
package org.jenkinsci.plugins.pipeline.maven;
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2729
import static org.junit.jupiter.api.condition.OS.LINUX;
2830

2931
import com.cloudbees.hudson.plugins.folder.Folder;
32+
import com.google.common.base.Predicate;
3033
import hudson.model.Result;
3134
import hudson.plugins.tasks.TasksResultAction;
35+
import hudson.tasks.junit.CaseResult;
36+
import hudson.tasks.junit.TestResult;
3237
import hudson.tasks.junit.TestResultAction;
33-
import hudson.tasks.junit.pipeline.JUnitResultsStepTest;
38+
import hudson.tasks.junit.pipeline.JUnitResultsStep;
39+
import hudson.tasks.test.PipelineBlockWithTests;
3440
import java.io.File;
3541
import java.nio.charset.StandardCharsets;
42+
import java.util.ArrayList;
3643
import java.util.Arrays;
3744
import java.util.Collection;
45+
import java.util.Collections;
46+
import java.util.List;
3847
import java.util.logging.Level;
3948
import java.util.logging.Logger;
4049
import java.util.stream.Stream;
4150
import jenkins.mvn.FilePathGlobalSettingsProvider;
4251
import jenkins.mvn.FilePathSettingsProvider;
4352
import jenkins.mvn.GlobalMavenConfig;
4453
import org.apache.commons.io.FileUtils;
54+
import org.hamcrest.BaseMatcher;
55+
import org.hamcrest.CoreMatchers;
56+
import org.hamcrest.Description;
57+
import org.hamcrest.MatcherAssert;
4558
import org.jenkinsci.Symbol;
4659
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
4760
import org.jenkinsci.plugins.configfiles.maven.GlobalMavenSettingsConfig;
@@ -59,9 +72,19 @@
5972
import org.jenkinsci.plugins.pipeline.maven.publishers.PipelineGraphPublisher;
6073
import org.jenkinsci.plugins.pipeline.maven.publishers.SpotBugsAnalysisPublisher;
6174
import org.jenkinsci.plugins.pipeline.maven.publishers.TasksScannerPublisher;
75+
import org.jenkinsci.plugins.workflow.actions.LabelAction;
76+
import org.jenkinsci.plugins.workflow.actions.ThreadNameAction;
77+
import org.jenkinsci.plugins.workflow.actions.WarningAction;
6278
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
79+
import org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode;
80+
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode;
81+
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
82+
import org.jenkinsci.plugins.workflow.graph.BlockStartNode;
83+
import org.jenkinsci.plugins.workflow.graph.FlowNode;
84+
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner;
6385
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
6486
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
87+
import org.jenkinsci.plugins.workflow.support.steps.StageStep;
6588
import org.junit.jupiter.api.AfterEach;
6689
import org.junit.jupiter.api.BeforeEach;
6790
import org.junit.jupiter.api.Test;
@@ -1277,9 +1300,117 @@ public void maven_build_test_results_by_stage_and_branch() throws Exception {
12771300
assertThat(testResultAction.getTotalCount()).isEqualTo(4);
12781301
assertThat(testResultAction.getFailCount()).isEqualTo(0);
12791302

1280-
JUnitResultsStepTest.assertStageResults(build, 4, 4, 0, "first");
1303+
assertStageResults(build, 4, 4, 0, "first");
12811304

1282-
JUnitResultsStepTest.assertBranchResults(build, 2, 2, 0, "a", "first", null);
1283-
JUnitResultsStepTest.assertBranchResults(build, 2, 2, 0, "b", "first", null);
1305+
assertBranchResults(build, 2, 2, 0, "a", "first", null, false);
1306+
assertBranchResults(build, 2, 2, 0, "b", "first", null, false);
1307+
}
1308+
1309+
private static void assertBranchResults(
1310+
WorkflowRun run,
1311+
int suiteCount,
1312+
int testCount,
1313+
int failCount,
1314+
String branchName,
1315+
String stageName,
1316+
String innerStageName,
1317+
boolean keepTestNames) {
1318+
FlowExecution execution = run.getExecution();
1319+
DepthFirstScanner scanner = new DepthFirstScanner();
1320+
BlockStartNode aBranch = (BlockStartNode) scanner.findFirstMatch(execution, branchForName(branchName));
1321+
assertNotNull(aBranch);
1322+
TestResult branchResult = assertBlockResults(run, suiteCount, testCount, failCount, aBranch);
1323+
String namePrefix;
1324+
if (!keepTestNames) {
1325+
namePrefix = stageName + " / " + branchName;
1326+
if (innerStageName != null) {
1327+
namePrefix += " / " + innerStageName;
1328+
}
1329+
namePrefix += " / ";
1330+
} else {
1331+
namePrefix = "";
1332+
}
1333+
for (CaseResult c : branchResult.getPassedTests()) {
1334+
assertEquals(namePrefix + c.getTransformedTestName(), c.getDisplayName());
1335+
}
1336+
}
1337+
1338+
private static void assertStageResults(
1339+
WorkflowRun run, int suiteCount, int testCount, int failCount, String stageName) {
1340+
FlowExecution execution = run.getExecution();
1341+
DepthFirstScanner scanner = new DepthFirstScanner();
1342+
BlockStartNode aStage = (BlockStartNode) scanner.findFirstMatch(execution, stageForName(stageName));
1343+
assertNotNull(aStage);
1344+
assertBlockResults(run, suiteCount, testCount, failCount, aStage);
1345+
}
1346+
1347+
private static TestResult assertBlockResults(
1348+
WorkflowRun run, int suiteCount, int testCount, int failCount, BlockStartNode blockNode) {
1349+
assertNotNull(blockNode);
1350+
1351+
TestResultAction action = run.getAction(TestResultAction.class);
1352+
assertNotNull(action);
1353+
1354+
TestResult aResult = action.getResult().getResultForPipelineBlock(blockNode.getId());
1355+
assertNotNull(aResult);
1356+
1357+
assertEquals(suiteCount, aResult.getSuites().size());
1358+
assertEquals(testCount, aResult.getTotalCount());
1359+
assertEquals(failCount, aResult.getFailCount());
1360+
if (failCount > 0) {
1361+
MatcherAssert.assertThat(findJUnitSteps(blockNode), CoreMatchers.hasItem(hasWarningAction()));
1362+
} else {
1363+
MatcherAssert.assertThat(
1364+
findJUnitSteps(blockNode), CoreMatchers.not(CoreMatchers.hasItem(hasWarningAction())));
1365+
}
1366+
1367+
PipelineBlockWithTests aBlock = action.getResult().getPipelineBlockWithTests(blockNode.getId());
1368+
1369+
assertNotNull(aBlock);
1370+
List<String> aTestNodes = new ArrayList<>(aBlock.nodesWithTests());
1371+
TestResult aFromNodes = action.getResult().getResultByNodes(aTestNodes);
1372+
assertNotNull(aFromNodes);
1373+
assertEquals(aResult.getSuites().size(), aFromNodes.getSuites().size());
1374+
assertEquals(aResult.getFailCount(), aFromNodes.getFailCount());
1375+
assertEquals(aResult.getSkipCount(), aFromNodes.getSkipCount());
1376+
assertEquals(aResult.getPassCount(), aFromNodes.getPassCount());
1377+
1378+
return aResult;
1379+
}
1380+
1381+
private static Predicate<FlowNode> stageForName(final String name) {
1382+
return input -> input instanceof StepStartNode
1383+
&& ((StepStartNode) input).getDescriptor() instanceof StageStep.DescriptorImpl
1384+
&& input.getDisplayName().equals(name);
1385+
}
1386+
1387+
private static Predicate<FlowNode> branchForName(final String name) {
1388+
return input -> input != null
1389+
&& input.getAction(LabelAction.class) != null
1390+
&& input.getAction(ThreadNameAction.class) != null
1391+
&& name.equals(input.getAction(ThreadNameAction.class).getThreadName());
1392+
}
1393+
1394+
private static List<FlowNode> findJUnitSteps(BlockStartNode blockStart) {
1395+
return new DepthFirstScanner()
1396+
.filteredNodes(
1397+
Collections.singletonList(blockStart.getEndNode()),
1398+
Collections.singletonList(blockStart),
1399+
node -> node instanceof StepAtomNode
1400+
&& ((StepAtomNode) node).getDescriptor() instanceof JUnitResultsStep.DescriptorImpl);
1401+
}
1402+
1403+
private static BaseMatcher<FlowNode> hasWarningAction() {
1404+
return new BaseMatcher<>() {
1405+
@Override
1406+
public boolean matches(Object item) {
1407+
return item instanceof FlowNode && ((FlowNode) item).getPersistentAction(WarningAction.class) != null;
1408+
}
1409+
1410+
@Override
1411+
public void describeTo(Description description) {
1412+
description.appendText("a FlowNode with a WarningAction");
1413+
}
1414+
};
12841415
}
12851416
}

0 commit comments

Comments
 (0)