1
0
Fork 0

Cleaned archiver tests

pull/1567/head
Matthieu Moquet 2012-10-21 18:23:35 +02:00 committed by Nils Adermann
parent 9d24e17003
commit a2b404e421
5 changed files with 105 additions and 84 deletions

View File

@ -33,7 +33,7 @@ class ArchiveManagerTest extends ArchiverTest
$factory = new Factory(); $factory = new Factory();
$this->manager = $factory->createArchiveManager(null, $factory->createConfig()); $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() public function testUnknownFormat()
@ -45,7 +45,7 @@ class ArchiveManagerTest extends ArchiverTest
$this->manager->archive($package, '__unknown_format__', $this->targetDir); $this->manager->archive($package, '__unknown_format__', $this->targetDir);
} }
public function testArchiveTarWithVcs() public function testArchiveTar()
{ {
$this->setupGitRepo(); $this->setupGitRepo();
@ -59,24 +59,6 @@ class ArchiveManagerTest extends ArchiverTest
$this->assertFileExists($target); $this->assertFileExists($target);
unlink($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) protected function getTargetName(PackageInterface $package, $format)
@ -86,4 +68,33 @@ class ArchiveManagerTest extends ArchiverTest
return $target; 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);
}
} }

View File

@ -41,46 +41,20 @@ abstract class ArchiverTest extends \PHPUnit_Framework_TestCase
{ {
$this->filesystem = new Filesystem(); $this->filesystem = new Filesystem();
$this->process = new ProcessExecutor(); $this->process = new ProcessExecutor();
$this->testDir = sys_get_temp_dir().'/composer_archivertest_git_repository'.mt_rand(); $this->testDir = sys_get_temp_dir().'/composer_archiver_test_'.mt_rand();
$this->filesystem->ensureDirectoryExists($this->testDir);
}
public function tearDown()
{
$this->filesystem->removeDirectory($this->testDir);
} }
/** /**
* Create local git repository to run tests against! * Util method to quickly setup a package using the source path built.
*
* @return \Composer\Package\Package
*/ */
protected function setupGitRepo()
{
$this->filesystem->removeDirectory($this->testDir);
$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()
{
$this->filesystem->removeDirectory($this->testDir);
}
protected function setupPackage() protected function setupPackage()
{ {
$package = new Package('archivertest/archivertest', 'master', 'master'); $package = new Package('archivertest/archivertest', 'master', 'master');

View File

@ -20,10 +20,12 @@ use Composer\Package\Archiver\GitArchiver;
*/ */
class GitArchiverTest extends ArchiverTest class GitArchiverTest extends ArchiverTest
{ {
protected $targetFile;
public function testZipArchive() public function testZipArchive()
{ {
// Set up repository
$this->setupGitRepo(); $this->setupGitRepo();
$package = $this->setupPackage(); $package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.zip'; $target = sys_get_temp_dir().'/composer_archiver_test.zip';
@ -33,13 +35,12 @@ class GitArchiverTest extends ArchiverTest
$this->assertFileExists($target); $this->assertFileExists($target);
unlink($target); unlink($target);
$this->removeGitRepo();
} }
public function testTarArchive() public function testTarArchive()
{ {
// Set up repository
$this->setupGitRepo(); $this->setupGitRepo();
$package = $this->setupPackage(); $package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.tar'; $target = sys_get_temp_dir().'/composer_archiver_test.tar';
@ -49,6 +50,34 @@ class GitArchiverTest extends ArchiverTest
$this->assertFileExists($target); $this->assertFileExists($target);
unlink($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);
} }
} }

View File

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

View File

@ -22,8 +22,8 @@ class PharArchiverTest extends ArchiverTest
{ {
public function testTarArchive() public function testTarArchive()
{ {
$this->setupGitRepo(); // Set up repository
$this->setupDummyRepo();
$package = $this->setupPackage(); $package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.tar'; $target = sys_get_temp_dir().'/composer_archiver_test.tar';
@ -33,13 +33,12 @@ class PharArchiverTest extends ArchiverTest
$this->assertFileExists($target); $this->assertFileExists($target);
unlink($target); unlink($target);
$this->removeGitRepo();
} }
public function testZipArchive() public function testZipArchive()
{ {
$this->setupGitRepo(); // Set up repository
$this->setupDummyRepo();
$package = $this->setupPackage(); $package = $this->setupPackage();
$target = sys_get_temp_dir().'/composer_archiver_test.zip'; $target = sys_get_temp_dir().'/composer_archiver_test.zip';
@ -49,6 +48,22 @@ class PharArchiverTest extends ArchiverTest
$this->assertFileExists($target); $this->assertFileExists($target);
unlink($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);
} }
} }