Skip to content

Commit

Permalink
Tests + correct command syntax
Browse files Browse the repository at this point in the history
Working for zip
Not quite working for tar

The tar command is correct and works when I run it on the command line, but the tests are failing to exclude the file properly.
  • Loading branch information
BrianHenryIE committed Jun 3, 2022
1 parent eba7d1c commit 84cc92a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
57 changes: 57 additions & 0 deletions features/dist-archive.feature
Expand Up @@ -149,6 +149,63 @@ Feature: Generate a distribution archive of a project
| zip | zip | unzip |
| targz | tar.gz | tar -zxvf |

Scenario Outline: Ignores files specified with absolute path and not similarly named files
Given an empty directory
And a foo/.distignore file:
"""
/maybe-ignore-me.txt
"""
And a foo/test.php file:
"""
<?php
echo 'Hello world;';
"""
And a foo/test-dir/test.php file:
"""
<?php
echo 'Hello world;';
"""
And a foo/maybe-ignore-me.txt file:
"""
Ignore
"""
And a foo/test-dir/maybe-ignore-me.txt file:
"""
Do not ignore
"""
And a foo/test-dir/foo/maybe-ignore-me.txt file:
"""
Do not ignore
"""

When I run `wp dist-archive foo --format=<format> --plugin-dirname=<plugin-dirname>`
Then STDOUT should be:
"""
Success: Created <plugin-dirname>.<extension>
"""
And the <plugin-dirname>.<extension> file should exist

When I run `rm -rf foo`
Then the foo directory should not exist

When I run `rm -rf <plugin-dirname>`
Then the <plugin-dirname> directory should not exist

When I try `<extract> <plugin-dirname>.<extension>`
Then the <plugin-dirname> directory should exist
And the <plugin-dirname>/test.php file should exist
And the <plugin-dirname>/test-dir/test.php file should exist
And the <plugin-dirname>/maybe-ignore-me.txt file should not exist
And the <plugin-dirname>/test-dir/maybe-ignore-me.txt file should exist
And the <plugin-dirname>/test-dir/foo/maybe-ignore-me.txt file should exist

Examples:
| format | extension | extract | plugin-dirname |
| zip | zip | unzip | foo |
| targz | tar.gz | tar -zxvf | foo |
| zip | zip | unzip | bar |
| targz | tar.gz | tar -zxvf | bar |

Scenario: Create directories automatically if requested
Given a WP install

Expand Down
14 changes: 10 additions & 4 deletions src/Dist_Archive_Command.php
Expand Up @@ -78,7 +78,8 @@ public function __invoke( $args, $assoc_args ) {

$maybe_ignored_files = explode( PHP_EOL, file_get_contents( $dist_ignore_path ) );
$ignored_files = array();
$archive_base = basename( $path );
$source_base = basename( $path );
$archive_base = isset( $assoc_args['plugin-dirname'] ) ? rtrim( $assoc_args['plugin-dirname'], '/' ) : $source_base;
foreach ( $maybe_ignored_files as $file ) {
$file = trim( $file );
if ( 0 === strpos( $file, '#' ) || empty( $file ) ) {
Expand All @@ -87,10 +88,15 @@ public function __invoke( $args, $assoc_args ) {
if ( is_dir( $path . '/' . $file ) ) {
$file = rtrim( $file, '/' ) . '/*';
}
// If a path is tied to the root of the plugin using `/`, match exactly, otherwise match liberally.
if ( 'zip' === $assoc_args['format'] ) {
$ignored_files[] = '*/' . $file;
$ignored_files[] = ( 0 === strpos( $file, '/' ) )
? $archive_base . $file
: '*/' . $file;
} elseif ( 'targz' === $assoc_args['format'] ) {
$ignored_files[] = $file;
$ignored_files[] = ( 0 === strpos( $file, '/' ) )
? '^' . $archive_base . $file
: $file;
}
}

Expand Down Expand Up @@ -119,7 +125,7 @@ public function __invoke( $args, $assoc_args ) {
}
}

if ( isset( $assoc_args['plugin-dirname'] ) && rtrim( $assoc_args['plugin-dirname'], '/' ) !== $archive_base ) {
if ( $archive_base !== $source_base ) {
$plugin_dirname = rtrim( $assoc_args['plugin-dirname'], '/' );
$archive_base = $plugin_dirname;
$tmp_dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $plugin_dirname . $version . '.' . time();
Expand Down

0 comments on commit 84cc92a

Please sign in to comment.