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;
|
|
|
|
use Composer\Util\Filesystem;
|
|
|
|
|
|
|
|
use Symfony\Component\Process\Process;
|
2013-04-26 09:02:53 +00:00
|
|
|
use Symfony\Component\Process\ExecutableFinder;
|
2013-03-26 11:15:39 +00:00
|
|
|
|
|
|
|
class ArchivableFilesFinderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
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(
|
|
|
|
realpath(sys_get_temp_dir()).'/composer_archiver_test'.uniqid(mt_rand(), true)
|
|
|
|
);
|
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',
|
|
|
|
'!important_too!.txt'
|
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.*',
|
|
|
|
'!*/*/*/prefixC.foo'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->finder = new ArchivableFilesFinder($this->sources, $excludes);
|
|
|
|
|
|
|
|
$this->assertArchivableFiles(array(
|
2014-02-28 10:43:28 +00:00
|
|
|
'/!important!.txt',
|
|
|
|
'/!important_too!.txt',
|
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()
|
|
|
|
{
|
2013-04-25 18:57:58 +00:00
|
|
|
// Ensure that git is available for testing.
|
2013-04-26 09:02:53 +00:00
|
|
|
if (!$this->isProcessAvailable('git')) {
|
2013-04-25 18:57:58 +00:00
|
|
|
return $this->markTestSkipped('git is not available.');
|
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
'\!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',
|
|
|
|
//'!/*/*/prefixC.foo export-ignore'
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->finder = new ArchivableFilesFinder($this->sources, array());
|
|
|
|
|
|
|
|
$this->assertArchivableFiles($this->getArchivedFiles('git init && '.
|
|
|
|
'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()
|
|
|
|
{
|
2013-04-25 18:57:58 +00:00
|
|
|
// Ensure that Mercurial is available for testing.
|
2013-04-26 09:02:53 +00:00
|
|
|
if (!$this->isProcessAvailable('hg')) {
|
2013-04-25 18:57:58 +00:00
|
|
|
return $this->markTestSkipped('Mercurial is not available.');
|
|
|
|
}
|
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());
|
|
|
|
|
|
|
|
$expectedFiles = $this->getArchivedFiles('hg init && '.
|
|
|
|
'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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-04-25 18:57:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether or not the given process is available.
|
|
|
|
*
|
|
|
|
* @param string $process The name of the binary to test.
|
|
|
|
*
|
|
|
|
* @return boolean True if the process is available, false otherwise.
|
|
|
|
*/
|
2013-04-26 09:02:53 +00:00
|
|
|
protected function isProcessAvailable($process)
|
2013-04-25 18:57:58 +00:00
|
|
|
{
|
2013-04-26 09:02:53 +00:00
|
|
|
$finder = new ExecutableFinder();
|
2013-04-25 18:57:58 +00:00
|
|
|
|
2013-04-26 09:02:53 +00:00
|
|
|
return (bool) $finder->find($process);
|
2013-04-25 18:57:58 +00:00
|
|
|
}
|
2013-03-26 11:15:39 +00:00
|
|
|
}
|