Skip to content

Commit

Permalink
Normalize pattern, use only regex without enclosing slashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
O'Briat committed Aug 19, 2020
1 parent 5838ebb commit 45055ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/Task/Archive/Pack.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Pack extends BaseTask implements PrintedInterface
private $archiveFile;

/**
* A list of patterns to exclude from the archive.
* A list of regex patterns to exclude from the archive.
*
* @var array
*/
Expand Down Expand Up @@ -146,20 +146,13 @@ public function add($item)
* Allow files or folder to be excluded from the archive. Use regex, without enclosing slashes.
*
* @param string|string[]
* A pattern to be excluded or an array of patterns.
* Zip allows regexp, glob or string.
* @see \Symfony\Component\Finder\Finder::notName
* Tar, gz and bz2 allow only regexp.
* @see \Archive_Tar::setIgnoreList
* A regex (or array of) to be excluded.
*
* @return $this
*/
public function exclude($ignoreList)
{
if (!is_array($ignoreList)) {
$ignoreList = [$ignoreList];
}
$this->ignoreList = $ignoreList;
$this->ignoreList = array_merge($this->ignoreList, (array) $ignoreList);
return $this;
}

Expand Down Expand Up @@ -266,7 +259,9 @@ protected function addItemsToZip($zip, $items)
foreach ($items as $placementLocation => $filesystemLocation) {
if (is_dir($filesystemLocation)) {
$finder = new Finder();
$finder->files()->in($filesystemLocation)->ignoreDotFiles(false)->notName($this->ignoreList);
// Add slashes so Symfony Finder patterns work like Archive_Tar ones.
$zipIgnoreList = preg_filter('/^|$/', '/', $this->ignoreList);
$finder->files()->in($filesystemLocation)->ignoreDotFiles(false)->notName($zipIgnoreList)->notPath($zipIgnoreList);

foreach ($finder as $file) {
// Replace Windows slashes or resulting zip will have issues on *nixes.
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/PackExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function archiveTypeProvider()
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return [['zip']];
}
return [['tar'], ['tar'], ['tar.gz'], ['tar.bz2'], ['tgz']];
return [['zip'], ['tar'], ['tar.gz'], ['tar.bz2'], ['tgz']];
}

/**
Expand All @@ -57,6 +57,7 @@ public function testPackExtract($archiveType)
$result = $this->taskPack("deeply.$archiveType")
->add(['deep' => 'some/deeply'])
->exclude(['structu3.*.re'])
->exclude('nested4')
->run();
$this->assertTrue($result->wasSuccessful(), $result->getMessage());
$this->assertFileExists("deeply.$archiveType");
Expand All @@ -75,6 +76,7 @@ public function testPackExtract($archiveType)
$this->assertFileExists("extracted-$archiveType/nested/structu.re");
$this->assertFileNotExists("extracted-$archiveType/nested3/structu31.re");
$this->assertFileNotExists("extracted-$archiveType/nested3/structu32.re");
$this->assertDirectoryNotExists("extracted-$archiveType/nested4");
// Next, we'll extract the same archive again, this time preserving
// the top-level folder.
$this->taskExtract("deeply.$archiveType")
Expand All @@ -86,6 +88,7 @@ public function testPackExtract($archiveType)
$this->assertFileExists("preserved-$archiveType/deep/nested/structu.re");
$this->assertFileNotExists("preserved-$archiveType/deep/nested3/structu31.re");
$this->assertFileNotExists("preserved-$archiveType/deep/nested3/structu32.re");
$this->assertDirectoryNotExists("preserved-$archiveType/deep/nested4");
// Make another archive, this time composed of fanciful locations
$result = $this->taskPack("composed.$archiveType")
->add(['a/b/existing_file' => 'some/deeply/existing_file'])
Expand Down

0 comments on commit 45055ac

Please sign in to comment.