File tree Expand file tree Collapse file tree 4 files changed +58
-4
lines changed Expand file tree Collapse file tree 4 files changed +58
-4
lines changed Original file line number Diff line number Diff line change @@ -346,7 +346,7 @@ public function read(string $path): string
346
346
347
347
public function write (string $ path , string $ contents ): void
348
348
{
349
- $ flags = \EIO_O_RDWR | \EIO_O_CREAT ;
349
+ $ flags = \EIO_O_RDWR | \EIO_O_CREAT | \ EIO_O_TRUNC ;
350
350
$ mode = \EIO_S_IRUSR | \EIO_S_IWUSR | \EIO_S_IXUSR ;
351
351
$ priority = \EIO_PRI_DEFAULT ;
352
352
Original file line number Diff line number Diff line change @@ -418,7 +418,7 @@ public function read(string $path): string
418
418
419
419
public function write (string $ path , string $ contents ): void
420
420
{
421
- $ flags = \UV ::O_WRONLY | \UV ::O_CREAT ;
421
+ $ flags = \UV ::O_WRONLY | \UV ::O_CREAT | \ UV :: O_TRUNC ;
422
422
$ mode = \UV ::S_IRWXU | \UV ::S_IRUSR ;
423
423
424
424
$ this ->poll ->listen ();
@@ -454,8 +454,10 @@ private function parseMode(string $mode): int
454
454
return match ($ mode ) {
455
455
"r " => \UV ::O_RDONLY ,
456
456
"r+ " => \UV ::O_RDWR ,
457
- "c " , "w " => \UV ::O_WRONLY | \UV ::O_CREAT ,
458
- "c+ " , "w+ " => \UV ::O_RDWR | \UV ::O_CREAT ,
457
+ "c " => \UV ::O_WRONLY | \UV ::O_CREAT ,
458
+ "w " => \UV ::O_WRONLY | \UV ::O_CREAT | \UV ::O_TRUNC ,
459
+ "c+ " => \UV ::O_RDWR | \UV ::O_CREAT ,
460
+ "w+ " => \UV ::O_RDWR | \UV ::O_CREAT | \UV ::O_TRUNC ,
459
461
"a " => \UV ::O_WRONLY | \UV ::O_CREAT | \UV ::O_APPEND ,
460
462
"a+ " => \UV ::O_RDWR | \UV ::O_CREAT | \UV ::O_APPEND ,
461
463
"x " => \UV ::O_WRONLY | \UV ::O_CREAT | \UV ::O_EXCL ,
Original file line number Diff line number Diff line change 4
4
5
5
use Amp \ByteStream \ClosedException ;
6
6
use Amp \File ;
7
+ use Amp \File \Whence ;
8
+
7
9
use function Amp \async ;
8
10
9
11
abstract class FileTest extends FilesystemTest
@@ -25,6 +27,35 @@ public function testWrite(): void
25
27
$ this ->assertSame ("foobar " , $ contents );
26
28
27
29
$ handle ->close ();
30
+
31
+ $ handle = $ this ->driver ->openFile ($ path , "c+ " );
32
+ $ handle ->seek (0 , Whence::End);
33
+ $ this ->assertSame (6 , $ handle ->tell ());
34
+
35
+ $ handle ->close ();
36
+ }
37
+
38
+ public function testWriteTruncate (): void
39
+ {
40
+ $ path = Fixture::path () . "/write " ;
41
+ $ handle = $ this ->driver ->openFile ($ path , "c+ " );
42
+ $ this ->assertSame (0 , $ handle ->tell ());
43
+
44
+ $ handle ->write ("foo " );
45
+ $ handle ->write ("bar " );
46
+ $ handle ->seek (0 );
47
+ $ contents = $ handle ->read ();
48
+ $ this ->assertSame (6 , $ handle ->tell ());
49
+ $ this ->assertTrue ($ handle ->eof ());
50
+ $ this ->assertSame ("foobar " , $ contents );
51
+
52
+ $ handle ->close ();
53
+
54
+ $ handle = $ this ->driver ->openFile ($ path , "w+ " );
55
+ $ handle ->seek (0 , Whence::End);
56
+ $ this ->assertSame (0 , $ handle ->tell ());
57
+
58
+ $ handle ->close ();
28
59
}
29
60
30
61
public function testEmptyWrite (): void
Original file line number Diff line number Diff line change @@ -506,6 +506,27 @@ public function testTouch(): void
506
506
$ this ->assertTrue ($ newStat ["mtime " ] > $ oldStat ["mtime " ]);
507
507
}
508
508
509
+ /**
510
+ * @group slow
511
+ */
512
+ public function testWrite (): void
513
+ {
514
+ $ fixtureDir = Fixture::path ();
515
+
516
+ $ contents1 = "write test longer " ;
517
+ $ contents2 = "write test " ;
518
+ $ path = "{$ fixtureDir }/write.txt " ;
519
+
520
+ $ this ->driver ->write ($ path , $ contents1 );
521
+ $ this ->assertSame ($ contents1 , $ this ->driver ->read ($ path ));
522
+
523
+ $ this ->driver ->write ($ path , $ contents2 );
524
+ $ this ->assertSame ($ contents2 , $ this ->driver ->read ($ path ));
525
+
526
+ $ this ->driver ->write ($ path , $ contents1 );
527
+ $ this ->assertSame ($ contents1 , $ this ->driver ->read ($ path ));
528
+ }
529
+
509
530
public function testTouchFailsOnNonexistentPath (): void
510
531
{
511
532
$ fixtureDir = Fixture::path ();
You can’t perform that action at this time.
0 commit comments