1
0
Fork 0

clean garbage dirs in tmp after composer tests #4694

pull/4702/head
Alexander Loutsenko 2015-12-14 17:35:27 +03:00
parent 1c525b76f8
commit fa5de786ff
8 changed files with 157 additions and 38 deletions

View File

@ -42,6 +42,11 @@ class AutoloadGeneratorTest extends TestCase
*/ */
private $workingDir; private $workingDir;
/**
* @var string
*/
private $origDir;
/** /**
* @var InstallationManager|MockObject * @var InstallationManager|MockObject
*/ */

View File

@ -12,15 +12,20 @@
namespace Composer\Test; namespace Composer\Test;
use Composer\Cache;
use Composer\TestCase; use Composer\TestCase;
use Composer\Util\Filesystem;
class CacheTest extends TestCase class CacheTest extends TestCase
{ {
private $files, $root, $finder, $cache; private $files, $root, $finder, $cache;
/**
* @var Filesystem
*/
private $fs;
public function setUp() public function setUp()
{ {
$this->fs = new Filesystem;
if (getenv('TRAVIS')) { if (getenv('TRAVIS')) {
$this->markTestSkipped('Test causes intermittent failures on Travis'); $this->markTestSkipped('Test causes intermittent failures on Travis');
} }
@ -48,6 +53,13 @@ class CacheTest extends TestCase
->will($this->returnValue($this->finder)); ->will($this->returnValue($this->finder));
} }
protected function tearDown()
{
if (is_dir($this->root)) {
$this->fs->removeDirectory($this->root);
}
}
public function testRemoveOutdatedFiles() public function testRemoveOutdatedFiles()
{ {
$outdated = array_slice($this->files, 1); $outdated = array_slice($this->files, 1);

View File

@ -18,6 +18,25 @@ use Composer\Util\Filesystem;
class GitDownloaderTest extends \PHPUnit_Framework_TestCase 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) protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
{ {
$io = $io ?: $this->getMock('Composer\IO\IOInterface'); $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"); $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 = $this->getMock('Composer\Package\PackageInterface');
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceReference') ->method('getSourceReference')
@ -266,23 +282,22 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue(0)); ->will($this->returnValue(0));
$processExecutor->expects($this->at(4)) $processExecutor->expects($this->at(4))
->method('execute') ->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)); ->will($this->returnValue(0));
$this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
$downloader->update($packageMock, $packageMock, $tmpDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
} }
/** /**
* @group failing
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */
public function testUpdateThrowsRuntimeExceptionIfGitCommandFails() public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
{ {
$expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer"); $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 = $this->getMock('Composer\Package\PackageInterface');
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceReference') ->method('getSourceReference')
@ -304,8 +319,9 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
->with($this->equalTo($expectedGitUpdateCommand)) ->with($this->equalTo($expectedGitUpdateCommand))
->will($this->returnValue(1)); ->will($this->returnValue(1));
$this->fs->ensureDirectoryExists($this->workingDir.'/.git');
$downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
$downloader->update($packageMock, $packageMock, $tmpDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
} }
public function testRemove() public function testRemove()

View File

@ -17,6 +17,25 @@ use Composer\Util\Filesystem;
class HgDownloaderTest extends \PHPUnit_Framework_TestCase 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) protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
{ {
$io = $io ?: $this->getMock('Composer\IO\IOInterface'); $io = $io ?: $this->getMock('Composer\IO\IOInterface');
@ -85,9 +104,8 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase
public function testUpdate() public function testUpdate()
{ {
$tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true));
$fs = new Filesystem; $fs = new Filesystem;
$fs->ensureDirectoryExists($tmpDir.'/.hg'); $fs->ensureDirectoryExists($this->workingDir.'/.hg');
$packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock = $this->getMock('Composer\Package\PackageInterface');
$packageMock->expects($this->any()) $packageMock->expects($this->any())
->method('getSourceReference') ->method('getSourceReference')
@ -109,7 +127,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue(0)); ->will($this->returnValue(0));
$downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader = $this->getDownloaderMock(null, null, $processExecutor);
$downloader->update($packageMock, $packageMock, $tmpDir); $downloader->update($packageMock, $packageMock, $this->workingDir);
} }
public function testRemove() public function testRemove()

View File

@ -16,6 +16,7 @@ use Composer\Downloader\PerforceDownloader;
use Composer\Config; use Composer\Config;
use Composer\Repository\VcsRepository; use Composer\Repository\VcsRepository;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Util\Filesystem;
/** /**
* @author Matt Whittom <Matt.Whittom@veteransunited.com> * @author Matt Whittom <Matt.Whittom@veteransunited.com>
@ -30,10 +31,13 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase
protected $repoConfig; protected $repoConfig;
protected $repository; protected $repository;
protected $testPath; protected $testPath;
protected $fs;
protected function setUp() protected function setUp()
{ {
$this->fs = new Filesystem;
$this->testPath = sys_get_temp_dir() . '/composer-test'; $this->testPath = sys_get_temp_dir() . '/composer-test';
$this->fs->ensureDirectoryExists($this->testPath);
$this->repoConfig = $this->getRepoConfig(); $this->repoConfig = $this->getRepoConfig();
$this->config = $this->getConfig(); $this->config = $this->getConfig();
$this->io = $this->getMockIoInterface(); $this->io = $this->getMockIoInterface();
@ -51,7 +55,9 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase
$this->io = null; $this->io = null;
$this->config = null; $this->config = null;
$this->repoConfig = null; $this->repoConfig = null;
$this->testPath = null; if (is_dir($this->testPath)) {
$this->fs->removeDirectory($this->testPath);
}
} }
protected function getMockProcessExecutor() protected function getMockProcessExecutor()

View File

@ -13,14 +13,32 @@
namespace Composer\Test\Downloader; namespace Composer\Test\Downloader;
use Composer\Downloader\XzDownloader; use Composer\Downloader\XzDownloader;
use Composer\Util\Filesystem;
class XzDownloaderTest extends \PHPUnit_Framework_TestCase class XzDownloaderTest extends \PHPUnit_Framework_TestCase
{ {
/**
* @var Filesystem
*/
private $fs;
/**
* @var string
*/
private $testName;
public function setUp() public function setUp()
{ {
if (defined('PHP_WINDOWS_VERSION_BUILD')) { if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->markTestSkipped('Skip test on Windows'); $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() public function testErrorMessages()
@ -44,7 +62,7 @@ class XzDownloaderTest extends \PHPUnit_Framework_TestCase
$config->expects($this->any()) $config->expects($this->any())
->method('get') ->method('get')
->with('vendor-dir') ->with('vendor-dir')
->will($this->returnValue(sys_get_temp_dir().'/composer-xz-test-vendor')); ->will($this->returnValue($this->testName));
$downloader = new XzDownloader($io, $config); $downloader = new XzDownloader($io, $config);
try { try {

View File

@ -13,14 +13,32 @@
namespace Composer\Test\Downloader; namespace Composer\Test\Downloader;
use Composer\Downloader\ZipDownloader; use Composer\Downloader\ZipDownloader;
use Composer\Util\Filesystem;
class ZipDownloaderTest extends \PHPUnit_Framework_TestCase class ZipDownloaderTest extends \PHPUnit_Framework_TestCase
{ {
/**
* @var Filesystem
*/
private $fs;
/**
* @var string
*/
private $testName;
public function setUp() public function setUp()
{ {
if (!class_exists('ZipArchive')) { if (!class_exists('ZipArchive')) {
$this->markTestSkipped('zip extension missing'); $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() public function testErrorMessages()
@ -44,7 +62,7 @@ class ZipDownloaderTest extends \PHPUnit_Framework_TestCase
$config->expects($this->any()) $config->expects($this->any())
->method('get') ->method('get')
->with('vendor-dir') ->with('vendor-dir')
->will($this->returnValue(sys_get_temp_dir().'/composer-zip-test-vendor')); ->will($this->returnValue($this->testName));
$downloader = new ZipDownloader($io, $config); $downloader = new ZipDownloader($io, $config);
try { try {

View File

@ -17,6 +17,21 @@ use Composer\TestCase;
class FilesystemTest extends TestCase class FilesystemTest extends TestCase
{ {
/**
* @var Filesystem
*/
private $fs;
/**
* @var string
*/
private $workingDir;
/**
* @var string
*/
private $testFile;
/** /**
* @dataProvider providePathCouplesAsCode * @dataProvider providePathCouplesAsCode
*/ */
@ -26,6 +41,23 @@ class FilesystemTest extends TestCase
$this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory)); $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() public function providePathCouplesAsCode()
{ {
return array( return array(
@ -121,33 +153,30 @@ class FilesystemTest extends TestCase
*/ */
public function testRemoveDirectoryPhp() public function testRemoveDirectoryPhp()
{ {
$tmp = sys_get_temp_dir(); @mkdir($this->workingDir . "/level1/level2", 0777, true);
@mkdir($tmp . "/composer_testdir/level1/level2", 0777, true); file_put_contents($this->workingDir . "/level1/level2/hello.txt", "hello world");
file_put_contents($tmp . "/composer_testdir/level1/level2/hello.txt", "hello world");
$fs = new Filesystem; $fs = new Filesystem;
$this->assertTrue($fs->removeDirectoryPhp($tmp . "/composer_testdir")); $this->assertTrue($fs->removeDirectoryPhp($this->workingDir));
$this->assertFalse(file_exists($tmp . "/composer_testdir/level1/level2/hello.txt")); $this->assertFalse(file_exists($this->workingDir . "/level1/level2/hello.txt"));
} }
public function testFileSize() public function testFileSize()
{ {
$tmp = sys_get_temp_dir(); file_put_contents($this->testFile, 'Hello');
file_put_contents("$tmp/composer_test_file", 'Hello');
$fs = new Filesystem; $fs = new Filesystem;
$this->assertGreaterThanOrEqual(5, $fs->size("$tmp/composer_test_file")); $this->assertGreaterThanOrEqual(5, $fs->size($this->testFile));
} }
public function testDirectorySize() public function testDirectorySize()
{ {
$tmp = sys_get_temp_dir(); @mkdir($this->workingDir, 0777, true);
@mkdir("$tmp/composer_testdir", 0777, true); file_put_contents($this->workingDir."/file1.txt", 'Hello');
file_put_contents("$tmp/composer_testdir/file1.txt", 'Hello'); file_put_contents($this->workingDir."/file2.txt", 'World');
file_put_contents("$tmp/composer_testdir/file2.txt", 'World');
$fs = new Filesystem; $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() public function testUnlinkSymlinkedDirectory()
{ {
$tmp = sys_get_temp_dir(); $basepath = $this->workingDir;
$basepath = $tmp . "/composer_testdir";
$symlinked = $basepath . "/linked"; $symlinked = $basepath . "/linked";
@mkdir($basepath . "/real", 0777, true); @mkdir($basepath . "/real", 0777, true);
touch($basepath . "/real/FILE"); touch($basepath . "/real/FILE");
@ -212,14 +240,12 @@ class FilesystemTest extends TestCase
*/ */
public function testRemoveSymlinkedDirectoryWithTrailingSlash() public function testRemoveSymlinkedDirectoryWithTrailingSlash()
{ {
$tmp = sys_get_temp_dir(); @mkdir($this->workingDir . "/real", 0777, true);
$basepath = $tmp . "/composer_testdir"; touch($this->workingDir . "/real/FILE");
@mkdir($basepath . "/real", 0777, true); $symlinked = $this->workingDir . "/linked";
touch($basepath . "/real/FILE");
$symlinked = $basepath . "/linked";
$symlinkedTrailingSlash = $symlinked . "/"; $symlinkedTrailingSlash = $symlinked . "/";
$result = @symlink($basepath . "/real", $symlinked); $result = @symlink($this->workingDir . "/real", $symlinked);
if (!$result) { if (!$result) {
$this->markTestSkipped('Symbolic links for directories not supported on this platform'); $this->markTestSkipped('Symbolic links for directories not supported on this platform');