Skip to content

Commit

Permalink
WIP: [Feature composer#1135] Util\Filesystem additional methods, unit…
Browse files Browse the repository at this point in the history
… tests, and update to Composer/Downloader/VcsDownloader.
  • Loading branch information
bzitzow authored and cordoval committed Oct 12, 2012
1 parent b9697a1 commit 3735ff8
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/Composer/Downloader/VcsDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public function download(PackageInterface $package, $path)
}

$this->io->write(" - Installing <info>" . $package->getName() . "</info> (<comment>" . VersionParser::formatVersion($package) . "</comment>)");
$this->filesystem->removeDirectory($path);

if (!$this->filesystem->directoryIsEmpty($path, array('composer.json'))) {
$this->filesystem->removeDirectory($path);
}

$this->doDownload($package, $path);
$this->io->write('');
}
Expand Down
6 changes: 1 addition & 5 deletions src/Composer/Installer/ProjectInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa
$fs = new FileSystem();

if ($fs->directoryExists($installPath)) {
if (!$fs->directoryIsEmpty($installPath)) {
if (!$fs->directoryIsEmpty($installPath, array('composer.json'))) {
throw new \InvalidArgumentException("Project directory \"$installPath\" must be empty.");
}
} else {
// if (!file_exists(dirname($installPath))) {
// throw new \InvalidArgumentException("Project root \"" . dirname($installPath) . "\" does not exist.");
// }

mkdir($installPath, 0777, true);
}

Expand Down
14 changes: 10 additions & 4 deletions src/Composer/Util/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,26 @@ public function directoryExists($dir)
return true;
}

public function directoryIsEmpty($dir, $excluding= null)
/**
* @param $dir
* @param null $excluding
* @return bool
* @throws \RuntimeException
*/
public function directoryIsEmpty($dir, $excluding = array())
{
if (!$this->directoryExists($dir)) {
throw new \RuntimeException("Directory \"$dir\" does not exist");
}

$excludeList = array('.', '..');
if (null !== $excluding) {

if (!empty($excluding)) {
$excludeList = array_merge($excludeList, $excluding);
}

$dirContent = array_diff(scandir($dir), $excludeList);

if(count($dirContent) >= 1) {
if (count($dirContent) >= 1) {
return false;
}

Expand Down
45 changes: 45 additions & 0 deletions tests/Composer/Test/Fixtures/projectTestDirectory/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "composer/composer",
"description": "Package Manager",
"keywords": ["package", "dependency", "autoload"],
"homepage": "http://getcomposer.org/",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Nils Adermann",
"email": "naderman@naderman.de",
"homepage": "http://www.naderman.de"
},
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be",
"homepage": "http://seld.be"
}
],
"support": {
"irc": "irc://irc.freenode.org/composer",
"issues": "https://github.com/composer/composer/issues"
},
"require": {
"php": ">=5.3.2",
"justinrainbow/json-schema": "1.1.*",
"seld/jsonlint": "1.*",
"symfony/console": "2.1.*",
"symfony/finder": "2.1.*",
"symfony/process": "2.1.*"
},
"suggest": {
"ext-zip": "Enabling the zip extension allows you to unzip archives, and allows gzip compression of all internet traffic",
"ext-openssl": "Enabling the openssl extension allows you to access https URLs for repositories and packages"
},
"autoload": {
"psr-0": { "Composer": "src/" }
},
"bin": ["bin/composer"],
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
37 changes: 35 additions & 2 deletions tests/Composer/Test/Util/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,44 @@ public function providePathCouples()
array('/tmp/test', '/tmp', "./"),
array('C:/Temp/test/sub', 'C:\Temp', "../"),
array('/tmp/test/sub', '/tmp', "../"),
array('/tmp/test/sub', '/tmp', "../../", true),
array('c:/tmp/test/sub', 'c:/tmp', "../../", true),
array('/tmp/test/sub', '/tmp', "../../", TRUE),
array('c:/tmp/test/sub', 'c:/tmp', "../../", TRUE),
array('/tmp', '/tmp/test', "test"),
array('C:/Temp', 'C:\Temp\test', "test"),
array('C:/Temp', 'c:\Temp\test', "test"),
);
}

public function testDirectoryExists()
{
$fs = new Filesystem();
$directory1 = '/does/not/exist';
$directory2 = __DIR__;

$this->assertFalse($fs->directoryExists($directory1));
$this->assertTrue($fs->directoryExists($directory2));
}

/**
* @expectedException \RuntimeException
*/
public function testDirectoryIsEmptyRuntimeException()
{
$fs = new Filesystem();
$dir = '/does/not/exist';

$fs->directoryIsEmpty($dir);
}

public function testDirectoryIsEmpty()
{
$fs = new Filesystem();
$dir = __DIR__;
$project = __DIR__.'/../Fixtures/projectTestDirectory';

$this->assertFalse($fs->directoryIsEmpty($dir));
$this->assertTrue($fs->directoryIsEmpty($project, array('composer.json')));
$this->assertFalse($fs->directoryIsEmpty($project));

}
}

0 comments on commit 3735ff8

Please sign in to comment.