1
0
Fork 0
composer/tests/Composer/Test/Util/FilesystemTest.php

380 lines
17 KiB
PHP
Raw Normal View History

<?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.
*/
namespace Composer\Test\Util;
use Composer\Util\Filesystem;
use Composer\Test\TestCase;
class FilesystemTest extends TestCase
{
/**
* @var Filesystem
*/
private $fs;
/**
* @var string
*/
private $workingDir;
/**
* @var string
*/
private $testFile;
2021-12-08 16:03:05 +00:00
public function setUp(): void
{
$this->fs = new Filesystem;
$this->workingDir = $this->getUniqueTmpDirectory();
$this->testFile = $this->getUniqueTmpDirectory() . '/composer_test_file';
}
protected function tearDown(): void
{
parent::tearDown();
if (is_dir($this->workingDir)) {
$this->fs->removeDirectory($this->workingDir);
}
if (is_file($this->testFile)) {
$this->fs->removeDirectory(dirname($this->testFile));
}
}
/**
* @dataProvider providePathCouplesAsCode
2021-10-27 14:18:46 +00:00
*
* @param string $a
* @param string $b
* @param bool $directory
* @param string $expected
* @param bool $static
*/
public function testFindShortestPathCode($a, $b, $directory, $expected, $static = false): void
{
$fs = new Filesystem;
$this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory, $static));
}
public function providePathCouplesAsCode()
{
return array(
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__"),
array('/foo/bar/', '/foo/bar', true, "__DIR__"),
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__"),
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'"),
array('/tmp/test', '/tmp', true, "dirname(__DIR__)"),
array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"),
array('C:/Temp', 'c:\Temp\test', true, "__DIR__ . '/test'"),
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'"),
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'"),
// static use case
array('/tmp/test/../vendor', '/tmp/test', true, "__DIR__ . '/..'.'/test'", true),
array('/tmp/test/.././vendor', '/tmp/test', true, "__DIR__ . '/..'.'/test'", true),
array('C:/Temp', 'c:\Temp\..\..\test', true, "__DIR__ . '/..'.'/test'", true),
array('C:/Temp/../..', 'd:\Temp\..\..\test', true, "'d:/test'", true),
array('/foo/bar', '/foo/bar_vendor', true, "__DIR__ . '/..'.'/bar_vendor'", true),
array('/foo/bar_vendor', '/foo/bar', true, "__DIR__ . '/..'.'/bar'", true),
array('/foo/bar_vendor', '/foo/bar/src', true, "__DIR__ . '/..'.'/bar/src'", true),
array('/foo/bar_vendor/src2', '/foo/bar/src/lib', true, "__DIR__ . '/../..'.'/bar/src/lib'", true),
);
}
/**
* @dataProvider providePathCouples
2021-10-27 14:18:46 +00:00
*
* @param string $a
* @param string $b
* @param string $expected
* @param bool $directory
*/
public function testFindShortestPath($a, $b, $expected, $directory = false): void
{
$fs = new Filesystem;
$this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
}
public function providePathCouples()
{
return array(
array('/foo/bar', '/foo/bar', "./bar"),
array('/foo/bar', '/foo/baz', "./baz"),
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),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
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),
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"),
array('C:/Temp/test', 'C:\Temp', "./"),
array('/tmp/test', '/tmp', "./"),
array('C:/Temp/test/sub', 'C:\Temp', "../"),
array('/tmp/test/sub', '/tmp', "../"),
array('/tmp/test/sub', '/tmp', "../../", true),
2012-03-24 17:10:16 +00:00
array('c:/tmp/test/sub', 'c:/tmp', "../../", true),
array('/tmp', '/tmp/test', "test"),
array('C:/Temp', 'C:\Temp\test', "test"),
array('C:/Temp', 'c:\Temp\test', "test"),
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),
array('C:/Temp/../..', 'D:\Temp\..\..\test', "d:/test", true),
array('/tmp', '/tmp/../../test', '/test', true),
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),
array('C:/', 'C:/foo/bar/', "foo/bar", true),
);
}
/**
* @group GH-1339
*/
public function testRemoveDirectoryPhp(): void
{
@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($this->workingDir));
2020-09-11 09:27:26 +00:00
$this->assertFileDoesNotExist($this->workingDir . "/level1/level2/hello.txt");
}
2012-12-16 19:04:39 +00:00
public function testFileSize(): void
2012-12-16 19:04:39 +00:00
{
file_put_contents($this->testFile, 'Hello');
2012-12-16 19:04:39 +00:00
$fs = new Filesystem;
$this->assertGreaterThanOrEqual(5, $fs->size($this->testFile));
2012-12-16 19:04:39 +00:00
}
public function testDirectorySize(): void
2012-12-16 19:04:39 +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;
$this->assertGreaterThanOrEqual(10, $fs->size($this->workingDir));
2012-12-16 19:04:39 +00:00
}
/**
* @dataProvider provideNormalizedPaths
2021-10-27 14:18:46 +00:00
*
* @param string $expected
* @param string $actual
*/
public function testNormalizePath($expected, $actual): void
{
$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:/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:/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('/', '//'),
array('/', '///'),
array('/Foo', '///Foo'),
2013-04-05 04:41:14 +00:00
array('c:/', 'c:\\'),
array('../src', 'Foo/Bar/../../../src'),
array('c:../b', 'c:.\\..\\a\\..\\b'),
array('phar://c:../Foo', 'phar://c:../Foo'),
array('//foo/bar', '\\\\foo\\bar'),
);
}
2014-07-24 12:17:42 +00:00
/**
* @link https://github.com/composer/composer/issues/3157
* @requires function symlink
2014-07-24 12:17:42 +00:00
*/
public function testUnlinkSymlinkedDirectory(): void
2014-07-24 12:17:42 +00:00
{
2017-03-08 14:07:29 +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).');
}
2017-03-08 14:07:29 +00:00
$fs = new Filesystem();
2014-07-24 12:17:42 +00:00
$result = $fs->unlink($symlinked);
$this->assertTrue($result);
2020-09-11 09:27:26 +00:00
$this->assertFileDoesNotExist($symlinked);
2014-07-24 12:17:42 +00:00
}
/**
* @link https://github.com/composer/composer/issues/3144
* @requires function symlink
*/
public function testRemoveSymlinkedDirectoryWithTrailingSlash(): void
{
@mkdir($this->workingDir . "/real", 0777, true);
touch($this->workingDir . "/real/FILE");
2017-03-08 14:07:29 +00:00
$symlinked = $this->workingDir . "/linked";
$symlinkedTrailingSlash = $symlinked . "/";
$result = @symlink($this->workingDir . "/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).');
}
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);
2020-09-11 09:27:26 +00:00
$this->assertFileDoesNotExist($symlinkedTrailingSlash);
$this->assertFileDoesNotExist($symlinked);
}
public function testJunctions(): void
{
@mkdir($this->workingDir . '/real/nesting/testing', 0777, true);
$fs = new Filesystem();
// Non-Windows systems do not support this and will return false on all tests, and an exception on creation
if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->assertFalse($fs->isJunction($this->workingDir));
$this->assertFalse($fs->removeJunction($this->workingDir));
2021-12-09 19:55:26 +00:00
self::expectException('LogicException');
self::expectExceptionMessage('not available on non-Windows platform');
}
$target = $this->workingDir . '/real/../real/nesting';
$junction = $this->workingDir . '/junction';
// Create and detect junction
$fs->junction($target, $junction);
$this->assertTrue($fs->isJunction($junction), $junction . ': is a junction');
$this->assertFalse($fs->isJunction($target), $target . ': is not a junction');
$this->assertTrue($fs->isJunction($target . '/../../junction'), $target . '/../../junction: is a junction');
$this->assertFalse($fs->isJunction($junction . '/../real'), $junction . '/../real: is not a junction');
$this->assertTrue($fs->isJunction($junction . '/../junction'), $junction . '/../junction: is a junction');
// Remove junction
$this->assertTrue(is_dir($junction), $junction . ' is a directory');
$this->assertTrue($fs->removeJunction($junction), $junction . ' has been removed');
$this->assertFalse(is_dir($junction), $junction . ' is not a directory');
}
public function testCopy(): void
{
@mkdir($this->workingDir . '/foo/bar', 0777, true);
@mkdir($this->workingDir . '/foo/baz', 0777, true);
file_put_contents($this->workingDir . '/foo/foo.file', 'foo');
file_put_contents($this->workingDir . '/foo/bar/foobar.file', 'foobar');
file_put_contents($this->workingDir . '/foo/baz/foobaz.file', 'foobaz');
file_put_contents($this->testFile, 'testfile');
2017-09-01 18:47:13 +00:00
$fs = new Filesystem();
2017-09-01 18:47:13 +00:00
$result1 = $fs->copy($this->workingDir . '/foo', $this->workingDir . '/foop');
2017-12-18 15:02:48 +00:00
$this->assertTrue($result1, 'Copying directory failed.');
2017-09-01 18:47:13 +00:00
$this->assertTrue(is_dir($this->workingDir . '/foop'), 'Not a directory: ' . $this->workingDir . '/foop');
$this->assertTrue(is_dir($this->workingDir . '/foop/bar'), 'Not a directory: ' . $this->workingDir . '/foop/bar');
$this->assertTrue(is_dir($this->workingDir . '/foop/baz'), 'Not a directory: ' . $this->workingDir . '/foop/baz');
$this->assertTrue(is_file($this->workingDir . '/foop/foo.file'), 'Not a file: ' . $this->workingDir . '/foop/foo.file');
$this->assertTrue(is_file($this->workingDir . '/foop/bar/foobar.file'), 'Not a file: ' . $this->workingDir . '/foop/bar/foobar.file');
$this->assertTrue(is_file($this->workingDir . '/foop/baz/foobaz.file'), 'Not a file: ' . $this->workingDir . '/foop/baz/foobaz.file');
$result2 = $fs->copy($this->testFile, $this->workingDir . '/testfile.file');
$this->assertTrue($result2);
2017-09-01 18:47:13 +00:00
$this->assertTrue(is_file($this->workingDir . '/testfile.file'));
}
public function testCopyThenRemove(): void
{
@mkdir($this->workingDir . '/foo/bar', 0777, true);
@mkdir($this->workingDir . '/foo/baz', 0777, true);
file_put_contents($this->workingDir . '/foo/foo.file', 'foo');
file_put_contents($this->workingDir . '/foo/bar/foobar.file', 'foobar');
file_put_contents($this->workingDir . '/foo/baz/foobaz.file', 'foobaz');
file_put_contents($this->testFile, 'testfile');
2017-09-01 18:47:13 +00:00
$fs = new Filesystem();
2017-09-01 18:47:13 +00:00
$fs->copyThenRemove($this->testFile, $this->workingDir . '/testfile.file');
$this->assertFalse(is_file($this->testFile), 'Still a file: ' . $this->testFile);
$fs->copyThenRemove($this->workingDir . '/foo', $this->workingDir . '/foop');
2017-09-01 18:47:13 +00:00
$this->assertFalse(is_file($this->workingDir . '/foo/baz/foobaz.file'), 'Still a file: ' . $this->workingDir . '/foo/baz/foobaz.file');
$this->assertFalse(is_file($this->workingDir . '/foo/bar/foobar.file'), 'Still a file: ' . $this->workingDir . '/foo/bar/foobar.file');
$this->assertFalse(is_file($this->workingDir . '/foo/foo.file'), 'Still a file: ' . $this->workingDir . '/foo/foo.file');
$this->assertFalse(is_dir($this->workingDir . '/foo/baz'), 'Still a directory: ' . $this->workingDir . '/foo/baz');
$this->assertFalse(is_dir($this->workingDir . '/foo/bar'), 'Still a directory: ' . $this->workingDir . '/foo/bar');
$this->assertFalse(is_dir($this->workingDir . '/foo'), 'Still a directory: ' . $this->workingDir . '/foo');
}
}