1
0
Fork 0

Remove CWD from only the beginning of a path in ZipArchiver

Fixes https://github.com/composer/composer/issues/9403
pull/9404/head
Tanel Pipar 2020-11-03 12:01:48 +02:00
parent cdd7460f9f
commit 8f454c6708
2 changed files with 25 additions and 10 deletions

View File

@ -39,7 +39,10 @@ class ZipArchiver implements ArchiverInterface
foreach ($files as $file) { foreach ($files as $file) {
/** @var \SplFileInfo $file */ /** @var \SplFileInfo $file */
$filepath = strtr($file->getPath()."/".$file->getFilename(), '\\', '/'); $filepath = strtr($file->getPath()."/".$file->getFilename(), '\\', '/');
$localname = str_replace($sources.'/', '', $filepath); $localname = $filepath;
if (strpos($localname, $sources . '/') === 0) {
$localname = substr($localname, strlen($sources . '/'));
}
if ($file->isDir()) { if ($file->isDir()) {
$zip->addEmptyDir($localname); $zip->addEmptyDir($localname);
} else { } else {

View File

@ -12,6 +12,7 @@
namespace Composer\Test\Package\Archiver; namespace Composer\Test\Package\Archiver;
use ZipArchive;
use Composer\Package\Archiver\ZipArchiver; use Composer\Package\Archiver\ZipArchiver;
class ZipArchiverTest extends ArchiverTest class ZipArchiverTest extends ArchiverTest
@ -22,8 +23,15 @@ class ZipArchiverTest extends ArchiverTest
$this->markTestSkipped('Cannot run ZipArchiverTest, missing class "ZipArchive".'); $this->markTestSkipped('Cannot run ZipArchiverTest, missing class "ZipArchive".');
} }
$files = array(
'file.txt',
'foo/bar/baz',
'x/baz',
'x/includeme',
'foo' . getcwd() . '/file.txt',
);
// Set up repository // Set up repository
$this->setupDummyRepo(); $this->setupDummyRepo(array_merge($files));
$package = $this->setupPackage(); $package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.zip'; $target = sys_get_temp_dir().'/composer_archiver_test.zip';
@ -31,23 +39,27 @@ class ZipArchiverTest extends ArchiverTest
$archiver = new ZipArchiver(); $archiver = new ZipArchiver();
$archiver->archive($package->getSourceUrl(), $target, 'zip'); $archiver->archive($package->getSourceUrl(), $target, 'zip');
$this->assertFileExists($target); $this->assertFileExists($target);
$zip = new ZipArchive();
$res = $zip->open($target);
self::assertTrue($res, 'Failed asserting that Zip file can be opened');
foreach ($files as $file) {
$this->assertSame('content', $zip->getFromName($file), 'Failed asserting that Zip contains ' . $file);
}
unlink($target); unlink($target);
} }
/** /**
* Create a local dummy repository to run tests against! * Create a local dummy repository to run tests against!
* @param array $files
*/ */
protected function setupDummyRepo() protected function setupDummyRepo($files)
{ {
$currentWorkDir = getcwd(); $currentWorkDir = getcwd();
chdir($this->testDir); chdir($this->testDir);
foreach ($files as $file) {
$this->writeFile('file.txt', 'content', $currentWorkDir); $this->writeFile($file, 'content', $currentWorkDir);
$this->writeFile('foo/bar/baz', 'content', $currentWorkDir); }
$this->writeFile('foo/bar/ignoreme', 'content', $currentWorkDir);
$this->writeFile('x/baz', 'content', $currentWorkDir);
$this->writeFile('x/includeme', 'content', $currentWorkDir);
chdir($currentWorkDir); chdir($currentWorkDir);
} }
@ -58,7 +70,7 @@ class ZipArchiverTest extends ArchiverTest
mkdir(dirname($path), 0777, true); mkdir(dirname($path), 0777, true);
} }
$result = file_put_contents($path, 'a'); $result = file_put_contents($path, $content);
if (false === $result) { if (false === $result) {
chdir($currentWorkDir); chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.'); throw new \RuntimeException('Could not save file.');