1
0
mirror of https://github.com/danog/file.git synced 2024-12-02 09:17:57 +01:00
file/test/DriverTest.php

541 lines
16 KiB
PHP
Raw Normal View History

<?php
2015-07-11 03:59:39 +02:00
2015-08-05 16:55:56 +02:00
namespace Amp\File\Test;
2015-07-11 03:59:39 +02:00
use Amp\File;
2019-08-23 20:23:33 +02:00
use Amp\File\FilesystemException;
2015-07-11 03:59:39 +02:00
2019-08-23 20:59:26 +02:00
abstract class DriverTest extends FilesystemTest
{
2020-06-24 22:00:24 +02:00
public function testListFiles()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$actual = yield File\scandir($fixtureDir);
$expected = ["dir", "dirlink", "fifo", "fifolink", "file", "filelink", "linkloop"];
2019-08-23 20:59:26 +02:00
$this->assertSame($expected, $actual);
2015-07-11 03:59:39 +02:00
}
2020-06-24 22:00:24 +02:00
public function testListFilesThrowsIfPathNotADirectory(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(FilesystemException::class);
yield File\scandir(__FILE__);
2015-08-08 16:09:07 +02:00
}
2020-06-24 22:00:24 +02:00
public function testListFilesThrowsIfPathDoesntExist(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/nonexistent";
yield File\scandir($path);
2015-08-08 16:09:07 +02:00
}
2020-06-24 22:00:24 +02:00
public function testCreateSymlink(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2015-07-11 03:59:39 +02:00
$original = "{$fixtureDir}/file";
2019-08-23 20:59:26 +02:00
$link = "{$fixtureDir}/symlink.txt";
$this->assertNull(yield File\symlink($original, $link));
2019-08-23 20:59:26 +02:00
$this->assertTrue(\is_link($link));
yield File\unlink($link);
2015-07-11 03:59:39 +02:00
}
2020-06-24 22:00:24 +02:00
public function testCreateSymlinkFailWhenLinkExists(): \Generator
{
$this->expectException(FilesystemException::class);
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
yield File\symlink($path, $path);
}
2020-06-24 22:00:24 +02:00
public function testCreateHardlink(): \Generator
{
$fixtureDir = Fixture::path();
$original = "{$fixtureDir}/file";
$link = "{$fixtureDir}/hardlink.txt";
$this->assertNull(yield File\link($original, $link));
$this->assertFileExists($link);
$this->assertFalse(\is_link($link));
yield File\unlink($link);
}
2020-06-24 22:00:24 +02:00
public function testCreateHardlinkFailWhenLinkExists(): \Generator
{
$this->expectException(FilesystemException::class);
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
yield File\link($path, $path);
}
2020-06-24 22:00:24 +02:00
public function testResolveSymlink(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$original = "{$fixtureDir}/file";
2019-08-23 20:59:26 +02:00
$link = "{$fixtureDir}/symlink.txt";
\symlink($original, $link);
2019-08-23 20:59:26 +02:00
$this->assertSame($original, yield File\readlink($link));
}
2020-06-24 22:00:24 +02:00
public function symlinkPathProvider(): array
{
return [
2020-06-24 22:00:24 +02:00
'nonExistingPath' => [
static function () {
return Fixture::path() . '/' . \uniqid('amphp-test-', true);
},
],
'notLink' => [
static function () {
return Fixture::path();
},
],
];
}
/**
2020-06-24 22:00:24 +02:00
* @dataProvider symlinkPathProvider
*
* @param \Closure $linkResolver
2020-06-24 22:00:24 +02:00
* @return \Generator
*/
2020-06-24 22:00:24 +02:00
public function testResolveSymlinkError(\Closure $linkResolver): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
$link = $linkResolver();
2019-08-23 20:59:26 +02:00
yield File\readlink($link);
}
2020-06-24 22:00:24 +02:00
public function testLinkStatus(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2015-07-11 03:59:39 +02:00
$target = "{$fixtureDir}/file";
2019-08-23 20:59:26 +02:00
$link = "{$fixtureDir}/symlink.txt";
yield File\symlink($target, $link);
2019-08-23 20:59:26 +02:00
$this->assertIsArray(yield File\lstat($link));
yield File\unlink($link);
2015-07-11 03:59:39 +02:00
}
2020-06-24 22:00:24 +02:00
public function testStatus(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-06-24 22:00:24 +02:00
$stat = yield File\stat($path);
$this->assertNotNull(File\StatCache::get($path));
2019-08-23 20:59:26 +02:00
$this->assertIsArray($stat);
2020-06-24 22:00:24 +02:00
$this->assertSameStatus(\stat($path), $stat);
2015-08-08 16:09:07 +02:00
}
2015-07-11 03:59:39 +02:00
2020-06-24 22:00:24 +02:00
public function testDirectoryStatus(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2020-06-24 22:00:24 +02:00
$stat = yield File\stat("{$fixtureDir}/dir");
2019-08-23 20:59:26 +02:00
$this->assertIsArray($stat);
2020-06-24 22:00:24 +02:00
$this->assertSameStatus(\stat("{$fixtureDir}/dir"), $stat);
2015-07-19 15:24:52 +02:00
}
2020-06-24 22:00:24 +02:00
public function testNonexistentPathStatusResolvesToNull(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2020-06-24 22:00:24 +02:00
$stat = yield File\stat("{$fixtureDir}/nonexistent");
2019-08-23 20:59:26 +02:00
$this->assertNull($stat);
2015-08-08 16:09:07 +02:00
}
2015-07-11 03:59:39 +02:00
2019-08-23 20:59:26 +02:00
public function testExists(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$this->assertFalse(yield File\exists("{$fixtureDir}/nonexistent"));
$this->assertTrue(yield File\exists("{$fixtureDir}/file"));
2015-07-19 15:24:52 +02:00
}
2020-06-24 22:00:24 +02:00
public function testRead(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$this->assertSame("small", yield File\get("{$fixtureDir}/file"));
2017-06-17 15:36:51 +02:00
}
2019-08-23 20:59:26 +02:00
public function testSize(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2019-08-23 20:59:26 +02:00
$stat = (yield File\stat($path));
$size = $stat["size"];
File\StatCache::clear($path);
$this->assertSame($size, (yield File\size($path)));
2015-08-08 16:09:07 +02:00
}
2015-07-11 03:59:39 +02:00
2019-08-23 20:59:26 +02:00
public function testSizeFailsOnNonexistentPath(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
yield File\size($path);
2015-08-08 16:09:07 +02:00
}
2019-08-23 20:59:26 +02:00
public function testSizeFailsOnDirectoryPath(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/dir";
$this->assertTrue(yield File\isdir($path));
File\StatCache::clear($path);
yield File\size($path);
2015-08-08 16:09:07 +02:00
}
/**
2020-06-24 22:00:24 +02:00
* @dataProvider dataForDirectoryCheck
*/
2020-06-24 22:00:24 +02:00
public function testIsDirectory(bool $expectedResult, string $name): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/{$name}";
$this->assertSame($expectedResult, yield File\isdir($path));
2015-08-08 16:09:07 +02:00
}
2020-06-24 22:00:24 +02:00
public function dataForDirectoryCheck(): \Generator
{
yield 'file' => [false, 'file'];
yield 'filelink' => [false, 'filelink'];
yield 'dir' => [true, 'dir'];
yield 'dirlink' => [true, 'dirlink'];
if (\extension_loaded('posix')) {
yield 'fifo' => [false, 'fifo'];
yield 'fifolink' => [false, 'fifolink'];
}
yield 'linkloop' => [false, 'linkloop'];
yield 'nonexistent' => [false, 'nonexistent'];
2015-08-08 16:09:07 +02:00
}
/**
2020-06-24 22:00:24 +02:00
* @dataProvider dataForFileCheck
*/
2020-06-24 22:00:24 +02:00
public function testIsFile(bool $expectedResult, string $name): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/{$name}";
$this->assertSame($expectedResult, yield File\isfile($path));
2015-08-08 16:09:07 +02:00
}
2020-06-24 22:00:24 +02:00
public function dataForFileCheck(): \Generator
{
yield 'file' => [true, 'file'];
yield 'filelink' => [true, 'filelink'];
yield 'dir' => [false, 'dir'];
yield 'dirlink' => [false, 'dirlink'];
if (\extension_loaded('posix')) {
yield 'fifo' => [false, 'fifo'];
yield 'fifolink' => [false, 'fifolink'];
}
yield 'linkloop' => [false, 'linkloop'];
yield 'nonexistent' => [false, 'nonexistent'];
2015-08-08 16:09:07 +02:00
}
/**
2020-06-24 22:00:24 +02:00
* @dataProvider dataForSymlinkCheck
*/
public function testIsSymlink(bool $expectedResult, string $name): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/{$name}";
$this->assertSame($expectedResult, yield File\isSymlink($path));
2015-08-08 16:09:07 +02:00
}
2020-06-24 22:00:24 +02:00
public function dataForSymlinkCheck(): \Generator
{
yield 'file' => [false, 'file'];
yield 'filelink' => [true, 'filelink'];
yield 'dir' => [false, 'dir'];
yield 'dirlink' => [true, 'dirlink'];
if (\extension_loaded('posix')) {
yield 'fifo' => [false, 'fifo'];
yield 'fifolink' => [true, 'fifolink'];
}
yield 'linkloop' => [true, 'linkloop'];
yield 'nonexistent' => [false, 'nonexistent'];
2015-07-11 03:59:39 +02:00
}
2020-06-24 22:00:24 +02:00
public function testMove(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2015-07-11 03:59:39 +02:00
2019-08-23 20:59:26 +02:00
$contents1 = "rename test";
$old = "{$fixtureDir}/rename1.txt";
$new = "{$fixtureDir}/rename2.txt";
2015-07-11 03:59:39 +02:00
2019-08-23 20:59:26 +02:00
yield File\put($old, $contents1);
$this->assertNull(yield File\rename($old, $new));
2019-08-23 20:59:26 +02:00
$contents2 = (yield File\get($new));
yield File\unlink($new);
2015-07-11 03:59:39 +02:00
2019-08-23 20:59:26 +02:00
$this->assertSame($contents1, $contents2);
2015-07-11 03:59:39 +02:00
}
2020-06-24 22:00:24 +02:00
public function testMoveFailsOnNonexistentPath(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
yield File\rename($path, $path);
}
2020-06-24 22:00:24 +02:00
public function testDeleteFile(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$toUnlink = "{$fixtureDir}/unlink";
yield File\stat($toUnlink);
2019-08-23 20:59:26 +02:00
yield File\put($toUnlink, "unlink me");
$this->assertNull(File\StatCache::get($toUnlink));
$this->assertNull(yield File\unlink($toUnlink));
2019-08-23 20:59:26 +02:00
$this->assertNull(yield File\stat($toUnlink));
2015-07-11 03:59:39 +02:00
}
2020-06-24 22:00:24 +02:00
public function testDeleteFileFailsOnNonexistentPath(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
yield File\unlink($path);
}
2020-06-24 22:00:24 +02:00
public function testDeleteFileFailsOnDirectory(): \Generator
{
$fixtureDir = Fixture::path();
$dir = "{$fixtureDir}/newdir";
yield File\mkdir($dir);
$this->expectException(FilesystemException::class);
yield File\unlink($dir);
}
2020-06-24 22:00:24 +02:00
public function testCreateAndDeleteDirectory(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2015-07-11 03:59:39 +02:00
2019-08-23 20:59:26 +02:00
$dir = "{$fixtureDir}/newdir";
2015-07-11 03:59:39 +02:00
2019-08-23 20:59:26 +02:00
\umask(0022);
2018-03-15 18:35:10 +01:00
$this->assertNull(yield File\mkdir($dir));
2019-08-23 20:59:26 +02:00
$stat = yield File\stat($dir);
2020-06-24 22:00:24 +02:00
$this->assertSame('0755', $this->getPermissionsFromStatus($stat));
$this->assertNull(yield File\rmdir($dir));
$this->assertNull(File\StatCache::get($dir));
2019-08-23 20:59:26 +02:00
$this->assertNull(yield File\stat($dir));
2016-09-28 12:39:24 +02:00
2019-08-23 20:59:26 +02:00
// test for 0, because previous array_filter made that not work
$dir = "{$fixtureDir}/newdir/with/recursive/creation/0/1/2";
2016-09-28 12:39:24 +02:00
$this->assertNull(yield File\mkdir($dir, 0764, true));
2019-08-23 20:59:26 +02:00
$stat = yield File\stat($dir);
2020-06-24 22:00:24 +02:00
$this->assertSame('0744', $this->getPermissionsFromStatus($stat));
2015-08-08 16:09:07 +02:00
}
2020-06-24 22:00:24 +02:00
public function testCreateDirectoryFailsOnNonexistentPath(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent/nonexistent";
$this->expectException(FilesystemException::class);
yield File\mkdir($path);
}
2020-06-24 22:00:24 +02:00
public function testDeleteDirectoryFailsOnNonexistentPath(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
yield File\rmdir($path);
}
2020-06-24 22:00:24 +02:00
public function testDeleteDirectoryFailsOnFile(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
$this->expectException(FilesystemException::class);
yield File\rmdir($path);
}
2019-08-23 20:59:26 +02:00
public function testMtime(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2019-08-23 20:59:26 +02:00
$stat = (yield File\stat($path));
$statMtime = $stat["mtime"];
File\StatCache::clear($path);
$this->assertSame($statMtime, (yield File\mtime($path)));
2015-08-08 16:09:07 +02:00
}
2019-08-23 20:59:26 +02:00
public function testMtimeFailsOnNonexistentPath(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
yield File\mtime($path);
2015-08-08 16:09:07 +02:00
}
2019-08-23 20:59:26 +02:00
public function testAtime(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2019-08-23 20:59:26 +02:00
$stat = (yield File\stat($path));
$statAtime = $stat["atime"];
File\StatCache::clear($path);
$this->assertSame($statAtime, (yield File\atime($path)));
2015-08-08 16:09:07 +02:00
}
2019-08-23 20:59:26 +02:00
public function testAtimeFailsOnNonexistentPath(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
yield File\atime($path);
2015-08-08 16:09:07 +02:00
}
2019-08-23 20:59:26 +02:00
public function testCtime(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2019-08-23 20:59:26 +02:00
$stat = (yield File\stat($path));
$statCtime = $stat["ctime"];
File\StatCache::clear($path);
$this->assertSame($statCtime, (yield File\ctime($path)));
2015-08-08 16:09:07 +02:00
}
2019-08-23 20:59:26 +02:00
public function testCtimeFailsOnNonexistentPath(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
yield File\ctime($path);
2015-07-11 03:59:39 +02:00
}
2015-07-18 20:53:46 +02:00
/**
* @group slow
*/
2019-08-23 20:59:26 +02:00
public function testTouch(): \Generator
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2015-07-18 20:53:46 +02:00
2019-08-23 20:59:26 +02:00
$touch = "{$fixtureDir}/touch";
yield File\put($touch, "touch me");
2015-07-18 20:53:46 +02:00
2020-06-24 22:00:24 +02:00
$oldStat = yield File\stat($touch);
$this->assertNull(yield File\touch($touch, \time() + 10, \time() + 20));
$this->assertNull(File\StatCache::get($touch));
2020-06-24 22:00:24 +02:00
$newStat = yield File\stat($touch);
2019-08-23 20:59:26 +02:00
yield File\unlink($touch);
2015-07-18 20:53:46 +02:00
2019-08-23 20:59:26 +02:00
$this->assertTrue($newStat["atime"] > $oldStat["atime"]);
$this->assertTrue($newStat["mtime"] > $oldStat["mtime"]);
2015-07-18 20:53:46 +02:00
}
2018-03-15 18:35:10 +01:00
public function testTouchFailsOnNonexistentPath(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent/nonexistent";
$this->expectException(FilesystemException::class);
yield File\touch($path);
}
2020-06-24 22:00:24 +02:00
public function testChangePermissions(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
$stat = yield File\stat($path);
$this->assertNotSame('0777', \substr(\decoct($stat['mode']), -4));
$this->assertNull(yield File\chmod($path, 0777));
$this->assertNull(File\StatCache::get($path));
$stat = yield File\stat($path);
$this->assertSame('0777', \substr(\decoct($stat['mode']), -4));
}
2020-06-24 22:00:24 +02:00
public function testChangePermissionsFailsOnNonexistentPath(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
yield File\chmod($path, 0777);
}
2020-06-24 22:00:24 +02:00
public function testChangeOwner(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
yield File\stat($path);
$user = \fileowner($path);
$this->assertNull(yield File\chown($path, $user));
$this->assertNull(File\StatCache::get($path));
}
2020-06-24 22:00:24 +02:00
public function testChangeOwnerFailsOnNonexistentPath(): \Generator
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
yield File\chown($path, 0);
}
2020-06-24 22:00:24 +02:00
private function assertSameStatus(array $expected, array $actual): void
{
$filter = function (array $stat) {
$filtered = \array_filter(
$stat,
function (string $key): bool {
return !\is_numeric($key);
},
ARRAY_FILTER_USE_KEY
);
\ksort($filtered);
return $filtered;
};
$this->assertSame($filter($expected), $filter($actual));
}
/**
* @param array $stat
*
* @return string
*/
private function getPermissionsFromStatus(array $stat): string
{
return \substr(\decoct($stat["mode"]), 1);
}
2015-07-11 03:59:39 +02:00
}