1
0
Fork 0

Fix issues and improve Output from ZipArchiverTest (#12324)

main
Philipp Scheit 2025-02-25 12:51:21 +01:00 committed by GitHub
parent e44b2d8872
commit 46e3110257
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 20 deletions

View File

@ -18,14 +18,33 @@ use Composer\Package\Archiver\ZipArchiver;
class ZipArchiverTest extends ArchiverTestCase
{
/** @var list<string> */
private $filesToCleanup = [];
public function testSimpleFiles(): void
{
$files = [
'file.txt' => null,
'foo/bar/baz' => null,
'x/baz' => null,
'x/includeme' => null,
];
if (!Platform::isWindows()) {
$files['zfoo' . Platform::getCwd() . '/file.txt'] = null;
}
$this->assertZipArchive($files);
}
/**
* @dataProvider provideGitignoreExcludeNegationTestCases
*/
public function testGitignoreExcludeNegation(string $include): void
{
$this->testZipArchive([
'docs/README.md' => '# The doc',
$this->assertZipArchive([
'.gitignore' => "/*\n.*\n!.git*\n$include",
'docs/README.md' => '# The doc',
]);
}
@ -49,30 +68,18 @@ class ZipArchiverTest extends ArchiverTestCase
}
/**
* @param array<string, string> $files
* @param array<string, string|null> $files
*/
public function testZipArchive(array $files = []): void
protected function assertZipArchive(array $files): void
{
if (!class_exists('ZipArchive')) {
$this->markTestSkipped('Cannot run ZipArchiverTest, missing class "ZipArchive".');
}
if (empty($files)) {
$files = [
'file.txt' => null,
'foo/bar/baz' => null,
'x/baz' => null,
'x/includeme' => null,
];
if (!Platform::isWindows()) {
$files['foo' . Platform::getCwd() . '/file.txt'] = null;
}
}
// Set up repository
$this->setupDummyRepo($files);
$package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.zip';
$target = $this->filesToCleanup[] = sys_get_temp_dir().'/composer_archiver_test.zip';
// Test archive
$archiver = new ZipArchiver();
@ -81,12 +88,20 @@ class ZipArchiverTest extends ArchiverTestCase
$zip = new ZipArchive();
$res = $zip->open($target);
static::assertTrue($res, 'Failed asserting that Zip file can be opened');
foreach ($files as $path => $content) {
static::assertSame($content, $zip->getFromName($path), 'Failed asserting that Zip contains ' . $path);
$zipContents = [];
for ($i = 0; $i < $zip->numFiles; $i++) {
$path = $zip->getNameIndex($i);
static::assertIsString($path);
$zipContents[$path] = $zip->getFromName($path);
}
$zip->close();
unlink($target);
static::assertSame(
$files,
$zipContents,
'Failed asserting that Zip created with the ZipArchiver contains all files from the repository.'
);
}
/**
@ -120,4 +135,12 @@ class ZipArchiverTest extends ArchiverTestCase
throw new \RuntimeException('Could not save file.');
}
}
protected function tearDown(): void
{
foreach ($this->filesToCleanup as $file) {
unlink($file);
}
parent::tearDown();
}
}