Skip to content

Commit a8166ed

Browse files
Merge pull request #114 from johnbillion/feature/invalid-dir
Clarify help text and error messages for invalid <dir> values
2 parents 122eaad + 24971cf commit a8166ed

File tree

4 files changed

+82
-17
lines changed

4 files changed

+82
-17
lines changed

features/scaffold-package-github.feature

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
Feature: Scaffold GitHub configuration for an existing package
22

3-
Scenario: Fails when invalid package directory provided
3+
Scenario: Fails when invalid directory provided
44
Given an empty directory
55

6-
When I run `wp package path`
7-
Then save STDOUT as {PACKAGE_PATH}
6+
When I try `wp scaffold package-github bar`
7+
Then the bar directory should not exist
8+
And STDERR should be:
9+
"""
10+
Error: Directory does not exist.
11+
"""
12+
13+
Scenario: Fails when invalid package provided
14+
Given an empty directory
15+
And a baz/empty file:
16+
"""
17+
"""
818

9-
When I try `wp scaffold package-github {PACKAGE_PATH}/local/wp-cli/default-github`
10-
Then STDERR should be:
19+
When I try `wp scaffold package-github baz`
20+
Then the baz directory should exist
21+
But the baz/.github directory should not exist
22+
And STDERR should be:
1123
"""
1224
Error: Invalid package directory. composer.json file must be present.
1325
"""

features/scaffold-package-readme.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
Feature: Scaffold a README.md file for an existing package
22

3+
Scenario: Fails when invalid directory provided
4+
Given an empty directory
5+
6+
When I try `wp scaffold package-readme bar`
7+
Then the bar directory should not exist
8+
And STDERR should be:
9+
"""
10+
Error: Directory does not exist.
11+
"""
12+
13+
Scenario: Fails when invalid package provided
14+
Given an empty directory
15+
And a baz/empty file:
16+
"""
17+
"""
18+
19+
When I try `wp scaffold package-readme baz`
20+
Then the baz directory should exist
21+
But the baz/README.md file should not exist
22+
And STDERR should be:
23+
"""
24+
Error: Invalid package directory. composer.json file must be present.
25+
"""
26+
327
Scenario: Scaffold a README.md based on the defaults
428
Given an empty directory
529

features/scaffold-package-tests.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ Feature: Scaffold the test suite for an existing package
2424
}
2525
"""
2626

27+
Scenario: Fails when invalid directory provided
28+
Given an empty directory
29+
30+
When I try `wp scaffold package-tests bar`
31+
Then the bar directory should not exist
32+
And STDERR should be:
33+
"""
34+
Error: Directory does not exist.
35+
"""
36+
37+
Scenario: Fails when invalid package provided
38+
Given an empty directory
39+
And a baz/empty file:
40+
"""
41+
"""
42+
43+
When I try `wp scaffold package-tests baz`
44+
Then the baz directory should exist
45+
But the baz/features directory should not exist
46+
And STDERR should be:
47+
"""
48+
Error: Invalid package directory. composer.json file must be present.
49+
"""
50+
2751
Scenario: Scaffold package tests
2852
Given a invalid-command/command.php file:
2953
"""

src/ScaffoldPackageCommand.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function package( $args, $assoc_args ) {
182182
* ## OPTIONS
183183
*
184184
* <dir>
185-
* : Directory of an existing command.
185+
* : Directory path to an existing package to generate a readme for.
186186
*
187187
* [--force]
188188
* : Overwrite the readme if it already exists.
@@ -194,9 +194,7 @@ public function package_readme( $args, $assoc_args ) {
194194

195195
list( $package_dir ) = $args;
196196

197-
if ( ! is_dir( $package_dir ) || ! file_exists( $package_dir . '/composer.json' ) ) {
198-
WP_CLI::error( "Invalid package directory. composer.json file must be present." );
199-
}
197+
self::check_if_valid_package_dir( $package_dir );
200198

201199
$composer_obj = json_decode( file_get_contents( $package_dir . '/composer.json' ), true );
202200
if ( ! $composer_obj ) {
@@ -384,7 +382,7 @@ public function package_readme( $args, $assoc_args ) {
384382
* ## OPTIONS
385383
*
386384
* <dir>
387-
* : The package directory to generate GitHub configuration for.
385+
* : Directory path to an existing package to generate GitHub configuration for.
388386
*
389387
* [--force]
390388
* : Overwrite files that already exist.
@@ -401,9 +399,8 @@ public function package_github( $args, $assoc_args ) {
401399
$package_dir = rtrim( $package_dir, '/' );
402400
}
403401

404-
if ( ! is_dir( $package_dir ) || ! file_exists( $package_dir . '/composer.json' ) ) {
405-
WP_CLI::error( "Invalid package directory. composer.json file must be present." );
406-
}
402+
self::check_if_valid_package_dir( $package_dir );
403+
407404
$force = Utils\get_flag_value( $assoc_args, 'force' );
408405
$template_path = dirname( dirname( __FILE__ ) ) . '/templates';
409406

@@ -492,7 +489,7 @@ public function package_github( $args, $assoc_args ) {
492489
* ## OPTIONS
493490
*
494491
* <dir>
495-
* : The package directory to generate tests for.
492+
* : Directory path to an existing package to generate tests for.
496493
*
497494
* [--ci=<provider>]
498495
* : Create a configuration file for a specific CI provider.
@@ -522,9 +519,7 @@ public function package_tests( $args, $assoc_args ) {
522519
$package_dir = rtrim( $package_dir, '/' );
523520
}
524521

525-
if ( ! is_dir( $package_dir ) || ! file_exists( $package_dir . '/composer.json' ) ) {
526-
WP_CLI::error( "Invalid package directory. composer.json file must be present." );
527-
}
522+
self::check_if_valid_package_dir( $package_dir );
528523

529524
$package_dir .= '/';
530525
$bin_dir = $package_dir . 'bin/';
@@ -664,4 +659,14 @@ private function create_files( $files_and_contents, $force ) {
664659
return $wrote_files;
665660
}
666661

662+
private static function check_if_valid_package_dir( $package_dir ) {
663+
if ( ! is_dir( $package_dir ) ) {
664+
WP_CLI::error( 'Directory does not exist.' );
665+
}
666+
667+
if ( ! file_exists( $package_dir . '/composer.json' ) ) {
668+
WP_CLI::error( 'Invalid package directory. composer.json file must be present.' );
669+
}
670+
}
671+
667672
}

0 commit comments

Comments
 (0)