|
24 | 24 | package org.jenkinsci.plugins.pipeline.maven; |
25 | 25 |
|
26 | 26 | 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; |
27 | 29 | import static org.junit.jupiter.api.condition.OS.LINUX; |
28 | 30 |
|
29 | 31 | import com.cloudbees.hudson.plugins.folder.Folder; |
| 32 | +import com.google.common.base.Predicate; |
30 | 33 | import hudson.model.Result; |
31 | 34 | import hudson.plugins.tasks.TasksResultAction; |
| 35 | +import hudson.tasks.junit.CaseResult; |
| 36 | +import hudson.tasks.junit.TestResult; |
32 | 37 | 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; |
34 | 40 | import java.io.File; |
35 | 41 | import java.nio.charset.StandardCharsets; |
| 42 | +import java.util.ArrayList; |
36 | 43 | import java.util.Arrays; |
37 | 44 | import java.util.Collection; |
| 45 | +import java.util.Collections; |
| 46 | +import java.util.List; |
38 | 47 | import java.util.logging.Level; |
39 | 48 | import java.util.logging.Logger; |
40 | 49 | import java.util.stream.Stream; |
41 | 50 | import jenkins.mvn.FilePathGlobalSettingsProvider; |
42 | 51 | import jenkins.mvn.FilePathSettingsProvider; |
43 | 52 | import jenkins.mvn.GlobalMavenConfig; |
44 | 53 | 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; |
45 | 58 | import org.jenkinsci.Symbol; |
46 | 59 | import org.jenkinsci.plugins.configfiles.GlobalConfigFiles; |
47 | 60 | import org.jenkinsci.plugins.configfiles.maven.GlobalMavenSettingsConfig; |
|
59 | 72 | import org.jenkinsci.plugins.pipeline.maven.publishers.PipelineGraphPublisher; |
60 | 73 | import org.jenkinsci.plugins.pipeline.maven.publishers.SpotBugsAnalysisPublisher; |
61 | 74 | 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; |
62 | 78 | 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; |
63 | 85 | import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
64 | 86 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
| 87 | +import org.jenkinsci.plugins.workflow.support.steps.StageStep; |
65 | 88 | import org.junit.jupiter.api.AfterEach; |
66 | 89 | import org.junit.jupiter.api.BeforeEach; |
67 | 90 | import org.junit.jupiter.api.Test; |
@@ -1277,9 +1300,117 @@ public void maven_build_test_results_by_stage_and_branch() throws Exception { |
1277 | 1300 | assertThat(testResultAction.getTotalCount()).isEqualTo(4); |
1278 | 1301 | assertThat(testResultAction.getFailCount()).isEqualTo(0); |
1279 | 1302 |
|
1280 | | - JUnitResultsStepTest.assertStageResults(build, 4, 4, 0, "first"); |
| 1303 | + assertStageResults(build, 4, 4, 0, "first"); |
1281 | 1304 |
|
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 | + }; |
1284 | 1415 | } |
1285 | 1416 | } |
0 commit comments