1
0
Fork 0

Merge remote-tracking branch 'chorry/4694-clean-temporary-dirs-in-tests'

pull/4525/merge
Jordi Boggiano 2016-01-09 18:37:50 +00:00
commit 44a00a28fa
8 changed files with 143 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,8 +12,8 @@
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
{ {
@ -48,6 +48,14 @@ class CacheTest extends TestCase
->will($this->returnValue($this->finder)); ->will($this->returnValue($this->finder));
} }
protected function tearDown()
{
if (is_dir($this->root)) {
$fs = new Filesystem;
$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,24 @@ 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));
}
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 +252,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 +281,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 +318,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,22 @@ use Composer\Util\Filesystem;
class HgDownloaderTest extends \PHPUnit_Framework_TestCase class HgDownloaderTest extends \PHPUnit_Framework_TestCase
{ {
/** @var string */
private $workingDir;
protected function setUp()
{
$this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true));
}
protected function tearDown()
{
if (is_dir($this->workingDir)) {
$fs = new Filesystem;
$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 +101,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 +124,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>
@ -51,7 +52,10 @@ 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)) {
$fs = new Filesystem;
$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->testName = sys_get_temp_dir().'/composer-xz-test-vendor';
}
public function tearDown()
{
$this->fs = new Filesystem;
$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,28 @@
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 string
*/
private $testDir;
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->testDir = sys_get_temp_dir().'/composer-zip-test-vendor';
}
public function tearDown()
{
$fs = new Filesystem;
$fs->removeDirectory($this->testDir);
} }
public function testErrorMessages() public function testErrorMessages()
@ -44,7 +58,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->testDir));
$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');