2011-12-03 19:44:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
namespace Composer\Test\Util;
|
2011-12-03 19:44:00 +00:00
|
|
|
|
2012-02-09 17:45:28 +00:00
|
|
|
use Composer\Util\Filesystem;
|
2013-09-25 08:14:42 +00:00
|
|
|
use Composer\TestCase;
|
2011-12-03 19:44:00 +00:00
|
|
|
|
|
|
|
class FilesystemTest extends TestCase
|
|
|
|
{
|
2015-12-14 14:35:27 +00:00
|
|
|
/**
|
|
|
|
* @var Filesystem
|
|
|
|
*/
|
|
|
|
private $fs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $workingDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $testFile;
|
|
|
|
|
2011-12-03 19:44:00 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providePathCouplesAsCode
|
|
|
|
*/
|
2011-12-03 22:21:10 +00:00
|
|
|
public function testFindShortestPathCode($a, $b, $directory, $expected)
|
2011-12-03 19:44:00 +00:00
|
|
|
{
|
|
|
|
$fs = new Filesystem;
|
2011-12-03 22:21:10 +00:00
|
|
|
$this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory));
|
2011-12-03 19:44:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 14:35:27 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->fs = new Filesystem;
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->workingDir = $this->getUniqueTmpDirectory();
|
|
|
|
$this->testFile = $this->getUniqueTmpDirectory() . '/composer_test_file';
|
2015-12-14 14:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
if (is_dir($this->workingDir)) {
|
|
|
|
$this->fs->removeDirectory($this->workingDir);
|
|
|
|
}
|
|
|
|
if (is_file($this->testFile)) {
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->fs->removeDirectory(dirname($this->testFile));
|
2015-12-14 14:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-03 19:44:00 +00:00
|
|
|
public function providePathCouplesAsCode()
|
|
|
|
{
|
|
|
|
return array(
|
2011-12-03 22:21:10 +00:00
|
|
|
array('/foo/bar', '/foo/bar', false, "__FILE__"),
|
|
|
|
array('/foo/bar', '/foo/baz', false, "__DIR__.'/baz'"),
|
|
|
|
array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
|
|
|
|
array('/foo/bin/run', '/bar/bin/run', false, "'/bar/bin/run'"),
|
|
|
|
array('c:/bin/run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
|
|
|
|
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
|
|
|
|
array('c:/bin/run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
|
|
|
|
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
|
|
|
|
array('/foo/bar', '/foo/bar', true, "__DIR__"),
|
2013-03-13 21:13:32 +00:00
|
|
|
array('/foo/bar/', '/foo/bar', true, "__DIR__"),
|
2011-12-03 22:21:10 +00:00
|
|
|
array('/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"),
|
|
|
|
array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
|
|
|
|
array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"),
|
2012-03-10 01:14:40 +00:00
|
|
|
array('/bin/run', '/bin/run', true, "__DIR__"),
|
|
|
|
array('c:/bin/run', 'c:\\bin/run', true, "__DIR__"),
|
2011-12-03 22:21:10 +00:00
|
|
|
array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
|
|
|
|
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
|
|
|
|
array('c:/bin/run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
|
|
|
|
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
|
|
|
|
array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"),
|
|
|
|
array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"),
|
2011-12-04 21:40:30 +00:00
|
|
|
array('/tmp/test', '/tmp', true, "dirname(__DIR__)"),
|
|
|
|
array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"),
|
2011-12-18 20:10:10 +00:00
|
|
|
array('C:/Temp', 'c:\Temp\test', true, "__DIR__ . '/test'"),
|
2013-03-13 21:13:32 +00:00
|
|
|
array('/tmp/test/./', '/tmp/test/', true, '__DIR__'),
|
|
|
|
array('/tmp/test/../vendor', '/tmp/test', true, "dirname(__DIR__).'/test'"),
|
|
|
|
array('/tmp/test/.././vendor', '/tmp/test', true, "dirname(__DIR__).'/test'"),
|
|
|
|
array('C:/Temp', 'c:\Temp\..\..\test', true, "dirname(__DIR__).'/test'"),
|
|
|
|
array('C:/Temp/../..', 'd:\Temp\..\..\test', true, "'d:/test'"),
|
2013-06-07 11:20:17 +00:00
|
|
|
array('/foo/bar', '/foo/bar_vendor', true, "dirname(__DIR__).'/bar_vendor'"),
|
|
|
|
array('/foo/bar_vendor', '/foo/bar', true, "dirname(__DIR__).'/bar'"),
|
2013-09-26 12:34:41 +00:00
|
|
|
array('/foo/bar_vendor', '/foo/bar/src', true, "dirname(__DIR__).'/bar/src'"),
|
|
|
|
array('/foo/bar_vendor/src2', '/foo/bar/src/lib', true, "dirname(dirname(__DIR__)).'/bar/src/lib'"),
|
2011-12-03 19:44:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providePathCouples
|
|
|
|
*/
|
2012-03-23 11:49:29 +00:00
|
|
|
public function testFindShortestPath($a, $b, $expected, $directory = false)
|
2011-12-03 19:44:00 +00:00
|
|
|
{
|
|
|
|
$fs = new Filesystem;
|
2012-03-23 11:49:29 +00:00
|
|
|
$this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
|
2011-12-03 19:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function providePathCouples()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('/foo/bar', '/foo/bar', "./bar"),
|
|
|
|
array('/foo/bar', '/foo/baz', "./baz"),
|
2012-03-22 09:11:48 +00:00
|
|
|
array('/foo/bar/', '/foo/baz', "./baz"),
|
|
|
|
array('/foo/bar', '/foo/bar', "./", true),
|
|
|
|
array('/foo/bar', '/foo/baz', "../baz", true),
|
|
|
|
array('/foo/bar/', '/foo/baz', "../baz", true),
|
2012-03-24 17:10:16 +00:00
|
|
|
array('C:/foo/bar/', 'c:/foo/baz', "../baz", true),
|
2011-12-03 19:44:00 +00:00
|
|
|
array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
|
|
|
|
array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
|
2012-03-22 09:11:48 +00:00
|
|
|
array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true),
|
2012-03-24 17:10:16 +00:00
|
|
|
array('c:/foo/bin/run', 'd:/bar/bin/run', "d:/bar/bin/run", true),
|
2011-12-03 19:44:00 +00:00
|
|
|
array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
|
|
|
|
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
|
|
|
|
array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
|
|
|
|
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
|
2011-12-04 21:40:30 +00:00
|
|
|
array('C:/Temp/test', 'C:\Temp', "./"),
|
|
|
|
array('/tmp/test', '/tmp', "./"),
|
|
|
|
array('C:/Temp/test/sub', 'C:\Temp', "../"),
|
|
|
|
array('/tmp/test/sub', '/tmp', "../"),
|
2012-03-22 09:11:48 +00:00
|
|
|
array('/tmp/test/sub', '/tmp', "../../", true),
|
2012-03-24 17:10:16 +00:00
|
|
|
array('c:/tmp/test/sub', 'c:/tmp', "../../", true),
|
2011-12-04 21:40:30 +00:00
|
|
|
array('/tmp', '/tmp/test', "test"),
|
2011-12-03 22:21:10 +00:00
|
|
|
array('C:/Temp', 'C:\Temp\test', "test"),
|
2011-12-18 20:10:10 +00:00
|
|
|
array('C:/Temp', 'c:\Temp\test', "test"),
|
2013-03-13 21:13:32 +00:00
|
|
|
array('/tmp/test/./', '/tmp/test', './', true),
|
|
|
|
array('/tmp/test/../vendor', '/tmp/test', '../test', true),
|
|
|
|
array('/tmp/test/.././vendor', '/tmp/test', '../test', true),
|
|
|
|
array('C:/Temp', 'c:\Temp\..\..\test', "../test", true),
|
|
|
|
array('C:/Temp/../..', 'c:\Temp\..\..\test', "./test", true),
|
2013-10-19 11:38:30 +00:00
|
|
|
array('C:/Temp/../..', 'D:\Temp\..\..\test', "d:/test", true),
|
2013-03-13 21:13:32 +00:00
|
|
|
array('/tmp', '/tmp/../../test', '/test', true),
|
2013-06-07 11:20:17 +00:00
|
|
|
array('/foo/bar', '/foo/bar_vendor', '../bar_vendor', true),
|
|
|
|
array('/foo/bar_vendor', '/foo/bar', '../bar', true),
|
2013-09-26 12:34:41 +00:00
|
|
|
array('/foo/bar_vendor', '/foo/bar/src', '../bar/src', true),
|
|
|
|
array('/foo/bar_vendor/src2', '/foo/bar/src/lib', '../../bar/src/lib', true),
|
2015-06-03 19:48:01 +00:00
|
|
|
array('C:/', 'C:/foo/bar/', "foo/bar", true),
|
2011-12-03 19:44:00 +00:00
|
|
|
);
|
|
|
|
}
|
2012-11-19 09:29:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group GH-1339
|
|
|
|
*/
|
|
|
|
public function testRemoveDirectoryPhp()
|
|
|
|
{
|
2015-12-14 14:35:27 +00:00
|
|
|
@mkdir($this->workingDir . "/level1/level2", 0777, true);
|
|
|
|
file_put_contents($this->workingDir . "/level1/level2/hello.txt", "hello world");
|
2012-11-19 09:29:32 +00:00
|
|
|
|
|
|
|
$fs = new Filesystem;
|
2015-12-14 14:35:27 +00:00
|
|
|
$this->assertTrue($fs->removeDirectoryPhp($this->workingDir));
|
|
|
|
$this->assertFalse(file_exists($this->workingDir . "/level1/level2/hello.txt"));
|
2012-11-19 09:29:32 +00:00
|
|
|
}
|
2012-12-16 19:04:39 +00:00
|
|
|
|
|
|
|
public function testFileSize()
|
|
|
|
{
|
2015-12-14 14:35:27 +00:00
|
|
|
file_put_contents($this->testFile, 'Hello');
|
2012-12-16 19:04:39 +00:00
|
|
|
|
|
|
|
$fs = new Filesystem;
|
2015-12-14 14:35:27 +00:00
|
|
|
$this->assertGreaterThanOrEqual(5, $fs->size($this->testFile));
|
2012-12-16 19:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDirectorySize()
|
|
|
|
{
|
2015-12-14 14:35:27 +00:00
|
|
|
@mkdir($this->workingDir, 0777, true);
|
|
|
|
file_put_contents($this->workingDir."/file1.txt", 'Hello');
|
|
|
|
file_put_contents($this->workingDir."/file2.txt", 'World');
|
2012-12-16 19:04:39 +00:00
|
|
|
|
|
|
|
$fs = new Filesystem;
|
2015-12-14 14:35:27 +00:00
|
|
|
$this->assertGreaterThanOrEqual(10, $fs->size($this->workingDir));
|
2012-12-16 19:04:39 +00:00
|
|
|
}
|
2013-04-03 17:28:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideNormalizedPaths
|
|
|
|
*/
|
|
|
|
public function testNormalizePath($expected, $actual)
|
|
|
|
{
|
|
|
|
$fs = new Filesystem;
|
|
|
|
$this->assertEquals($expected, $fs->normalizePath($actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideNormalizedPaths()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('../foo', '../foo'),
|
|
|
|
array('c:/foo/bar', 'c:/foo//bar'),
|
|
|
|
array('C:/foo/bar', 'C:/foo/./bar'),
|
|
|
|
array('C:/bar', 'C:/foo/../bar'),
|
|
|
|
array('/bar', '/foo/../bar/'),
|
|
|
|
array('phar://c:/Foo', 'phar://c:/Foo/Bar/..'),
|
|
|
|
array('phar://c:/', 'phar://c:/Foo/Bar/../../../..'),
|
|
|
|
array('/', '/Foo/Bar/../../../..'),
|
2013-04-05 04:41:14 +00:00
|
|
|
array('/', '/'),
|
|
|
|
array('c:/', 'c:\\'),
|
|
|
|
array('../src', 'Foo/Bar/../../../src'),
|
|
|
|
array('c:../b', 'c:.\\..\\a\\..\\b'),
|
|
|
|
array('phar://c:../Foo', 'phar://c:../Foo'),
|
2013-04-03 17:28:06 +00:00
|
|
|
);
|
|
|
|
}
|
2014-07-24 12:17:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @link https://github.com/composer/composer/issues/3157
|
2015-04-21 00:15:28 +00:00
|
|
|
* @requires function symlink
|
2014-07-24 12:17:42 +00:00
|
|
|
*/
|
|
|
|
public function testUnlinkSymlinkedDirectory()
|
|
|
|
{
|
2015-12-14 14:35:27 +00:00
|
|
|
$basepath = $this->workingDir;
|
2014-07-24 12:17:42 +00:00
|
|
|
$symlinked = $basepath . "/linked";
|
|
|
|
@mkdir($basepath . "/real", 0777, true);
|
|
|
|
touch($basepath . "/real/FILE");
|
|
|
|
|
|
|
|
$result = @symlink($basepath . "/real", $symlinked);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$this->markTestSkipped('Symbolic links for directories not supported on this platform');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dir($symlinked)) {
|
|
|
|
$this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
|
|
|
|
}
|
|
|
|
|
|
|
|
$fs = new Filesystem();
|
|
|
|
$result = $fs->unlink($symlinked);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertFalse(file_exists($symlinked));
|
|
|
|
}
|
2014-07-24 12:42:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @link https://github.com/composer/composer/issues/3144
|
2015-04-21 00:15:28 +00:00
|
|
|
* @requires function symlink
|
2014-07-24 12:42:37 +00:00
|
|
|
*/
|
|
|
|
public function testRemoveSymlinkedDirectoryWithTrailingSlash()
|
|
|
|
{
|
2015-12-14 14:35:27 +00:00
|
|
|
@mkdir($this->workingDir . "/real", 0777, true);
|
|
|
|
touch($this->workingDir . "/real/FILE");
|
|
|
|
$symlinked = $this->workingDir . "/linked";
|
2014-07-24 12:42:37 +00:00
|
|
|
$symlinkedTrailingSlash = $symlinked . "/";
|
|
|
|
|
2015-12-14 14:35:27 +00:00
|
|
|
$result = @symlink($this->workingDir . "/real", $symlinked);
|
2014-07-24 12:42:37 +00:00
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$this->markTestSkipped('Symbolic links for directories not supported on this platform');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dir($symlinked)) {
|
|
|
|
$this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dir($symlinkedTrailingSlash)) {
|
|
|
|
$this->fail('Precondition assertion failed (is_dir false w trailing slash).');
|
|
|
|
}
|
|
|
|
|
|
|
|
$fs = new Filesystem();
|
|
|
|
|
|
|
|
$result = $fs->removeDirectory($symlinkedTrailingSlash);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertFalse(file_exists($symlinkedTrailingSlash));
|
|
|
|
$this->assertFalse(file_exists($symlinked));
|
|
|
|
}
|
2011-12-03 19:44:00 +00:00
|
|
|
}
|