2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-04-09 17:35:29 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file no-deprecated-deno-api
|
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertRejects,
|
|
|
|
assertThrows,
|
|
|
|
} from "./test_util.ts";
|
2023-01-15 15:09:26 -05:00
|
|
|
import { copy } from "../../../test_util/std/streams/copy.ts";
|
2018-09-27 00:56:39 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function filesStdioFileDescriptors() {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(Deno.stdin.rid, 0);
|
|
|
|
assertEquals(Deno.stdout.rid, 1);
|
|
|
|
assertEquals(Deno.stderr.rid, 2);
|
2018-09-27 00:56:39 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function filesCopyToStdout() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/fixture.json";
|
2019-02-12 10:08:56 -05:00
|
|
|
const file = await Deno.open(filename);
|
2018-10-10 11:59:36 -04:00
|
|
|
assert(file.rid > 2);
|
2021-07-12 13:44:42 -04:00
|
|
|
const bytesWritten = await copy(file, Deno.stdout);
|
2020-03-14 22:57:42 -04:00
|
|
|
const fileSize = Deno.statSync(filename).size;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(bytesWritten, fileSize);
|
2020-02-29 12:45:47 -05:00
|
|
|
file.close();
|
2018-09-27 00:56:39 -04:00
|
|
|
});
|
2018-10-31 10:29:13 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function filesIter() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2020-04-22 15:30:45 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function filesIterCustomBufSize() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2020-03-04 11:31:14 -05:00
|
|
|
const file = await Deno.open(filename);
|
2018-10-31 10:29:13 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
let totalSize = 0;
|
2020-04-22 15:30:45 -04:00
|
|
|
let iterations = 0;
|
2020-04-24 18:05:48 -04:00
|
|
|
for await (const buf of Deno.iter(file, { bufSize: 6 })) {
|
2020-03-04 11:31:14 -05:00
|
|
|
totalSize += buf.byteLength;
|
2020-04-22 15:30:45 -04:00
|
|
|
iterations += 1;
|
2020-03-04 11:31:14 -05:00
|
|
|
}
|
2018-10-31 10:29:13 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(totalSize, 12);
|
2020-04-22 15:30:45 -04:00
|
|
|
assertEquals(iterations, 2);
|
2020-03-04 11:31:14 -05:00
|
|
|
file.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-12-12 12:05:58 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, function filesIterSync() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2020-04-22 15:30:45 -04:00
|
|
|
const file = Deno.openSync(filename);
|
|
|
|
|
|
|
|
let totalSize = 0;
|
|
|
|
for (const buf of Deno.iterSync(file)) {
|
|
|
|
totalSize += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
file.close();
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function filesIterSyncCustomBufSize() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2020-04-22 15:30:45 -04:00
|
|
|
const file = Deno.openSync(filename);
|
|
|
|
|
|
|
|
let totalSize = 0;
|
|
|
|
let iterations = 0;
|
2020-04-24 18:05:48 -04:00
|
|
|
for (const buf of Deno.iterSync(file, { bufSize: 6 })) {
|
2020-04-22 15:30:45 -04:00
|
|
|
totalSize += buf.byteLength;
|
|
|
|
iterations += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
assertEquals(iterations, 2);
|
|
|
|
file.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-22 15:30:45 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function readerIter() {
|
2019-05-11 10:05:56 -04:00
|
|
|
// ref: https://github.com/denoland/deno/issues/2330
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
class TestReader implements Deno.Reader {
|
2020-03-28 13:03:49 -04:00
|
|
|
#offset = 0;
|
|
|
|
#buf: Uint8Array;
|
2019-05-11 10:05:56 -04:00
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
constructor(s: string) {
|
|
|
|
this.#buf = new Uint8Array(encoder.encode(s));
|
|
|
|
}
|
2019-05-11 10:05:56 -04:00
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
read(p: Uint8Array): Promise<number | null> {
|
2020-03-28 13:03:49 -04: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 10:05:56 -04:00
|
|
|
|
2019-07-06 10:16:03 -04:00
|
|
|
if (n === 0) {
|
2020-04-28 12:40:43 -04:00
|
|
|
return Promise.resolve(null);
|
2019-07-06 10:16:03 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve(n);
|
2019-05-11 10:05:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const reader = new TestReader("hello world!");
|
|
|
|
|
|
|
|
let totalSize = 0;
|
2020-04-22 15:30:45 -04:00
|
|
|
for await (const buf of Deno.iter(reader)) {
|
|
|
|
totalSize += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function readerIterSync() {
|
2020-04-22 15:30:45 -04:00
|
|
|
// ref: https://github.com/denoland/deno/issues/2330
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
2020-04-28 07:23:30 -04:00
|
|
|
class TestReader implements Deno.ReaderSync {
|
2020-04-22 15:30:45 -04:00
|
|
|
#offset = 0;
|
|
|
|
#buf: Uint8Array;
|
|
|
|
|
|
|
|
constructor(s: string) {
|
|
|
|
this.#buf = new Uint8Array(encoder.encode(s));
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
readSync(p: Uint8Array): number | null {
|
2020-04-22 15:30:45 -04: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 12:40:43 -04:00
|
|
|
return null;
|
2020-04-22 15:30:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const reader = new TestReader("hello world!");
|
|
|
|
|
|
|
|
let totalSize = 0;
|
|
|
|
for await (const buf of Deno.iterSync(reader)) {
|
2019-05-11 10:05:56 -04:00
|
|
|
totalSize += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(totalSize, 12);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-16 15:02:41 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { read: true, write: true },
|
2020-03-16 15:02:41 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function openSyncMode() {
|
2020-03-16 15:02:41 -04:00
|
|
|
const path = Deno.makeTempDirSync() + "/test_openSync.txt";
|
|
|
|
const file = Deno.openSync(path, {
|
|
|
|
write: true,
|
|
|
|
createNew: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
mode: 0o626,
|
2020-03-16 15:02:41 -04:00
|
|
|
});
|
|
|
|
file.close();
|
|
|
|
const pathInfo = Deno.statSync(path);
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-03-16 15:02:41 -04:00
|
|
|
assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask());
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-16 15:02:41 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-16 15:02:41 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { read: true, write: true },
|
2020-03-16 15:02:41 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function openMode() {
|
2020-03-16 15:02:41 -04:00
|
|
|
const path = (await Deno.makeTempDir()) + "/test_open.txt";
|
|
|
|
const file = await Deno.open(path, {
|
|
|
|
write: true,
|
|
|
|
createNew: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
mode: 0o626,
|
2020-03-16 15:02:41 -04:00
|
|
|
});
|
|
|
|
file.close();
|
|
|
|
const pathInfo = Deno.statSync(path);
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-03-16 15:02:41 -04:00
|
|
|
assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask());
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-16 15:02:41 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-06-11 12:36:20 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { read: true, write: true },
|
2020-06-11 12:36:20 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
function openSyncUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fileUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${
|
|
|
|
Deno.build.os === "windows" ? "/" : ""
|
|
|
|
}${tempDir}/test_open.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
const file = Deno.openSync(fileUrl, {
|
|
|
|
write: true,
|
|
|
|
createNew: true,
|
|
|
|
mode: 0o626,
|
|
|
|
});
|
|
|
|
file.close();
|
|
|
|
const pathInfo = Deno.statSync(fileUrl);
|
|
|
|
if (Deno.build.os !== "windows") {
|
|
|
|
assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask());
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-06-11 12:36:20 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { read: true, write: true },
|
2020-06-11 12:36:20 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function openUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
const fileUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${
|
|
|
|
Deno.build.os === "windows" ? "/" : ""
|
|
|
|
}${tempDir}/test_open.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
const file = await Deno.open(fileUrl, {
|
|
|
|
write: true,
|
|
|
|
createNew: true,
|
|
|
|
mode: 0o626,
|
|
|
|
});
|
|
|
|
file.close();
|
|
|
|
const pathInfo = Deno.statSync(fileUrl);
|
|
|
|
if (Deno.build.os !== "windows") {
|
|
|
|
assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask());
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: false } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writePermFailure() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const filename = "tests/hello.txt";
|
2020-04-24 18:45:55 -04:00
|
|
|
const openOptions: Deno.OpenOptions[] = [{ write: true }, { append: true }];
|
|
|
|
for (const options of openOptions) {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-04-24 18:45:55 -04:00
|
|
|
await Deno.open(filename, options);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-12-13 16:20:37 -05:00
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-12-13 16:20:37 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function openOptions() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/fixture.json";
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2020-06-24 18:57:08 -04:00
|
|
|
await Deno.open(filename, { write: false });
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"OpenOptions requires at least one option to be true",
|
2020-01-21 04:49:42 -05:00
|
|
|
);
|
|
|
|
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2020-06-24 18:57:08 -04:00
|
|
|
await Deno.open(filename, { truncate: true, write: false });
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"'truncate' option requires 'write' option",
|
2020-06-05 23:43:00 -04:00
|
|
|
);
|
2020-01-21 04:49:42 -05:00
|
|
|
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2020-06-24 18:57:08 -04:00
|
|
|
await Deno.open(filename, { create: true, write: false });
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"'create' or 'createNew' options require 'write' or 'append' option",
|
2020-01-21 04:49:42 -05:00
|
|
|
);
|
|
|
|
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2020-06-24 18:57:08 -04:00
|
|
|
await Deno.open(filename, { createNew: true, append: false });
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"'create' or 'createNew' options require 'write' or 'append' option",
|
2020-01-21 04:49:42 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: false } }, async function readPermFailure() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-04-24 18:45:55 -04:00
|
|
|
await Deno.open("package.json", { read: true });
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeNullBufferFailure() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "hello.txt";
|
|
|
|
const w = {
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
create: true,
|
2020-03-04 11:31:14 -05:00
|
|
|
};
|
|
|
|
const file = await Deno.open(filename, w);
|
|
|
|
|
|
|
|
// writing null should throw an error
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-06-24 18:57:08 -04:00
|
|
|
await file.write(null as any);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2021-01-16 18:32:59 -05:00
|
|
|
); // TODO(bartlomieju): Check error kind when dispatch_minimal pipes errors properly
|
2020-03-04 11:31:14 -05:00
|
|
|
file.close();
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-05-15 14:50:54 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function readNullBufferFailure() {
|
2019-05-15 14:50:54 -04:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "hello.txt";
|
2020-04-24 18:45:55 -04:00
|
|
|
const file = await Deno.open(filename, {
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
create: true,
|
|
|
|
});
|
2019-05-15 14:50:54 -04:00
|
|
|
|
2019-12-28 08:48:36 -05:00
|
|
|
// reading into an empty buffer should return 0 immediately
|
|
|
|
const bytesRead = await file.read(new Uint8Array(0));
|
|
|
|
assert(bytesRead === 0);
|
|
|
|
|
2019-05-15 14:50:54 -04:00
|
|
|
// reading file into null buffer should throw an error
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-06-02 00:24:44 -04:00
|
|
|
await file.read(null as any);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, TypeError);
|
2021-01-16 18:32:59 -05:00
|
|
|
// TODO(bartlomieju): Check error kind when dispatch_minimal pipes errors properly
|
2019-05-15 14:50:54 -04:00
|
|
|
|
|
|
|
file.close();
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-05-15 14:50:54 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: false, read: false } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function readWritePermFailure() {
|
2019-04-21 16:40:10 -04:00
|
|
|
const filename = "tests/hello.txt";
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-04-24 18:45:55 -04:00
|
|
|
await Deno.open(filename, { read: true });
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-11 09:21:18 -04:00
|
|
|
{ permissions: { write: true, read: true } },
|
|
|
|
async function openNotFound() {
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.open("bad_file_name");
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`open 'bad_file_name'`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-11 09:21:18 -04:00
|
|
|
{ permissions: { write: true, read: true } },
|
|
|
|
function openSyncNotFound() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.openSync("bad_file_name");
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`open 'bad_file_name'`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function createFile() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
const filename = tempDir + "/test.txt";
|
|
|
|
const f = await Deno.create(filename);
|
|
|
|
let fileInfo = Deno.statSync(filename);
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(fileInfo.isFile);
|
2020-03-14 22:57:42 -04:00
|
|
|
assert(fileInfo.size === 0);
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
await f.write(data);
|
|
|
|
fileInfo = Deno.statSync(filename);
|
2020-03-14 22:57:42 -04:00
|
|
|
assert(fileInfo.size === 5);
|
2020-03-04 11:31:14 -05:00
|
|
|
f.close();
|
2018-12-12 12:05:58 -05:00
|
|
|
|
2021-01-16 18:32:59 -05:00
|
|
|
// TODO(bartlomieju): test different modes
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function createFileWithUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
const fileUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
const f = await Deno.create(fileUrl);
|
|
|
|
let fileInfo = Deno.statSync(fileUrl);
|
|
|
|
assert(fileInfo.isFile);
|
|
|
|
assert(fileInfo.size === 0);
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
await f.write(data);
|
|
|
|
fileInfo = Deno.statSync(fileUrl);
|
|
|
|
assert(fileInfo.size === 5);
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function createSyncFile() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
const filename = tempDir + "/test.txt";
|
|
|
|
const f = Deno.createSync(filename);
|
|
|
|
let fileInfo = Deno.statSync(filename);
|
|
|
|
assert(fileInfo.isFile);
|
|
|
|
assert(fileInfo.size === 0);
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
await f.write(data);
|
|
|
|
fileInfo = Deno.statSync(filename);
|
|
|
|
assert(fileInfo.size === 5);
|
|
|
|
f.close();
|
|
|
|
|
2021-01-16 18:32:59 -05:00
|
|
|
// TODO(bartlomieju): test different modes
|
2020-06-11 12:36:20 -04:00
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function createSyncFileWithUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
const fileUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
const f = Deno.createSync(fileUrl);
|
|
|
|
let fileInfo = Deno.statSync(fileUrl);
|
|
|
|
assert(fileInfo.isFile);
|
|
|
|
assert(fileInfo.size === 0);
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
await f.write(data);
|
|
|
|
fileInfo = Deno.statSync(fileUrl);
|
|
|
|
assert(fileInfo.size === 5);
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function openModeWrite() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const filename = tempDir + "hello.txt";
|
|
|
|
const data = encoder.encode("Hello world!\n");
|
2020-04-24 18:45:55 -04:00
|
|
|
let file = await Deno.open(filename, {
|
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
});
|
2020-03-04 11:31:14 -05:00
|
|
|
// assert file was created
|
|
|
|
let fileInfo = Deno.statSync(filename);
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(fileInfo.isFile);
|
2020-03-14 22:57:42 -04:00
|
|
|
assertEquals(fileInfo.size, 0);
|
2020-03-04 11:31:14 -05:00
|
|
|
// write some data
|
|
|
|
await file.write(data);
|
|
|
|
fileInfo = Deno.statSync(filename);
|
2020-03-14 22:57:42 -04:00
|
|
|
assertEquals(fileInfo.size, 13);
|
2020-03-04 11:31:14 -05:00
|
|
|
// assert we can't read from file
|
|
|
|
let thrown = false;
|
|
|
|
try {
|
|
|
|
const buf = new Uint8Array(20);
|
|
|
|
await file.read(buf);
|
2021-04-09 17:35:29 -04:00
|
|
|
} catch (_e) {
|
2020-03-04 11:31:14 -05:00
|
|
|
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 18:45:55 -04:00
|
|
|
file = await Deno.open(filename, {
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
});
|
2020-03-04 11:31:14 -05:00
|
|
|
file.close();
|
2020-03-14 22:57:42 -04:00
|
|
|
const fileSize = Deno.statSync(filename).size;
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(fileSize, 0);
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-12-12 12:05:58 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function openModeWriteRead() {
|
2019-04-21 16:40:10 -04:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const filename = tempDir + "hello.txt";
|
|
|
|
const data = encoder.encode("Hello world!\n");
|
|
|
|
|
2020-04-24 18:45:55 -04:00
|
|
|
const file = await Deno.open(filename, {
|
|
|
|
write: true,
|
|
|
|
truncate: true,
|
|
|
|
create: true,
|
|
|
|
read: true,
|
|
|
|
});
|
2020-03-02 11:44:46 -05:00
|
|
|
const seekPosition = 0;
|
2019-04-21 16:40:10 -04:00
|
|
|
// assert file was created
|
|
|
|
let fileInfo = Deno.statSync(filename);
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(fileInfo.isFile);
|
2020-03-14 22:57:42 -04:00
|
|
|
assertEquals(fileInfo.size, 0);
|
2019-04-21 16:40:10 -04:00
|
|
|
// write some data
|
|
|
|
await file.write(data);
|
|
|
|
fileInfo = Deno.statSync(filename);
|
2020-03-14 22:57:42 -04:00
|
|
|
assertEquals(fileInfo.size, 13);
|
2018-12-12 12:05:58 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
const buf = new Uint8Array(20);
|
2020-03-02 11:44:46 -05:00
|
|
|
// seeking from beginning of a file
|
2020-04-28 06:30:59 -04:00
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.Start);
|
2020-03-02 11:44:46 -05:00
|
|
|
assertEquals(seekPosition, cursorPosition);
|
2019-04-21 16:40:10 -04:00
|
|
|
const result = await file.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(result, 13);
|
2019-04-21 16:40:10 -04:00
|
|
|
file.close();
|
2018-12-12 12:05:58 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2019-02-18 18:26:41 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function seekStart() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2019-02-18 18:26:41 -05:00
|
|
|
const file = await Deno.open(filename);
|
2020-03-02 11:44:46 -05:00
|
|
|
const seekPosition = 6;
|
2019-02-18 18:26:41 -05:00
|
|
|
// Deliberately move 1 step forward
|
|
|
|
await file.read(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "Hello "
|
2020-03-02 11:44:46 -05:00
|
|
|
// seeking from beginning of a file plus seekPosition
|
2020-04-28 06:30:59 -04:00
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.Start);
|
2020-03-02 11:44:46 -05:00
|
|
|
assertEquals(seekPosition, cursorPosition);
|
2019-02-18 18:26:41 -05:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
await file.read(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 12:45:47 -05:00
|
|
|
file.close();
|
2019-02-18 18:26:41 -05:00
|
|
|
});
|
|
|
|
|
2023-01-18 23:30:56 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function seekStartBigInt() {
|
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
|
|
|
const file = await Deno.open(filename);
|
|
|
|
const seekPosition = 6n;
|
|
|
|
// Deliberately move 1 step forward
|
|
|
|
await file.read(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "Hello "
|
|
|
|
// seeking from beginning of a file plus seekPosition
|
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.Start);
|
|
|
|
assertEquals(seekPosition, BigInt(cursorPosition));
|
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
await file.read(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
|
|
|
assertEquals(decoded, "world!");
|
|
|
|
file.close();
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, function seekSyncStart() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2019-03-27 23:29:36 -04:00
|
|
|
const file = Deno.openSync(filename);
|
2020-03-02 11:44:46 -05:00
|
|
|
const seekPosition = 6;
|
2019-03-27 23:29:36 -04:00
|
|
|
// Deliberately move 1 step forward
|
|
|
|
file.readSync(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "Hello "
|
2020-03-02 11:44:46 -05:00
|
|
|
// seeking from beginning of a file plus seekPosition
|
2020-04-28 06:30:59 -04:00
|
|
|
const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.Start);
|
2020-03-02 11:44:46 -05:00
|
|
|
assertEquals(seekPosition, cursorPosition);
|
2019-03-27 23:29:36 -04:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
file.readSync(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 12:45:47 -05:00
|
|
|
file.close();
|
2019-03-27 23:29:36 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function seekCurrent() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2019-02-18 18:26:41 -05:00
|
|
|
const file = await Deno.open(filename);
|
|
|
|
// Deliberately move 1 step forward
|
|
|
|
await file.read(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "ello "
|
2020-03-02 11:44:46 -05:00
|
|
|
const seekPosition = 5;
|
|
|
|
// seekPosition is relative to current cursor position after read
|
2020-04-28 06:30:59 -04:00
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.Current);
|
2020-03-02 11:44:46 -05:00
|
|
|
assertEquals(seekPosition + 1, cursorPosition);
|
2019-02-18 18:26:41 -05:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
await file.read(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 12:45:47 -05:00
|
|
|
file.close();
|
2019-02-18 18:26:41 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, function seekSyncCurrent() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2019-03-27 23:29:36 -04:00
|
|
|
const file = Deno.openSync(filename);
|
|
|
|
// Deliberately move 1 step forward
|
|
|
|
file.readSync(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "ello "
|
2020-03-02 11:44:46 -05:00
|
|
|
const seekPosition = 5;
|
|
|
|
// seekPosition is relative to current cursor position after read
|
2020-04-28 06:30:59 -04:00
|
|
|
const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.Current);
|
2020-03-02 11:44:46 -05:00
|
|
|
assertEquals(seekPosition + 1, cursorPosition);
|
2019-03-27 23:29:36 -04:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
file.readSync(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 12:45:47 -05:00
|
|
|
file.close();
|
2019-03-27 23:29:36 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function seekEnd() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2019-02-18 18:26:41 -05:00
|
|
|
const file = await Deno.open(filename);
|
2020-03-02 11:44:46 -05:00
|
|
|
const seekPosition = -6;
|
|
|
|
// seek from end of file that has 12 chars, 12 - 6 = 6
|
2020-04-28 06:30:59 -04:00
|
|
|
const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.End);
|
2020-03-02 11:44:46 -05:00
|
|
|
assertEquals(6, cursorPosition);
|
2019-02-18 18:26:41 -05:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
await file.read(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 12:45:47 -05:00
|
|
|
file.close();
|
2019-02-18 18:26:41 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, function seekSyncEnd() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2019-03-27 23:29:36 -04:00
|
|
|
const file = Deno.openSync(filename);
|
2020-03-02 11:44:46 -05:00
|
|
|
const seekPosition = -6;
|
|
|
|
// seek from end of file that has 12 chars, 12 - 6 = 6
|
2020-04-28 06:30:59 -04:00
|
|
|
const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.End);
|
2020-03-02 11:44:46 -05:00
|
|
|
assertEquals(6, cursorPosition);
|
2019-03-27 23:29:36 -04:00
|
|
|
const buf = new Uint8Array(6);
|
|
|
|
file.readSync(buf);
|
|
|
|
const decoded = new TextDecoder().decode(buf);
|
|
|
|
assertEquals(decoded, "world!");
|
2020-02-29 12:45:47 -05:00
|
|
|
file.close();
|
2019-03-27 23:29:36 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function seekMode() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2019-02-18 18:26:41 -05:00
|
|
|
const file = await Deno.open(filename);
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2023-03-21 11:46:40 -04:00
|
|
|
await file.seek(1, -1 as unknown as Deno.SeekMode);
|
2020-06-24 18:57:08 -04:00
|
|
|
},
|
|
|
|
TypeError,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Invalid seek mode",
|
2020-06-24 18:57:08 -04:00
|
|
|
);
|
2019-03-26 22:47:17 -04:00
|
|
|
|
|
|
|
// We should still be able to read the file
|
|
|
|
// since it is still open.
|
2019-09-07 12:27:18 -04:00
|
|
|
const buf = new Uint8Array(1);
|
2019-03-26 22:47:17 -04:00
|
|
|
await file.read(buf); // "H"
|
|
|
|
assertEquals(new TextDecoder().decode(buf), "H");
|
2020-02-29 12:45:47 -05:00
|
|
|
file.close();
|
2019-02-18 18:26:41 -05:00
|
|
|
});
|
2022-02-15 07:35:22 -05:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
function fileTruncateSyncSuccess() {
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test_fileTruncateSync.txt";
|
|
|
|
const file = Deno.openSync(filename, {
|
|
|
|
create: true,
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
file.truncateSync(20);
|
|
|
|
assertEquals(Deno.readFileSync(filename).byteLength, 20);
|
|
|
|
file.truncateSync(5);
|
|
|
|
assertEquals(Deno.readFileSync(filename).byteLength, 5);
|
|
|
|
file.truncateSync(-5);
|
|
|
|
assertEquals(Deno.readFileSync(filename).byteLength, 0);
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
Deno.removeSync(filename);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function fileTruncateSuccess() {
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test_fileTruncate.txt";
|
|
|
|
const file = await Deno.open(filename, {
|
|
|
|
create: true,
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await file.truncate(20);
|
|
|
|
assertEquals((await Deno.readFile(filename)).byteLength, 20);
|
|
|
|
await file.truncate(5);
|
|
|
|
assertEquals((await Deno.readFile(filename)).byteLength, 5);
|
|
|
|
await file.truncate(-5);
|
|
|
|
assertEquals((await Deno.readFile(filename)).byteLength, 0);
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
await Deno.remove(filename);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test({ permissions: { read: true } }, function fileStatSyncSuccess() {
|
|
|
|
const file = Deno.openSync("README.md");
|
|
|
|
const fileInfo = file.statSync();
|
|
|
|
assert(fileInfo.isFile);
|
|
|
|
assert(!fileInfo.isSymlink);
|
|
|
|
assert(!fileInfo.isDirectory);
|
|
|
|
assert(fileInfo.size);
|
|
|
|
assert(fileInfo.atime);
|
|
|
|
assert(fileInfo.mtime);
|
|
|
|
// The `birthtime` field is not available on Linux before kernel version 4.11.
|
|
|
|
assert(fileInfo.birthtime || Deno.build.os === "linux");
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(async function fileStatSuccess() {
|
|
|
|
const file = await Deno.open("README.md");
|
|
|
|
const fileInfo = await file.stat();
|
|
|
|
assert(fileInfo.isFile);
|
|
|
|
assert(!fileInfo.isSymlink);
|
|
|
|
assert(!fileInfo.isDirectory);
|
|
|
|
assert(fileInfo.size);
|
|
|
|
assert(fileInfo.atime);
|
|
|
|
assert(fileInfo.mtime);
|
|
|
|
// The `birthtime` field is not available on Linux before kernel version 4.11.
|
|
|
|
assert(fileInfo.birthtime || Deno.build.os === "linux");
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({ permissions: { read: true } }, async function readableStream() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2022-02-15 07:35:22 -05:00
|
|
|
const file = await Deno.open(filename);
|
|
|
|
assert(file.readable instanceof ReadableStream);
|
|
|
|
const chunks = [];
|
|
|
|
for await (const chunk of file.readable) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks.length, 1);
|
|
|
|
assertEquals(chunks[0].byteLength, 12);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function readableStreamTextEncoderPipe() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const filename = "cli/tests/testdata/assets/hello.txt";
|
2022-02-15 07:35:22 -05:00
|
|
|
const file = await Deno.open(filename);
|
|
|
|
const readable = file.readable.pipeThrough(new TextDecoderStream());
|
|
|
|
const chunks = [];
|
|
|
|
for await (const chunk of readable) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
assertEquals(chunks.length, 1);
|
|
|
|
assertEquals(chunks[0].length, 12);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function writableStream() {
|
|
|
|
const path = await Deno.makeTempFile();
|
|
|
|
const file = await Deno.open(path, { write: true });
|
|
|
|
assert(file.writable instanceof WritableStream);
|
|
|
|
const readable = new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(new TextEncoder().encode("hello "));
|
|
|
|
controller.enqueue(new TextEncoder().encode("world!"));
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await readable.pipeTo(file.writable);
|
|
|
|
const res = await Deno.readTextFile(path);
|
|
|
|
assertEquals(res, "hello world!");
|
|
|
|
},
|
|
|
|
);
|
2022-05-01 22:13:05 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function readTextFileNonUtf8() {
|
|
|
|
const path = await Deno.makeTempFile();
|
|
|
|
const file = await Deno.open(path, { write: true });
|
|
|
|
await file.write(new TextEncoder().encode("hello "));
|
|
|
|
await file.write(new Uint8Array([0xC0]));
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
const res = await Deno.readTextFile(path);
|
|
|
|
const resSync = Deno.readTextFileSync(path);
|
|
|
|
assertEquals(res, resSync);
|
|
|
|
assertEquals(res, "hello \uFFFD");
|
|
|
|
},
|
|
|
|
);
|
2023-11-01 15:26:12 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function fsFileExplicitResourceManagement() {
|
|
|
|
let file2: Deno.FsFile;
|
|
|
|
|
|
|
|
{
|
|
|
|
using file = await Deno.open("cli/tests/testdata/assets/hello.txt");
|
|
|
|
file2 = file;
|
|
|
|
|
|
|
|
const stat = file.statSync();
|
|
|
|
assert(stat.isFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows(() => file2.statSync(), Deno.errors.BadResource);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function fsFileExplicitResourceManagementManualClose() {
|
|
|
|
using file = await Deno.open("cli/tests/testdata/assets/hello.txt");
|
|
|
|
file.close();
|
|
|
|
assertThrows(() => file.statSync(), Deno.errors.BadResource); // definitely closed
|
|
|
|
// calling [Symbol.dispose] after manual close is a no-op
|
|
|
|
},
|
|
|
|
);
|