From 84cc92a44d06597dde8275460853cc6d7707454e Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 2 Jun 2022 17:00:49 -0700 Subject: [PATCH] Tests + correct command syntax 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. --- features/dist-archive.feature | 57 +++++++++++++++++++++++++++++++++++ src/Dist_Archive_Command.php | 14 ++++++--- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/features/dist-archive.feature b/features/dist-archive.feature index e5a5132..39a0ccb 100644 --- a/features/dist-archive.feature +++ b/features/dist-archive.feature @@ -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: + """ + --plugin-dirname=` + Then STDOUT should be: + """ + Success: Created . + """ + And the . file should exist + + When I run `rm -rf foo` + Then the foo directory should not exist + + When I run `rm -rf ` + Then the directory should not exist + + When I try ` .` + Then the directory should exist + And the /test.php file should exist + And the /test-dir/test.php file should exist + And the /maybe-ignore-me.txt file should not exist + And the /test-dir/maybe-ignore-me.txt file should exist + And the /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 diff --git a/src/Dist_Archive_Command.php b/src/Dist_Archive_Command.php index 024171e..e3bb705 100644 --- a/src/Dist_Archive_Command.php +++ b/src/Dist_Archive_Command.php @@ -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 ) ) { @@ -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; } } @@ -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();