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

542 lines
15 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-10-08 16:23:44 +02:00
protected File\Filesystem $driver;
2020-06-30 21:45:09 +02:00
2020-10-08 16:23:44 +02:00
public function testListFiles()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2020-10-08 16:23:44 +02:00
$actual = $this->driver->listFiles($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-10-08 16:23:44 +02:00
public function testListFilesThrowsIfPathNotADirectory()
{
2019-08-23 20:23:33 +02:00
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->listFiles(__FILE__);
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testListFilesThrowsIfPathDoesntExist()
{
2019-08-23 20:23:33 +02:00
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/nonexistent";
2020-10-08 16:23:44 +02:00
$this->driver->listFiles($path);
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testCreateSymlink()
{
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";
2020-10-08 16:23:44 +02:00
$this->assertNull($this->driver->createSymlink($original, $link));
2019-08-23 20:59:26 +02:00
$this->assertTrue(\is_link($link));
2020-10-08 16:23:44 +02:00
$this->driver->deleteFile($link);
2015-07-11 03:59:39 +02:00
}
2020-10-08 16:23:44 +02:00
public function testCreateSymlinkFailWhenLinkExists()
{
$this->expectException(FilesystemException::class);
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$this->driver->createSymlink($path, $path);
}
2020-10-08 16:23:44 +02:00
public function testCreateHardlink()
{
$fixtureDir = Fixture::path();
$original = "{$fixtureDir}/file";
$link = "{$fixtureDir}/hardlink.txt";
2020-10-08 16:23:44 +02:00
$this->assertNull($this->driver->createHardlink($original, $link));
$this->assertFileExists($link);
$this->assertFalse(\is_link($link));
2020-10-08 16:23:44 +02:00
$this->driver->deleteFile($link);
}
2020-10-08 16:23:44 +02:00
public function testCreateHardlinkFailWhenLinkExists()
{
$this->expectException(FilesystemException::class);
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$this->driver->createHardlink($path, $path);
}
2020-10-08 16:23:44 +02:00
public function testResolveSymlink()
{
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);
2020-10-08 16:23:44 +02:00
$this->assertSame($original, $this->driver->resolveSymlink($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-30 21:45:09 +02:00
*
2020-06-24 22:00:24 +02:00
* @return \Generator
*/
2020-10-08 16:23:44 +02:00
public function testResolveSymlinkError(\Closure $linkResolver)
{
2019-08-23 20:23:33 +02:00
$this->expectException(FilesystemException::class);
2019-08-23 20:59:26 +02:00
$link = $linkResolver();
2020-10-08 16:23:44 +02:00
$this->driver->resolveSymlink($link);
}
2020-10-08 16:23:44 +02:00
public function testLinkStatus()
{
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";
2020-10-08 16:23:44 +02:00
$this->driver->createSymlink($target, $link);
$this->assertIsArray($this->driver->getLinkStatus($link));
$this->driver->deleteFile($link);
2015-07-11 03:59:39 +02:00
}
2020-10-08 16:23:44 +02:00
public function testStatus()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$stat = $this->driver->getStatus($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-10-08 16:23:44 +02:00
public function testDirectoryStatus()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2020-10-08 16:23:44 +02:00
$stat = $this->driver->getStatus("{$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-10-08 16:23:44 +02:00
public function testNonexistentPathStatusResolvesToNull()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2020-10-08 16:23:44 +02:00
$stat = $this->driver->getStatus("{$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
2020-10-08 16:23:44 +02:00
public function testExists()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2020-10-08 16:23:44 +02:00
$this->assertFalse($this->driver->exists("{$fixtureDir}/nonexistent"));
$this->assertTrue($this->driver->exists("{$fixtureDir}/file"));
2015-07-19 15:24:52 +02:00
}
2020-10-08 16:23:44 +02:00
public function testRead()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
2020-10-08 16:23:44 +02:00
$this->assertSame("small", $this->driver->read("{$fixtureDir}/file"));
2017-06-17 15:36:51 +02:00
}
2020-10-08 16:23:44 +02:00
public function testSize()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$stat = ($this->driver->getStatus($path));
2019-08-23 20:59:26 +02:00
$size = $stat["size"];
2020-10-08 16:23:44 +02:00
$this->assertSame($size, $this->driver->getSize($path));
2015-08-08 16:09:07 +02:00
}
2015-07-11 03:59:39 +02:00
2020-10-08 16:23:44 +02:00
public function testSizeFailsOnNonexistentPath()
{
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";
2020-10-08 16:23:44 +02:00
$this->driver->getSize($path);
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testSizeFailsOnDirectoryPath()
{
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";
2020-10-08 16:23:44 +02:00
$this->assertTrue($this->driver->isDirectory($path));
$this->driver->getSize($path);
2015-08-08 16:09:07 +02:00
}
/**
2020-06-24 22:00:24 +02:00
* @dataProvider dataForDirectoryCheck
*/
2020-10-08 16:23:44 +02:00
public function testIsDirectory(bool $expectedResult, string $name)
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/{$name}";
2020-10-08 16:23:44 +02:00
$this->assertSame($expectedResult, $this->driver->isDirectory($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-10-08 16:23:44 +02:00
public function testIsFile(bool $expectedResult, string $name)
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/{$name}";
2020-10-08 16:23:44 +02:00
$this->assertSame($expectedResult, $this->driver->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
*/
2020-10-08 16:23:44 +02:00
public function testIsSymlink(bool $expectedResult, string $name)
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/{$name}";
2020-10-08 16:23:44 +02:00
$this->assertSame($expectedResult, $this->driver->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-10-08 16:23:44 +02:00
public function testMove()
{
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
2020-10-08 16:23:44 +02:00
$this->driver->write($old, $contents1);
$this->assertNull($this->driver->move($old, $new));
$contents2 = $this->driver->read($new);
$this->driver->deleteFile($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-10-08 16:23:44 +02:00
public function testMoveFailsOnNonexistentPath()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->move($path, $path);
}
2020-10-08 16:23:44 +02:00
public function testDeleteFile()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$toUnlink = "{$fixtureDir}/unlink";
2020-10-08 16:23:44 +02:00
$this->driver->getStatus($toUnlink);
$this->driver->write($toUnlink, "unlink me");
$this->assertNull($this->driver->deleteFile($toUnlink));
$this->assertNull($this->driver->getStatus($toUnlink));
2015-07-11 03:59:39 +02:00
}
2020-10-08 16:23:44 +02:00
public function testDeleteFileFailsOnNonexistentPath()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->deleteFile($path);
}
2020-10-08 16:23:44 +02:00
public function testDeleteFileFailsOnDirectory()
{
$fixtureDir = Fixture::path();
$dir = "{$fixtureDir}/newdir";
2020-10-08 16:23:44 +02:00
$this->driver->createDirectory($dir);
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->deleteFile($dir);
}
2020-10-08 16:23:44 +02:00
public function testCreateAndDeleteDirectory()
{
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
2020-10-08 16:23:44 +02:00
$this->assertNull($this->driver->createDirectory($dir));
$stat = $this->driver->getStatus($dir);
2020-06-24 22:00:24 +02:00
$this->assertSame('0755', $this->getPermissionsFromStatus($stat));
2020-10-08 16:23:44 +02:00
$this->assertNull($this->driver->deleteDirectory($dir));
$this->assertNull($this->driver->getStatus($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
2020-10-08 16:23:44 +02:00
$this->assertNull($this->driver->createDirectoryRecursively($dir, 0764));
$stat = $this->driver->getStatus($dir);
2020-06-24 22:00:24 +02:00
$this->assertSame('0744', $this->getPermissionsFromStatus($stat));
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testCreateDirectoryFailsOnNonexistentPath()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->createDirectory($path);
}
2020-10-08 16:23:44 +02:00
public function testDeleteDirectoryFailsOnNonexistentPath()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->deleteDirectory($path);
}
2020-10-08 16:23:44 +02:00
public function testDeleteDirectoryFailsOnFile()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->deleteDirectory($path);
}
2020-10-08 16:23:44 +02:00
public function testMtime()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$stat = $this->driver->getStatus($path);
2019-08-23 20:59:26 +02:00
$statMtime = $stat["mtime"];
2020-10-08 16:23:44 +02:00
$this->assertSame($statMtime, $this->driver->getModificationTime($path));
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testMtimeFailsOnNonexistentPath()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->getModificationTime($path);
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testAtime()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$stat = $this->driver->getStatus($path);
2019-08-23 20:59:26 +02:00
$statAtime = $stat["atime"];
2020-10-08 16:23:44 +02:00
$this->assertSame($statAtime, $this->driver->getAccessTime($path));
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testAtimeFailsOnNonexistentPath()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->getAccessTime($path);
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testCtime()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$stat = $this->driver->getStatus($path);
2019-08-23 20:59:26 +02:00
$statCtime = $stat["ctime"];
2020-10-08 16:23:44 +02:00
$this->assertSame($statCtime, $this->driver->getCreationTime($path));
2015-08-08 16:09:07 +02:00
}
2020-10-08 16:23:44 +02:00
public function testCtimeFailsOnNonexistentPath()
{
2019-08-23 20:59:26 +02:00
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->getCreationTime($path);
2015-07-11 03:59:39 +02:00
}
2015-07-18 20:53:46 +02:00
/**
* @group slow
*/
2020-10-08 16:23:44 +02:00
public function testTouch()
{
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";
2020-10-08 16:23:44 +02:00
$this->driver->write($touch, "touch me");
2015-07-18 20:53:46 +02:00
2020-10-08 16:23:44 +02:00
$oldStat = $this->driver->getStatus($touch);
$this->assertNull($this->driver->touch($touch, \time() + 10, \time() + 20));
$newStat = $this->driver->getStatus($touch);
$this->driver->deleteFile($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
2020-10-08 16:23:44 +02:00
public function testTouchFailsOnNonexistentPath()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->touch($path);
}
2020-10-08 16:23:44 +02:00
public function testChangePermissions()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$stat = $this->driver->getStatus($path);
$this->assertNotSame('0777', \substr(\decoct($stat['mode']), -4));
2020-10-08 16:23:44 +02:00
$this->assertNull($this->driver->changePermissions($path, 0777));
$stat = $this->driver->getStatus($path);
$this->assertSame('0777', \substr(\decoct($stat['mode']), -4));
}
2020-10-08 16:23:44 +02:00
public function testChangePermissionsFailsOnNonexistentPath()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->changePermissions($path, 0777);
}
2020-10-08 16:23:44 +02:00
public function testChangeOwner()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/file";
2020-10-08 16:23:44 +02:00
$this->driver->getStatus($path);
$user = \fileowner($path);
2020-10-08 16:23:44 +02:00
$this->assertNull($this->driver->changeOwner($path, $user, null));
}
2020-10-08 16:23:44 +02:00
public function testChangeOwnerFailsOnNonexistentPath()
{
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->expectException(FilesystemException::class);
2020-10-08 16:23:44 +02:00
$this->driver->changeOwner($path, 0, null);
}
2020-06-24 22:00:24 +02:00
2020-06-30 21:45:09 +02:00
protected function setUp(): void
{
parent::setUp();
$this->driver = new File\Filesystem($this->createDriver());
}
abstract protected function createDriver(): File\Driver;
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
}