2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { test, testPerm, assert, assertEquals } from "./test_util.ts";
|
2018-09-27 00:56:39 -04:00
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true }, async function filesCopyToStdout() {
|
2018-09-27 00:56:39 -04:00
|
|
|
const filename = "package.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);
|
2019-02-12 10:08:56 -05:00
|
|
|
const bytesWritten = await Deno.copy(Deno.stdout, file);
|
|
|
|
const fileSize = Deno.statSync(filename).len;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(bytesWritten, fileSize);
|
2018-09-27 00:56:39 -04:00
|
|
|
console.log("bytes written", bytesWritten);
|
|
|
|
});
|
2018-10-31 10:29:13 -04:00
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true }, async function filesToAsyncIterator() {
|
2018-10-31 10:29:13 -04:00
|
|
|
const filename = "tests/hello.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
const file = await Deno.open(filename);
|
2018-10-31 10:29:13 -04:00
|
|
|
|
|
|
|
let totalSize = 0;
|
2019-02-12 10:08:56 -05:00
|
|
|
for await (const buf of Deno.toAsyncIterator(file)) {
|
2018-10-31 10:29:13 -04:00
|
|
|
totalSize += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(totalSize, 12);
|
2018-10-31 10:29:13 -04:00
|
|
|
});
|
2018-12-12 12:05:58 -05:00
|
|
|
|
2018-12-13 16:20:37 -05:00
|
|
|
testPerm({ write: false }, async function writePermFailure() {
|
|
|
|
const filename = "tests/hello.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
const writeModes: Deno.OpenMode[] = ["w", "a", "x"];
|
2018-12-13 16:20:37 -05:00
|
|
|
for (const mode of writeModes) {
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.open(filename, mode);
|
2018-12-13 16:20:37 -05:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-12-13 16:20:37 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: false }, async function readPermFailure() {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.open("package.json", "r");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(e.name, "PermissionDenied");
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false, read: false }, async function readWritePermFailure() {
|
|
|
|
const filename = "tests/hello.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
const writeModes: Deno.OpenMode[] = ["r+", "w+", "a+", "x+"];
|
2019-02-08 15:59:38 -05:00
|
|
|
for (const mode of writeModes) {
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.open(filename, mode);
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ read: true, write: true }, async function createFile() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
2018-12-12 12:05:58 -05:00
|
|
|
const filename = tempDir + "/test.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
const f = await Deno.open(filename, "w");
|
|
|
|
let fileInfo = Deno.statSync(filename);
|
2018-12-12 12:05:58 -05:00
|
|
|
assert(fileInfo.isFile());
|
|
|
|
assert(fileInfo.len === 0);
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
await f.write(data);
|
2019-02-12 10:08:56 -05:00
|
|
|
fileInfo = Deno.statSync(filename);
|
2018-12-12 12:05:58 -05:00
|
|
|
assert(fileInfo.len === 5);
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
// TODO: test different modes
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2018-12-12 12:05:58 -05:00
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function openModeWrite() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-12-12 12:05:58 -05:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const filename = tempDir + "hello.txt";
|
|
|
|
const data = encoder.encode("Hello world!\n");
|
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
let file = await Deno.open(filename, "w");
|
2018-12-12 12:05:58 -05:00
|
|
|
// assert file was created
|
2019-02-12 10:08:56 -05:00
|
|
|
let fileInfo = Deno.statSync(filename);
|
2018-12-12 12:05:58 -05:00
|
|
|
assert(fileInfo.isFile());
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileInfo.len, 0);
|
2018-12-12 12:05:58 -05:00
|
|
|
// write some data
|
|
|
|
await file.write(data);
|
2019-02-12 10:08:56 -05:00
|
|
|
fileInfo = Deno.statSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileInfo.len, 13);
|
2018-12-12 12:05:58 -05: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
|
2019-02-12 10:08:56 -05:00
|
|
|
file = await Deno.open(filename, "w");
|
2018-12-12 12:05:58 -05:00
|
|
|
file.close();
|
2019-02-12 10:08:56 -05:00
|
|
|
const fileSize = Deno.statSync(filename).len;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileSize, 0);
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2018-12-12 12:05:58 -05:00
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true, write: true }, async function openModeWriteRead() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
2018-12-12 12:05:58 -05:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const filename = tempDir + "hello.txt";
|
|
|
|
const data = encoder.encode("Hello world!\n");
|
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
const file = await Deno.open(filename, "w+");
|
2018-12-12 12:05:58 -05:00
|
|
|
// assert file was created
|
2019-02-12 10:08:56 -05:00
|
|
|
let fileInfo = Deno.statSync(filename);
|
2018-12-12 12:05:58 -05:00
|
|
|
assert(fileInfo.isFile());
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileInfo.len, 0);
|
2018-12-12 12:05:58 -05:00
|
|
|
// write some data
|
|
|
|
await file.write(data);
|
2019-02-12 10:08:56 -05:00
|
|
|
fileInfo = Deno.statSync(filename);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(fileInfo.len, 13);
|
2018-12-12 12:05:58 -05:00
|
|
|
|
2019-02-18 18:26:41 -05:00
|
|
|
const buf = new Uint8Array(20);
|
|
|
|
await file.seek(0, Deno.SeekMode.SEEK_START);
|
|
|
|
const result = await file.read(buf);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(result.nread, 13);
|
2019-02-18 18:26:41 -05:00
|
|
|
file.close();
|
2018-12-12 12:05:58 -05:00
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
2018-12-12 12:05:58 -05:00
|
|
|
});
|
2019-02-18 18:26:41 -05:00
|
|
|
|
|
|
|
testPerm({ read: true }, async function seekStart() {
|
|
|
|
const filename = "tests/hello.txt";
|
|
|
|
const file = await Deno.open(filename);
|
|
|
|
// Deliberately move 1 step forward
|
|
|
|
await file.read(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "Hello "
|
|
|
|
await file.seek(6, Deno.SeekMode.SEEK_START);
|
|
|
|
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!");
|
2019-02-18 18:26:41 -05:00
|
|
|
});
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
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!");
|
|
|
|
});
|
|
|
|
|
2019-02-18 18:26:41 -05:00
|
|
|
testPerm({ read: true }, async function seekCurrent() {
|
|
|
|
const filename = "tests/hello.txt";
|
|
|
|
const file = await Deno.open(filename);
|
|
|
|
// Deliberately move 1 step forward
|
|
|
|
await file.read(new Uint8Array(1)); // "H"
|
|
|
|
// Skipping "ello "
|
|
|
|
await file.seek(5, Deno.SeekMode.SEEK_CURRENT);
|
|
|
|
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!");
|
2019-02-18 18:26:41 -05:00
|
|
|
});
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
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!");
|
|
|
|
});
|
|
|
|
|
2019-02-18 18:26:41 -05:00
|
|
|
testPerm({ read: true }, async function seekEnd() {
|
|
|
|
const filename = "tests/hello.txt";
|
|
|
|
const file = await Deno.open(filename);
|
|
|
|
await file.seek(-6, Deno.SeekMode.SEEK_END);
|
|
|
|
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!");
|
2019-02-18 18:26:41 -05:00
|
|
|
});
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
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!");
|
|
|
|
});
|
|
|
|
|
2019-02-18 18:26:41 -05:00
|
|
|
testPerm({ read: true }, async function seekMode() {
|
|
|
|
const filename = "tests/hello.txt";
|
|
|
|
const file = await Deno.open(filename);
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await file.seek(1, -1);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.InvalidSeekMode);
|
|
|
|
assertEquals(err.name, "InvalidSeekMode");
|
2019-03-26 22:47:17 -04:00
|
|
|
|
|
|
|
// We should still be able to read the file
|
|
|
|
// since it is still open.
|
|
|
|
let buf = new Uint8Array(1);
|
|
|
|
await file.read(buf); // "H"
|
|
|
|
assertEquals(new TextDecoder().decode(buf), "H");
|
2019-02-18 18:26:41 -05:00
|
|
|
});
|