1
0
Fork 0

Merge remote-tracking branch 'Rudloff/xz'

pull/4623/head
Jordi Boggiano 2015-11-21 19:16:59 +00:00
commit 67a1924173
3 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,59 @@
<?php
/*
* 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\Downloader;
use Composer\Config;
use Composer\Cache;
use Composer\EventDispatcher\EventDispatcher;
use Composer\Package\PackageInterface;
use Composer\Util\ProcessExecutor;
use Composer\IO\IOInterface;
/**
* Xz archive downloader.
*
* @author Pavel Puchkin <i@neoascetic.me>
* @author Pierre Rudloff <contact@rudloff.pro>
*/
class XzDownloader extends ArchiveDownloader
{
protected $process;
public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null)
{
$this->process = $process ?: new ProcessExecutor($io);
parent::__construct($io, $config, $eventDispatcher, $cache);
}
protected function extract($file, $path)
{
$command = 'tar -xJf ' . ProcessExecutor::escape($file) . ' -C ' . ProcessExecutor::escape($path);
if (0 === $this->process->execute($command, $ignoredOutput)) {
return;
}
$processError = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput();
throw new \RuntimeException($processError);
}
/**
* {@inheritdoc}
*/
protected function getFileName(PackageInterface $package, $path)
{
return $path.'/'.pathinfo(parse_url($package->getDistUrl(), PHP_URL_PATH), PATHINFO_BASENAME);
}
}

View File

@ -416,6 +416,7 @@ class Factory
$dm->setDownloader('rar', new Downloader\RarDownloader($io, $config, $eventDispatcher, $cache));
$dm->setDownloader('tar', new Downloader\TarDownloader($io, $config, $eventDispatcher, $cache));
$dm->setDownloader('gzip', new Downloader\GzipDownloader($io, $config, $eventDispatcher, $cache));
$dm->setDownloader('xz', new Downloader\XzDownloader($io, $config, $eventDispatcher, $cache));
$dm->setDownloader('phar', new Downloader\PharDownloader($io, $config, $eventDispatcher, $cache));
$dm->setDownloader('file', new Downloader\FileDownloader($io, $config, $eventDispatcher, $cache));
$dm->setDownloader('path', new Downloader\PathDownloader($io, $config, $eventDispatcher, $cache));

View File

@ -0,0 +1,57 @@
<?php
/*
* 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\Downloader;
use Composer\Downloader\XzDownloader;
class XzDownloaderTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->markTestSkipped('Skip test on Windows');
}
}
public function testErrorMessages()
{
$packageMock = $this->getMock('Composer\Package\PackageInterface');
$packageMock->expects($this->any())
->method('getDistUrl')
->will($this->returnValue($distUrl = 'file://'.__FILE__))
;
$packageMock->expects($this->any())
->method('getDistUrls')
->will($this->returnValue(array($distUrl)))
;
$packageMock->expects($this->atLeastOnce())
->method('getTransportOptions')
->will($this->returnValue(array()))
;
$io = $this->getMock('Composer\IO\IOInterface');
$config = $this->getMock('Composer\Config');
$config->expects($this->any())
->method('get')
->with('vendor-dir')
->will($this->returnValue(sys_get_temp_dir().'/composer-xz-test-vendor'));
$downloader = new XzDownloader($io, $config);
try {
$downloader->download($packageMock, sys_get_temp_dir().'/composer-xz-test');
$this->fail('Download of invalid tarball should throw an exception');
} catch (\RuntimeException $e) {
$this->assertContains('File format not recognized', $e->getMessage());
}
}
}