Skip to content

Commit

Permalink
Merge pull request #175 from wp-cli/try/172-exclude-include
Browse files Browse the repository at this point in the history
Investigate include/exclude oddities
  • Loading branch information
schlessera committed Aug 13, 2019
2 parents 162affb + 3f5d261 commit 0912b1e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,7 +1,7 @@
.DS_Store
wp-cli.local.yml
node_modules/
vendor/
/node_modules
/vendor
*.zip
*.tar.gz
*.swp
Expand Down
20 changes: 17 additions & 3 deletions src/IterableCodeExtractor.php
Expand Up @@ -205,6 +205,10 @@ function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions ) {
/** @var RecursiveCallbackFilterIterator $iterator */
/** @var SplFileInfo $file */

// Normalize include and exclude paths.
$include = array_map( 'static::trim_leading_slash', $include );
$exclude = array_map( 'static::trim_leading_slash', $exclude );

// If no $include is passed everything gets the weakest possible matching score.
$inclusion_score = empty( $include ) ? 0.1 : static::calculateMatchScore( $file, $include );
$exclusion_score = static::calculateMatchScore( $file, $exclude );
Expand All @@ -214,12 +218,12 @@ function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions ) {
return true;
}

if ( 0 === $inclusion_score || $exclusion_score > $inclusion_score ) {
if ( ( 0 === $inclusion_score || $exclusion_score > $inclusion_score ) && $iterator->hasChildren() ) {
// Always include directories that may have matching children even if they are excluded.
return $iterator->hasChildren() && static::containsMatchingChildren( $file, $include );
return static::containsMatchingChildren( $file, $include );
}

return ( $file->isFile() && in_array( $file->getExtension(), $extensions, true ) );
return ( ( $inclusion_score >= $exclusion_score ) && $file->isFile() && in_array( $file->getExtension(), $extensions, true ) );
}
),
RecursiveIteratorIterator::CHILD_FIRST
Expand All @@ -238,4 +242,14 @@ function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions ) {

return $filtered_files;
}

/**
* Trim leading slash from a path.
*
* @param string $path Path to trim.
* @return string Trimmed path.
*/
private static function trim_leading_slash( $path ) {
return ltrim( $path, '/' );
}
}
25 changes: 25 additions & 0 deletions tests/IterableCodeExtractorTest.php
Expand Up @@ -120,7 +120,32 @@ public function test_can_return_all_directory_files_sorted() {
static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php',
static::$base . 'foo/bar/foofoo/included.js',
static::$base . 'hoge/should_NOT_be_included.js',
static::$base . 'vendor/vendor-file.php',
);
$this->assertEquals( $expected, $result );
}

public function test_can_include_file_in_excluded_folder() {
$includes = [ 'vendor/vendor-file.php' ];
$excludes = [ 'vendor' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'vendor/vendor-file.php';
$this->assertContains( $expected, $result );
}

public function test_can_include_file_in_excluded_folder_with_leading_slash() {
$includes = [ '/vendor/vendor-file.php' ];
$excludes = [ 'vendor' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'vendor/vendor-file.php';
$this->assertContains( $expected, $result );
}

public function test_can_include_file_in_excluded_folder_by_wildcard() {
$includes = [ 'vendor/**' ];
$excludes = [ 'vendor' ];
$result = IterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] );
$expected = static::$base . 'vendor/vendor-file.php';
$this->assertContains( $expected, $result );
}
}
Empty file.

0 comments on commit 0912b1e

Please sign in to comment.