2012-08-23 19:35:17 +00:00
|
|
|
<?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\Package\Archiver;
|
|
|
|
|
2018-10-31 11:44:54 +00:00
|
|
|
use Composer\IO\NullIO;
|
2012-08-28 21:55:26 +00:00
|
|
|
use Composer\Factory;
|
2015-10-05 00:53:07 +00:00
|
|
|
use Composer\Package\Archiver\ArchiveManager;
|
2021-08-21 15:41:52 +00:00
|
|
|
use Composer\Package\CompletePackage;
|
2019-01-17 16:12:33 +00:00
|
|
|
use Composer\Util\Loop;
|
2018-10-31 11:44:54 +00:00
|
|
|
use Composer\Test\Mock\FactoryMock;
|
2020-06-04 13:30:20 +00:00
|
|
|
use Composer\Util\ProcessExecutor;
|
2012-08-23 19:35:17 +00:00
|
|
|
|
|
|
|
class ArchiveManagerTest extends ArchiverTest
|
|
|
|
{
|
2015-10-05 00:53:07 +00:00
|
|
|
/**
|
|
|
|
* @var ArchiveManager
|
|
|
|
*/
|
2012-08-23 19:35:17 +00:00
|
|
|
protected $manager;
|
2015-10-05 00:53:07 +00:00
|
|
|
|
2021-10-16 08:16:06 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-10-21 10:16:28 +00:00
|
|
|
protected $targetDir;
|
2012-08-23 19:35:17 +00:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2012-08-28 21:55:26 +00:00
|
|
|
$factory = new Factory();
|
2018-10-31 11:44:54 +00:00
|
|
|
$dm = $factory->createDownloadManager(
|
|
|
|
$io = new NullIO,
|
|
|
|
$config = FactoryMock::createConfig(),
|
2020-06-04 13:30:20 +00:00
|
|
|
$httpDownloader = $factory->createHttpDownloader($io, $config),
|
|
|
|
new ProcessExecutor($io)
|
2018-10-31 11:44:54 +00:00
|
|
|
);
|
2019-01-17 16:12:33 +00:00
|
|
|
$loop = new Loop($httpDownloader);
|
|
|
|
$this->manager = $factory->createArchiveManager($factory->createConfig(), $dm, $loop);
|
2012-10-21 16:23:35 +00:00
|
|
|
$this->targetDir = $this->testDir.'/composer_archiver_tests';
|
2012-08-23 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnknownFormat()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('RuntimeException');
|
|
|
|
|
|
|
|
$package = $this->setupPackage();
|
|
|
|
|
2012-10-21 10:16:28 +00:00
|
|
|
$this->manager->archive($package, '__unknown_format__', $this->targetDir);
|
2012-08-23 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
2012-10-21 16:23:35 +00:00
|
|
|
public function testArchiveTar()
|
2012-08-23 19:35:17 +00:00
|
|
|
{
|
2016-12-22 17:14:57 +00:00
|
|
|
$this->skipIfNotExecutable('git');
|
|
|
|
|
2012-08-23 19:35:17 +00:00
|
|
|
$this->setupGitRepo();
|
|
|
|
|
|
|
|
$package = $this->setupPackage();
|
|
|
|
|
2012-10-21 10:16:28 +00:00
|
|
|
$this->manager->archive($package, 'tar', $this->targetDir);
|
2012-08-23 19:35:17 +00:00
|
|
|
|
|
|
|
$target = $this->getTargetName($package, 'tar');
|
|
|
|
$this->assertFileExists($target);
|
2013-10-11 23:12:02 +00:00
|
|
|
|
2013-07-17 11:28:15 +00:00
|
|
|
$tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
|
2020-09-11 09:27:26 +00:00
|
|
|
$this->assertFileDoesNotExist($tmppath);
|
2012-08-23 19:35:17 +00:00
|
|
|
|
|
|
|
unlink($target);
|
|
|
|
}
|
|
|
|
|
2015-10-05 00:53:07 +00:00
|
|
|
public function testArchiveCustomFileName()
|
2012-08-23 19:35:17 +00:00
|
|
|
{
|
2016-12-22 17:14:57 +00:00
|
|
|
$this->skipIfNotExecutable('git');
|
|
|
|
|
2015-10-05 00:53:07 +00:00
|
|
|
$this->setupGitRepo();
|
|
|
|
|
|
|
|
$package = $this->setupPackage();
|
|
|
|
|
2015-10-07 17:42:19 +00:00
|
|
|
$fileName = 'testArchiveName';
|
2015-10-05 00:53:07 +00:00
|
|
|
|
|
|
|
$this->manager->archive($package, 'tar', $this->targetDir, $fileName);
|
|
|
|
|
2015-10-07 17:42:19 +00:00
|
|
|
$target = $this->targetDir . '/' . $fileName . '.tar';
|
2015-10-05 00:53:07 +00:00
|
|
|
|
|
|
|
$this->assertFileExists($target);
|
|
|
|
|
|
|
|
$tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
|
2020-09-11 09:27:26 +00:00
|
|
|
$this->assertFileDoesNotExist($tmppath);
|
2015-10-05 00:53:07 +00:00
|
|
|
|
|
|
|
unlink($target);
|
|
|
|
}
|
2015-10-25 15:19:15 +00:00
|
|
|
|
2021-11-01 20:44:12 +00:00
|
|
|
/**
|
|
|
|
* @param string $format
|
|
|
|
* @param string|null $fileName
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-08-21 15:41:52 +00:00
|
|
|
protected function getTargetName(CompletePackage $package, $format, $fileName = null)
|
2015-10-05 00:53:07 +00:00
|
|
|
{
|
2015-10-25 15:19:15 +00:00
|
|
|
if (null === $fileName) {
|
2015-10-05 00:53:07 +00:00
|
|
|
$packageName = $this->manager->getPackageFilename($package);
|
2015-10-05 01:03:06 +00:00
|
|
|
} else {
|
|
|
|
$packageName = $fileName;
|
2015-10-05 00:53:07 +00:00
|
|
|
}
|
2015-10-05 01:03:06 +00:00
|
|
|
|
2017-09-11 17:40:43 +00:00
|
|
|
return $this->targetDir.'/'.$packageName.'.'.$format;
|
2012-08-23 19:35:17 +00:00
|
|
|
}
|
2012-10-21 16:23:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create local git repository to run tests against!
|
2021-11-01 20:44:12 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2012-10-21 16:23:35 +00:00
|
|
|
*/
|
|
|
|
protected function setupGitRepo()
|
|
|
|
{
|
|
|
|
$currentWorkDir = getcwd();
|
|
|
|
chdir($this->testDir);
|
|
|
|
|
2013-03-28 12:21:55 +00:00
|
|
|
$output = null;
|
2020-10-30 16:54:14 +00:00
|
|
|
$result = $this->process->execute('git init -q', $output, $this->testDir);
|
2012-10-21 16:23:35 +00:00
|
|
|
if ($result > 0) {
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:54:14 +00:00
|
|
|
$result = $this->process->execute('git checkout -b master', $output, $this->testDir);
|
|
|
|
if ($result > 0) {
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
throw new \RuntimeException('Could not checkout master branch: '.$this->process->getErrorOutput());
|
|
|
|
}
|
|
|
|
|
2015-02-16 03:20:45 +00:00
|
|
|
$result = $this->process->execute('git config user.email "you@example.com"', $output, $this->testDir);
|
|
|
|
if ($result > 0) {
|
|
|
|
chdir($currentWorkDir);
|
2019-02-18 16:00:57 +00:00
|
|
|
throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->process->execute('git config commit.gpgsign false', $output, $this->testDir);
|
|
|
|
if ($result > 0) {
|
|
|
|
chdir($currentWorkDir);
|
2015-02-16 03:20:45 +00:00
|
|
|
throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->process->execute('git config user.name "Your Name"', $output, $this->testDir);
|
|
|
|
if ($result > 0) {
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
|
|
|
|
}
|
|
|
|
|
2013-12-05 14:25:20 +00:00
|
|
|
$result = file_put_contents('composer.json', '{"name":"faker/faker", "description": "description", "license": "MIT"}');
|
2012-10-21 16:23:35 +00:00
|
|
|
if (false === $result) {
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
throw new \RuntimeException('Could not save file.');
|
|
|
|
}
|
|
|
|
|
2013-12-05 14:25:20 +00:00
|
|
|
$result = $this->process->execute('git add composer.json && git commit -m "commit composer.json" -q', $output, $this->testDir);
|
2012-10-21 16:23:35 +00:00
|
|
|
if ($result > 0) {
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
|
|
|
|
}
|
|
|
|
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
}
|
2012-08-23 19:35:17 +00:00
|
|
|
}
|