Permalink
Browse files

Merge pull request #4 from ulfharn/feature-dist

Merge TarDumper and improved tests.
  • Loading branch information...
till committed Jun 18, 2012
2 parents 7355cfd + fa26228 commit 3b72a046b14899acc59831a5c9399c687ffb2a3d
@@ -0,0 +1,52 @@
+<?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\Package\Dumper;
+
+use Composer\Package\Dumper\BaseDumper;
+use Composer\Package\Dumper\DumperInterface;
+use Composer\Package\PackageInterface;
+use Composer\Util\ProcessExecutor;
+
+/**
+ * @author Ulf Härnhammar <ulfharn@gmail.com>
+ */
+class TarDumper extends BaseDumper
+{
+ protected $format = 'tar';
+
+ public function dump(PackageInterface $package)
+ {
+ $workDir = $this->getAndEnsureWorkDirectory($package);
+
+ $fileName = $this->getFilename($package, 'tar');
+ $sourceType = $package->getSourceType();
+ $sourceRef = $package->getSourceReference();
+
+ switch ($sourceType) {
+ case 'git':
+ $this->downloadGit($package, $workDir);
+ $this->packageGit($fileName, $sourceRef, $workDir);
+ break;
+ case 'hg':
+ $this->downloadHg($package, $workDir);
+ $this->packageHg($fileName, $sourceRef, $workDir);
+ break;
+ case 'svn':
+ $dir = $workDir . (substr($sourceRef, 0, 1) !== '/')?'/':'' . $sourceRef;
+ $this->downloadSvn($package, $workDir);
+ $this->package($fileName, $dir, \Phar::TAR);
+ break;
+ default:
+ throw new \InvalidArgumentException("Unable to handle repositories of type '{$sourceType}'.");
+ }
+ }
+}
@@ -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\Package\Dumper;
+
+use Composer\Package\MemoryPackage;
+
+abstract class DumperTest extends \PHPUnit_Framework_TestCase
+{
+ protected $testdir = '';
+
+ public function setUp()
+ {
+ $this->testdir = sys_get_temp_dir() . '/composer_dumpertest_git_repository' . mt_rand();
+ }
+
+ protected function getTestDir()
+ {
+ return $this->testdir;
+ }
+
+ protected function setupGitRepo()
+ {
+ $td = $this->getTestDir();
+ system("rm -rf $td; mkdir $td");
+ system("cd $td; git init; echo 'a' > b; git add b; git commit -m test");
+ }
+
+ protected function removeGitRepo()
+ {
+ $td = $this->getTestDir();
+ system("rm -rf $td");
+ }
+
+ protected function setupPackage()
+ {
+ $td = $this->getTestDir();
+ $package = new MemoryPackage('dumpertest/dumpertest', 'master', 'master');
+ $package->setSourceUrl("file://$td");
+ $package->setSourceReference('master');
+ $package->setSourceType('git');
+ return $package;
+ }
+
+ protected function getPackageFileName(MemoryPackage $package)
+ {
+ return preg_replace('#[^a-z0-9_-]#', '-', $package->getUniqueName());
+ }
+}
@@ -0,0 +1,43 @@
+<?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\Dumper;
+
+use Composer\Package\Dumper\TarDumper;
+
+class TarDumperTest extends DumperTest
+{
+ public function testThis()
+ {
+ $this->setupGitRepo();
+ $package = $this->setupPackage();
+ $name = $this->getPackageFileName($package);
+
+ $temp = sys_get_temp_dir();
+ $tar = new TarDumper($temp);
+ $tar->dump($package);
+
+ $dist = sprintf('%s/%s.tar',
+ $temp, $name
+ );
+ $this->assertFileExists($dist);
+ unlink($dist);
+ $this->removeGitRepo();
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testException()
+ {
+ new TarDumper("/totally-random-" . time());
+ }
+}
@@ -12,32 +12,25 @@
namespace Composer\Test\Package\Dumper;
use Composer\Package\Dumper\ZipDumper;
-use Composer\Package\MemoryPackage;
-class ZipDumperTest extends \PHPUnit_Framework_TestCase
+class ZipDumperTest extends DumperTest
{
- /**
- * @todo Replace with local git repo to run offline.
- */
public function testThis()
{
- $package = new MemoryPackage('lagged/Lagged_Session_SaveHandler_Memcache', '0.5.0', '0.5.0');
- $package->setSourceUrl('git://github.com/lagged/Lagged_Session_SaveHandler_Memcache.git');
- $package->setSourceReference('0.5.0');
- $package->setSourceType('git');
+ $this->setupGitRepo();
+ $package = $this->setupPackage();
+ $name = $this->getPackageFileName($package);
$temp = sys_get_temp_dir();
-
$zip = new ZipDumper($temp);
$zip->dump($package);
- $name = preg_replace('#[^a-z0-9_-]#', '-', $package->getUniqueName());
-
$dist = sprintf('%s/%s.zip',
$temp, $name
);
$this->assertFileExists($dist);
unlink($dist);
+ $this->removeGitRepo();
}
/**
@@ -47,4 +40,4 @@ public function testException()
{
new ZipDumper("/totally-random-" . time());
}
-}
+}

0 comments on commit 3b72a04

Please sign in to comment.