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;
/**
* @var string
*/
private $origDir;
/**
* @var InstallationManager|MockObject
*/

View File

@ -12,8 +12,8 @@
namespace Composer\Test;
use Composer\Cache;
use Composer\TestCase;
use Composer\Util\Filesystem;
class CacheTest extends TestCase
{
@ -48,6 +48,14 @@ class CacheTest extends TestCase
->will($this->returnValue($this->finder));
}
protected function tearDown()
{
if (is_dir($this->root)) {
$fs = new Filesystem;
$fs->removeDirectory($this->root);
}
}
public function testRemoveOutdatedFiles()
{
$outdated = array_slice($this->files, 1);

View File

@ -18,6 +18,24 @@ 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));
}
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 +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");
$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 +281,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 +317,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()

View File

@ -17,6 +17,22 @@ use Composer\Util\Filesystem;
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)
{
$io = $io ?: $this->getMock('Composer\IO\IOInterface');
@ -85,9 +101,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 +124,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()

View File

@ -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 <Matt.Whittom@veteransunited.com>
@ -51,7 +52,10 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase
$this->io = null;
$this->config = null;
$this->repoConfig = null;
$this->testPath = null;
if (is_dir($this->testPath)) {
$fs = new Filesystem;
$fs->removeDirectory($this->testPath);
}
}
protected function getMockProcessExecutor()

View File

@ -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->testName = sys_get_temp_dir().'/composer-xz-test-vendor';
}
public function tearDown()
{
$this->fs = new Filesystem;
$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 {

View File

@ -13,14 +13,28 @@
namespace Composer\Test\Downloader;
use Composer\Downloader\ZipDownloader;
use Composer\Util\Filesystem;
class ZipDownloaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
*/
private $testDir;
public function setUp()
{
if (!class_exists('ZipArchive')) {
$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()
@ -44,7 +58,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->testDir));
$downloader = new ZipDownloader($io, $config);
try {

View File

@ -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');