Skip to content

Commit 926c9eb

Browse files
Ask user for confirmation before overwriting if file already exists (#85)
* Add guard if file exists displaying a warning and asking for confirmation and arg --yes * Add test without testing it * undo debug command * remove empty line * divide STDOUT and STDERR in two different commands * join STDOUT and STDERR assertions in the same when * confirm test STDERR should contain * refactor in favor of scaffold file replacement flow * Update dist archive ask for confirmation tests * undo formatting test * Tweak 'File' to 'Archive file' --------- Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent 9ae0c30 commit 926c9eb

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

features/dist-archive.feature

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,60 @@ Feature: Generate a distribution archive of a project
437437
"""
438438
And STDERR should be empty
439439
And the foo.zip file should exist
440+
441+
Scenario: Ask for confirmation if archive file exists
442+
Given a WP install
443+
444+
When I run `wp scaffold plugin hello-world`
445+
Then the wp-content/plugins/hello-world directory should exist
446+
And the wp-content/plugins/hello-world/hello-world.php file should exist
447+
And the wp-content/plugins/hello-world/.travis.yml file should exist
448+
And the wp-content/plugins/hello-world/bin directory should exist
449+
450+
When I run `mkdir subdir`
451+
Then the subdir directory should exist
452+
453+
When I run `wp dist-archive wp-content/plugins/hello-world ./subdir/hello-world-dist.zip`
454+
Then STDOUT should be:
455+
"""
456+
Success: Created hello-world-dist.zip
457+
"""
458+
And STDERR should be empty
459+
And the {RUN_DIR}/subdir/hello-world-dist.zip file should exist
460+
461+
When I try `echo "s" | wp dist-archive wp-content/plugins/hello-world ./subdir/hello-world-dist.zip`
462+
Then STDERR should contain:
463+
"""
464+
Warning: Archive file already exists
465+
"""
466+
And STDOUT should contain:
467+
"""
468+
Do you want to skip or replace it with a new archive? [s/r]:
469+
"""
470+
And STDOUT should contain:
471+
"""
472+
Archive generation skipped.
473+
"""
474+
And STDOUT should not contain:
475+
"""
476+
Success: Created hello-world-dist.zip
477+
"""
478+
And the {RUN_DIR}/subdir/hello-world-dist.zip file should exist
479+
And the return code should be 0
480+
481+
482+
When I try `echo "r" | wp dist-archive wp-content/plugins/hello-world ./subdir/hello-world-dist.zip`
483+
And STDERR should contain:
484+
"""
485+
Warning: Archive file already exists
486+
"""
487+
And STDOUT should contain:
488+
"""
489+
Do you want to skip or replace it with a new archive? [s/r]:
490+
"""
491+
And STDOUT should contain:
492+
"""
493+
Success: Created hello-world-dist.zip
494+
"""
495+
And the {RUN_DIR}/subdir/hello-world-dist.zip file should exist
496+
And the return code should be 0

src/Dist_Archive_Command.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,23 @@ public function __invoke( $args, $assoc_args ) {
195195
}
196196
$archive_absolute_filepath = "{$archive_path}/{$archive_filename}";
197197

198+
if ( file_exists( $archive_absolute_filepath ) ) {
199+
WP_CLI::warning( 'Archive file already exists' );
200+
WP_CLI::log( $archive_absolute_filepath );
201+
$answer = \cli\prompt(
202+
'Do you want to skip or replace it with a new archive?',
203+
$default = false,
204+
$marker = ' [s/r]: '
205+
);
206+
$should_overwrite = 'r' === strtolower( $answer );
207+
if ( ! $should_overwrite ) {
208+
WP_CLI::log( 'Skipping' . PHP_EOL );
209+
WP_CLI::log( 'Archive generation skipped.' );
210+
exit( 0 );
211+
}
212+
WP_CLI::log( 'Replacing' . PHP_EOL );
213+
}
214+
198215
chdir( dirname( $source_path ) );
199216

200217
if ( Utils\get_flag_value( $assoc_args, 'create-target-dir' ) ) {

0 commit comments

Comments
 (0)