1
0
mirror of https://github.com/danog/file.git synced 2024-11-30 04:19:39 +01:00

Use Fixture in Driver test; general test cleanup

This commit is contained in:
Aaron Piotrowski 2017-06-16 18:12:42 -05:00
parent 7e864b9578
commit 477683f58f
10 changed files with 160 additions and 211 deletions

View File

@ -3,7 +3,7 @@
namespace Amp\File\Test;
class BlockingDriverTest extends DriverTest {
protected function lRun(callable $cb) {
protected function execute(callable $cb) {
\Amp\Loop::run(function() use ($cb) {
\Amp\File\filesystem(new \Amp\File\BlockingDriver);
\Amp\Promise\rethrow(new \Amp\Coroutine($cb()));

View File

@ -3,7 +3,7 @@
namespace Amp\File\Test;
class BlockingHandleTest extends HandleTest {
protected function lRun(callable $cb) {
protected function execute(callable $cb) {
\Amp\Loop::run(function() use ($cb) {
\Amp\File\filesystem(new \Amp\File\BlockingDriver);
\Amp\Promise\rethrow(new \Amp\Coroutine($cb()));

View File

@ -6,68 +6,21 @@ use Amp\File as file;
use Amp\PHPUnit\TestCase;
abstract class DriverTest extends TestCase {
private static $fixtureId;
private static $umask;
private static function getFixturePath() {
if (empty(self::$fixtureId)) {
self::$fixtureId = \uniqid();
}
return \sys_get_temp_dir() . "/amphp_file_fixture/" . __CLASS__ . self::$fixtureId;
}
private static function clearFixtureDir() {
$fixtureDir = self::getFixturePath();
if (!file_exists($fixtureDir)) {
return;
}
if (\stripos(\PHP_OS, "win") === 0) {
\system('rd /Q /S "' . $fixtureDir . '"');
} else {
\system('/bin/rm -rf ' . \escapeshellarg($fixtureDir));
}
}
public static function setUpBeforeClass() {
$fixtureDir = self::getFixturePath();
self::clearFixtureDir();
self::$umask = umask(0022);
if (!\mkdir($fixtureDir, $mode = 0777, $recursive = true)) {
throw new \RuntimeException(
"Failed creating temporary test fixture directory: {$fixtureDir}"
);
}
if (!\mkdir($fixtureDir . "/dir", $mode = 0777, $recursive = true)) {
throw new \RuntimeException(
"Failed creating temporary test fixture directory"
);
}
if (!\file_put_contents($fixtureDir . "/small.txt", "small")) {
throw new \RuntimeException(
"Failed creating temporary test fixture file"
);
}
}
public static function tearDownAfterClass() {
self::clearFixtureDir();
umask(self::$umask);
}
protected function setUp() {
file\StatCache::clear();
Fixture::init();
File\StatCache::clear();
}
abstract protected function lRun(callable $cb);
protected function tearDown() {
Fixture::clear();
}
abstract protected function execute(callable $cb);
public function testScandir() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$actual = (yield file\scandir($fixtureDir));
$this->execute(function () {
$fixtureDir = Fixture::path();
$actual = (yield File\scandir($fixtureDir));
$expected = ["dir", "small.txt"];
$this->assertSame($expected, $actual);
});
@ -77,8 +30,8 @@ abstract class DriverTest extends TestCase {
* @expectedException \Amp\File\FilesystemException
*/
public function testScandirThrowsIfPathNotADirectory() {
$this->lRun(function () {
(yield file\scandir(__FILE__));
$this->execute(function () {
(yield File\scandir(__FILE__));
});
}
@ -86,76 +39,76 @@ abstract class DriverTest extends TestCase {
* @expectedException \Amp\File\FilesystemException
*/
public function testScandirThrowsIfPathDoesntExist() {
$this->lRun(function () {
$path = self::getFixturePath() . "/nonexistent";
(yield file\scandir($path));
$this->execute(function () {
$path = Fixture::path() . "/nonexistent";
(yield File\scandir($path));
});
}
public function testSymlink() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$original = "{$fixtureDir}/small.txt";
$link = "{$fixtureDir}/symlink.txt";
$this->assertTrue(yield file\symlink($original, $link));
$this->assertTrue(yield File\symlink($original, $link));
$this->assertTrue(\is_link($link));
yield file\unlink($link);
yield File\unlink($link);
});
}
public function testLstat() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$target = "{$fixtureDir}/small.txt";
$link = "{$fixtureDir}/symlink.txt";
$this->assertTrue(yield file\symlink($target, $link));
$this->assertTrue(is_array(yield file\lstat($link)));
yield file\unlink($link);
$this->assertTrue(yield File\symlink($target, $link));
$this->assertTrue(is_array(yield File\lstat($link)));
yield File\unlink($link);
});
}
public function testFileStat() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$stat = (yield file\stat("{$fixtureDir}/small.txt"));
$this->execute(function () {
$fixtureDir = Fixture::path();
$stat = (yield File\stat("{$fixtureDir}/small.txt"));
$this->assertInternalType("array", $stat);
});
}
public function testDirStat() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$stat = (yield file\stat("{$fixtureDir}/dir"));
$this->execute(function () {
$fixtureDir = Fixture::path();
$stat = (yield File\stat("{$fixtureDir}/dir"));
$this->assertInternalType("array", $stat);
});
}
public function testNonexistentPathStatResolvesToNull() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$stat = (yield file\stat("{$fixtureDir}/nonexistent"));
$this->execute(function () {
$fixtureDir = Fixture::path();
$stat = (yield File\stat("{$fixtureDir}/nonexistent"));
$this->assertNull($stat);
});
}
public function testExists() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->assertFalse(yield file\exists("{$fixtureDir}/nonexistent"));
$this->assertTrue(yield file\exists("{$fixtureDir}/small.txt"));
$this->execute(function () {
$fixtureDir = Fixture::path();
$this->assertFalse(yield File\exists("{$fixtureDir}/nonexistent"));
$this->assertTrue(yield File\exists("{$fixtureDir}/small.txt"));
});
}
public function testSize() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/small.txt";
$stat = (yield file\stat($path));
$stat = (yield File\stat($path));
$size = $stat["size"];
file\StatCache::clear($path);
$this->assertSame($size, (yield file\size($path)));
File\StatCache::clear($path);
$this->assertSame($size, (yield File\size($path)));
});
}
@ -163,10 +116,10 @@ abstract class DriverTest extends TestCase {
* @expectedException \Amp\File\FilesystemException
*/
public function testSizeFailsOnNonexistentPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
yield file\size($path);
yield File\size($path);
});
}
@ -174,118 +127,118 @@ abstract class DriverTest extends TestCase {
* @expectedException \Amp\File\FilesystemException
*/
public function testSizeFailsOnDirectoryPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/dir";
$this->assertTrue(yield file\isdir($path));
file\StatCache::clear($path);
yield file\size($path);
$this->assertTrue(yield File\isdir($path));
File\StatCache::clear($path);
yield File\size($path);
});
}
public function testIsdirResolvesTrueOnDirectoryPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/dir";
$this->assertTrue(yield file\isdir($path));
$this->assertTrue(yield File\isdir($path));
});
}
public function testIsdirResolvesFalseOnFilePath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/small.txt";
$this->assertFalse(yield file\isdir($path));
$this->assertFalse(yield File\isdir($path));
});
}
public function testIsdirResolvesFalseOnNonexistentPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->assertFalse(yield file\isdir($path));
$this->assertFalse(yield File\isdir($path));
});
}
public function testIsfileResolvesTrueOnFilePath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/small.txt";
$this->assertTrue(yield file\isfile($path));
$this->assertTrue(yield File\isfile($path));
});
}
public function testIsfileResolvesFalseOnDirectoryPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/dir";
$this->assertFalse(yield file\isfile($path));
$this->assertFalse(yield File\isfile($path));
});
}
public function testIsfileResolvesFalseOnNonexistentPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
$this->assertFalse(yield file\isfile($path));
$this->assertFalse(yield File\isfile($path));
});
}
public function testRename() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$contents1 = "rename test";
$old = "{$fixtureDir}/rename1.txt";
$new = "{$fixtureDir}/rename2.txt";
yield file\put($old, $contents1);
yield file\rename($old, $new);
$contents2 = (yield file\get($new));
yield file\unlink($new);
yield File\put($old, $contents1);
yield File\rename($old, $new);
$contents2 = (yield File\get($new));
yield File\unlink($new);
$this->assertSame($contents1, $contents2);
});
}
public function testUnlink() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$toUnlink = "{$fixtureDir}/unlink";
yield file\put($toUnlink, "unlink me");
yield file\unlink($toUnlink);
$this->assertNull(yield file\stat($toUnlink));
yield File\put($toUnlink, "unlink me");
yield File\unlink($toUnlink);
$this->assertNull(yield File\stat($toUnlink));
});
}
public function testMkdirRmdir() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$dir = "{$fixtureDir}/newdir";
yield file\mkdir($dir);
$stat = yield file\stat($dir);
yield File\mkdir($dir);
$stat = yield File\stat($dir);
$this->assertTrue(($stat["mode"] & 0777) === 0644);
yield file\rmdir($dir);
$this->assertNull(yield file\stat($dir));
yield File\rmdir($dir);
$this->assertNull(yield File\stat($dir));
$dir = "{$fixtureDir}/newdir/with/recursive/creation";
yield file\mkdir($dir, 0764, true); // the umask is 022 by default
$stat = yield file\stat($dir);
yield File\mkdir($dir, 0764, true); // the umask is 022 by default
$stat = yield File\stat($dir);
$this->assertTrue(($stat["mode"] & 0777) == 0744);
});
}
public function testMtime() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/small.txt";
$stat = (yield file\stat($path));
$stat = (yield File\stat($path));
$statMtime = $stat["mtime"];
file\StatCache::clear($path);
$this->assertSame($statMtime, (yield file\mtime($path)));
File\StatCache::clear($path);
$this->assertSame($statMtime, (yield File\mtime($path)));
});
}
@ -293,21 +246,21 @@ abstract class DriverTest extends TestCase {
* @expectedException \Amp\File\FilesystemException
*/
public function testMtimeFailsOnNonexistentPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
yield file\mtime($path);
yield File\mtime($path);
});
}
public function testAtime() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/small.txt";
$stat = (yield file\stat($path));
$stat = (yield File\stat($path));
$statAtime = $stat["atime"];
file\StatCache::clear($path);
$this->assertSame($statAtime, (yield file\atime($path)));
File\StatCache::clear($path);
$this->assertSame($statAtime, (yield File\atime($path)));
});
}
@ -315,21 +268,21 @@ abstract class DriverTest extends TestCase {
* @expectedException \Amp\File\FilesystemException
*/
public function testAtimeFailsOnNonexistentPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
yield file\atime($path);
yield File\atime($path);
});
}
public function testCtime() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/small.txt";
$stat = (yield file\stat($path));
$stat = (yield File\stat($path));
$statCtime = $stat["ctime"];
file\StatCache::clear($path);
$this->assertSame($statCtime, (yield file\ctime($path)));
File\StatCache::clear($path);
$this->assertSame($statCtime, (yield File\ctime($path)));
});
}
@ -337,10 +290,10 @@ abstract class DriverTest extends TestCase {
* @expectedException \Amp\File\FilesystemException
*/
public function testCtimeFailsOnNonexistentPath() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/nonexistent";
yield file\ctime($path);
yield File\ctime($path);
});
}
@ -348,18 +301,18 @@ abstract class DriverTest extends TestCase {
* @group slow
*/
public function testTouch() {
$this->lRun(function () {
$fixtureDir = self::getFixturePath();
$this->execute(function () {
$fixtureDir = Fixture::path();
$touch = "{$fixtureDir}/touch";
yield file\put($touch, "touch me");
yield File\put($touch, "touch me");
$oldStat = (yield file\stat($touch));
$oldStat = (yield File\stat($touch));
sleep(1);
yield file\touch($touch);
file\StatCache::clear($touch);
$newStat = (yield file\stat($touch));
yield file\unlink($touch);
yield File\touch($touch);
File\StatCache::clear($touch);
$newStat = (yield File\stat($touch));
yield File\unlink($touch);
$this->assertTrue($newStat["atime"] > $oldStat["atime"]);
$this->assertTrue($newStat["mtime"] > $oldStat["mtime"]);

View File

@ -3,7 +3,7 @@
namespace Amp\File\Test;
class EioDriverTest extends DriverTest {
protected function lRun(callable $cb) {
protected function execute(callable $cb) {
if (\extension_loaded("eio")) {
\Amp\Loop::run(function() use ($cb) {
\Amp\File\filesystem(new \Amp\File\EioDriver);

View File

@ -5,7 +5,7 @@ namespace Amp\File\Test;
use Amp\File as file;
class EioHandleTest extends HandleTest {
protected function lRun(callable $cb) {
protected function execute(callable $cb) {
if (\extension_loaded("eio")) {
\Amp\Loop::run(function() use ($cb) {
\Amp\File\filesystem(new \Amp\File\EioDriver);
@ -19,7 +19,7 @@ class EioHandleTest extends HandleTest {
}
public function testQueuedWritesOverrideEachOtherIfNotWaitedUpon() {
$this->lRun(function () {
$this->execute(function () {
$path = Fixture::path() . "/write";
$handle = (yield file\open($path, "c+"));
$this->assertSame(0, $handle->tell());

View File

@ -2,28 +2,25 @@
namespace Amp\File\Test;
use Amp\File as file;
use Amp\File;
use Amp\PHPUnit\TestCase;
abstract class HandleTest extends TestCase {
public static function setUpBeforeClass() {
protected function setUp() {
Fixture::init();
File\StatCache::clear();
}
public static function tearDownAfterClass() {
protected function tearDown() {
Fixture::clear();
}
protected function setUp() {
file\StatCache::clear();
}
abstract protected function lRun(callable $cb);
abstract protected function execute(callable $cb);
public function testWrite() {
$this->lRun(function () {
$this->execute(function () {
$path = Fixture::path() . "/write";
$handle = (yield file\open($path, "c+"));
$handle = yield File\open($path, "c+");
$this->assertSame(0, $handle->tell());
yield $handle->write("foo");
@ -35,35 +32,34 @@ abstract class HandleTest extends TestCase {
$this->assertSame("foobar", $contents);
yield $handle->close();
yield file\unlink($path);
});
}
public function testReadingToEof() {
$this->lRun(function () {
$handle = (yield file\open(__FILE__, "r"));
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
$contents = "";
$position = 0;
$stat = (yield file\stat(__FILE__));
$stat = yield File\stat(__FILE__);
$chunkSize = (int) \floor(($stat["size"] / 5));
while (!$handle->eof()) {
$chunk = (yield $handle->read($chunkSize));
$chunk = yield $handle->read($chunkSize);
$contents .= $chunk;
$position += \strlen($chunk);
$this->assertSame($position, $handle->tell());
}
$this->assertSame((yield file\get(__FILE__)), $contents);
$this->assertSame((yield File\get(__FILE__)), $contents);
yield $handle->close();
});
}
public function testQueuedReads() {
$this->lRun(function () {
$handle = (yield file\open(__FILE__, "r"));
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
$contents = "";
$read1 = $handle->read(10);
@ -73,7 +69,7 @@ abstract class HandleTest extends TestCase {
$contents .= (yield $read1);
$contents .= (yield $read2);
$expected = \substr((yield file\get(__FILE__)), 0, 20);
$expected = \substr((yield File\get(__FILE__)), 0, 20);
$this->assertSame($expected, $contents);
yield $handle->close();
@ -81,14 +77,14 @@ abstract class HandleTest extends TestCase {
}
public function testReadingFromOffset() {
$this->lRun(function () {
$handle = (yield file\open(__FILE__, "r"));
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
$this->assertSame(0, $handle->tell());
yield $handle->seek(10);
$this->assertSame(10, $handle->tell());
$chunk = (yield $handle->read(90));
$this->assertSame(100, $handle->tell());
$expected = \substr((yield file\get(__FILE__)), 10, 90);
$expected = \substr((yield File\get(__FILE__)), 10, 90);
$this->assertSame($expected, $chunk);
yield $handle->close();
@ -99,9 +95,9 @@ abstract class HandleTest extends TestCase {
* @expectedException \Error
*/
public function testSeekThrowsOnInvalidWhence() {
$this->lRun(function () {
$this->execute(function () {
try {
$handle = (yield file\open(__FILE__, "r"));
$handle = yield File\open(__FILE__, "r");
yield $handle->seek(0, 99999);
} finally {
yield $handle->close();
@ -110,8 +106,8 @@ abstract class HandleTest extends TestCase {
}
public function testSeekSetCur() {
$this->lRun(function () {
$handle = (yield file\open(__FILE__, "r"));
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
$this->assertSame(0, $handle->tell());
yield $handle->seek(10);
$this->assertSame(10, $handle->tell());
@ -122,9 +118,9 @@ abstract class HandleTest extends TestCase {
}
public function testSeekSetEnd() {
$this->lRun(function () {
$size = (yield file\size(__FILE__));
$handle = (yield file\open(__FILE__, "r"));
$this->execute(function () {
$size = yield File\size(__FILE__);
$handle = yield File\open(__FILE__, "r");
$this->assertSame(0, $handle->tell());
yield $handle->seek(-10, \SEEK_END);
$this->assertSame($size - 10, $handle->tell());
@ -133,24 +129,24 @@ abstract class HandleTest extends TestCase {
}
public function testPath() {
$this->lRun(function () {
$handle = (yield file\open(__FILE__, "r"));
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
$this->assertSame(__FILE__, $handle->path());
yield $handle->close();
});
}
public function testMode() {
$this->lRun(function () {
$handle = (yield file\open(__FILE__, "r"));
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
$this->assertSame("r", $handle->mode());
yield $handle->close();
});
}
public function testClose() {
$this->lRun(function () {
$handle = (yield file\open(__FILE__, "r"));
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
yield $handle->close();
});
}

View File

@ -8,7 +8,7 @@ use Amp\Parallel\Worker\DefaultPool;
use function Amp\call;
class ParallelDriverTest extends DriverTest {
protected function lRun(callable $cb) {
protected function execute(callable $cb) {
Loop::run(function() use ($cb) {
$pool = new DefaultPool;
$pool->start();

View File

@ -8,7 +8,7 @@ use Amp\Parallel\Worker\DefaultPool;
use function Amp\call;
class ParallelHandleTest extends HandleTest {
protected function lRun(callable $cb) {
protected function execute(callable $cb) {
Loop::run(function() use ($cb) {
$pool = new DefaultPool;
$pool->start();

View File

@ -5,7 +5,7 @@ namespace Amp\File\Test;
use Amp\Loop;
class UvDriverTest extends DriverTest {
protected function lRun(callable $cb) {
protected function execute(callable $cb) {
if (\extension_loaded("uv")) {
$loop = new Loop\UvDriver;
Loop::set($loop);

View File

@ -6,7 +6,7 @@ use Amp\Loop;
use Amp\File as file;
class UvHandleTest extends HandleTest {
protected function lRun(callable $cb) {
protected function execute(callable $cb) {
if (\extension_loaded("uv")) {
$loop = new Loop\UvDriver;
Loop::set($loop);
@ -22,7 +22,7 @@ class UvHandleTest extends HandleTest {
}
public function testQueuedWritesOverrideEachOtherIfNotWaitedUpon() {
$this->lRun(function () {
$this->execute(function () {
$path = Fixture::path() . "/write";
$handle = (yield file\open($path, "c+"));
$this->assertSame(0, $handle->tell());