Browse Source

Use unique cache dir for integration tests as well

Jordi Boggiano 9 years ago
parent
commit
eb84ca564f

+ 7 - 0
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())

+ 2 - 1
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),
         ));
 

+ 16 - 16
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();