From e5a581840a892ba8ce569b3283d4b2a3ac0be4be Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Tue, 9 Dec 2014 22:47:06 +0000 Subject: [PATCH] Only remove files which are being uninstalled from the gitignore --- README.md | 2 +- composer.json | 3 +- composer.lock | 51 ++++++++++-- src/CoreInstaller.php | 9 +-- src/CoreManager.php | 11 ++- src/CoreUnInstaller.php | 13 +-- src/GitIgnore.php | 23 +++++- src/Options.php | 2 +- test/CoreInstallerTest.php | 151 +++++++++++++++-------------------- test/CoreUnInstallerTest.php | 116 ++++++++++++++------------- test/GitIgnoreTest.php | 64 +++++++++------ test/OptionsTest.php | 2 +- 12 files changed, 254 insertions(+), 193 deletions(-) diff --git a/README.md b/README.md index 4ef2064..df1247a 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Available configuration * `excludes` - An array of files to not copy when installing the core, useful if local overrides are necessary, or you want to track a `.htaccess`. * `ignore-directories` - An array of folders to group ignores together to make the core `.gitignore` smaller. - * `git-ignore-append` - Defaults to `false`. Whether to append to the `.gitignore` in the `magento-root-dir` folder. Otherwise it will be wiped out on each deploy. + * `git-ignore-append` - Defaults to `true`. Whether to append to the `.gitignore` in the `magento-root-dir` folder. If false it will be wiped out on each deploy. Example configuration: diff --git a/composer.json b/composer.json index 4efa61f..b5612b6 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,8 @@ "name": "aydin-hassan/magento-core-composer-installer", "type": "composer-plugin", "require": { - "composer-plugin-api": "~1.0" + "composer-plugin-api": "~1.0", + "mikey179/vfsStream": "~1.4" }, "require-dev": { "squizlabs/php_codesniffer": "~1.5", diff --git a/composer.lock b/composer.lock index 6694738..83f5f4d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,47 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "09d176f4256bcdc18c0a4a87e23872cc", - "packages": [], + "hash": "3acd2eceaadecb6364374d2307f88e81", + "packages": [ + { + "name": "mikey179/vfsStream", + "version": "v1.4.0", + "source": { + "type": "git", + "url": "https://github.com/mikey179/vfsStream.git", + "reference": "61b12172292cf539685507aa65b076c1530e83c1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mikey179/vfsStream/zipball/61b12172292cf539685507aa65b076c1530e83c1", + "reference": "61b12172292cf539685507aa65b076c1530e83c1", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "psr-0": { + "org\\bovigo\\vfs\\": "src/main/php" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD" + ], + "homepage": "http://vfs.bovigo.org/", + "time": "2014-09-14 10:18:53" + } + ], "packages-dev": [ { "name": "composer/composer", @@ -13,12 +52,12 @@ "source": { "type": "git", "url": "https://github.com/composer/composer.git", - "reference": "46d8e50bf373bbfe5d3274b26dc15e84644b7f91" + "reference": "5133c3fe9ad06964f35a5c52e8224435c95ce803" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/composer/zipball/46d8e50bf373bbfe5d3274b26dc15e84644b7f91", - "reference": "46d8e50bf373bbfe5d3274b26dc15e84644b7f91", + "url": "https://api.github.com/repos/composer/composer/zipball/5133c3fe9ad06964f35a5c52e8224435c95ce803", + "reference": "5133c3fe9ad06964f35a5c52e8224435c95ce803", "shasum": "" }, "require": { @@ -73,7 +112,7 @@ "dependency", "package" ], - "time": "2014-12-07 14:02:17" + "time": "2014-12-09 14:12:13" }, { "name": "doctrine/instantiator", diff --git a/src/CoreInstaller.php b/src/CoreInstaller.php index f8679fc..402196b 100644 --- a/src/CoreInstaller.php +++ b/src/CoreInstaller.php @@ -16,7 +16,7 @@ class CoreInstaller /** * @var GitIgnore */ - protected $gitIgnore = true; + protected $gitIgnore; /** * @var array @@ -27,7 +27,7 @@ class CoreInstaller * @param array $excludes * @param GitIgnore $gitIgnore */ - public function __construct(array $excludes, GitIgnore $gitIgnore = null) + public function __construct(array $excludes, GitIgnore $gitIgnore) { $this->gitIgnore = $gitIgnore; $this->excludes = $excludes; @@ -64,10 +64,7 @@ public function install($source, $destination) } copy($item, $destinationFile); - - if ($this->gitIgnore) { - $this->gitIgnore->addEntry($iterator->getSubPathName()); - } + $this->gitIgnore->addEntry($iterator->getSubPathName()); } } diff --git a/src/CoreManager.php b/src/CoreManager.php index 41b4618..549b9e8 100644 --- a/src/CoreManager.php +++ b/src/CoreManager.php @@ -173,7 +173,6 @@ public function installCore(PackageEvent $event) if ($package->getType() === $this->type) { $options = new Options($this->composer->getPackage()->getExtra()); - $gitIgnore = new GitIgnore( sprintf("%s/.gitignore", $options->getMagentoRootDir()), $options->getIgnoreDirectories(), @@ -220,9 +219,13 @@ public function uninstallCore(PackageEvent $event) } if ($package->getType() === $this->type) { - $options = new Options($this->composer->getPackage()->getExtra()); - $gitIgnore = new GitIgnore(sprintf("%s/.gitignore", $options->getMagentoRootDir()), array(), false); - $unInstaller = new CoreUnInstaller($this->filesystem, $gitIgnore); + $options = new Options($this->composer->getPackage()->getExtra()); + $gitIgnore = new GitIgnore( + sprintf("%s/.gitignore", $options->getMagentoRootDir()), + $options->getIgnoreDirectories(), + $options->appendToGitIgnore() + ); + $unInstaller = new CoreUnInstaller($this->filesystem, $gitIgnore); $this->removeCorePackage($package, $unInstaller, $options); } } diff --git a/src/CoreUnInstaller.php b/src/CoreUnInstaller.php index feefa9e..e29b762 100644 --- a/src/CoreUnInstaller.php +++ b/src/CoreUnInstaller.php @@ -16,17 +16,18 @@ class CoreUnInstaller { /** - * @var Filesystem + * @var GitIgnore */ - protected $fileSystem; + protected $gitIgnore; /** - * @var GitIgnore + * @var Filesystem */ - protected $gitIgnore; + protected $fileSystem; /** * @param Filesystem $fileSystem + * @param GitIgnore $gitIgnore */ public function __construct(Filesystem $fileSystem, GitIgnore $gitIgnore) { @@ -52,6 +53,7 @@ public function unInstall($source, $destination) $destinationFile = sprintf("%s/%s", $destination, $iterator->getSubPathName()); if (!file_exists($destinationFile)) { + $this->gitIgnore->removeEntry($iterator->getSubPathName()); continue; } @@ -65,8 +67,9 @@ public function unInstall($source, $destination) } $this->fileSystem->unlink($destinationFile); + $this->gitIgnore->removeEntry($iterator->getSubPathName()); } - $this->gitIgnore->wipeOut(); + $this->gitIgnore->removeIgnoreDirectories(); } } diff --git a/src/GitIgnore.php b/src/GitIgnore.php index 23acd5a..339a313 100644 --- a/src/GitIgnore.php +++ b/src/GitIgnore.php @@ -35,7 +35,7 @@ class GitIgnore * @param array $directoriesToIgnoreEntirely * @param bool $gitIgnoreAppend */ - public function __construct($fileLocation, array $directoriesToIgnoreEntirely, $gitIgnoreAppend = false) + public function __construct($fileLocation, array $directoriesToIgnoreEntirely, $gitIgnoreAppend = true) { $this->gitIgnoreLocation = $fileLocation; @@ -69,6 +69,27 @@ public function addEntry($file) $this->hasChanges = true; } + /** + * @param string $file + */ + public function removeEntry($file) + { + if (isset($this->lines[$file])) { + unset($this->lines[$file]); + $this->hasChanges = true; + } + } + + /** + * Remove all the directories to ignore + */ + public function removeIgnoreDirectories() + { + foreach ($this->directoriesToIgnoreEntirely as $directory) { + $this->removeEntry($directory); + } + } + /** * @return array */ diff --git a/src/Options.php b/src/Options.php index 8943e50..ab92e7a 100644 --- a/src/Options.php +++ b/src/Options.php @@ -50,7 +50,7 @@ class Options * * @var bool */ - protected $appendToGitIgnore = false; + protected $appendToGitIgnore = true; /** * Magento Root Directory diff --git a/test/CoreInstallerTest.php b/test/CoreInstallerTest.php index d498df2..a157440 100644 --- a/test/CoreInstallerTest.php +++ b/test/CoreInstallerTest.php @@ -3,7 +3,7 @@ namespace AydinHassan\MagentoCoreComposerInstallerTest; use AydinHassan\MagentoCoreComposerInstaller\CoreInstaller; -use Composer\Util\Filesystem; +use org\bovigo\vfs\vfsStream; /** * Class CoreInstallerTest @@ -13,113 +13,90 @@ class CoreInstallerTest extends \PHPUnit_Framework_TestCase { protected $installer; - protected $fileSystem; protected $gitIgnore; - - protected $sourceFolder; - protected $destinationFolder; + protected $root; public function setUp() { $this->gitIgnore = $this->getMockBuilder('AydinHassan\MagentoCoreComposerInstaller\GitIgnore') ->disableOriginalConstructor() - ->setMethods(array('__destruct')) + ->setMethods(array('addEntry', '__destruct')) ->getMock(); - $this->fileSystem = new Filesystem(); - $this->installer = new CoreInstaller(array(), $this->gitIgnore); - $this->sourceFolder = sprintf("%s/magento-core-composer-installer/source", sys_get_temp_dir()); - $this->destinationFolder = sprintf("%s/magento-core-composer-installer/dest", sys_get_temp_dir()); - mkdir($this->sourceFolder, 0777, true); - mkdir($this->destinationFolder, 0777, true); - } - - public function testInstallerCopiesAllFiles() - { - //creating without gitignore object - $this->installer = new CoreInstaller(array()); - - $file1 = 'file1.txt'; - $file2 = 'folder1/file2.txt'; - $file3 = 'folder1/file3.txt'; - $file4 = 'folder1/folder2/file4.txt'; - - mkdir(sprintf('%s/%s', $this->sourceFolder, dirname($file2))); - touch(sprintf('%s/%s', $this->sourceFolder, $file1)); - touch(sprintf('%s/%s', $this->sourceFolder, $file2)); - touch(sprintf('%s/%s', $this->sourceFolder, $file3)); - mkdir(sprintf('%s/%s', $this->sourceFolder, dirname($file4))); - touch(sprintf('%s/%s', $this->sourceFolder, $file4)); - - $this->installer->install($this->sourceFolder, $this->destinationFolder); - - $this->assertFileExists(sprintf('%s/%s', $this->destinationFolder, $file1)); - $this->assertFileExists(sprintf('%s/%s', $this->destinationFolder, $file2)); - $this->assertFileExists(sprintf('%s/%s', $this->destinationFolder, $file3)); - $this->assertFileExists(sprintf('%s/%s', $this->destinationFolder, $file4)); + $this->installer = new CoreInstaller(array(), $this->gitIgnore); + $this->root = vfsStream::setup('root', null, array('source' => array(), 'destination' => array())); } - public function testIfGitIgnorePresentOnlyFilesAreAppended() + public function testInstallerCopiesAllFilesAndAppendsToGitIgnore() { - $file1 = 'file1.txt'; - $file2 = 'folder1/file2.txt'; - $file3 = 'folder1/file3.txt'; - $file4 = 'folder1/folder2/file4.txt'; - - mkdir(sprintf('%s/%s', $this->sourceFolder, dirname($file2))); - touch(sprintf('%s/%s', $this->sourceFolder, $file1)); - touch(sprintf('%s/%s', $this->sourceFolder, $file2)); - touch(sprintf('%s/%s', $this->sourceFolder, $file3)); - mkdir(sprintf('%s/%s', $this->sourceFolder, dirname($file4))); - touch(sprintf('%s/%s', $this->sourceFolder, $file4)); - - $this->installer->install($this->sourceFolder, $this->destinationFolder); - - $entries = $this->gitIgnore->getEntries(); - $expected = array($file1, $file2, $file3, $file4); - - $res = array_diff($entries, $expected); - $this->assertEquals(0, count($res), 'Git ignore should contain the same rows.'); + $this->installer = new CoreInstaller(array(), $this->gitIgnore); + + $source = $this->root->getChild('source'); + vfsStream::newFile('file1.txt')->at($source); + vfsStream::newDirectory('folder1')->at($source); + vfsStream::newFile('file2.txt')->at($source->getChild('folder1')); + vfsStream::newFile('file3.txt')->at($source->getChild('folder1')); + vfsStream::newDirectory('folder2')->at($source->getChild('folder1')); + vfsStream::newFile('file4.txt')->at($source->getChild('folder1/folder2')); + + $this->gitIgnore + ->expects($this->at(0)) + ->method('addEntry') + ->with('file1.txt'); + + $this->gitIgnore + ->expects($this->at(1)) + ->method('addEntry') + ->with('folder1/file2.txt'); + + $this->gitIgnore + ->expects($this->at(2)) + ->method('addEntry') + ->with('folder1/file3.txt'); + + $this->gitIgnore + ->expects($this->at(3)) + ->method('addEntry') + ->with('folder1/folder2/file4.txt'); + + $this->installer->install(vfsStream::url('root/source'), vfsStream::url('root/destination')); + + $this->assertTrue($this->root->hasChild('destination/file1.txt')); + $this->assertTrue($this->root->hasChild('destination/folder1/file2.txt')); + $this->assertTrue($this->root->hasChild('destination/folder1/file3.txt')); + $this->assertTrue($this->root->hasChild('destination/folder1/folder2/file4.txt')); } public function testIfFolderExistsInDestinationItemIsSkipped() { - $this->installer = new CoreInstaller(array()); - $file1 = 'folder1/file1.txt'; + $this->installer = new CoreInstaller(array(), $this->gitIgnore); - mkdir(sprintf('%s/%s', $this->sourceFolder, dirname($file1))); - touch(sprintf('%s/%s', $this->sourceFolder, $file1)); - mkdir(sprintf('%s/%s', $this->destinationFolder, dirname($file1))); + vfsStream::newDirectory('folder1')->at($this->root->getChild('source')); + vfsStream::newDirectory('folder1')->at($this->root->getChild('destination')); + vfsStream::newFile('file1.txt')->at($this->root->getChild('source/folder1')); + $this->installer->install(vfsStream::url('root/source'), vfsStream::url('root/destination')); - $this->installer->install($this->sourceFolder, $this->destinationFolder); - $this->assertFileExists(sprintf('%s/%s', $this->destinationFolder, $file1)); + $this->assertTrue($this->root->hasChild('destination/folder1/file1.txt')); } public function testExcludedFilesAreNotCopied() { - $this->installer = new CoreInstaller(array('file1.txt', 'folder1/file2.txt')); - - $file1 = 'file1.txt'; - $file2 = 'folder1/file2.txt'; - $file3 = 'folder1/file3.txt'; - - mkdir(sprintf('%s/%s', $this->sourceFolder, dirname($file2))); - touch(sprintf('%s/%s', $this->sourceFolder, $file1)); - touch(sprintf('%s/%s', $this->sourceFolder, $file2)); - touch(sprintf('%s/%s', $this->sourceFolder, $file3)); - - $this->installer->install($this->sourceFolder, $this->destinationFolder); - $this->assertFileExists(sprintf('%s/%s', $this->destinationFolder, $file3)); - $this->assertFileNotExists(sprintf('%s/%s', $this->destinationFolder, $file2)); - $this->assertFileNotExists(sprintf('%s/%s', $this->destinationFolder, $file1)); - - } - - public function tearDown() - { - $fs = new Filesystem(); - $fs->removeDirectory($this->sourceFolder); - $fs->removeDirectory($this->destinationFolder); + $this->installer = new CoreInstaller(array('file1.txt', 'folder1/file2.txt'), $this->gitIgnore); + + $source = $this->root->getChild('source'); + vfsStream::newFile('file1.txt')->at($source); + vfsStream::newDirectory('folder1')->at($source); + vfsStream::newFile('file2.txt')->at($source->getChild('folder1')); + vfsStream::newFile('file3.txt')->at($source->getChild('folder1')); + vfsStream::newDirectory('folder2')->at($source->getChild('folder1')); + vfsStream::newFile('file4.txt')->at($source->getChild('folder1/folder2')); + + $this->installer->install(vfsStream::url('root/source'), vfsStream::url('root/destination')); + + $this->assertFalse($this->root->hasChild('destination/file1.txt')); + $this->assertFalse($this->root->hasChild('destination/folder1/file2.txt')); + $this->assertTrue($this->root->hasChild('destination/folder1/file3.txt')); + $this->assertTrue($this->root->hasChild('destination/folder1/folder2/file4.txt')); } } diff --git a/test/CoreUnInstallerTest.php b/test/CoreUnInstallerTest.php index ff895d5..44109fd 100644 --- a/test/CoreUnInstallerTest.php +++ b/test/CoreUnInstallerTest.php @@ -4,6 +4,7 @@ use AydinHassan\MagentoCoreComposerInstaller\CoreUnInstaller; use Composer\Util\Filesystem; +use org\bovigo\vfs\vfsStream; /** * Class CoreUnInstallerTest @@ -13,85 +14,90 @@ class CoreUnInstallerTest extends \PHPUnit_Framework_TestCase { protected $unInstaller; - protected $fileSystem; protected $gitIgnore; - protected $sourceFolder; - protected $destinationFolder; + protected $root; public function setUp() { $this->gitIgnore = $this->getMockBuilder('AydinHassan\MagentoCoreComposerInstaller\GitIgnore') ->disableOriginalConstructor() - ->setMethods(array('wipeOut', '__destruct')) + ->setMethods(array('removeEntry', 'removeIgnoreDirectories', '__destruct')) ->getMock(); - $this->fileSystem = new Filesystem(); - $this->unInstaller = new CoreUnInstaller($this->fileSystem, $this->gitIgnore); - $this->sourceFolder = sprintf("%s/magento-core-composer-installer/source", sys_get_temp_dir()); - $this->destinationFolder = sprintf("%s/magento-core-composer-installer/dest", sys_get_temp_dir()); - mkdir($this->sourceFolder, 0777, true); - mkdir($this->destinationFolder, 0777, true); + $this->unInstaller = new CoreUnInstaller(new Filesystem, $this->gitIgnore); + $this->root = vfsStream::setup('root', null, array('source' => array(), 'destination' => array())); } - public function testUninstallRemovesAllFilesWhichExistInSource() + /** + * Create a directory structure for us to work on + */ + private function createDirStructure() { - $this->createFile('file1.txt'); - $this->createFile('file2.txt'); - $this->createFolder('folder1'); - $this->createFile('folder1/file3.txt'); + vfsStream::newFile('file1.txt')->at($this->root->getChild('source')); + vfsStream::newFile('file1.txt')->at($this->root->getChild('destination')); + vfsStream::newFile('file2.txt')->at($this->root->getChild('source')); + vfsStream::newFile('file2.txt')->at($this->root->getChild('destination')); + vfsStream::newDirectory('folder1')->at($this->root->getChild('source')); + vfsStream::newDirectory('folder1')->at($this->root->getChild('destination')); + vfsStream::newFile('file3.txt')->at($this->root->getChild('source/folder1')); + vfsStream::newFile('file3.txt')->at($this->root->getChild('destination/folder1')); + } + + public function testUninstallRemovesAllFilesWhichExistInSourceAndRemovesEntriesFromGitIgnore() + { + $this->createDirStructure(); + + $this->gitIgnore + ->expects($this->at(0)) + ->method('removeEntry') + ->with('file1.txt'); + + $this->gitIgnore + ->expects($this->at(1)) + ->method('removeEntry') + ->with('file2.txt'); + + $this->gitIgnore + ->expects($this->at(2)) + ->method('removeEntry') + ->with('folder1/file3.txt'); $this->gitIgnore ->expects($this->once()) - ->method('wipeOut'); + ->method('removeIgnoreDirectories'); + + $this->unInstaller->unInstall(vfsStream::url('root/source'), vfsStream::url('root/destination')); - $this->unInstaller->uninstall($this->sourceFolder, $this->destinationFolder); - $this->assertTrue($this->fileSystem->isDirEmpty($this->destinationFolder)); + $this->assertFalse($this->root->hasChild('destination/file1.txt')); + $this->assertFalse($this->root->hasChild('destination/file2.txt')); + $this->assertFalse($this->root->hasChild('destination/folder1/file3.txt')); + $this->assertEmpty($this->gitIgnore->getEntries()); } public function testUninstallKeepsFoldersWhichOnlyExistInDestination() { - $this->createFile('file1.txt'); - $this->createFile('file2.txt'); - $this->createFolder('folder1'); - $this->createFile('folder1/file3.txt'); - touch(sprintf('%s/folder1/file4.txt', $this->destinationFolder)); - - $this->unInstaller->uninstall($this->sourceFolder, $this->destinationFolder); - - $this->assertFileExists(sprintf('%s/folder1/file4.txt', $this->destinationFolder)); - $this->assertFileNotExists(sprintf('%s/folder1/file3.txt', $this->destinationFolder)); - $this->assertFileNotExists(sprintf('%s/file1.txt', $this->destinationFolder)); - $this->assertFileNotExists(sprintf('%s/file2.txt', $this->destinationFolder)); - } + $this->createDirStructure(); - public function testFileIsSkippedIfItDoesNotExistInDestination() - { - $this->createFile('file1.txt'); - $this->createFile('file2.txt'); - $this->createFolder('folder1'); - $this->createFile('folder1/file3.txt'); - touch(sprintf('%s/folder1/file4.txt', $this->sourceFolder)); - - $this->unInstaller->uninstall($this->sourceFolder, $this->destinationFolder); - $this->assertTrue($this->fileSystem->isDirEmpty($this->destinationFolder)); - } + vfsStream::newFile('file4.txt')->at($this->root->getChild('destination/folder1')); - protected function createFile($file) - { - touch(sprintf('%s/%s', $this->sourceFolder, $file)); - touch(sprintf('%s/%s', $this->destinationFolder, $file)); - } + $this->unInstaller->unInstall(vfsStream::url('root/source'), vfsStream::url('root/destination')); - protected function createFolder($folder) - { - mkdir(sprintf('%s/%s', $this->sourceFolder, $folder)); - mkdir(sprintf('%s/%s', $this->destinationFolder, $folder)); + $this->assertTrue($this->root->hasChild('destination/folder1/file4.txt')); + $this->assertFalse($this->root->hasChild('destination/file1.txt')); + $this->assertFalse($this->root->hasChild('destination/file2.txt')); + $this->assertFalse($this->root->hasChild('destination/folder1/file3.txt')); } - public function tearDown() + public function testFileIsSkippedIfItDoesNotExistInDestination() { - $fs = new Filesystem(); - $fs->removeDirectory($this->sourceFolder); - $fs->removeDirectory($this->destinationFolder); + $this->createDirStructure(); + vfsStream::newFile('file4')->at($this->root->getChild('source')); + + $this->unInstaller->unInstall(vfsStream::url('root/source'), vfsStream::url('root/destination')); + + $this->assertFalse($this->root->hasChild('destination/file4.txt')); + $this->assertFalse($this->root->hasChild('destination/file1.txt')); + $this->assertFalse($this->root->hasChild('destination/file2.txt')); + $this->assertFalse($this->root->hasChild('destination/folder1/file3.txt')); } } diff --git a/test/GitIgnoreTest.php b/test/GitIgnoreTest.php index 74c4282..55f3163 100644 --- a/test/GitIgnoreTest.php +++ b/test/GitIgnoreTest.php @@ -3,6 +3,7 @@ namespace AydinHassan\MagentoCoreComposerInstallerTest; use AydinHassan\MagentoCoreComposerInstaller\GitIgnore; +use org\bovigo\vfs\vfsStream; /** * Class GitIgnoreTest @@ -11,60 +12,61 @@ */ class GitIgnoreTest extends \PHPUnit_Framework_TestCase { - protected $tmpGitIgnore; + protected $gitIgnoreFile; public function setUp() { - $this->tmpGitIgnore = sprintf("%s/magento-core-composer-installer/.gitignore", sys_get_temp_dir()); + vfsStream::setup('root'); + $this->gitIgnoreFile = vfsStream::url('root/.gitignore'); } public function testIfFileNotExistsItIsCreated() { - $gitIgnore = new GitIgnore($this->tmpGitIgnore, array()); + $gitIgnore = new GitIgnore($this->gitIgnoreFile, array()); $gitIgnore->addEntry("file1"); unset($gitIgnore); - $this->assertFileExists($this->tmpGitIgnore); + $this->assertFileExists($this->gitIgnoreFile); } public function testIfFileExistsExistingLinesAreLoaded() { $lines = array('line1', 'line2'); - file_put_contents($this->tmpGitIgnore, implode("\n", $lines)); + file_put_contents($this->gitIgnoreFile, implode("\n", $lines)); - $gitIgnore = new GitIgnore($this->tmpGitIgnore, array(), true); + $gitIgnore = new GitIgnore($this->gitIgnoreFile, array(), true); - $this->assertFileExists($this->tmpGitIgnore); + $this->assertFileExists($this->gitIgnoreFile); $this->assertSame($lines, $gitIgnore->getEntries()); } public function testWipeOutRemovesAllEntries() { $lines = array('line1', 'line2'); - file_put_contents($this->tmpGitIgnore, implode("\n", $lines)); + file_put_contents($this->gitIgnoreFile, implode("\n", $lines)); - $gitIgnore = new GitIgnore($this->tmpGitIgnore, array(), true); + $gitIgnore = new GitIgnore($this->gitIgnoreFile, array(), true); $gitIgnore->wipeOut(); - $this->assertFileExists($this->tmpGitIgnore); + $this->assertFileExists($this->gitIgnoreFile); $this->assertSame(array(), $gitIgnore->getEntries()); unset($gitIgnore); - $this->assertEquals("", file_get_contents($this->tmpGitIgnore)); + $this->assertEquals("", file_get_contents($this->gitIgnoreFile)); } public function testIgnoreDirectoriesAreAddedToGitIgnore() { $folders = array('folder1', 'folder2'); - $gitIgnore = new GitIgnore($this->tmpGitIgnore, $folders, true); + $gitIgnore = new GitIgnore($this->gitIgnoreFile, $folders, true); $gitIgnore->addEntry('folder1/file1.txt'); $this->assertSame($folders, $gitIgnore->getEntries()); unset($gitIgnore); - $this->assertEquals("folder1\nfolder2", file_get_contents($this->tmpGitIgnore)); + $this->assertEquals("folder1\nfolder2", file_get_contents($this->gitIgnoreFile)); } public function testAddEntryDoesNotAddDuplicates() { - $gitIgnore = new GitIgnore($this->tmpGitIgnore, array(), true); + $gitIgnore = new GitIgnore($this->gitIgnoreFile, array(), true); $gitIgnore->addEntry("file1.txt"); $gitIgnore->addEntry("file1.txt"); $this->assertCount(1, $gitIgnore->getEntries()); @@ -73,7 +75,7 @@ public function testAddEntryDoesNotAddDuplicates() public function testAddEntryDoesNotAddFileOrDirectoryIfItIsInsideAnIgnoredDirectory() { $ignoreDirs = array("dir1", "dir2/lol/"); - $gitIgnore = new GitIgnore($this->tmpGitIgnore, $ignoreDirs); + $gitIgnore = new GitIgnore($this->gitIgnoreFile, $ignoreDirs); $gitIgnore->addEntry("dir1/file1.txt"); $gitIgnore->addEntry("dir2/lol/file2.txt"); $gitIgnore->addEntry("dir2/file3.txt"); @@ -90,30 +92,42 @@ public function testAddEntryDoesNotAddFileOrDirectoryIfItIsInsideAnIgnoredDirect public function testIgnoreDirectoriesAreNotWrittenIfNoEntriesAreAdded() { $folders = array('folder1', 'folder2'); - $gitIgnore = new GitIgnore($this->tmpGitIgnore, $folders, true); + $gitIgnore = new GitIgnore($this->gitIgnoreFile, $folders, true); $this->assertSame($folders, $gitIgnore->getEntries()); unset($gitIgnore); - $this->assertFileNotExists($this->tmpGitIgnore); + $this->assertFileNotExists($this->gitIgnoreFile); } public function testGitIgnoreIsNotWrittenIfNoAdditions() { $lines = array('line1', 'line2'); - file_put_contents($this->tmpGitIgnore, implode("\n", $lines)); - $writeTime = filemtime($this->tmpGitIgnore); + file_put_contents($this->gitIgnoreFile, implode("\n", $lines)); + $writeTime = filemtime($this->gitIgnoreFile); $folders = array('folder1', 'folder2'); - $gitIgnore = new GitIgnore($this->tmpGitIgnore, $folders, true); + $gitIgnore = new GitIgnore($this->gitIgnoreFile, $folders, true); unset($gitIgnore); clearstatcache(); - $this->assertEquals($writeTime, filemtime($this->tmpGitIgnore)); + $this->assertEquals($writeTime, filemtime($this->gitIgnoreFile)); } - public function tearDown() + public function testCanRemoveEntry() { - if (file_exists($this->tmpGitIgnore)) { - unlink($this->tmpGitIgnore); - } + $lines = array('line1', 'line2'); + file_put_contents($this->gitIgnoreFile, implode("\n", $lines)); + + $gitIgnore = new GitIgnore($this->gitIgnoreFile, array(), true); + $gitIgnore->removeEntry('line1'); + + $this->assertEquals(array('line2'), $gitIgnore->getEntries()); + } + + public function testRemoveIgnoreDirectoriesSuccessfullyRemovesEntries() + { + $gitIgnore = new GitIgnore($this->gitIgnoreFile, array('line1', 'line2')); + $this->assertEquals(array('line1', 'line2'), $gitIgnore->getEntries()); + $gitIgnore->removeIgnoreDirectories(); + $this->assertEquals(array(), $gitIgnore->getEntries()); } } diff --git a/test/OptionsTest.php b/test/OptionsTest.php index 378ed7d..2a9ad85 100644 --- a/test/OptionsTest.php +++ b/test/OptionsTest.php @@ -15,7 +15,7 @@ public function testDefaults() { $options = new Options(array('magento-root-dir' => '/')); - $this->assertFalse($options->appendToGitIgnore()); + $this->assertTrue($options->appendToGitIgnore()); $this->assertSame("", $options->getMagentoRootDir()); $this->assertSame(array(".git"), $options->getDeployExcludes()); $this->assertInternalType('array', $options->getIgnoreDirectories());