35
35
import java .util .EnumMap ;
36
36
import java .util .HashMap ;
37
37
import java .util .Map ;
38
+ import java .util .function .BiConsumer ;
38
39
import static toolbox .ToolBox .lineSeparator ;
39
40
40
41
/**
@@ -50,7 +51,9 @@ abstract class AbstractTask<T extends AbstractTask<T>> implements Task {
50
51
private final Map <OutputKind , String > redirects = new EnumMap <>(OutputKind .class );
51
52
private final Map <String , String > envVars = new HashMap <>();
52
53
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 ;
54
57
55
58
/**
56
59
* Create a task that will execute in the specified mode.
@@ -67,7 +70,7 @@ protected AbstractTask(ToolBox tb, Mode mode) {
67
70
* @return the result of calling {@code run()}
68
71
*/
69
72
public Result run (Expect expect ) {
70
- expect (expect , Integer . MIN_VALUE );
73
+ expect (expect , ( _ , _ ) -> {} );
71
74
return run ();
72
75
}
73
76
@@ -83,17 +86,56 @@ public Result run(Expect expect, int exitCode) {
83
86
return run ();
84
87
}
85
88
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
+
86
123
/**
87
124
* Sets the expected outcome and expected exit code of the task.
88
125
* The exit code will not be checked if the outcome is
89
126
* {@code Expect.SUCCESS} or if the exit code is set to
90
127
* {@code Integer.MIN_VALUE}.
91
128
* @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}
93
134
*/
94
- protected void expect (Expect expect , int exitCode ) {
135
+ protected void expect (Expect expect ,
136
+ BiConsumer <Integer , String > exitCodeValidator ) {
95
137
this .expect = expect ;
96
- this .expectedExitCode = exitCode ;
138
+ this .exitCodeValidator = exitCodeValidator ;
97
139
}
98
140
99
141
/**
@@ -119,11 +161,11 @@ protected Result checkExit(Result result) throws TaskError {
119
161
throw new TaskError ("Task " + name () + " succeeded unexpectedly" );
120
162
}
121
163
122
- if (expectedExitCode != Integer .MIN_VALUE
123
- && result .exitCode != expectedExitCode ) {
164
+ try {
165
+ exitCodeValidator .accept (result .exitCode , name ());
166
+ } catch (Throwable t ) {
124
167
result .writeAll ();
125
- throw new TaskError ("Task " + name () + "failed with unexpected exit code "
126
- + result .exitCode + ", expected " + expectedExitCode );
168
+ throw t ;
127
169
}
128
170
break ;
129
171
}
0 commit comments