From 1d51e54a31dfe0225b3ebc5aaac8618555ec5856 Mon Sep 17 00:00:00 2001 From: Sandy Pleyte Date: Fri, 28 Feb 2014 11:43:28 +0100 Subject: [PATCH] Updated the regexp and added more test cases. --- .../Package/Archiver/BaseExcludeFilter.php | 7 ++- .../Archiver/ArchivableFilesFinderTest.php | 43 +++++++++++++------ .../Package/Archiver/GitExcludeFilterTest.php | 4 +- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/Composer/Package/Archiver/BaseExcludeFilter.php b/src/Composer/Package/Archiver/BaseExcludeFilter.php index 032a32d35..a62d56596 100644 --- a/src/Composer/Package/Archiver/BaseExcludeFilter.php +++ b/src/Composer/Package/Archiver/BaseExcludeFilter.php @@ -136,12 +136,15 @@ abstract class BaseExcludeFilter if (strlen($rule) && $rule[0] === '/') { $pattern .= '^/'; $rule = substr($rule, 1); - } elseif (false === strpos($rule, '/') || strlen($rule) - 1 === strpos($rule, '/')) { + } elseif (strlen($rule) - 1 === strpos($rule, '/')) { + $pattern .= '^/'; + $rule = substr($rule, 0, -1); + } elseif (false === strpos($rule, '/')) { $pattern .= '/'; } // remove delimiters as well as caret (^) and dollar sign ($) from the regex - $pattern .= substr(Finder\Glob::toRegex($rule), 2, -2) . '/*.*$'; + $pattern .= substr(Finder\Glob::toRegex($rule), 2, -2) . '(?=$|/)'; return array($pattern . '#', $negate, false); } diff --git a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php index 7200a699f..ed87ee2f0 100644 --- a/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php @@ -58,12 +58,10 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'D/prefixD', 'D/prefixE', 'D/prefixF', - 'E/prefixA.foo', - 'E/prefixB.foo', - 'E/prefixC.foo', - 'E/prefixD.foo', - 'E/prefixE.foo', - 'E/prefixF.foo', + '/E/subtestA.foo', + '/F/subtestA.foo', + '/G/subtestA.foo', + '/H/subtestA.foo', 'toplevelA.foo', 'toplevelB.foo', 'prefixA.foo', @@ -72,6 +70,10 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'prefixD.foo', 'prefixE.foo', 'prefixF.foo', + 'parameters.yml', + 'parameters.yml.dist', + '!important!.txt', + '!important_too!.txt' ); foreach ($fileTree as $relativePath) { @@ -100,6 +102,8 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase $this->finder = new ArchivableFilesFinder($this->sources, $excludes); $this->assertArchivableFiles(array( + '/!important!.txt', + '/!important_too!.txt', '/A/prefixA.foo', '/A/prefixD.foo', '/A/prefixE.foo', @@ -119,10 +123,12 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase '/D/prefixD', '/D/prefixE', '/D/prefixF', - '/E/prefixA.foo', - '/E/prefixD.foo', - '/E/prefixE.foo', - '/E/prefixF.foo', + '/E/subtestA.foo', + '/F/subtestA.foo', + '/G/subtestA.foo', + '/H/subtestA.foo', + '/parameters.yml', + '/parameters.yml.dist', '/prefixB.foo', '/prefixD.foo', '/prefixE.foo', @@ -154,7 +160,12 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'refixD.foo', '/C', 'D/prefixA', - 'E/' + 'E', + 'F/', + 'G/*', + 'H/**', + 'parameters.yml', + '\!important!.txt' ))); // git does not currently support negative git attributes @@ -195,9 +206,15 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase '# comments', '', '^prefixD.foo', + 'D/prefixA', + 'parameters.yml', + '\!important!.txt', + 'E', + 'F/', 'syntax: glob', 'prefixF.*', 'B/*', + 'H/**', ))); $this->finder = new ArchivableFilesFinder($this->sources, array()); @@ -208,7 +225,9 @@ class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase 'hg archive archive.zip' ); - array_shift($expectedFiles); // remove .hg_archival.txt + // Remove .hg_archival.txt from the expectedFiles + $archiveKey = array_search('/.hg_archival.txt', $expectedFiles); + array_splice($expectedFiles, $archiveKey, 1); $this->assertArchivableFiles($expectedFiles); } diff --git a/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php b/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php index e2eef1722..97c02c8e6 100644 --- a/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php +++ b/tests/Composer/Test/Package/Archiver/GitExcludeFilterTest.php @@ -29,8 +29,8 @@ class GitExcludeFilterTest extends \PHPUnit_Framework_TestCase public function patterns() { return array( - array('app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml/*.*$#', false, false)), - array('!app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml/*.*$#', true, false)), + array('app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml(?=$|/)#', false, false)), + array('!app/config/parameters.yml', array('#(?=[^\.])app/(?=[^\.])config/(?=[^\.])parameters\.yml(?=$|/)#', true, false)), ); } }