2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-21 09:49:42 +00:00
|
|
|
import {
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest,
|
2020-01-21 09:49:42 +00:00
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-06-06 03:43:00 +00:00
|
|
|
assertStringContains,
|
2020-01-21 09:49:42 +00:00
|
|
|
} from "./test_util.ts";
|
2018-09-27 04:56:39 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(function filesStdioFileDescriptors(): void {
|
2019-03-07 01:48:46 +00:00
|
|
|
assertEquals(Deno.stdin.rid, 0);
|
|
|
|
assertEquals(Deno.stdout.rid, 1);
|
|
|
|
assertEquals(Deno.stderr.rid, 2);
|
2018-09-27 04:56:39 +00:00
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: true } }, async function filesCopyToStdout(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-11-01 02:33:27 +00:00
|
|
|
const filename = "cli/tests/fixture.json";
|
2019-02-12 15:08:56 +00:00
|
|
|
const file = await Deno.open(filename);
|
2018-10-10 15:59:36 +00:00
|
|
|
assert(file.rid > 2);
|
2020-04-24 22:09:14 +00:00
|
|
|
const bytesWritten = await Deno.copy(file, Deno.stdout);
|
2020-03-15 02:57:42 +00:00
|
|
|
const fileSize = Deno.statSync(filename).size;
|
2019-03-07 01:48:46 +00:00
|
|
|
assertEquals(bytesWritten, fileSize);
|
2018-09-27 04:56:39 +00:00
|
|
|
console.log("bytes written", bytesWritten);
|
2020-02-29 17:45:47 +00:00
|
|
|
file.close();
|
2018-09-27 04:56:39 +00:00
|
|
|
});
|
2018-10-31 14:29:13 +00:00
|
|
|
|
2020-04-22 19:30:45 +00:00
|
|
|
unitTest({ perms: { read: true } }, async function filesIter(): Promise<void> {
|
|
|
|
const filename = "cli/tests/hello.txt";
|
|
|
|
const file = await Deno.open(filename);
|
|
|
|
|
|
|
|
let totalSize = 0;
|
|
|
|
for await (const buf of Deno.iter(file)) {
|
|
|
|
totalSize += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
file.close();
|
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
2020-04-22 19:30:45 +00:00
|
|
|
async function filesIterCustomBufSize(): Promise<void> {
|
2020-03-04 16:31:14 +00:00
|
|
|
const filename = "cli/tests/hello.txt";
|
|
|
|
const file = await Deno.open(filename);
|
2018-10-31 14:29:13 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
let totalSize = 0;
|
2020-04-22 19:30:45 +00:00
|
|
|
let iterations = 0;
|
2020-04-24 22:05:48 +00:00
|
|
|
for await (const buf of Deno.iter(file, { bufSize: 6 })) {
|
2020-03-04 16:31:14 +00:00
|
|
|
totalSize += buf.byteLength;
|
2020-04-22 19:30:45 +00:00
|
|
|
iterations += 1;
|
2020-03-04 16:31:14 +00:00
|
|
|
}
|
2018-10-31 14:29:13 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
assertEquals(totalSize, 12);
|
2020-04-22 19:30:45 +00:00
|
|
|
assertEquals(iterations, 2);
|
2020-03-04 16:31:14 +00:00
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
);
|
2018-12-12 17:05:58 +00:00
|
|
|
|
2020-04-22 19:30:45 +00:00
|
|
|
unitTest({ perms: { read: true } }, function filesIterSync(): void {
|
|
|
|
const filename = "cli/tests/hello.txt";
|
|
|
|
const file = Deno.openSync(filename);
|
|
|
|
|
|
|
|
let totalSize = 0;
|
|
|
|
for (const buf of Deno.iterSync(file)) {
|
|
|
|
totalSize += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
file.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
|
|
|
function filesIterSyncCustomBufSize(): void {
|
|
|
|
const filename = "cli/tests/hello.txt";
|
|
|
|
const file = Deno.openSync(filename);
|
|
|
|
|
|
|
|
let totalSize = 0;
|
|
|
|
let iterations = 0;
|
2020-04-24 22:05:48 +00:00
|
|
|
for (const buf of Deno.iterSync(file, { bufSize: 6 })) {
|
2020-04-22 19:30:45 +00:00
|
|
|
totalSize += buf.byteLength;
|
|
|
|
iterations += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
assertEquals(iterations, 2);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(async function readerIter(): Promise<void> {
|
2019-05-11 14:05:56 +00:00
|
|
|
// ref: https://github.com/denoland/deno/issues/2330
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
class TestReader implements Deno.Reader {
|
2020-03-28 17:03:49 +00:00
|
|
|
#offset = 0;
|
|
|
|
#buf: Uint8Array;
|
2019-05-11 14:05:56 +00:00
|
|
|
|
2020-03-28 17:03:49 +00:00
|
|
|
constructor(s: string) {
|
|
|
|
this.#buf = new Uint8Array(encoder.encode(s));
|
|
|
|
}
|
2019-05-11 14:05:56 +00:00
|
|
|
|
2020-04-28 16:40:43 +00:00
|
|
|
read(p: Uint8Array): Promise<number | null> {
|
2020-03-28 17:03:49 +00:00
|
|
|
const n = Math.min(p.byteLength, this.#buf.byteLength - this.#offset);
|
|
|
|
p.set(this.#buf.slice(this.#offset, this.#offset + n));
|
|
|
|
this.#offset += n;
|
2019-05-11 14:05:56 +00:00
|
|
|
|
2019-07-06 14:16:03 +00:00
|
|
|
if (n === 0) {
|
2020-04-28 16:40:43 +00:00
|
|
|
return Promise.resolve(null);
|
2019-07-06 14:16:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 13:38:34 +00:00
|
|
|
return Promise.resolve(n);
|
2019-05-11 14:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const reader = new TestReader("hello world!");
|
|
|
|
|
|
|
|
let totalSize = 0;
|
2020-04-22 19:30:45 +00:00
|
|
|
for await (const buf of Deno.iter(reader)) {
|
|
|
|
totalSize += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function readerIterSync(): Promise<void> {
|
|
|
|
// ref: https://github.com/denoland/deno/issues/2330
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
2020-04-28 11:23:30 +00:00
|
|
|
class TestReader implements Deno.ReaderSync {
|
2020-04-22 19:30:45 +00:00
|
|
|
#offset = 0;
|
|
|
|
#buf: Uint8Array;
|
|
|
|
|
|
|
|
constructor(s: string) {
|
|
|
|
this.#buf = new Uint8Array(encoder.encode(s));
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:40:43 +00:00
|
|
|
readSync(p: Uint8Array): number | null {
|
2020-04-22 19:30:45 +00:00
|
|
|
const n = Math.min(p.byteLength, this.#buf.byteLength - this.#offset);
|
|
|
|
p.set(this.#buf.slice(this.#offset, this.#offset + n));
|
|
|
|
this.#offset += n;
|
|
|
|
|
|
|
|
if (n === 0) {
|
2020-04-28 16:40:43 +00:00
|
|
|
return null;
|
2020-04-22 19:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const reader = new TestReader("hello world!");
|
|
|
|
|
|
|
|
let totalSize = 0;
|
|
|
|
for await (const buf of Deno.iterSync(reader)) {
|
2019-05-11 14:05:56 +00:00
|
|
|
totalSize += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
});
|
|
|
|
|
2020-03-16 19:02:41 +00:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 17:03:49 +00:00
|
|
|
perms: { read: true, write: true },
|
2020-03-16 19:02:41 +00:00
|
|
|
},
|
|
|
|
function openSyncMode(): void {
|
|
|
|
const path = Deno.makeTempDirSync() + "/test_openSync.txt";
|
|
|
|
const file = Deno.openSync(path, {
|
|
|
|
write: true,
|
|
|
|
createNew: true,
|
2020-03-28 17:03:49 +00:00
|
|
|
mode: 0o626,
|
2020-03-16 19:02:41 +00:00
|
|
|
});
|
|
|
|
file.close();
|
|
|
|
const pathInfo = Deno.statSync(path);
|
2020-04-28 16:35:23 +00:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-03-16 19:02:41 +00:00
|
|
|
assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 17:03:49 +00:00
|
|
|
perms: { read: true, write: true },
|
2020-03-16 19:02:41 +00:00
|
|
|
},
|
|
|
|
async function openMode(): Promise<void> {
|
|
|
|
const path = (await Deno.makeTempDir()) + "/test_open.txt";
|
|
|
|
const file = await Deno.open(path, {
|
|
|
|
write: true,
|
|
|
|
createNew: true,
|
2020-03-28 17:03:49 +00:00
|
|
|
mode: 0o626,
|
2020-03-16 19:02:41 +00:00
|
|
|
});
|
|
|
|
file.close();
|
|
|
|
const pathInfo = Deno.statSync(path);
|
2020-04-28 16:35:23 +00:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-03-16 19:02:41 +00:00
|
|
|
assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: false } },
|
|
|
|
async function writePermFailure(): Promise<void> {
|
|
|
|
const filename = "tests/hello.txt";
|
2020-04-24 22:45:55 +00:00
|
|
|
const openOptions: Deno.OpenOptions[] = [{ write: true }, { append: true }];
|
|
|
|
for (const options of openOptions) {
|
2020-03-04 16:31:14 +00:00
|
|
|
let err;
|
|
|
|
try {
|
2020-04-24 22:45:55 +00:00
|
|
|
await Deno.open(filename, options);
|
2020-03-04 16:31:14 +00:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-12-13 21:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 16:31:14 +00:00
|
|
|
);
|
2018-12-13 21:20:37 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(async function openOptions(): Promise<void> {
|
2020-01-21 09:49:42 +00:00
|
|
|
const filename = "cli/tests/fixture.json";
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await Deno.open(filename, { write: false });
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-06-06 03:43:00 +00:00
|
|
|
assertStringContains(
|
2020-01-21 09:49:42 +00:00
|
|
|
err.message,
|
|
|
|
"OpenOptions requires at least one option to be true"
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Deno.open(filename, { truncate: true, write: false });
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-06-06 03:43:00 +00:00
|
|
|
assertStringContains(
|
|
|
|
err.message,
|
|
|
|
"'truncate' option requires 'write' option"
|
|
|
|
);
|
2020-01-21 09:49:42 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await Deno.open(filename, { create: true, write: false });
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-06-06 03:43:00 +00:00
|
|
|
assertStringContains(
|
2020-01-21 09:49:42 +00:00
|
|
|
err.message,
|
|
|
|
"'create' or 'createNew' options require 'write' or 'append' option"
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Deno.open(filename, { createNew: true, append: false });
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-06-06 03:43:00 +00:00
|
|
|
assertStringContains(
|
2020-01-21 09:49:42 +00:00
|
|
|
err.message,
|
|
|
|
"'create' or 'createNew' options require 'write' or 'append' option"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: false } }, async function readPermFailure(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-02-08 20:59:38 +00:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2020-04-24 22:45:55 +00:00
|
|
|
await Deno.open("package.json", { read: true });
|
|
|
|
await Deno.open("cli/tests/fixture.json", { read: true });
|
2019-02-08 20:59:38 +00:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2020-02-24 20:48:35 +00:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-02-08 20:59:38 +00:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: true } },
|
|
|
|
async function writeNullBufferFailure(): Promise<void> {
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "hello.txt";
|
|
|
|
const w = {
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
2020-03-28 17:03:49 +00:00
|
|
|
create: true,
|
2020-03-04 16:31:14 +00:00
|
|
|
};
|
|
|
|
const file = await Deno.open(filename, w);
|
|
|
|
|
|
|
|
// writing null should throw an error
|
|
|
|
let err;
|
|
|
|
try {
|
2020-06-02 04:24:44 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
await file.write(null as any);
|
2020-03-04 16:31:14 +00:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// TODO: Check error kind when dispatch_minimal pipes errors properly
|
|
|
|
assert(!!err);
|
2019-05-15 18:50:54 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
file.close();
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
|
|
|
}
|
|
|
|
);
|
2019-05-15 18:50:54 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: true, read: true } },
|
2019-05-15 18:50:54 +00:00
|
|
|
async function readNullBufferFailure(): Promise<void> {
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "hello.txt";
|
2020-04-24 22:45:55 +00:00
|
|
|
const file = await Deno.open(filename, {
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
create: true,
|
|
|
|
});
|
2019-05-15 18:50:54 +00:00
|
|
|
|
2019-12-28 13:48:36 +00:00
|
|
|
// reading into an empty buffer should return 0 immediately
|
|
|
|
const bytesRead = await file.read(new Uint8Array(0));
|
|
|
|
assert(bytesRead === 0);
|
|
|
|
|
2019-05-15 18:50:54 +00:00
|
|
|
// reading file into null buffer should throw an error
|
|
|
|
let err;
|
|
|
|
try {
|
2020-06-02 04:24:44 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
await file.read(null as any);
|
2019-05-15 18:50:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// TODO: Check error kind when dispatch_minimal pipes errors properly
|
|
|
|
assert(!!err);
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: false, read: false } },
|
2019-04-21 20:40:10 +00:00
|
|
|
async function readWritePermFailure(): Promise<void> {
|
|
|
|
const filename = "tests/hello.txt";
|
2020-04-24 22:45:55 +00:00
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await Deno.open(filename, { read: true });
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
2019-02-08 20:59:38 +00:00
|
|
|
}
|
2020-04-24 22:45:55 +00:00
|
|
|
assert(!!err);
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2019-02-08 20:59:38 +00:00
|
|
|
}
|
2019-04-21 20:40:10 +00:00
|
|
|
);
|
2019-02-08 20:59:38 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function createFile(): Promise<void> {
|
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
const filename = tempDir + "/test.txt";
|
|
|
|
const f = await Deno.create(filename);
|
|
|
|
let fileInfo = Deno.statSync(filename);
|
2020-04-16 05:40:30 +00:00
|
|
|
assert(fileInfo.isFile);
|
2020-03-15 02:57:42 +00:00
|
|
|
assert(fileInfo.size === 0);
|
2020-03-04 16:31:14 +00:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
await f.write(data);
|
|
|
|
fileInfo = Deno.statSync(filename);
|
2020-03-15 02:57:42 +00:00
|
|
|
assert(fileInfo.size === 5);
|
2020-03-04 16:31:14 +00:00
|
|
|
f.close();
|
2018-12-12 17:05:58 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
// TODO: test different modes
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2018-12-12 17:05:58 +00:00
|
|
|
}
|
2020-03-04 16:31:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function openModeWrite(): Promise<void> {
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const filename = tempDir + "hello.txt";
|
|
|
|
const data = encoder.encode("Hello world!\n");
|
2020-04-24 22:45:55 +00:00
|
|
|
let file = await Deno.open(filename, {
|
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
});
|
2020-03-04 16:31:14 +00:00
|
|
|
// assert file was created
|
|
|
|
let fileInfo = Deno.statSync(filename);
|
2020-04-16 05:40:30 +00:00
|
|
|
assert(fileInfo.isFile);
|
2020-03-15 02:57:42 +00:00
|
|
|
assertEquals(fileInfo.size, 0);
|
2020-03-04 16:31:14 +00:00
|
|
|
// write some data
|
|
|
|
await file.write(data);
|
|
|
|
fileInfo = Deno.statSync(filename);
|
2020-03-15 02:57:42 +00:00
|
|
|
assertEquals(fileInfo.size, 13);
|
2020-03-04 16:31:14 +00:00
|
|
|
// assert we can't read from file
|
|
|
|
let thrown = false;
|
|
|
|
try {
|
|
|
|
const buf = new Uint8Array(20);
|
|
|
|
await file.read(buf);
|
|
|
|
} catch (e) {
|
|
|
|
thrown = true;
|
|
|
|
} finally {
|
|
|
|
assert(thrown, "'w' mode shouldn't allow to read file");
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
// assert that existing file is truncated on open
|
2020-04-24 22:45:55 +00:00
|
|
|
file = await Deno.open(filename, {
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
});
|
2020-03-04 16:31:14 +00:00
|
|
|
file.close();
|
2020-03-15 02:57:42 +00:00
|
|
|
const fileSize = Deno.statSync(filename).size;
|
2020-03-04 16:31:14 +00:00
|
|
|
assertEquals(fileSize, 0);
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
|
|
|
}
|
|
|
|
);
|
2018-12-12 17:05:58 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-04-21 20:40:10 +00:00
|
|
|
async function openModeWriteRead(): Promise<void> {
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const filename = tempDir + "hello.txt";
|
|
|
|
const data = encoder.encode("Hello world!\n");
|
|
|
|
|
2020-04-24 22:45:55 +00:00
|
|
|
const file = await Deno.open(filename, {
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
create: true,
|
|
|
|
read: true,
|
|
|
|
});
|
2020-03-02 16:44:46 +00:00
|
|
|
const seekPosition = 0;
|
2019-04-21 20:40:10 +00:00
|
|
|
// assert file was created
|
|
|
|
let fileInfo = Deno.statSync(filename);
|
2020-04-16 05:40:30 +00:00
|
|
|
assert(fileInfo.isFile);
|
2020-03-15 02:57:42 +00:00
|
|
|
assertEquals(fileInfo.size, 0);
|
2019-04-21 20:40:10 +00:00
|
|
|
// write some data
|
|
|
|
await file.write(data);
|
|
|
|
fileInfo = Deno.statSync(filename);
|
2020-03-15 02:57:42 +00:00
|
|
|
assertEquals(fileInfo.size, 13);
|
2018-12-12 17:05:58 +00:00
|
|
|
|
2019-04-21 20:40:10 +00:00
|
|
|
const buf = new Uint8Array(20);
|
2020-03-02 16:44:46 +00:00
|
|
|
// seeking from beginning of a file
|
2020-04-28 10:30:59 +00:00
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.Start);
|
2020-03-02 16:44:46 +00:00
|
|
|
assertEquals(seekPosition, cursorPosition);
|
2019-04-21 20:40:10 +00:00
|
|
|
const result = await file.read(buf);
|
2019-07-06 14:16:03 +00:00
|
|
|
assertEquals(result, 13);
|
2019-04-21 20:40:10 +00:00
|
|
|
file.close();
|
2018-12-12 17:05:58 +00:00
|
|
|
|
2019-04-21 20:40:10 +00:00
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
|
|
|
}
|
|
|
|
);
|
2019-02-18 23:26:41 +00:00
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: true } }, async function seekStart(): Promise<void> {
|
2020-02-02 21:55:22 +00:00
|
|
|
const filename = "cli/tests/hello.txt";
|
2019-02-18 23:26:41 +00:00
|
|
|
const file = await Deno.open(filename);
|
2020-03-02 16:44:46 +00:00
|
|
|
const seekPosition = 6;
|
2019-02-18 23:26:41 +00:00
|
|
|
// Deliberately move 1 step forward
|
|
|
|
await file.read(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "Hello "
|
2020-03-02 16:44:46 +00:00
|
|
|
// seeking from beginning of a file plus seekPosition
|
2020-04-28 10:30:59 +00:00
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.Start);
|
2020-03-02 16:44:46 +00:00
|
|
|
assertEquals(seekPosition, cursorPosition);
|
2019-02-18 23:26:41 +00:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
await file.read(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
2019-03-07 01:48:46 +00:00
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 17:45:47 +00:00
|
|
|
file.close();
|
2019-02-18 23:26:41 +00:00
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: true } }, function seekSyncStart(): void {
|
2020-02-02 21:55:22 +00:00
|
|
|
const filename = "cli/tests/hello.txt";
|
2019-03-28 03:29:36 +00:00
|
|
|
const file = Deno.openSync(filename);
|
2020-03-02 16:44:46 +00:00
|
|
|
const seekPosition = 6;
|
2019-03-28 03:29:36 +00:00
|
|
|
// Deliberately move 1 step forward
|
|
|
|
file.readSync(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "Hello "
|
2020-03-02 16:44:46 +00:00
|
|
|
// seeking from beginning of a file plus seekPosition
|
2020-04-28 10:30:59 +00:00
|
|
|
const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.Start);
|
2020-03-02 16:44:46 +00:00
|
|
|
assertEquals(seekPosition, cursorPosition);
|
2019-03-28 03:29:36 +00:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
file.readSync(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 17:45:47 +00:00
|
|
|
file.close();
|
2019-03-28 03:29:36 +00:00
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: true } }, async function seekCurrent(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2020-02-02 21:55:22 +00:00
|
|
|
const filename = "cli/tests/hello.txt";
|
2019-02-18 23:26:41 +00:00
|
|
|
const file = await Deno.open(filename);
|
|
|
|
// Deliberately move 1 step forward
|
|
|
|
await file.read(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "ello "
|
2020-03-02 16:44:46 +00:00
|
|
|
const seekPosition = 5;
|
|
|
|
// seekPosition is relative to current cursor position after read
|
2020-04-28 10:30:59 +00:00
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.Current);
|
2020-03-02 16:44:46 +00:00
|
|
|
assertEquals(seekPosition + 1, cursorPosition);
|
2019-02-18 23:26:41 +00:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
await file.read(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
2019-03-07 01:48:46 +00:00
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 17:45:47 +00:00
|
|
|
file.close();
|
2019-02-18 23:26:41 +00:00
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: true } }, function seekSyncCurrent(): void {
|
2020-02-02 21:55:22 +00:00
|
|
|
const filename = "cli/tests/hello.txt";
|
2019-03-28 03:29:36 +00:00
|
|
|
const file = Deno.openSync(filename);
|
|
|
|
// Deliberately move 1 step forward
|
|
|
|
file.readSync(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "ello "
|
2020-03-02 16:44:46 +00:00
|
|
|
const seekPosition = 5;
|
|
|
|
// seekPosition is relative to current cursor position after read
|
2020-04-28 10:30:59 +00:00
|
|
|
const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.Current);
|
2020-03-02 16:44:46 +00:00
|
|
|
assertEquals(seekPosition + 1, cursorPosition);
|
2019-03-28 03:29:36 +00:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
file.readSync(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 17:45:47 +00:00
|
|
|
file.close();
|
2019-03-28 03:29:36 +00:00
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: true } }, async function seekEnd(): Promise<void> {
|
2020-02-02 21:55:22 +00:00
|
|
|
const filename = "cli/tests/hello.txt";
|
2019-02-18 23:26:41 +00:00
|
|
|
const file = await Deno.open(filename);
|
2020-03-02 16:44:46 +00:00
|
|
|
const seekPosition = -6;
|
|
|
|
// seek from end of file that has 12 chars, 12 - 6 = 6
|
2020-04-28 10:30:59 +00:00
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.End);
|
2020-03-02 16:44:46 +00:00
|
|
|
assertEquals(6, cursorPosition);
|
2019-02-18 23:26:41 +00:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
await file.read(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
2019-03-07 01:48:46 +00:00
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 17:45:47 +00:00
|
|
|
file.close();
|
2019-02-18 23:26:41 +00:00
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: true } }, function seekSyncEnd(): void {
|
2020-02-02 21:55:22 +00:00
|
|
|
const filename = "cli/tests/hello.txt";
|
2019-03-28 03:29:36 +00:00
|
|
|
const file = Deno.openSync(filename);
|
2020-03-02 16:44:46 +00:00
|
|
|
const seekPosition = -6;
|
|
|
|
// seek from end of file that has 12 chars, 12 - 6 = 6
|
2020-04-28 10:30:59 +00:00
|
|
|
const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.End);
|
2020-03-02 16:44:46 +00:00
|
|
|
assertEquals(6, cursorPosition);
|
2019-03-28 03:29:36 +00:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
file.readSync(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 17:45:47 +00:00
|
|
|
file.close();
|
2019-03-28 03:29:36 +00:00
|
|
|
});
|
|
|
|
|
2020-03-04 16:31:14 +00:00
|
|
|
unitTest({ perms: { read: true } }, async function seekMode(): Promise<void> {
|
2020-02-02 21:55:22 +00:00
|
|
|
const filename = "cli/tests/hello.txt";
|
2019-02-18 23:26:41 +00:00
|
|
|
const file = await Deno.open(filename);
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await file.seek(1, -1);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-02-21 15:36:13 +00:00
|
|
|
assert(err instanceof TypeError);
|
2020-06-06 03:43:00 +00:00
|
|
|
assertStringContains(err.message, "Invalid seek mode");
|
2019-03-27 02:47:17 +00:00
|
|
|
|
|
|
|
// We should still be able to read the file
|
|
|
|
// since it is still open.
|
2019-09-07 16:27:18 +00:00
|
|
|
const buf = new Uint8Array(1);
|
2019-03-27 02:47:17 +00:00
|
|
|
await file.read(buf); // "H"
|
|
|
|
assertEquals(new TextDecoder().decode(buf), "H");
|
2020-02-29 17:45:47 +00:00
|
|
|
file.close();
|
2019-02-18 23:26:41 +00:00
|
|
|
});
|