From eb84ca564fca28b268929a5911cff74d534eb951 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 1 Apr 2016 12:07:42 +0100 Subject: [PATCH] Use unique cache dir for integration tests as well --- tests/Composer/Test/InstallerTest.php | 7 ++++++ tests/Composer/Test/Mock/FactoryMock.php | 3 ++- tests/Composer/TestCase.php | 32 ++++++++++++------------ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/tests/Composer/Test/InstallerTest.php b/tests/Composer/Test/InstallerTest.php index 5aa9f862e..985ba64c2 100644 --- a/tests/Composer/Test/InstallerTest.php +++ b/tests/Composer/Test/InstallerTest.php @@ -15,6 +15,7 @@ namespace Composer\Test; use Composer\Installer; use Composer\Console\Application; use Composer\Json\JsonFile; +use Composer\Util\Filesystem; use Composer\Repository\ArrayRepository; use Composer\Repository\RepositoryManager; use Composer\Repository\InstalledArrayRepository; @@ -34,6 +35,7 @@ use Composer\IO\BufferIO; class InstallerTest extends TestCase { protected $prevCwd; + protected $tempComposerHome; public function setUp() { @@ -44,6 +46,10 @@ class InstallerTest extends TestCase public function tearDown() { chdir($this->prevCwd); + if (is_dir($this->tempComposerHome)) { + $fs = new Filesystem; + $fs->removeDirectory($this->tempComposerHome); + } } /** @@ -159,6 +165,7 @@ class InstallerTest extends TestCase // Create Composer mock object according to configuration $composer = FactoryMock::create($io, $composerConfig); + $this->tempComposerHome = $composer->getConfig()->get('home'); $jsonMock = $this->getMockBuilder('Composer\Json\JsonFile')->disableOriginalConstructor()->getMock(); $jsonMock->expects($this->any()) diff --git a/tests/Composer/Test/Mock/FactoryMock.php b/tests/Composer/Test/Mock/FactoryMock.php index 43d41c5a8..bde22d41f 100644 --- a/tests/Composer/Test/Mock/FactoryMock.php +++ b/tests/Composer/Test/Mock/FactoryMock.php @@ -18,6 +18,7 @@ use Composer\Repository\RepositoryManager; use Composer\Repository\WritableRepositoryInterface; use Composer\Installer; use Composer\IO\IOInterface; +use Composer\TestCase; class FactoryMock extends Factory { @@ -26,7 +27,7 @@ class FactoryMock extends Factory $config = new Config(true, $cwd); $config->merge(array( - 'config' => array('home' => sys_get_temp_dir().'/composer-test'), + 'config' => array('home' => TestCase::getUniqueTmpDirectory()), 'repositories' => array('packagist' => false), )); diff --git a/tests/Composer/TestCase.php b/tests/Composer/TestCase.php index 7186ae556..44029ad5e 100644 --- a/tests/Composer/TestCase.php +++ b/tests/Composer/TestCase.php @@ -22,6 +22,22 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { private static $parser; + public static function getUniqueTmpDirectory() + { + $attempts = 5; + $root = sys_get_temp_dir(); + + do { + $unique = $root . DIRECTORY_SEPARATOR . uniqid('composer-test-' . rand(1000, 9000)); + + if (!file_exists($unique) && Silencer::call('mkdir', $unique, 0777)) { + return realpath($unique); + } + } while (--$attempts); + + throw new \RuntimeException('Failed to create a unique temporary directory.'); + } + protected static function getVersionParser() { if (!self::$parser) { @@ -57,22 +73,6 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase return new AliasPackage($package, $normVersion, $version); } - protected static function getUniqueTmpDirectory() - { - $attempts = 5; - $root = sys_get_temp_dir(); - - do { - $unique = $root . DIRECTORY_SEPARATOR . uniqid('composer-test-' . rand(1000, 9000)); - - if (!file_exists($unique) && Silencer::call('mkdir', $unique, 0777)) { - return realpath($unique); - } - } while (--$attempts); - - throw new \RuntimeException('Failed to create a unique temporary directory.'); - } - protected static function ensureDirectoryExistsAndClear($directory) { $fs = new Filesystem();