Add exception for multiple composer.json files (#3)

main
Wissem Riahi 4 years ago committed by GitHub
parent 942562c382
commit c353ac835c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -88,7 +88,12 @@ class ArtifactRepository extends ArrayRepository implements ConfigurableReposito
private function getComposerInformation(\SplFileInfo $file) private function getComposerInformation(\SplFileInfo $file)
{ {
$json = Zip::getComposerJson($file->getPathname()); $json = null;
try {
$json = Zip::getComposerJson($file->getPathname());
} catch (\Exception $exception) {
$this->io->write('Failed loading package '.$file->getPathname().': '.$exception->getMessage(), false, IOInterface::VERBOSE);
}
if (null === $json) { if (null === $json) {
return false; return false;

@ -66,8 +66,9 @@ class Zip
* *
* @param \ZipArchive $zip * @param \ZipArchive $zip
* @param string $filename * @param string $filename
* @throws \RuntimeException
* *
* @return bool|int * @return int
*/ */
private static function locateFile(\ZipArchive $zip, $filename) private static function locateFile(\ZipArchive $zip, $filename)
{ {
@ -85,8 +86,7 @@ class Zip
if ($dirname === '.') { if ($dirname === '.') {
$topLevelPaths[$name] = true; $topLevelPaths[$name] = true;
if (\count($topLevelPaths) > 1) { if (\count($topLevelPaths) > 1) {
// archive can only contain one top level directory throw new \RuntimeException('Archive has more than one top level directories, and no composer.json was found on the top level, so it\'s an invalid archive. Top level paths found were: '.implode(',', array_keys($topLevelPaths)));
return false;
} }
continue; continue;
} }
@ -95,8 +95,7 @@ class Zip
if (false === strpos('\\', $dirname) && false === strpos('/', $dirname)) { if (false === strpos('\\', $dirname) && false === strpos('/', $dirname)) {
$topLevelPaths[$dirname.'/'] = true; $topLevelPaths[$dirname.'/'] = true;
if (\count($topLevelPaths) > 1) { if (\count($topLevelPaths) > 1) {
// archive can only contain one top level directory throw new \RuntimeException('Archive has more than one top level directories, and no composer.json was found on the top level, so it\'s an invalid archive. Top level paths found were: '.implode(',', array_keys($topLevelPaths)));
return false;
} }
} }
} }
@ -105,7 +104,6 @@ class Zip
return $index; return $index;
} }
// no composer.json found either at the top level or within the topmost directory throw new \RuntimeException('No composer.json found either at the top level or within the topmost directory');
return false;
} }
} }

@ -55,28 +55,30 @@ class ZipTest extends TestCase
$this->assertNull($result); $this->assertNull($result);
} }
public function testReturnsNullIfTheZipHasNoComposerJson() /**
* @expectedException \RuntimeException
*/
public function testThrowsExceptionIfTheZipHasNoComposerJson()
{ {
if (!extension_loaded('zip')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $this->markTestSkipped('The PHP zip extension is not loaded.');
return; return;
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/nojson.zip'); Zip::getComposerJson(__DIR__.'/Fixtures/Zip/nojson.zip');
$this->assertNull($result);
} }
public function testReturnsNullIfTheComposerJsonIsInASubSubfolder() /**
* @expectedException \RuntimeException
*/
public function testThrowsExceptionIfTheComposerJsonIsInASubSubfolder()
{ {
if (!extension_loaded('zip')) { if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.'); $this->markTestSkipped('The PHP zip extension is not loaded.');
return; return;
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/subfolders.zip'); Zip::getComposerJson(__DIR__.'/Fixtures/Zip/subfolders.zip');
$this->assertNull($result);
} }
public function testReturnsComposerJsonInZipRoot() public function testReturnsComposerJsonInZipRoot()
@ -99,10 +101,12 @@ class ZipTest extends TestCase
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/folder.zip'); $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/folder.zip');
$this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result); $this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result);
} }
/**
* @expectedException \RuntimeException
*/
public function testMultipleTopLevelDirsIsInvalid() public function testMultipleTopLevelDirsIsInvalid()
{ {
if (!extension_loaded('zip')) { if (!extension_loaded('zip')) {
@ -110,9 +114,7 @@ class ZipTest extends TestCase
return; return;
} }
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/multiple.zip'); Zip::getComposerJson(__DIR__.'/Fixtures/Zip/multiple.zip');
$this->assertNull($result);
} }
public function testReturnsComposerJsonFromFirstSubfolder() public function testReturnsComposerJsonFromFirstSubfolder()
@ -126,4 +128,17 @@ class ZipTest extends TestCase
$this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result); $this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result);
} }
/**
* @expectedException \RuntimeException
*/
public function testThrowsExceptionIfMultipleComposerInSubFoldersWereFound()
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
return;
}
Zip::getComposerJson(__DIR__.'/Fixtures/Zip/multiple_subfolders.zip');
}
} }

Loading…
Cancel
Save