From 8d3b40ff8ee5b8f550e187b141df4d00626aefed Mon Sep 17 00:00:00 2001 From: Wookieb Date: Fri, 24 Feb 2012 10:40:47 +0100 Subject: [PATCH 1/2] Tests should remove created directories after testing process --- .../Test/Autoload/AutoloadGeneratorTest.php | 11 ++++++---- .../Test/Installer/LibraryInstallerTest.php | 21 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index a4e8b9449..9d93ee95c 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -23,18 +23,18 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase private $im; private $repository; private $generator; + private $fs; protected function setUp() { - $fs = new Filesystem; + $this->fs = new Filesystem; $that = $this; $this->workingDir = realpath(sys_get_temp_dir()); $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload'; - if (is_dir($this->vendorDir)) { - $fs->removeDirectory($this->vendorDir); + if (!is_dir($this->vendorDir)) { + mkdir($this->vendorDir); } - mkdir($this->vendorDir); $this->dir = getcwd(); chdir($this->workingDir); @@ -60,6 +60,9 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase protected function tearDown() { + if (is_dir($this->vendorDir)) { + $this->fs->removeDirectory($this->vendorDir); + } chdir($this->dir); } diff --git a/tests/Composer/Test/Installer/LibraryInstallerTest.php b/tests/Composer/Test/Installer/LibraryInstallerTest.php index ba86954e3..9df4e8893 100644 --- a/tests/Composer/Test/Installer/LibraryInstallerTest.php +++ b/tests/Composer/Test/Installer/LibraryInstallerTest.php @@ -30,16 +30,15 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase $this->fs = new Filesystem; $this->vendorDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-vendor'; - if (is_dir($this->vendorDir)) { - $this->fs->removeDirectory($this->vendorDir); + if (!is_dir($this->vendorDir)) { + mkdir($this->vendorDir); } - mkdir($this->vendorDir); + $this->binDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-bin'; - if (is_dir($this->binDir)) { - $this->fs->removeDirectory($this->binDir); + if (!is_dir($this->binDir)) { + mkdir($this->binDir); } - mkdir($this->binDir); $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager') ->disableOriginalConstructor() @@ -52,6 +51,16 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase ->getMock(); } + protected function tearDown() { + if (is_dir($this->vendorDir)) { + $this->fs->removeDirectory($this->vendorDir); + } + + if (is_dir($this->binDir)) { + $this->fs->removeDirectory($this->binDir); + } + } + public function testInstallerCreationShouldNotCreateVendorDirectory() { $this->fs->removeDirectory($this->vendorDir); From f93135b3bd312dd728003154321b626bc32333be Mon Sep 17 00:00:00 2001 From: Wookieb Date: Fri, 24 Feb 2012 12:28:41 +0100 Subject: [PATCH 2/2] Cosmetic tests fix - remove test directories --- .../Test/Autoload/AutoloadGeneratorTest.php | 36 ++++++++++--------- .../Test/Installer/LibraryInstallerTest.php | 15 ++++---- tests/Composer/Test/TestCase.php | 10 ++++++ 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 9d93ee95c..f8e188906 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -15,8 +15,9 @@ namespace Composer\Test\Installer; use Composer\Autoload\AutoloadGenerator; use Composer\Util\Filesystem; use Composer\Package\MemoryPackage; +use Composer\Test\TestCase; -class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase +class AutoloadGeneratorTest extends TestCase { public $vendorDir; private $workingDir; @@ -32,9 +33,7 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase $this->workingDir = realpath(sys_get_temp_dir()); $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload'; - if (!is_dir($this->vendorDir)) { - mkdir($this->vendorDir); - } + $this->ensureDirectoryExistsAndClear($this->vendorDir); $this->dir = getcwd(); chdir($this->workingDir); @@ -53,14 +52,18 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase return $that->vendorDir; })); - $this->repo = $this->getMock('Composer\Repository\RepositoryInterface'); + $this->repository = $this->getMock('Composer\Repository\RepositoryInterface'); $this->generator = new AutoloadGenerator(); } protected function tearDown() { - if (is_dir($this->vendorDir)) { + if ($this->vendorDir === $this->workingDir) { + if (is_dir($this->workingDir.'/.composer')) { + $this->fs->removeDirectory($this->workingDir.'/.composer'); + } + } elseif (is_dir($this->vendorDir)) { $this->fs->removeDirectory($this->vendorDir); } chdir($this->dir); @@ -71,12 +74,12 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase $package = new MemoryPackage('a', '1.0', '1.0'); $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); - $this->repo->expects($this->once()) + $this->repository->expects($this->once()) ->method('getPackages') ->will($this->returnValue(array())); mkdir($this->vendorDir.'/.composer'); - $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer'); + $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main', $this->vendorDir.'/.composer'); } @@ -87,14 +90,15 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase $package = new MemoryPackage('a', '1.0', '1.0'); $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); - $this->repo->expects($this->once()) + $this->repository->expects($this->once()) ->method('getPackages') ->will($this->returnValue(array())); if (!is_dir($this->vendorDir.'/.composer')) { mkdir($this->vendorDir.'/.composer', 0777, true); } - $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer'); + + $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main3', $this->vendorDir.'/.composer'); } @@ -103,13 +107,13 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase $package = new MemoryPackage('a', '1.0', '1.0'); $package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); - $this->repo->expects($this->once()) + $this->repository->expects($this->once()) ->method('getPackages') ->will($this->returnValue(array())); $this->vendorDir .= '/subdir'; mkdir($this->vendorDir.'/.composer', 0777, true); - $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer'); + $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer'); } @@ -123,12 +127,12 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'))); $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/'))); - $this->repo->expects($this->once()) + $this->repository->expects($this->once()) ->method('getPackages') ->will($this->returnValue($packages)); mkdir($this->vendorDir.'/.composer', 0777, true); - $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer'); + $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('vendors', $this->vendorDir.'/.composer'); } @@ -143,12 +147,12 @@ class AutoloadGeneratorTest extends \PHPUnit_Framework_TestCase $a->setAutoload(array('psr-0' => array('A' => 'src/', 'A\\B' => 'lib/'))); $b->setAutoload(array('psr-0' => array('B\\Sub\\Name' => 'src/'))); - $this->repo->expects($this->once()) + $this->repository->expects($this->once()) ->method('getPackages') ->will($this->returnValue($packages)); mkdir($this->vendorDir.'/.composer', 0777, true); - $this->generator->dump($this->repo, $package, $this->im, $this->vendorDir.'/.composer'); + $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer'); } diff --git a/tests/Composer/Test/Installer/LibraryInstallerTest.php b/tests/Composer/Test/Installer/LibraryInstallerTest.php index 9df4e8893..b89bf9b9a 100644 --- a/tests/Composer/Test/Installer/LibraryInstallerTest.php +++ b/tests/Composer/Test/Installer/LibraryInstallerTest.php @@ -15,8 +15,9 @@ namespace Composer\Test\Installer; use Composer\Installer\LibraryInstaller; use Composer\DependencyResolver\Operation; use Composer\Util\Filesystem; +use Composer\Test\TestCase; -class LibraryInstallerTest extends \PHPUnit_Framework_TestCase +class LibraryInstallerTest extends TestCase { private $vendorDir; private $binDir; @@ -30,15 +31,10 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase $this->fs = new Filesystem; $this->vendorDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-vendor'; - if (!is_dir($this->vendorDir)) { - mkdir($this->vendorDir); - } - + $this->ensureDirectoryExistsAndClear($this->vendorDir); $this->binDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-bin'; - if (!is_dir($this->binDir)) { - mkdir($this->binDir); - } + $this->ensureDirectoryExistsAndClear($this->binDir); $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager') ->disableOriginalConstructor() @@ -51,7 +47,8 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase ->getMock(); } - protected function tearDown() { + protected function tearDown() + { if (is_dir($this->vendorDir)) { $this->fs->removeDirectory($this->vendorDir); } diff --git a/tests/Composer/Test/TestCase.php b/tests/Composer/Test/TestCase.php index 1e3ae257e..f0c8ab8fd 100644 --- a/tests/Composer/Test/TestCase.php +++ b/tests/Composer/Test/TestCase.php @@ -15,6 +15,7 @@ namespace Composer\Test; use Composer\Package\Version\VersionParser; use Composer\Package\MemoryPackage; use Composer\Package\LinkConstraint\VersionConstraint; +use Composer\Util\Filesystem; abstract class TestCase extends \PHPUnit_Framework_TestCase { @@ -40,4 +41,13 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase $normVersion = self::$versionParser->normalize($version); return new MemoryPackage($name, $normVersion, $version); } + + protected function ensureDirectoryExistsAndClear($directory) + { + $fs = new Filesystem(); + if (is_dir($directory)) { + $fs->removeDirectory($directory); + } + mkdir($directory, 0777, true); + } }