2013-03-26 11:15:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Package\Archiver;
|
|
|
|
|
|
|
|
use Composer\Package\Archiver\ArchivableFilesFinder;
|
2016-01-21 12:01:55 +00:00
|
|
|
use Composer\TestCase;
|
2013-03-26 11:15:39 +00:00
|
|
|
use Composer\Util\Filesystem;
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
class ArchivableFilesFinderTest extends TestCase
|
2013-03-26 11:15:39 +00:00
|
|
|
{
|
|
|
|
protected $sources;
|
|
|
|
protected $finder;
|
2013-04-02 09:32:11 +00:00
|
|
|
protected $fs;
|
2013-03-26 11:15:39 +00:00
|
|
|
|
2013-04-02 09:32:11 +00:00
|
|
|
protected function setUp()
|
2013-03-26 11:15:39 +00:00
|
|
|
{
|
|
|
|
$fs = new Filesystem;
|
2013-04-02 09:32:11 +00:00
|
|
|
$this->fs = $fs;
|
2013-03-26 11:15:39 +00:00
|
|
|
|
2013-04-02 09:32:11 +00:00
|
|
|
$this->sources = $fs->normalizePath(
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->getUniqueTmpDirectory()
|
2013-04-02 09:32:11 +00:00
|
|
|
);
|
2013-03-26 11:15:39 +00:00
|
|
|
|
|
|
|
$fileTree = array(
|
|
|
|
'A/prefixA.foo',
|
|
|
|
'A/prefixB.foo',
|
|
|
|
'A/prefixC.foo',
|
|
|
|
'A/prefixD.foo',
|
|
|
|
'A/prefixE.foo',
|
|
|
|
'A/prefixF.foo',
|
|
|
|
'B/sub/prefixA.foo',
|
|
|
|
'B/sub/prefixB.foo',
|
|
|
|
'B/sub/prefixC.foo',
|
|
|
|
'B/sub/prefixD.foo',
|
|
|
|
'B/sub/prefixE.foo',
|
|
|
|
'B/sub/prefixF.foo',
|
2014-02-27 14:17:15 +00:00
|
|
|
'C/prefixA.foo',
|
|
|
|
'C/prefixB.foo',
|
|
|
|
'C/prefixC.foo',
|
|
|
|
'C/prefixD.foo',
|
|
|
|
'C/prefixE.foo',
|
|
|
|
'C/prefixF.foo',
|
|
|
|
'D/prefixA',
|
|
|
|
'D/prefixB',
|
|
|
|
'D/prefixC',
|
|
|
|
'D/prefixD',
|
|
|
|
'D/prefixE',
|
|
|
|
'D/prefixF',
|
2014-02-28 15:30:55 +00:00
|
|
|
'E/subtestA.foo',
|
|
|
|
'F/subtestA.foo',
|
|
|
|
'G/subtestA.foo',
|
|
|
|
'H/subtestA.foo',
|
|
|
|
'I/J/subtestA.foo',
|
|
|
|
'K/dirJ/subtestA.foo',
|
2013-03-26 11:15:39 +00:00
|
|
|
'toplevelA.foo',
|
|
|
|
'toplevelB.foo',
|
|
|
|
'prefixA.foo',
|
|
|
|
'prefixB.foo',
|
|
|
|
'prefixC.foo',
|
|
|
|
'prefixD.foo',
|
|
|
|
'prefixE.foo',
|
|
|
|
'prefixF.foo',
|
2014-02-28 10:43:28 +00:00
|
|
|
'parameters.yml',
|
|
|
|
'parameters.yml.dist',
|
|
|
|
'!important!.txt',
|
2015-06-29 07:42:40 +00:00
|
|
|
'!important_too!.txt',
|
|
|
|
'#weirdfile',
|
2013-03-26 11:15:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($fileTree as $relativePath) {
|
|
|
|
$path = $this->sources.'/'.$relativePath;
|
|
|
|
$fs->ensureDirectoryExists(dirname($path));
|
|
|
|
file_put_contents($path, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
$fs = new Filesystem;
|
|
|
|
$fs->removeDirectory($this->sources);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManualExcludes()
|
|
|
|
{
|
|
|
|
$excludes = array(
|
|
|
|
'prefixB.foo',
|
|
|
|
'!/prefixB.foo',
|
|
|
|
'/prefixA.foo',
|
|
|
|
'prefixC.*',
|
2015-06-29 07:42:40 +00:00
|
|
|
'!*/*/*/prefixC.foo',
|
2013-03-26 11:15:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->finder = new ArchivableFilesFinder($this->sources, $excludes);
|
|
|
|
|
|
|
|
$this->assertArchivableFiles(array(
|
2014-02-28 10:43:28 +00:00
|
|
|
'/!important!.txt',
|
|
|
|
'/!important_too!.txt',
|
2015-06-29 07:42:40 +00:00
|
|
|
'/#weirdfile',
|
2013-03-26 11:15:39 +00:00
|
|
|
'/A/prefixA.foo',
|
|
|
|
'/A/prefixD.foo',
|
|
|
|
'/A/prefixE.foo',
|
|
|
|
'/A/prefixF.foo',
|
|
|
|
'/B/sub/prefixA.foo',
|
|
|
|
'/B/sub/prefixC.foo',
|
|
|
|
'/B/sub/prefixD.foo',
|
|
|
|
'/B/sub/prefixE.foo',
|
|
|
|
'/B/sub/prefixF.foo',
|
2014-02-27 14:17:15 +00:00
|
|
|
'/C/prefixA.foo',
|
|
|
|
'/C/prefixD.foo',
|
|
|
|
'/C/prefixE.foo',
|
|
|
|
'/C/prefixF.foo',
|
|
|
|
'/D/prefixA',
|
|
|
|
'/D/prefixB',
|
|
|
|
'/D/prefixC',
|
|
|
|
'/D/prefixD',
|
|
|
|
'/D/prefixE',
|
|
|
|
'/D/prefixF',
|
2014-02-28 10:43:28 +00:00
|
|
|
'/E/subtestA.foo',
|
|
|
|
'/F/subtestA.foo',
|
|
|
|
'/G/subtestA.foo',
|
|
|
|
'/H/subtestA.foo',
|
2014-02-28 15:30:55 +00:00
|
|
|
'/I/J/subtestA.foo',
|
|
|
|
'/K/dirJ/subtestA.foo',
|
2014-02-28 10:43:28 +00:00
|
|
|
'/parameters.yml',
|
|
|
|
'/parameters.yml.dist',
|
2013-03-26 11:15:39 +00:00
|
|
|
'/prefixB.foo',
|
|
|
|
'/prefixD.foo',
|
|
|
|
'/prefixE.foo',
|
|
|
|
'/prefixF.foo',
|
|
|
|
'/toplevelA.foo',
|
|
|
|
'/toplevelB.foo',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGitExcludes()
|
|
|
|
{
|
2016-12-22 17:14:57 +00:00
|
|
|
$this->skipIfNotExecutable('git');
|
2013-04-25 18:57:58 +00:00
|
|
|
|
2013-03-26 11:15:39 +00:00
|
|
|
file_put_contents($this->sources.'/.gitignore', implode("\n", array(
|
|
|
|
'# gitignore rules with comments and blank lines',
|
|
|
|
'',
|
|
|
|
'prefixE.foo',
|
|
|
|
'# and more',
|
|
|
|
'# comments',
|
|
|
|
'',
|
|
|
|
'!/prefixE.foo',
|
|
|
|
'/prefixD.foo',
|
|
|
|
'prefixF.*',
|
|
|
|
'!/*/*/prefixF.foo',
|
|
|
|
'',
|
2013-04-01 21:04:00 +00:00
|
|
|
'refixD.foo',
|
2014-02-27 14:17:15 +00:00
|
|
|
'/C',
|
|
|
|
'D/prefixA',
|
2014-02-28 10:43:28 +00:00
|
|
|
'E',
|
|
|
|
'F/',
|
|
|
|
'G/*',
|
|
|
|
'H/**',
|
2014-02-28 15:30:55 +00:00
|
|
|
'J/',
|
2014-02-28 10:43:28 +00:00
|
|
|
'parameters.yml',
|
2015-06-29 07:42:40 +00:00
|
|
|
'\!important!.txt',
|
|
|
|
'\#*',
|
2013-03-26 11:15:39 +00:00
|
|
|
)));
|
|
|
|
|
|
|
|
// git does not currently support negative git attributes
|
|
|
|
file_put_contents($this->sources.'/.gitattributes', implode("\n", array(
|
|
|
|
'',
|
|
|
|
'# gitattributes rules with comments and blank lines',
|
|
|
|
'prefixB.foo export-ignore',
|
|
|
|
//'!/prefixB.foo export-ignore',
|
|
|
|
'/prefixA.foo export-ignore',
|
|
|
|
'prefixC.* export-ignore',
|
2015-06-29 07:42:40 +00:00
|
|
|
//'!/*/*/prefixC.foo export-ignore',
|
2013-03-26 11:15:39 +00:00
|
|
|
)));
|
|
|
|
|
|
|
|
$this->finder = new ArchivableFilesFinder($this->sources, array());
|
|
|
|
|
2018-07-24 12:32:52 +00:00
|
|
|
$this->assertArchivableFiles($this->getArchivedFiles(
|
|
|
|
'git init && '.
|
2015-02-16 03:20:45 +00:00
|
|
|
'git config user.email "you@example.com" && '.
|
|
|
|
'git config user.name "Your Name" && '.
|
2013-03-26 11:15:39 +00:00
|
|
|
'git add .git* && '.
|
|
|
|
'git commit -m "ignore rules" && '.
|
|
|
|
'git add . && '.
|
|
|
|
'git commit -m "init" && '.
|
|
|
|
'git archive --format=zip --prefix=archive/ -o archive.zip HEAD'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHgExcludes()
|
|
|
|
{
|
2016-12-22 17:14:57 +00:00
|
|
|
$this->skipIfNotExecutable('hg');
|
2013-04-26 09:02:53 +00:00
|
|
|
|
2013-03-26 11:15:39 +00:00
|
|
|
file_put_contents($this->sources.'/.hgignore', implode("\n", array(
|
|
|
|
'# hgignore rules with comments, blank lines and syntax changes',
|
|
|
|
'',
|
|
|
|
'pre*A.foo',
|
|
|
|
'prefixE.foo',
|
|
|
|
'# and more',
|
|
|
|
'# comments',
|
|
|
|
'',
|
|
|
|
'^prefixD.foo',
|
2014-02-28 10:43:28 +00:00
|
|
|
'D/prefixA',
|
|
|
|
'parameters.yml',
|
|
|
|
'\!important!.txt',
|
|
|
|
'E',
|
|
|
|
'F/',
|
2013-03-26 11:15:39 +00:00
|
|
|
'syntax: glob',
|
|
|
|
'prefixF.*',
|
|
|
|
'B/*',
|
2014-02-28 10:43:28 +00:00
|
|
|
'H/**',
|
2013-03-26 11:15:39 +00:00
|
|
|
)));
|
|
|
|
|
|
|
|
$this->finder = new ArchivableFilesFinder($this->sources, array());
|
|
|
|
|
2018-07-24 12:32:52 +00:00
|
|
|
$expectedFiles = $this->getArchivedFiles(
|
|
|
|
'hg init && '.
|
2013-03-26 11:15:39 +00:00
|
|
|
'hg add && '.
|
|
|
|
'hg commit -m "init" && '.
|
|
|
|
'hg archive archive.zip'
|
|
|
|
);
|
|
|
|
|
2014-02-28 10:43:28 +00:00
|
|
|
// Remove .hg_archival.txt from the expectedFiles
|
|
|
|
$archiveKey = array_search('/.hg_archival.txt', $expectedFiles);
|
|
|
|
array_splice($expectedFiles, $archiveKey, 1);
|
2013-03-26 11:15:39 +00:00
|
|
|
|
|
|
|
$this->assertArchivableFiles($expectedFiles);
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:15:20 +00:00
|
|
|
public function testSkipExcludes()
|
|
|
|
{
|
|
|
|
$excludes = array(
|
|
|
|
'prefixB.foo',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->finder = new ArchivableFilesFinder($this->sources, $excludes, true);
|
|
|
|
|
|
|
|
$this->assertArchivableFiles(array(
|
|
|
|
'/!important!.txt',
|
|
|
|
'/!important_too!.txt',
|
|
|
|
'/#weirdfile',
|
|
|
|
'/A/prefixA.foo',
|
|
|
|
'/A/prefixB.foo',
|
|
|
|
'/A/prefixC.foo',
|
|
|
|
'/A/prefixD.foo',
|
|
|
|
'/A/prefixE.foo',
|
|
|
|
'/A/prefixF.foo',
|
|
|
|
'/B/sub/prefixA.foo',
|
|
|
|
'/B/sub/prefixB.foo',
|
|
|
|
'/B/sub/prefixC.foo',
|
|
|
|
'/B/sub/prefixD.foo',
|
|
|
|
'/B/sub/prefixE.foo',
|
|
|
|
'/B/sub/prefixF.foo',
|
|
|
|
'/C/prefixA.foo',
|
|
|
|
'/C/prefixB.foo',
|
|
|
|
'/C/prefixC.foo',
|
|
|
|
'/C/prefixD.foo',
|
|
|
|
'/C/prefixE.foo',
|
|
|
|
'/C/prefixF.foo',
|
|
|
|
'/D/prefixA',
|
|
|
|
'/D/prefixB',
|
|
|
|
'/D/prefixC',
|
|
|
|
'/D/prefixD',
|
|
|
|
'/D/prefixE',
|
|
|
|
'/D/prefixF',
|
|
|
|
'/E/subtestA.foo',
|
|
|
|
'/F/subtestA.foo',
|
|
|
|
'/G/subtestA.foo',
|
|
|
|
'/H/subtestA.foo',
|
|
|
|
'/I/J/subtestA.foo',
|
|
|
|
'/K/dirJ/subtestA.foo',
|
|
|
|
'/parameters.yml',
|
|
|
|
'/parameters.yml.dist',
|
|
|
|
'/prefixA.foo',
|
|
|
|
'/prefixB.foo',
|
|
|
|
'/prefixC.foo',
|
|
|
|
'/prefixD.foo',
|
|
|
|
'/prefixE.foo',
|
|
|
|
'/prefixF.foo',
|
|
|
|
'/toplevelA.foo',
|
|
|
|
'/toplevelB.foo',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:15:39 +00:00
|
|
|
protected function getArchivableFiles()
|
|
|
|
{
|
|
|
|
$files = array();
|
2013-03-26 12:02:32 +00:00
|
|
|
foreach ($this->finder as $file) {
|
2013-03-26 11:15:39 +00:00
|
|
|
if (!$file->isDir()) {
|
2013-04-02 09:32:11 +00:00
|
|
|
$files[] = preg_replace('#^'.preg_quote($this->sources, '#').'#', '', $this->fs->normalizePath($file->getRealPath()));
|
2013-03-26 11:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort($files);
|
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getArchivedFiles($command)
|
|
|
|
{
|
|
|
|
$process = new Process($command, $this->sources);
|
|
|
|
$process->run();
|
|
|
|
|
|
|
|
$archive = new \PharData($this->sources.'/archive.zip');
|
|
|
|
$iterator = new \RecursiveIteratorIterator($archive);
|
|
|
|
|
|
|
|
$files = array();
|
|
|
|
foreach ($iterator as $file) {
|
2013-04-02 09:32:11 +00:00
|
|
|
$files[] = preg_replace('#^phar://'.preg_quote($this->sources, '#').'/archive\.zip/archive#', '', $this->fs->normalizePath($file));
|
2013-03-26 11:15:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 09:32:11 +00:00
|
|
|
unset($archive, $iterator, $file);
|
2013-03-26 11:15:39 +00:00
|
|
|
unlink($this->sources.'/archive.zip');
|
2013-04-02 09:32:11 +00:00
|
|
|
|
2013-03-26 11:15:39 +00:00
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertArchivableFiles($expectedFiles)
|
|
|
|
{
|
|
|
|
$actualFiles = $this->getArchivableFiles();
|
|
|
|
|
|
|
|
$this->assertEquals($expectedFiles, $actualFiles);
|
|
|
|
}
|
|
|
|
}
|