Skip to content

Commit 68e948f

Browse files
committed
Improve error message for failed steps
1 parent 26313e2 commit 68e948f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Context/Support.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ protected function check_string( $output, $expected, $action, $message = false,
123123
$message = $output;
124124
}
125125

126+
$action_message = '';
127+
switch ( $action ) {
128+
case 'be':
129+
$action_message = 'Output does not exactly match expected string:';
130+
break;
131+
case 'contain':
132+
$action_message = 'Output does not contain expected string:';
133+
break;
134+
case 'not contain':
135+
$action_message = 'Output unexpectedly contains string:';
136+
break;
137+
}
138+
139+
if ( ! empty( $action_message ) ) {
140+
$message .= "\n\n" . $action_message . "\n" . $expected;
141+
}
142+
126143
$diff = $this->generate_diff( $expected, rtrim( $output, "\n" ) );
127144
if ( ! empty( $diff ) ) {
128145
$message .= "\n\n" . $diff;

src/Context/WhenStepDefinitions.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,27 @@ public function wpcli_tests_invoke_proc( $proc, $mode ) {
1919
);
2020
$method = $map[ $mode ];
2121

22-
return $proc->$method();
22+
try {
23+
return $proc->$method();
24+
} catch ( \RuntimeException $e ) {
25+
if ( 'run' === $mode ) {
26+
$message = $e->getMessage();
27+
$status = 'unknown';
28+
if ( preg_match( '/exit status: (\d+)$/', $message, $matches ) ) {
29+
$status = $matches[1];
30+
}
31+
32+
if ( '0' === $status ) {
33+
$message .= "\n\nThe command unexpectedly produced STDERR output. If you expect a non-zero exit status or STDERR output, use `When I try [...]`. Else, this may be a bug in your code or test.";
34+
} else {
35+
$message .= "\n\nThe command unexpectedly exited with status {$status}. If you expect a non-zero exit status, use `When I try [...]`. Else, this may be a bug in your code or test.";
36+
}
37+
38+
throw new \RuntimeException( $message, $e->getCode(), $e );
39+
}
40+
41+
throw $e;
42+
}
2343
}
2444

2545
/**

0 commit comments

Comments
 (0)