3535import java .util .EnumMap ;
3636import java .util .HashMap ;
3737import java .util .Map ;
38+ import java .util .function .BiConsumer ;
3839import static toolbox .ToolBox .lineSeparator ;
3940
4041/**
@@ -50,7 +51,9 @@ abstract class AbstractTask<T extends AbstractTask<T>> implements Task {
5051 private final Map <OutputKind , String > redirects = new EnumMap <>(OutputKind .class );
5152 private final Map <String , String > envVars = new HashMap <>();
5253 private Expect expect = Expect .SUCCESS ;
53- int expectedExitCode = 0 ;
54+ //validator for exit codes, first parameter is the exit code
55+ //the second the test name:
56+ private BiConsumer <Integer , String > exitCodeValidator = null ;
5457
5558 /**
5659 * Create a task that will execute in the specified mode.
@@ -67,7 +70,7 @@ protected AbstractTask(ToolBox tb, Mode mode) {
6770 * @return the result of calling {@code run()}
6871 */
6972 public Result run (Expect expect ) {
70- expect (expect , Integer . MIN_VALUE );
73+ expect (expect , ( _ , _ ) -> {} );
7174 return run ();
7275 }
7376
@@ -83,17 +86,56 @@ public Result run(Expect expect, int exitCode) {
8386 return run ();
8487 }
8588
89+ /**
90+ * Sets the expected outcome of the task and calls {@code run()}.
91+ * @param expect the expected outcome
92+ * @param exitCodeValidator an exit code validator. The first parameter will
93+ * be the actual exit code, the second test name,
94+ * should throw TaskError if the exit code is not
95+ * as expected. Only used if the expected outcome
96+ * is {@code FAIL}
97+ * @return the result of calling {@code run()}
98+ */
99+ public Result run (Expect expect ,
100+ BiConsumer <Integer , String > exitCodeValidator ) {
101+ expect (expect , exitCodeValidator );
102+ return run ();
103+ }
104+
105+ /**
106+ * Sets the expected outcome and expected exit code of the task.
107+ * The exit code will not be checked if the outcome is
108+ * {@code Expect.SUCCESS} or if the exit code is set to
109+ * {@code Integer.MIN_VALUE}.
110+ * @param expect the expected outcome
111+ * @param expectedExitCode the expected exit code
112+ */
113+ protected void expect (Expect expect , int expectedExitCode ) {
114+ expect (expect , (exitCode , testName ) -> {
115+ if (expectedExitCode != Integer .MIN_VALUE &&
116+ exitCode != expectedExitCode ) {
117+ throw new TaskError ("Task " + testName + "failed with unexpected exit code "
118+ + exitCode + ", expected " + expectedExitCode );
119+ }
120+ });
121+ }
122+
86123 /**
87124 * Sets the expected outcome and expected exit code of the task.
88125 * The exit code will not be checked if the outcome is
89126 * {@code Expect.SUCCESS} or if the exit code is set to
90127 * {@code Integer.MIN_VALUE}.
91128 * @param expect the expected outcome
92- * @param exitCode the expected exit code
129+ * @param exitCodeValidator an exit code validator. The first parameter will
130+ * be the actual exit code, the second test name,
131+ * should throw TaskError if the exit code is not
132+ * as expected. Only used if the expected outcome
133+ * is {@code FAIL}
93134 */
94- protected void expect (Expect expect , int exitCode ) {
135+ protected void expect (Expect expect ,
136+ BiConsumer <Integer , String > exitCodeValidator ) {
95137 this .expect = expect ;
96- this .expectedExitCode = exitCode ;
138+ this .exitCodeValidator = exitCodeValidator ;
97139 }
98140
99141 /**
@@ -119,11 +161,11 @@ protected Result checkExit(Result result) throws TaskError {
119161 throw new TaskError ("Task " + name () + " succeeded unexpectedly" );
120162 }
121163
122- if (expectedExitCode != Integer .MIN_VALUE
123- && result .exitCode != expectedExitCode ) {
164+ try {
165+ exitCodeValidator .accept (result .exitCode , name ());
166+ } catch (Throwable t ) {
124167 result .writeAll ();
125- throw new TaskError ("Task " + name () + "failed with unexpected exit code "
126- + result .exitCode + ", expected " + expectedExitCode );
168+ throw t ;
127169 }
128170 break ;
129171 }
0 commit comments