Cleaned archiver tests

main
Matthieu Moquet 12 years ago committed by Nils Adermann
parent 9d24e17003
commit a2b404e421

@ -33,7 +33,7 @@ class ArchiveManagerTest extends ArchiverTest
$factory = new Factory();
$this->manager = $factory->createArchiveManager(null, $factory->createConfig());
$this->targetDir = sys_get_temp_dir().'/composer_archiver_tests';
$this->targetDir = $this->testDir.'/composer_archiver_tests';
}
public function testUnknownFormat()
@ -45,7 +45,7 @@ class ArchiveManagerTest extends ArchiverTest
$this->manager->archive($package, '__unknown_format__', $this->targetDir);
}
public function testArchiveTarWithVcs()
public function testArchiveTar()
{
$this->setupGitRepo();
@ -59,24 +59,6 @@ class ArchiveManagerTest extends ArchiverTest
$this->assertFileExists($target);
unlink($target);
$this->removeGitRepo();
}
public function testArchiveTarWithoutVcs()
{
$this->setupGitRepo();
$package = $this->setupPackage();
// This should use the TarArchiver
$this->manager->archive($package, 'tar', $this->targetDir);
$package->setSourceType('__unknown_type__'); // disable VCS recognition
$target = $this->getTargetName($package, 'tar');
$this->assertFileExists($target);
unlink($target);
$this->removeGitRepo();
}
protected function getTargetName(PackageInterface $package, $format)
@ -86,4 +68,33 @@ class ArchiveManagerTest extends ArchiverTest
return $target;
}
/**
* Create local git repository to run tests against!
*/
protected function setupGitRepo()
{
$currentWorkDir = getcwd();
chdir($this->testDir);
$result = $this->process->execute('git init -q');
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
}
$result = file_put_contents('b', 'a');
if (false === $result) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.');
}
$result = $this->process->execute('git add b && git commit -m "commit b" -q');
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
}
chdir($currentWorkDir);
}
}

@ -41,46 +41,20 @@ abstract class ArchiverTest extends \PHPUnit_Framework_TestCase
{
$this->filesystem = new Filesystem();
$this->process = new ProcessExecutor();
$this->testDir = sys_get_temp_dir().'/composer_archivertest_git_repository'.mt_rand();
}
/**
* Create local git repository to run tests against!
*/
protected function setupGitRepo()
{
$this->filesystem->removeDirectory($this->testDir);
$this->testDir = sys_get_temp_dir().'/composer_archiver_test_'.mt_rand();
$this->filesystem->ensureDirectoryExists($this->testDir);
$currentWorkDir = getcwd();
chdir($this->testDir);
$result = $this->process->execute('git init -q');
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
}
$result = file_put_contents('b', 'a');
if (false === $result) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.');
}
$result = $this->process->execute('git add b && git commit -m "commit b" -q');
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
}
chdir($currentWorkDir);
}
protected function removeGitRepo()
public function tearDown()
{
$this->filesystem->removeDirectory($this->testDir);
}
/**
* Util method to quickly setup a package using the source path built.
*
* @return \Composer\Package\Package
*/
protected function setupPackage()
{
$package = new Package('archivertest/archivertest', 'master', 'master');

@ -20,10 +20,12 @@ use Composer\Package\Archiver\GitArchiver;
*/
class GitArchiverTest extends ArchiverTest
{
protected $targetFile;
public function testZipArchive()
{
// Set up repository
$this->setupGitRepo();
$package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.zip';
@ -33,13 +35,12 @@ class GitArchiverTest extends ArchiverTest
$this->assertFileExists($target);
unlink($target);
$this->removeGitRepo();
}
public function testTarArchive()
{
// Set up repository
$this->setupGitRepo();
$package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.tar';
@ -49,6 +50,34 @@ class GitArchiverTest extends ArchiverTest
$this->assertFileExists($target);
unlink($target);
$this->removeGitRepo();
}
/**
* Create local git repository to run tests against!
*/
protected function setupGitRepo()
{
$currentWorkDir = getcwd();
chdir($this->testDir);
$result = $this->process->execute('git init -q');
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
}
$result = file_put_contents('b', 'a');
if (false === $result) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.');
}
$result = $this->process->execute('git add b && git commit -m "commit b" -q');
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
}
chdir($currentWorkDir);
}
}

@ -23,9 +23,9 @@ class MercurialArchiverTest extends ArchiverTest
{
public function testZipArchive()
{
// Set up repository
$this->setupMercurialRepo();
$package = $this->setupMercurialPackage();
$package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.zip';
// Test archive
@ -34,14 +34,13 @@ class MercurialArchiverTest extends ArchiverTest
$this->assertFileExists($target);
unlink($target);
$this->removeMercurialRepo();
}
public function testTarArchive()
{
// Set up repository
$this->setupMercurialRepo();
$package = $this->setupMercurialPackage();
$package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.tar';
// Test archive
@ -50,47 +49,40 @@ class MercurialArchiverTest extends ArchiverTest
$this->assertFileExists($target);
unlink($target);
$this->removeMercurialRepo();
}
/**
* Create local git repository to run tests against!
* Create local mercurial repository to run tests against!
*/
protected function setupMercurialRepo()
{
$this->filesystem->removeDirectory($this->testDir);
$this->filesystem->ensureDirectoryExists($this->testDir);
$currentWorkDir = getcwd();
chdir($this->testDir);
$result = $this->process->execute('hg init -q');
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
}
$result = file_put_contents('b', 'a');
if (false === $result) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.');
}
$result = $this->process->execute('hg add b && hg commit -m "commit b" --config ui.username=test -q');
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
}
chdir($currentWorkDir);
}
protected function removeMercurialRepo()
{
$this->filesystem->removeDirectory($this->testDir);
}
protected function setupMercurialPackage()
protected function setupPackage()
{
$package = new Package('archivertest/archivertest', 'master', 'master');
$package->setSourceUrl(realpath($this->testDir));
$package = parent::setupPackage();
$package->setSourceReference('default');
$package->setSourceType('hg');

@ -22,8 +22,8 @@ class PharArchiverTest extends ArchiverTest
{
public function testTarArchive()
{
$this->setupGitRepo();
// Set up repository
$this->setupDummyRepo();
$package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.tar';
@ -33,13 +33,12 @@ class PharArchiverTest extends ArchiverTest
$this->assertFileExists($target);
unlink($target);
$this->removeGitRepo();
}
public function testZipArchive()
{
$this->setupGitRepo();
// Set up repository
$this->setupDummyRepo();
$package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.zip';
@ -49,6 +48,22 @@ class PharArchiverTest extends ArchiverTest
$this->assertFileExists($target);
unlink($target);
$this->removeGitRepo();
}
/**
* Create a local dummy repository to run tests against!
*/
protected function setupDummyRepo()
{
$currentWorkDir = getcwd();
chdir($this->testDir);
$result = file_put_contents('b', 'a');
if (false === $result) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.');
}
chdir($currentWorkDir);
}
}

Loading…
Cancel
Save