From 8f454c67084f0364f294030255c39c37ca19990d Mon Sep 17 00:00:00 2001 From: Tanel Pipar Date: Tue, 3 Nov 2020 12:01:48 +0200 Subject: [PATCH 1/4] Remove CWD from only the beginning of a path in ZipArchiver Fixes https://github.com/composer/composer/issues/9403 --- src/Composer/Package/Archiver/ZipArchiver.php | 5 +++- .../Test/Package/Archiver/ZipArchiverTest.php | 30 +++++++++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/Composer/Package/Archiver/ZipArchiver.php b/src/Composer/Package/Archiver/ZipArchiver.php index b6cfaf73f..ec241a026 100644 --- a/src/Composer/Package/Archiver/ZipArchiver.php +++ b/src/Composer/Package/Archiver/ZipArchiver.php @@ -39,7 +39,10 @@ class ZipArchiver implements ArchiverInterface foreach ($files as $file) { /** @var \SplFileInfo $file */ $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()) { $zip->addEmptyDir($localname); } else { diff --git a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php index c76843c39..a11320325 100644 --- a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php @@ -12,6 +12,7 @@ namespace Composer\Test\Package\Archiver; +use ZipArchive; use Composer\Package\Archiver\ZipArchiver; class ZipArchiverTest extends ArchiverTest @@ -22,8 +23,15 @@ class ZipArchiverTest extends ArchiverTest $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 - $this->setupDummyRepo(); + $this->setupDummyRepo(array_merge($files)); $package = $this->setupPackage(); $target = sys_get_temp_dir().'/composer_archiver_test.zip'; @@ -31,23 +39,27 @@ class ZipArchiverTest extends ArchiverTest $archiver = new ZipArchiver(); $archiver->archive($package->getSourceUrl(), $target, 'zip'); $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); } /** * Create a local dummy repository to run tests against! + * @param array $files */ - protected function setupDummyRepo() + protected function setupDummyRepo($files) { $currentWorkDir = getcwd(); chdir($this->testDir); - - $this->writeFile('file.txt', '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); + foreach ($files as $file) { + $this->writeFile($file, 'content', $currentWorkDir); + } chdir($currentWorkDir); } @@ -58,7 +70,7 @@ class ZipArchiverTest extends ArchiverTest mkdir(dirname($path), 0777, true); } - $result = file_put_contents($path, 'a'); + $result = file_put_contents($path, $content); if (false === $result) { chdir($currentWorkDir); throw new \RuntimeException('Could not save file.'); From 4d05cbffe308018b59b0ecac743331be79bda957 Mon Sep 17 00:00:00 2001 From: Tanel Pipar Date: Tue, 3 Nov 2020 12:04:02 +0200 Subject: [PATCH 2/4] Remove CWD from only the beginning of a path in ZipArchiver Fixes https://github.com/composer/composer/issues/9403 --- tests/Composer/Test/Package/Archiver/ZipArchiverTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php index a11320325..47a9d7c7f 100644 --- a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php @@ -31,7 +31,7 @@ class ZipArchiverTest extends ArchiverTest 'foo' . getcwd() . '/file.txt', ); // Set up repository - $this->setupDummyRepo(array_merge($files)); + $this->setupDummyRepo($files); $package = $this->setupPackage(); $target = sys_get_temp_dir().'/composer_archiver_test.zip'; From b57792a80a7b911c866dcb7c36880ef06e8f639b Mon Sep 17 00:00:00 2001 From: Tanel Pipar Date: Tue, 3 Nov 2020 12:20:44 +0200 Subject: [PATCH 3/4] Skip CWD-in-path test on Windows --- tests/Composer/Test/Package/Archiver/ZipArchiverTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php index 47a9d7c7f..2359c4808 100644 --- a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php @@ -12,6 +12,7 @@ namespace Composer\Test\Package\Archiver; +use Composer\Util\Platform; use ZipArchive; use Composer\Package\Archiver\ZipArchiver; @@ -28,8 +29,10 @@ class ZipArchiverTest extends ArchiverTest 'foo/bar/baz', 'x/baz', 'x/includeme', - 'foo' . getcwd() . '/file.txt', ); + if (!Platform::isWindows()) { + $files[] = 'foo' . getcwd() . '/file.txt'; + } // Set up repository $this->setupDummyRepo($files); $package = $this->setupPackage(); From ad2fcf8d9223bb018c5e478b4656b4f6a14d2bc1 Mon Sep 17 00:00:00 2001 From: Tanel Pipar Date: Tue, 3 Nov 2020 12:27:15 +0200 Subject: [PATCH 4/4] Close zip archive after test finishes --- tests/Composer/Test/Package/Archiver/ZipArchiverTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php index 2359c4808..be1b5b6d1 100644 --- a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php @@ -48,6 +48,7 @@ class ZipArchiverTest extends ArchiverTest foreach ($files as $file) { $this->assertSame('content', $zip->getFromName($file), 'Failed asserting that Zip contains ' . $file); } + $zip->close(); unlink($target); }