1
0
Fork 0
composer/tests/Composer/Test/Package/Archiver/ArchivableFilesFinderTest.php

312 lines
8.5 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
/*
* 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\Pcre\Preg;
use Composer\Test\TestCase;
use Composer\Util\Filesystem;
use Symfony\Component\Process\Process;
class ArchivableFilesFinderTest extends TestCase
{
2021-10-16 08:16:06 +00:00
/**
* @var string
*/
protected $sources;
2021-10-16 08:16:06 +00:00
/**
* @var ArchivableFilesFinder
*/
protected $finder;
2021-10-16 08:16:06 +00:00
/**
* @var Filesystem
*/
2013-04-02 09:32:11 +00:00
protected $fs;
2021-12-08 16:03:05 +00:00
protected function setUp(): void
{
$fs = new Filesystem;
2013-04-02 09:32:11 +00:00
$this->fs = $fs;
2013-04-02 09:32:11 +00:00
$this->sources = $fs->normalizePath(
self::getUniqueTmpDirectory()
2013-04-02 09:32:11 +00:00
);
2022-08-17 12:20:07 +00:00
$fileTree = [
'.foo',
'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',
'toplevelA.foo',
'toplevelB.foo',
'prefixA.foo',
'prefixB.foo',
'prefixC.foo',
'prefixD.foo',
'prefixE.foo',
'prefixF.foo',
'parameters.yml',
'parameters.yml.dist',
'!important!.txt',
2015-06-29 07:42:40 +00:00
'!important_too!.txt',
'#weirdfile',
2022-08-17 12:20:07 +00:00
];
foreach ($fileTree as $relativePath) {
$path = $this->sources.'/'.$relativePath;
$fs->ensureDirectoryExists(dirname($path));
file_put_contents($path, '');
}
}
2021-12-08 16:03:05 +00:00
protected function tearDown(): void
{
parent::tearDown();
$fs = new Filesystem;
$fs->removeDirectory($this->sources);
}
public function testManualExcludes(): void
{
2022-08-17 12:20:07 +00:00
$excludes = [
'prefixB.foo',
'!/prefixB.foo',
'/prefixA.foo',
'prefixC.*',
2015-06-29 07:42:40 +00:00
'!*/*/*/prefixC.foo',
'.*',
2022-08-17 12:20:07 +00:00
];
$this->finder = new ArchivableFilesFinder($this->sources, $excludes);
2022-08-17 12:20:07 +00:00
$this->assertArchivableFiles([
'/!important!.txt',
'/!important_too!.txt',
2015-06-29 07:42:40 +00:00
'/#weirdfile',
'/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',
'/C/prefixA.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',
'/prefixB.foo',
'/prefixD.foo',
'/prefixE.foo',
'/prefixF.foo',
'/toplevelA.foo',
'/toplevelB.foo',
2022-08-17 12:20:07 +00:00
]);
}
public function testGitExcludes(): void
{
$this->skipIfNotExecutable('git');
2022-08-17 12:20:07 +00:00
file_put_contents($this->sources.'/.gitattributes', implode("\n", [
'',
'# gitattributes rules with comments and blank lines',
'prefixB.foo export-ignore',
'/prefixA.foo export-ignore',
'prefixC.* export-ignore',
'',
'prefixE.foo export-ignore',
'# and more',
'# comments',
'',
'/prefixE.foo -export-ignore',
'/prefixD.foo export-ignore',
'prefixF.* export-ignore',
'/*/*/prefixF.foo -export-ignore',
'',
'refixD.foo export-ignore',
'/C export-ignore',
'D/prefixA export-ignore',
'E export-ignore',
'F/ export-ignore',
'G/* export-ignore',
'H/** export-ignore',
'J/ export-ignore',
'parameters.yml export-ignore',
'\!important!.txt export-ignore',
'\#* export-ignore',
2022-08-17 12:20:07 +00:00
]));
2022-08-17 12:20:07 +00:00
$this->finder = new ArchivableFilesFinder($this->sources, []);
2018-07-24 12:32:52 +00:00
$this->assertArchivableFiles($this->getArchivedFiles(
'git init && '.
'git config user.email "you@example.com" && '.
'git config user.name "Your Name" && '.
'git config commit.gpgsign false && '.
'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 testSkipExcludes(): void
{
2022-08-17 12:20:07 +00:00
$excludes = [
'prefixB.foo',
2022-08-17 12:20:07 +00:00
];
$this->finder = new ArchivableFilesFinder($this->sources, $excludes, true);
2022-08-17 12:20:07 +00:00
$this->assertArchivableFiles([
'/!important!.txt',
'/!important_too!.txt',
'/#weirdfile',
'/.foo',
'/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',
2022-08-17 12:20:07 +00:00
]);
}
2021-11-01 20:44:12 +00:00
/**
* @return string[]
*/
protected function getArchivableFiles(): array
{
2022-08-17 12:20:07 +00:00
$files = [];
foreach ($this->finder as $file) {
if (!$file->isDir()) {
$files[] = Preg::replace('#^'.preg_quote($this->sources, '#').'#', '', $this->fs->normalizePath($file->getRealPath()));
}
}
sort($files);
return $files;
}
2021-11-01 20:44:12 +00:00
/**
* @return string[]
*/
2022-02-22 15:47:09 +00:00
protected function getArchivedFiles(string $command): array
{
$process = Process::fromShellCommandline($command, $this->sources);
$process->run();
$archive = new \PharData($this->sources.'/archive.zip');
$iterator = new \RecursiveIteratorIterator($archive);
2022-08-17 12:20:07 +00:00
$files = [];
foreach ($iterator as $file) {
2022-02-23 15:57:47 +00:00
$files[] = Preg::replace('#^phar://'.preg_quote($this->sources, '#').'/archive\.zip/archive#', '', $this->fs->normalizePath((string) $file));
}
2013-04-02 09:32:11 +00:00
unset($archive, $iterator, $file);
unlink($this->sources.'/archive.zip');
2013-04-02 09:32:11 +00:00
return $files;
}
2021-11-01 20:44:12 +00:00
/**
* @param string[] $expectedFiles
*/
2022-02-22 15:47:09 +00:00
protected function assertArchivableFiles(array $expectedFiles): void
{
$actualFiles = $this->getArchivableFiles();
$this->assertEquals($expectedFiles, $actualFiles);
}
}