Browse Source

Fix installer that create vendor and bin directory even if --dry-run parameter provided
* Move directories creation from constructor to "install" and "update" method
* Tests for LibraryInstaller

Wookieb 13 years ago
parent
commit
2467456d3f

+ 8 - 4
src/Composer/Installer/LibraryInstaller.php

@@ -53,10 +53,8 @@ class LibraryInstaller implements InstallerInterface
         $this->type = $type;
 
         $this->filesystem = new Filesystem();
-        $this->filesystem->ensureDirectoryExists($vendorDir);
-        $this->filesystem->ensureDirectoryExists($binDir);
-        $this->vendorDir = realpath($vendorDir);
-        $this->binDir = realpath($binDir);
+        $this->vendorDir = rtrim($vendorDir, '/');
+        $this->binDir = rtrim($binDir, '/');
     }
 
     /**
@@ -82,6 +80,9 @@ class LibraryInstaller implements InstallerInterface
     {
         $downloadPath = $this->getInstallPath($package);
 
+        $this->filesystem->ensureDirectoryExists($this->vendorDir);
+        $this->filesystem->ensureDirectoryExists($this->binDir);
+
         // remove the binaries if it appears the package files are missing
         if (!is_readable($downloadPath) && $this->repository->hasPackage($package)) {
             $this->removeBinaries($package);
@@ -105,6 +106,9 @@ class LibraryInstaller implements InstallerInterface
 
         $downloadPath = $this->getInstallPath($initial);
 
+        $this->filesystem->ensureDirectoryExists($this->vendorDir);
+        $this->filesystem->ensureDirectoryExists($this->binDir);
+
         $this->removeBinaries($initial);
         $this->downloadManager->update($initial, $target, $downloadPath);
         $this->installBinaries($target);

+ 27 - 11
tests/Composer/Test/Installer/LibraryInstallerTest.php

@@ -22,22 +22,22 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
     private $binDir;
     private $dm;
     private $repository;
-    private $library;
     private $io;
+    private $fs;
 
     protected function setUp()
     {
-        $fs = new Filesystem;
+        $this->fs = new Filesystem;
 
         $this->vendorDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-vendor';
         if (is_dir($this->vendorDir)) {
-            $fs->removeDirectory($this->vendorDir);
+            $this->fs->removeDirectory($this->vendorDir);
         }
         mkdir($this->vendorDir);
 
         $this->binDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-bin';
         if (is_dir($this->binDir)) {
-            $fs->removeDirectory($this->binDir);
+            $this->fs->removeDirectory($this->binDir);
         }
         mkdir($this->binDir);
 
@@ -52,16 +52,20 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
             ->getMock();
     }
 
-    public function testInstallerCreation()
+    public function testInstallerCreationShouldNotCreateVendorDirectory()
     {
-        $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->repository, $this->io);
-        $this->assertTrue(is_dir($this->vendorDir));
+        $this->fs->removeDirectory($this->vendorDir);
+
+        new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->repository, $this->io);
+        $this->assertFileNotExists($this->vendorDir);
+    }
 
-        $file = sys_get_temp_dir().'/file';
-        touch($file);
+    public function testInstallerCreationShouldNotCreateBinDirectory()
+    {
+        $this->fs->removeDirectory($this->binDir);
 
-        $this->setExpectedException('RuntimeException');
-        $library = new LibraryInstaller($file, $this->binDir, $this->dm, $this->repository, $this->io);
+        new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->repository, $this->io);
+        $this->assertFileNotExists($this->binDir);
     }
 
     public function testIsInstalled()
@@ -79,6 +83,10 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($library->isInstalled($package));
     }
 
+    /**
+     * @depends testInstallerCreationShouldNotCreateVendorDirectory
+     * @depends testInstallerCreationShouldNotCreateBinDirectory
+     */
     public function testInstall()
     {
         $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->repository, $this->io);
@@ -100,8 +108,14 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
             ->with($package);
 
         $library->install($package);
+        $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
+        $this->assertFileExists($this->binDir, 'Bin dir should be created');
     }
 
+    /**
+     * @depends testInstallerCreationShouldNotCreateVendorDirectory
+     * @depends testInstallerCreationShouldNotCreateBinDirectory
+     */
     public function testUpdate()
     {
         $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->repository, $this->io);
@@ -135,6 +149,8 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
             ->with($target);
 
         $library->update($initial, $target);
+        $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
+        $this->assertFileExists($this->binDir, 'Bin dir should be created');
 
         $this->setExpectedException('InvalidArgumentException');