From 11c55f81732f098ec958d8703b328e4f71d05110 Mon Sep 17 00:00:00 2001 From: Aleksi Simell Date: Fri, 15 Nov 2024 08:40:23 +0200 Subject: [PATCH] Add possibility to add error messages to failed token macro (#79) --- .../robot/tokens/RobotFailedCasesTokenMacro.java | 6 ++++++ .../tokens/RobotFailedCasesTokenMacroTest.java | 13 ++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/hudson/plugins/robot/tokens/RobotFailedCasesTokenMacro.java b/src/main/java/hudson/plugins/robot/tokens/RobotFailedCasesTokenMacro.java index efc4487..167c438 100644 --- a/src/main/java/hudson/plugins/robot/tokens/RobotFailedCasesTokenMacro.java +++ b/src/main/java/hudson/plugins/robot/tokens/RobotFailedCasesTokenMacro.java @@ -17,6 +17,9 @@ @Extension(optional = true) public class RobotFailedCasesTokenMacro extends DataBoundTokenMacro { + @Parameter + public boolean addErrorMessages; + @Override public String evaluate(AbstractBuild context, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, @@ -35,6 +38,9 @@ public String evaluate(Run context, FilePath workspace, TaskListener liste String newline = ""; for (RobotCaseResult failedCase : result.getAllFailedCases()){ builder.append(newline).append(failedCase.getRelativePackageName(result)); + if (addErrorMessages && failedCase.getErrorMsg() != null && !failedCase.getErrorMsg().isEmpty()) { + builder.append(": ").append(failedCase.getErrorMsg()); + } newline = "\n"; } diff --git a/src/test/java/hudson/plugins/robot/tokens/RobotFailedCasesTokenMacroTest.java b/src/test/java/hudson/plugins/robot/tokens/RobotFailedCasesTokenMacroTest.java index e95d6d3..39c6125 100644 --- a/src/test/java/hudson/plugins/robot/tokens/RobotFailedCasesTokenMacroTest.java +++ b/src/test/java/hudson/plugins/robot/tokens/RobotFailedCasesTokenMacroTest.java @@ -36,10 +36,12 @@ public void setUp(){ RobotCaseResult case1 = Mockito.mock(RobotCaseResult.class); Mockito.when(case1.getRelativePackageName(result)).thenReturn("Failcases.subcases.Failure1"); - + Mockito.when(case1.getErrorMsg()).thenReturn("Case1 failed"); + RobotCaseResult case2 = Mockito.mock(RobotCaseResult.class); Mockito.when(case2.getRelativePackageName(result)).thenReturn("Morefails.Failure2"); - + Mockito.when(case2.getErrorMsg()).thenReturn("Case2 failed"); + failedList.add(case1); failedList.add(case2); @@ -52,7 +54,12 @@ public void testAcceptsName(){ assertTrue(new RobotFailedCasesTokenMacro().acceptsMacroName(macroName)); } - public void testTokenConversion() throws MacroEvaluationException, IOException, InterruptedException{ + public void testTokenConversionWithoutMessages() throws MacroEvaluationException, IOException, InterruptedException{ assertEquals("Failcases.subcases.Failure1\nMorefails.Failure2",token.evaluate(build, listener, macroName)); } + + public void testTokenConversionWithMessages() throws MacroEvaluationException, IOException, InterruptedException{ + token.addErrorMessages = true; + assertEquals("Failcases.subcases.Failure1: Case1 failed\nMorefails.Failure2: Case2 failed",token.evaluate(build, listener, macroName)); + } }