0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/js/files_test.ts

157 lines
4.6 KiB
TypeScript
Raw Normal View History

2019-01-21 14:03:30 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as deno from "deno";
2018-12-12 12:05:58 -05:00
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
test(function filesStdioFileDescriptors() {
2018-10-10 11:59:36 -04:00
assertEqual(deno.stdin.rid, 0);
assertEqual(deno.stdout.rid, 1);
assertEqual(deno.stderr.rid, 2);
});
testPerm({ read: true }, async function filesCopyToStdout() {
const filename = "package.json";
const file = await deno.open(filename);
2018-10-10 11:59:36 -04:00
assert(file.rid > 2);
const bytesWritten = await deno.copy(deno.stdout, file);
const fileSize = deno.statSync(filename).len;
assertEqual(bytesWritten, fileSize);
console.log("bytes written", bytesWritten);
});
testPerm({ read: true }, async function filesToAsyncIterator() {
const filename = "tests/hello.txt";
const file = await deno.open(filename);
let totalSize = 0;
for await (const buf of deno.toAsyncIterator(file)) {
totalSize += buf.byteLength;
}
assertEqual(totalSize, 12);
});
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";
const writeModes: deno.OpenMode[] = ["w", "a", "x"];
2018-12-13 16:20:37 -05:00
for (const mode of writeModes) {
let err;
try {
await deno.open(filename, mode);
} catch (e) {
err = e;
}
assert(!!err);
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
}
});
testPerm({ read: false }, async function readPermFailure() {
let caughtError = false;
try {
await deno.open("package.json", "r");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ write: false, read: false }, async function readWritePermFailure() {
const filename = "tests/hello.txt";
const writeModes: deno.OpenMode[] = ["r+", "w+", "a+", "x+"];
for (const mode of writeModes) {
let err;
try {
await deno.open(filename, mode);
} catch (e) {
err = e;
}
assert(!!err);
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
}
});
testPerm({ read: true, write: true }, async function createFile() {
2018-12-12 12:05:58 -05:00
const tempDir = await deno.makeTempDir();
const filename = tempDir + "/test.txt";
2018-12-13 16:20:37 -05:00
const f = await deno.open(filename, "w");
2018-12-12 12:05:58 -05:00
let fileInfo = deno.statSync(filename);
assert(fileInfo.isFile());
assert(fileInfo.len === 0);
const enc = new TextEncoder();
const data = enc.encode("Hello");
await f.write(data);
fileInfo = deno.statSync(filename);
assert(fileInfo.len === 5);
f.close();
// TODO: test different modes
await deno.remove(tempDir, { recursive: true });
2018-12-12 12:05:58 -05:00
});
testPerm({ read: true, write: true }, async function openModeWrite() {
2018-12-12 12:05:58 -05:00
const tempDir = deno.makeTempDirSync();
const encoder = new TextEncoder();
const filename = tempDir + "hello.txt";
const data = encoder.encode("Hello world!\n");
let file = await deno.open(filename, "w");
// assert file was created
let fileInfo = deno.statSync(filename);
assert(fileInfo.isFile());
assertEqual(fileInfo.len, 0);
// write some data
await file.write(data);
fileInfo = deno.statSync(filename);
assertEqual(fileInfo.len, 13);
// 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
file = await deno.open(filename, "w");
file.close();
const fileSize = deno.statSync(filename).len;
assertEqual(fileSize, 0);
await deno.remove(tempDir, { recursive: true });
2018-12-12 12:05:58 -05:00
});
testPerm({ read: true, write: true }, async function openModeWriteRead() {
2018-12-12 12:05:58 -05:00
const tempDir = deno.makeTempDirSync();
const encoder = new TextEncoder();
const filename = tempDir + "hello.txt";
const data = encoder.encode("Hello world!\n");
2018-12-13 16:20:37 -05:00
const file = await deno.open(filename, "w+");
2018-12-12 12:05:58 -05:00
// assert file was created
let fileInfo = deno.statSync(filename);
assert(fileInfo.isFile());
assertEqual(fileInfo.len, 0);
// write some data
await file.write(data);
fileInfo = deno.statSync(filename);
assertEqual(fileInfo.len, 13);
// TODO: this test is not working, I expect because
// file handle points to the end of file, but ATM
// deno has no seek implementation on Rust side
// assert file can be read
// const buf = new Uint8Array(20);
// const result = await file.read(buf);
// console.log(result.eof, result.nread);
// assertEqual(result.nread, 13);
// file.close();
await deno.remove(tempDir, { recursive: true });
2018-12-12 12:05:58 -05:00
});