Skip to content

Commit c9a48df

Browse files
Merge pull request #155 from wp-cli/update-tests-enable-error-reporting
Update scaffolded tests to enable error reporting
2 parents 7bc8ce6 + 3a9bde1 commit c9a48df

10 files changed

+106
-27
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ private static function get_process_env_variables() {
131131
if ( $php_args = getenv( 'WP_CLI_PHP_ARGS' ) ) {
132132
$env['WP_CLI_PHP_ARGS'] = $php_args;
133133
}
134+
if ( $php_used = getenv( 'WP_CLI_PHP_USED' ) ) {
135+
$env['WP_CLI_PHP_USED'] = $php_used;
136+
}
137+
if ( $php = getenv( 'WP_CLI_PHP' ) ) {
138+
$env['WP_CLI_PHP'] = $php;
139+
}
134140
if ( $travis_build_dir = getenv( 'TRAVIS_BUILD_DIR' ) ) {
135141
$env['TRAVIS_BUILD_DIR'] = $travis_build_dir;
136142
}
@@ -526,7 +532,8 @@ public function background_proc( $cmd ) {
526532
$status = proc_get_status( $proc );
527533

528534
if ( !$status['running'] ) {
529-
throw new RuntimeException( stream_get_contents( $pipes[2] ) );
535+
$stderr = is_resource( $pipes[2] ) ? ( ': ' . stream_get_contents( $pipes[2] ) ) : '';
536+
throw new RuntimeException( sprintf( "Failed to start background process '%s'%s.", $cmd, $stderr ) );
530537
} else {
531538
$this->running_procs[] = $proc;
532539
}
@@ -617,7 +624,8 @@ public function install_wp( $subdir = '' ) {
617624
'title' => 'WP CLI Site',
618625
'admin_user' => 'admin',
619626
'admin_email' => 'admin@example.com',
620-
'admin_password' => 'password1'
627+
'admin_password' => 'password1',
628+
'skip-email' => true,
621629
);
622630

623631
$install_cache_path = '';
@@ -658,7 +666,8 @@ public function install_wp_with_composer( $vendor_directory = 'vendor' ) {
658666
'title' => 'WP CLI Site with both WordPress and wp-cli as Composer dependencies',
659667
'admin_user' => 'admin',
660668
'admin_email' => 'admin@example.com',
661-
'admin_password' => 'password1'
669+
'admin_password' => 'password1',
670+
'skip-email' => true,
662671
);
663672

664673
$this->proc( 'wp core install', $install_args )->run_check();
@@ -686,26 +695,13 @@ public function composer_require_current_wp_cli() {
686695
$this->composer_command( 'require wp-cli/wp-cli:dev-master --optimize-autoloader --no-interaction' );
687696
}
688697

689-
public function get_php_binary() {
690-
if ( getenv( 'WP_CLI_PHP_USED' ) )
691-
return getenv( 'WP_CLI_PHP_USED' );
692-
693-
if ( getenv( 'WP_CLI_PHP' ) )
694-
return getenv( 'WP_CLI_PHP' );
695-
696-
if ( defined( 'PHP_BINARY' ) )
697-
return PHP_BINARY;
698-
699-
return 'php';
700-
}
701-
702698
public function start_php_server( $subdir = '' ) {
703699
$dir = $this->variables['RUN_DIR'] . '/';
704700
if ( $subdir ) {
705701
$dir .= trim( $subdir, '/' ) . '/';
706702
}
707703
$cmd = Utils\esc_cmd( '%s -S %s -t %s -c %s %s',
708-
$this->get_php_binary(),
704+
Utils\get_php_binary(),
709705
'localhost:8080',
710706
$dir,
711707
get_cfg_var( 'cfg_file_path' ),

features/bootstrap/Process.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,20 @@ public function run_check() {
115115

116116
return $r;
117117
}
118+
119+
/**
120+
* Run the command, but throw an Exception on error.
121+
* Same as `run_check()` above, but checks the correct stderr.
122+
*
123+
* @return ProcessRun
124+
*/
125+
public function run_check_stderr() {
126+
$r = $this->run();
127+
128+
if ( $r->return_code || ! empty( $r->stderr ) ) {
129+
throw new \RuntimeException( $r );
130+
}
131+
132+
return $r;
133+
}
118134
}

features/bootstrap/utils.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,18 @@ function parse_ssh_url( $url, $component = -1 ) {
833833
$bits[ $key ] = $matches[ $i ];
834834
}
835835
}
836+
837+
// Find the hostname from `vagrant ssh-config` automatically.
838+
if ( preg_match( '/^vagrant:?/', $url ) ) {
839+
if ( 'vagrant' === $bits['host'] && empty( $bits['scheme'] ) ) {
840+
$ssh_config = shell_exec( 'vagrant ssh-config 2>/dev/null' );
841+
if ( preg_match( '/Host\s(.+)/', $ssh_config, $matches ) ) {
842+
$bits['scheme'] = 'vagrant';
843+
$bits['host'] = $matches[1];
844+
}
845+
}
846+
}
847+
836848
switch ( $component ) {
837849
case PHP_URL_SCHEME:
838850
return isset( $bits['scheme'] ) ? $bits['scheme'] : null;
@@ -1269,3 +1281,40 @@ function past_tense_verb( $verb ) {
12691281
}
12701282
return $verb . 'ed';
12711283
}
1284+
1285+
/**
1286+
* Get the path to the PHP binary used when executing WP-CLI.
1287+
*
1288+
* Environment values permit specific binaries to be indicated.
1289+
*
1290+
* @access public
1291+
* @category System
1292+
*
1293+
* @return string
1294+
*/
1295+
function get_php_binary() {
1296+
if ( $wp_cli_php_used = getenv( 'WP_CLI_PHP_USED' ) ) {
1297+
return $wp_cli_php_used;
1298+
}
1299+
1300+
if ( $wp_cli_php = getenv( 'WP_CLI_PHP' ) ) {
1301+
return $wp_cli_php;
1302+
}
1303+
1304+
// Available since PHP 5.4.
1305+
if ( defined( 'PHP_BINARY' ) ) {
1306+
return PHP_BINARY;
1307+
}
1308+
1309+
// @codingStandardsIgnoreLine
1310+
if ( @is_executable( PHP_BINDIR . '/php' ) ) {
1311+
return PHP_BINDIR . '/php';
1312+
}
1313+
1314+
// @codingStandardsIgnoreLine
1315+
if ( is_windows() && @is_executable( PHP_BINDIR . '/php.exe' ) ) {
1316+
return PHP_BINDIR . '/php.exe';
1317+
}
1318+
1319+
return 'php';
1320+
}

features/scaffold-package-github.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Feature: Scaffold GitHub configuration for an existing package
99
"""
1010
Error: Directory does not exist.
1111
"""
12+
And the return code should be 1
1213

1314
Scenario: Fails when invalid package provided
1415
Given an empty directory
@@ -23,6 +24,7 @@ Feature: Scaffold GitHub configuration for an existing package
2324
"""
2425
Error: Invalid package directory. composer.json file must be present.
2526
"""
27+
And the return code should be 1
2628

2729
Scenario: Scaffold GitHub configuration based on defaults
2830
Given an empty directory

features/scaffold-package-readme.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Feature: Scaffold a README.md file for an existing package
99
"""
1010
Error: Directory does not exist.
1111
"""
12+
And the return code should be 1
1213

1314
Scenario: Fails when invalid package provided
1415
Given an empty directory
@@ -23,6 +24,7 @@ Feature: Scaffold a README.md file for an existing package
2324
"""
2425
Error: Invalid package directory. composer.json file must be present.
2526
"""
27+
And the return code should be 1
2628

2729
Scenario: Scaffold a README.md based on the defaults
2830
Given an empty directory
@@ -249,6 +251,7 @@ Feature: Scaffold a README.md file for an existing package
249251
"""
250252
Error: Missing one or more commands defined in composer.json -> extra -> commands.
251253
"""
254+
And the return code should be 1
252255

253256
Scenario: README for a bundled command
254257
Given an empty directory

features/scaffold-package-tests.feature

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Feature: Scaffold the test suite for an existing package
3333
"""
3434
Error: Directory does not exist.
3535
"""
36+
And the return code should be 1
3637

3738
Scenario: Fails when invalid package provided
3839
Given an empty directory
@@ -47,6 +48,7 @@ Feature: Scaffold the test suite for an existing package
4748
"""
4849
Error: Invalid package directory. composer.json file must be present.
4950
"""
51+
And the return code should be 1
5052

5153
Scenario: Scaffold package tests
5254
Given a invalid-command/command.php file:
@@ -100,6 +102,7 @@ Feature: Scaffold the test suite for an existing package
100102
"""
101103
Error: Invalid package directory. composer.json file must be present.
102104
"""
105+
And the return code should be 1
103106

104107
Scenario: Scaffolds .travis.yml configuration file by default
105108
When I run `wp scaffold package-tests community-command`
@@ -135,6 +138,11 @@ Feature: Scaffold the test suite for an existing package
135138
Then the community-command/features/load-wp-cli.feature file should not exist
136139
And the community-command/features/command.feature file should exist
137140

138-
When I run `wp scaffold package-tests community-command --force`
141+
When I try `wp scaffold package-tests community-command --force`
139142
Then the community-command/features/load-wp-cli.feature file should not exist
140143
And the community-command/features/command.feature file should exist
144+
And STDERR should contain:
145+
"""
146+
Warning: File already exists
147+
"""
148+
And the return code should be 0

features/scaffold-package.feature

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Feature: Scaffold WP-CLI commands
108108
"""
109109
Error: 'foo' is an invalid package name. Package scaffold expects '<author>/<package>'.
110110
"""
111+
And the return code should be 1
111112

112113
Scenario: Scaffold a WP-CLI command to a custom directory
113114
Given an empty directory
@@ -155,7 +156,7 @@ Feature: Scaffold WP-CLI commands
155156
Success: Created package files
156157
"""
157158

158-
When I run `wp scaffold package wp-cli/same-package --skip-tests --skip-github < session`
159+
When I try `wp scaffold package wp-cli/same-package --skip-tests --skip-github < session`
159160
And STDERR should contain:
160161
"""
161162
Warning: File already exists
@@ -164,6 +165,8 @@ Feature: Scaffold WP-CLI commands
164165
"""
165166
All package files were skipped
166167
"""
168+
And the return code should be 0
169+
167170
When I run `wp package uninstall wp-cli/same-package`
168171
Then STDOUT should contain:
169172
"""
@@ -226,12 +229,12 @@ Feature: Scaffold WP-CLI commands
226229
Scenario: Use tilde for HOME in package directory path
227230
Given an empty directory
228231
229-
When I run `wp scaffold package bar/foo --dir=~/foo --force --skip-tests --skip-readme`
232+
When I run `HOME={RUN_DIR} wp scaffold package bar/foo --dir=~/foo --force --skip-tests --skip-readme`
230233
Then STDOUT should contain:
231234
"""
232235
Success: Package installed.
233236
"""
234-
And the /tmp/wp-cli-home/foo directory should exist
237+
And the {RUN_DIR}/foo directory should exist
235238
236239
Scenario: Scaffold a package but skip installation and GitHub templates
237240
Given an empty directory

features/steps/then.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
use Behat\Gherkin\Node\PyStringNode,
44
Behat\Gherkin\Node\TableNode;
55

6-
$steps->Then( '/^the return code should be (\d+)$/',
7-
function ( $world, $return_code ) {
8-
if ( $return_code != $world->result->return_code ) {
6+
$steps->Then( '/^the return code should( not)? be (\d+)$/',
7+
function ( $world, $not, $return_code ) {
8+
if ( ( ! $not && $return_code != $world->result->return_code ) || ( $not && $return_code == $world->result->return_code ) ) {
99
throw new RuntimeException( $world->result );
1010
}
1111
}

features/steps/when.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
function invoke_proc( $proc, $mode ) {
88
$map = array(
9-
'run' => 'run_check',
9+
'run' => 'run_check_stderr',
1010
'try' => 'run'
1111
);
1212
$method = $map[ $mode ];

utils/behat-tags.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ function version_tags( $prefix, $current, $operator = '<' ) {
4444
version_tags( 'less-than-php', PHP_VERSION, '>' )
4545
);
4646

47-
# Skip Github API tests by default because of rate limiting. See https://github.com/wp-cli/wp-cli/issues/1612
48-
$skip_tags[] = '@github-api';
47+
# Skip Github API tests if `GITHUB_TOKEN` not available because of rate limiting. See https://github.com/wp-cli/wp-cli/issues/1612
48+
if ( ! getenv( 'GITHUB_TOKEN' ) ) {
49+
$skip_tags[] = '@github-api';
50+
}
4951

5052
# Skip tests known to be broken.
5153
$skip_tags[] = '@broken';

0 commit comments

Comments
 (0)