From fa5de786ffeb48584a43b82632a1d5f6251c1582 Mon Sep 17 00:00:00 2001 From: Alexander Loutsenko Date: Mon, 14 Dec 2015 17:35:27 +0300 Subject: [PATCH 1/3] clean garbage dirs in tmp after composer tests #4694 --- .../Test/Autoload/AutoloadGeneratorTest.php | 5 ++ tests/Composer/Test/CacheTest.php | 14 +++- .../Test/Downloader/GitDownloaderTest.php | 36 +++++++--- .../Test/Downloader/HgDownloaderTest.php | 24 ++++++- .../Downloader/PerforceDownloaderTest.php | 8 ++- .../Test/Downloader/XzDownloaderTest.php | 20 +++++- .../Test/Downloader/ZipDownloaderTest.php | 20 +++++- tests/Composer/Test/Util/FilesystemTest.php | 68 +++++++++++++------ 8 files changed, 157 insertions(+), 38 deletions(-) diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index ce33801ac..502a483ab 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -42,6 +42,11 @@ class AutoloadGeneratorTest extends TestCase */ private $workingDir; + /** + * @var string + */ + private $origDir; + /** * @var InstallationManager|MockObject */ diff --git a/tests/Composer/Test/CacheTest.php b/tests/Composer/Test/CacheTest.php index b01dbfd89..fe3e76dad 100644 --- a/tests/Composer/Test/CacheTest.php +++ b/tests/Composer/Test/CacheTest.php @@ -12,15 +12,20 @@ namespace Composer\Test; -use Composer\Cache; use Composer\TestCase; +use Composer\Util\Filesystem; class CacheTest extends TestCase { private $files, $root, $finder, $cache; + /** + * @var Filesystem + */ + private $fs; public function setUp() { + $this->fs = new Filesystem; if (getenv('TRAVIS')) { $this->markTestSkipped('Test causes intermittent failures on Travis'); } @@ -48,6 +53,13 @@ class CacheTest extends TestCase ->will($this->returnValue($this->finder)); } + protected function tearDown() + { + if (is_dir($this->root)) { + $this->fs->removeDirectory($this->root); + } + } + public function testRemoveOutdatedFiles() { $outdated = array_slice($this->files, 1); diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 7521ac51a..75b5ab46c 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -18,6 +18,25 @@ use Composer\Util\Filesystem; class GitDownloaderTest extends \PHPUnit_Framework_TestCase { + /** @var Filesystem */ + private $fs; + /** @var string */ + private $workingDir; + + protected function setUp() + { + $this->fs = new Filesystem; + $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); + $this->fs->ensureDirectoryExists($this->workingDir); + } + + protected function tearDown() + { + if (is_dir($this->workingDir)) { + $this->fs->removeDirectory($this->workingDir); + } + } + protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) { $io = $io ?: $this->getMock('Composer\IO\IOInterface'); @@ -234,9 +253,6 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase { $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer"); - $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); - $fs = new Filesystem; - $fs->ensureDirectoryExists($tmpDir.'/.git'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') @@ -266,23 +282,22 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue(0)); $processExecutor->expects($this->at(4)) ->method('execute') - ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($tmpDir))) + ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir))) ->will($this->returnValue(0)); + $this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); - $downloader->update($packageMock, $packageMock, $tmpDir); + $downloader->update($packageMock, $packageMock, $this->workingDir); } /** + * @group failing * @expectedException \RuntimeException */ public function testUpdateThrowsRuntimeExceptionIfGitCommandFails() { $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer"); - $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); - $fs = new Filesystem; - $fs->ensureDirectoryExists($tmpDir.'/.git'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') @@ -303,9 +318,10 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedGitUpdateCommand)) ->will($this->returnValue(1)); - + + $this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); - $downloader->update($packageMock, $packageMock, $tmpDir); + $downloader->update($packageMock, $packageMock, $this->workingDir); } public function testRemove() diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php index ab9ec28cd..504f9d2c4 100644 --- a/tests/Composer/Test/Downloader/HgDownloaderTest.php +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -17,6 +17,25 @@ use Composer\Util\Filesystem; class HgDownloaderTest extends \PHPUnit_Framework_TestCase { + /** @var Filesystem */ + private $fs; + /** @var string */ + private $workingDir; + + protected function setUp() + { + $this->fs = new Filesystem; + $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); + $this->fs->ensureDirectoryExists($this->workingDir); + } + + protected function tearDown() + { + if (is_dir($this->workingDir)) { + $this->fs->removeDirectory($this->workingDir); + } + } + protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) { $io = $io ?: $this->getMock('Composer\IO\IOInterface'); @@ -85,9 +104,8 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase public function testUpdate() { - $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); $fs = new Filesystem; - $fs->ensureDirectoryExists($tmpDir.'/.hg'); + $fs->ensureDirectoryExists($this->workingDir.'/.hg'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') @@ -109,7 +127,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue(0)); $downloader = $this->getDownloaderMock(null, null, $processExecutor); - $downloader->update($packageMock, $packageMock, $tmpDir); + $downloader->update($packageMock, $packageMock, $this->workingDir); } public function testRemove() diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index 2c113b5ae..2e01ddee2 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -16,6 +16,7 @@ use Composer\Downloader\PerforceDownloader; use Composer\Config; use Composer\Repository\VcsRepository; use Composer\IO\IOInterface; +use Composer\Util\Filesystem; /** * @author Matt Whittom @@ -30,10 +31,13 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase protected $repoConfig; protected $repository; protected $testPath; + protected $fs; protected function setUp() { + $this->fs = new Filesystem; $this->testPath = sys_get_temp_dir() . '/composer-test'; + $this->fs->ensureDirectoryExists($this->testPath); $this->repoConfig = $this->getRepoConfig(); $this->config = $this->getConfig(); $this->io = $this->getMockIoInterface(); @@ -51,7 +55,9 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase $this->io = null; $this->config = null; $this->repoConfig = null; - $this->testPath = null; + if (is_dir($this->testPath)) { + $this->fs->removeDirectory($this->testPath); + } } protected function getMockProcessExecutor() diff --git a/tests/Composer/Test/Downloader/XzDownloaderTest.php b/tests/Composer/Test/Downloader/XzDownloaderTest.php index 02e6701a0..900975694 100644 --- a/tests/Composer/Test/Downloader/XzDownloaderTest.php +++ b/tests/Composer/Test/Downloader/XzDownloaderTest.php @@ -13,14 +13,32 @@ namespace Composer\Test\Downloader; use Composer\Downloader\XzDownloader; +use Composer\Util\Filesystem; class XzDownloaderTest extends \PHPUnit_Framework_TestCase { + /** + * @var Filesystem + */ + private $fs; + + /** + * @var string + */ + private $testName; + public function setUp() { if (defined('PHP_WINDOWS_VERSION_BUILD')) { $this->markTestSkipped('Skip test on Windows'); } + $this->fs = new Filesystem; + $this->testName = sys_get_temp_dir().'/composer-xz-test-vendor'; + } + + public function tearDown() + { + $this->fs->removeDirectory($this->testName); } public function testErrorMessages() @@ -44,7 +62,7 @@ class XzDownloaderTest extends \PHPUnit_Framework_TestCase $config->expects($this->any()) ->method('get') ->with('vendor-dir') - ->will($this->returnValue(sys_get_temp_dir().'/composer-xz-test-vendor')); + ->will($this->returnValue($this->testName)); $downloader = new XzDownloader($io, $config); try { diff --git a/tests/Composer/Test/Downloader/ZipDownloaderTest.php b/tests/Composer/Test/Downloader/ZipDownloaderTest.php index 58e0078b0..b50072084 100644 --- a/tests/Composer/Test/Downloader/ZipDownloaderTest.php +++ b/tests/Composer/Test/Downloader/ZipDownloaderTest.php @@ -13,14 +13,32 @@ namespace Composer\Test\Downloader; use Composer\Downloader\ZipDownloader; +use Composer\Util\Filesystem; class ZipDownloaderTest extends \PHPUnit_Framework_TestCase { + /** + * @var Filesystem + */ + private $fs; + + /** + * @var string + */ + private $testName; + public function setUp() { if (!class_exists('ZipArchive')) { $this->markTestSkipped('zip extension missing'); } + $this->fs = new Filesystem; + $this->testName = sys_get_temp_dir().'/composer-zip-test-vendor'; + } + + public function tearDown() + { + $this->fs->removeDirectory($this->testName); } public function testErrorMessages() @@ -44,7 +62,7 @@ class ZipDownloaderTest extends \PHPUnit_Framework_TestCase $config->expects($this->any()) ->method('get') ->with('vendor-dir') - ->will($this->returnValue(sys_get_temp_dir().'/composer-zip-test-vendor')); + ->will($this->returnValue($this->testName)); $downloader = new ZipDownloader($io, $config); try { diff --git a/tests/Composer/Test/Util/FilesystemTest.php b/tests/Composer/Test/Util/FilesystemTest.php index 4607512b6..d7c986369 100644 --- a/tests/Composer/Test/Util/FilesystemTest.php +++ b/tests/Composer/Test/Util/FilesystemTest.php @@ -17,6 +17,21 @@ use Composer\TestCase; class FilesystemTest extends TestCase { + /** + * @var Filesystem + */ + private $fs; + + /** + * @var string + */ + private $workingDir; + + /** + * @var string + */ + private $testFile; + /** * @dataProvider providePathCouplesAsCode */ @@ -26,6 +41,23 @@ class FilesystemTest extends TestCase $this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory)); } + public function setUp() + { + $this->fs = new Filesystem; + $this->workingDir = sys_get_temp_dir() . '/composer_testdir'; + $this->testFile = sys_get_temp_dir() . '/composer_test_file'; + } + + public function tearDown() + { + if (is_dir($this->workingDir)) { + $this->fs->removeDirectory($this->workingDir); + } + if (is_file($this->testFile)) { + $this->fs->remove($this->testFile); + } + } + public function providePathCouplesAsCode() { return array( @@ -121,33 +153,30 @@ class FilesystemTest extends TestCase */ public function testRemoveDirectoryPhp() { - $tmp = sys_get_temp_dir(); - @mkdir($tmp . "/composer_testdir/level1/level2", 0777, true); - file_put_contents($tmp . "/composer_testdir/level1/level2/hello.txt", "hello world"); + @mkdir($this->workingDir . "/level1/level2", 0777, true); + file_put_contents($this->workingDir . "/level1/level2/hello.txt", "hello world"); $fs = new Filesystem; - $this->assertTrue($fs->removeDirectoryPhp($tmp . "/composer_testdir")); - $this->assertFalse(file_exists($tmp . "/composer_testdir/level1/level2/hello.txt")); + $this->assertTrue($fs->removeDirectoryPhp($this->workingDir)); + $this->assertFalse(file_exists($this->workingDir . "/level1/level2/hello.txt")); } public function testFileSize() { - $tmp = sys_get_temp_dir(); - file_put_contents("$tmp/composer_test_file", 'Hello'); + file_put_contents($this->testFile, 'Hello'); $fs = new Filesystem; - $this->assertGreaterThanOrEqual(5, $fs->size("$tmp/composer_test_file")); + $this->assertGreaterThanOrEqual(5, $fs->size($this->testFile)); } public function testDirectorySize() { - $tmp = sys_get_temp_dir(); - @mkdir("$tmp/composer_testdir", 0777, true); - file_put_contents("$tmp/composer_testdir/file1.txt", 'Hello'); - file_put_contents("$tmp/composer_testdir/file2.txt", 'World'); + @mkdir($this->workingDir, 0777, true); + file_put_contents($this->workingDir."/file1.txt", 'Hello'); + file_put_contents($this->workingDir."/file2.txt", 'World'); $fs = new Filesystem; - $this->assertGreaterThanOrEqual(10, $fs->size("$tmp/composer_testdir")); + $this->assertGreaterThanOrEqual(10, $fs->size($this->workingDir)); } /** @@ -184,8 +213,7 @@ class FilesystemTest extends TestCase */ public function testUnlinkSymlinkedDirectory() { - $tmp = sys_get_temp_dir(); - $basepath = $tmp . "/composer_testdir"; + $basepath = $this->workingDir; $symlinked = $basepath . "/linked"; @mkdir($basepath . "/real", 0777, true); touch($basepath . "/real/FILE"); @@ -212,14 +240,12 @@ class FilesystemTest extends TestCase */ public function testRemoveSymlinkedDirectoryWithTrailingSlash() { - $tmp = sys_get_temp_dir(); - $basepath = $tmp . "/composer_testdir"; - @mkdir($basepath . "/real", 0777, true); - touch($basepath . "/real/FILE"); - $symlinked = $basepath . "/linked"; + @mkdir($this->workingDir . "/real", 0777, true); + touch($this->workingDir . "/real/FILE"); + $symlinked = $this->workingDir . "/linked"; $symlinkedTrailingSlash = $symlinked . "/"; - $result = @symlink($basepath . "/real", $symlinked); + $result = @symlink($this->workingDir . "/real", $symlinked); if (!$result) { $this->markTestSkipped('Symbolic links for directories not supported on this platform'); From 397ae2d7c8765f13ddaafb389b9fa14208626c20 Mon Sep 17 00:00:00 2001 From: Alexander Loutsenko Date: Mon, 14 Dec 2015 18:50:04 +0300 Subject: [PATCH 2/3] review fixes --- .../Composer/Test/Downloader/GitDownloaderTest.php | 1 - .../Composer/Test/Downloader/HgDownloaderTest.php | 7 ++----- .../Test/Downloader/PerforceDownloaderTest.php | 6 ++---- .../Composer/Test/Downloader/XzDownloaderTest.php | 2 +- .../Composer/Test/Downloader/ZipDownloaderTest.php | 14 +++++--------- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 75b5ab46c..f0b6699e5 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -27,7 +27,6 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase { $this->fs = new Filesystem; $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); - $this->fs->ensureDirectoryExists($this->workingDir); } protected function tearDown() diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php index 504f9d2c4..b2dee627b 100644 --- a/tests/Composer/Test/Downloader/HgDownloaderTest.php +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -17,22 +17,19 @@ use Composer\Util\Filesystem; class HgDownloaderTest extends \PHPUnit_Framework_TestCase { - /** @var Filesystem */ - private $fs; /** @var string */ private $workingDir; protected function setUp() { - $this->fs = new Filesystem; $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); - $this->fs->ensureDirectoryExists($this->workingDir); } protected function tearDown() { if (is_dir($this->workingDir)) { - $this->fs->removeDirectory($this->workingDir); + $fs = new Filesystem; + $fs->removeDirectory($this->workingDir); } } diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index 2e01ddee2..fc4297633 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -31,13 +31,10 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase protected $repoConfig; protected $repository; protected $testPath; - protected $fs; protected function setUp() { - $this->fs = new Filesystem; $this->testPath = sys_get_temp_dir() . '/composer-test'; - $this->fs->ensureDirectoryExists($this->testPath); $this->repoConfig = $this->getRepoConfig(); $this->config = $this->getConfig(); $this->io = $this->getMockIoInterface(); @@ -56,7 +53,8 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase $this->config = null; $this->repoConfig = null; if (is_dir($this->testPath)) { - $this->fs->removeDirectory($this->testPath); + $fs = new Filesystem; + $fs->removeDirectory($this->testPath); } } diff --git a/tests/Composer/Test/Downloader/XzDownloaderTest.php b/tests/Composer/Test/Downloader/XzDownloaderTest.php index 900975694..2acc7b4d0 100644 --- a/tests/Composer/Test/Downloader/XzDownloaderTest.php +++ b/tests/Composer/Test/Downloader/XzDownloaderTest.php @@ -32,12 +32,12 @@ class XzDownloaderTest extends \PHPUnit_Framework_TestCase if (defined('PHP_WINDOWS_VERSION_BUILD')) { $this->markTestSkipped('Skip test on Windows'); } - $this->fs = new Filesystem; $this->testName = sys_get_temp_dir().'/composer-xz-test-vendor'; } public function tearDown() { + $this->fs = new Filesystem; $this->fs->removeDirectory($this->testName); } diff --git a/tests/Composer/Test/Downloader/ZipDownloaderTest.php b/tests/Composer/Test/Downloader/ZipDownloaderTest.php index b50072084..25a95ace6 100644 --- a/tests/Composer/Test/Downloader/ZipDownloaderTest.php +++ b/tests/Composer/Test/Downloader/ZipDownloaderTest.php @@ -17,28 +17,24 @@ use Composer\Util\Filesystem; class ZipDownloaderTest extends \PHPUnit_Framework_TestCase { - /** - * @var Filesystem - */ - private $fs; /** * @var string */ - private $testName; + private $testDir; public function setUp() { if (!class_exists('ZipArchive')) { $this->markTestSkipped('zip extension missing'); } - $this->fs = new Filesystem; - $this->testName = sys_get_temp_dir().'/composer-zip-test-vendor'; + $this->testDir = sys_get_temp_dir().'/composer-zip-test-vendor'; } public function tearDown() { - $this->fs->removeDirectory($this->testName); + $fs = new Filesystem; + $fs->removeDirectory($this->testDir); } public function testErrorMessages() @@ -62,7 +58,7 @@ class ZipDownloaderTest extends \PHPUnit_Framework_TestCase $config->expects($this->any()) ->method('get') ->with('vendor-dir') - ->will($this->returnValue($this->testName)); + ->will($this->returnValue($this->testDir)); $downloader = new ZipDownloader($io, $config); try { From 909a679ab5e727a3fb27dfcabc4184aae93934fa Mon Sep 17 00:00:00 2001 From: Alexander Loutsenko Date: Mon, 14 Dec 2015 18:52:02 +0300 Subject: [PATCH 3/3] Filesystem is not needed as a property for CacheTest --- tests/Composer/Test/CacheTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/Composer/Test/CacheTest.php b/tests/Composer/Test/CacheTest.php index fe3e76dad..293064598 100644 --- a/tests/Composer/Test/CacheTest.php +++ b/tests/Composer/Test/CacheTest.php @@ -18,14 +18,9 @@ use Composer\Util\Filesystem; class CacheTest extends TestCase { private $files, $root, $finder, $cache; - /** - * @var Filesystem - */ - private $fs; public function setUp() { - $this->fs = new Filesystem; if (getenv('TRAVIS')) { $this->markTestSkipped('Test causes intermittent failures on Travis'); } @@ -56,7 +51,8 @@ class CacheTest extends TestCase protected function tearDown() { if (is_dir($this->root)) { - $this->fs->removeDirectory($this->root); + $fs = new Filesystem; + $fs->removeDirectory($this->root); } }