Rewrite readFile and writeFile (#2000)

Using open/read/write
This commit is contained in:
Bartek Iwańczuk 2019-03-28 04:29:36 +01:00 committed by Ryan Dahl
parent d0b6152f11
commit 597ee38ef2
10 changed files with 337 additions and 190 deletions

View file

@ -163,6 +163,19 @@ testPerm({ read: true }, async function seekStart() {
assertEquals(decoded, "world!");
});
testPerm({ read: true }, function seekSyncStart() {
const filename = "tests/hello.txt";
const file = Deno.openSync(filename);
// Deliberately move 1 step forward
file.readSync(new Uint8Array(1)); // "H"
// Skipping "Hello "
file.seekSync(6, Deno.SeekMode.SEEK_START);
const buf = new Uint8Array(6);
file.readSync(buf);
const decoded = new TextDecoder().decode(buf);
assertEquals(decoded, "world!");
});
testPerm({ read: true }, async function seekCurrent() {
const filename = "tests/hello.txt";
const file = await Deno.open(filename);
@ -176,6 +189,19 @@ testPerm({ read: true }, async function seekCurrent() {
assertEquals(decoded, "world!");
});
testPerm({ read: true }, function seekSyncCurrent() {
const filename = "tests/hello.txt";
const file = Deno.openSync(filename);
// Deliberately move 1 step forward
file.readSync(new Uint8Array(1)); // "H"
// Skipping "ello "
file.seekSync(5, Deno.SeekMode.SEEK_CURRENT);
const buf = new Uint8Array(6);
file.readSync(buf);
const decoded = new TextDecoder().decode(buf);
assertEquals(decoded, "world!");
});
testPerm({ read: true }, async function seekEnd() {
const filename = "tests/hello.txt";
const file = await Deno.open(filename);
@ -186,6 +212,16 @@ testPerm({ read: true }, async function seekEnd() {
assertEquals(decoded, "world!");
});
testPerm({ read: true }, function seekSyncEnd() {
const filename = "tests/hello.txt";
const file = Deno.openSync(filename);
file.seekSync(-6, Deno.SeekMode.SEEK_END);
const buf = new Uint8Array(6);
file.readSync(buf);
const decoded = new TextDecoder().decode(buf);
assertEquals(decoded, "world!");
});
testPerm({ read: true }, async function seekMode() {
const filename = "tests/hello.txt";
const file = await Deno.open(filename);