2021-01-11 12:13:41 -05:00
|
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-27 06:22:32 -04:00
|
|
|
|
import { assert, assertEquals, unitTest } from "./test_util.ts";
|
2018-11-04 13:05:02 -05:00
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-02-19 15:36:18 -05:00
|
|
|
|
function testFirstArgument(arg1: any[], expectedSize: number): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
const file = new File(arg1, "name");
|
|
|
|
|
assert(file instanceof File);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(file.name, "name");
|
|
|
|
|
assertEquals(file.size, expectedSize);
|
|
|
|
|
assertEquals(file.type, "");
|
2018-11-04 13:05:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileEmptyFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileStringFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["bits"], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUnicodeStringFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["𝓽𝓮𝔁𝓽"], 16);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileStringObjectFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new String("string object")], 13);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileEmptyBlobFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob()], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileBlobFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob(["bits"])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileEmptyFileFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File([], "world.txt")], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileFileFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File(["bits"], "world.txt")], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileArrayBufferFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new ArrayBuffer(8)], 8);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileTypedArrayFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileVariousFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(
|
|
|
|
|
[
|
|
|
|
|
"bits",
|
|
|
|
|
new Blob(["bits"]),
|
|
|
|
|
new Blob(),
|
|
|
|
|
new Uint8Array([0x50, 0x41]),
|
|
|
|
|
new Uint16Array([0x5353]),
|
2020-03-28 13:03:49 -04:00
|
|
|
|
new Uint32Array([0x53534150]),
|
2018-11-04 13:05:02 -05:00
|
|
|
|
],
|
2020-07-14 15:24:17 -04:00
|
|
|
|
16,
|
2018-11-04 13:05:02 -05:00
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileNumberInFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([12], 2);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileArrayInFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([[1, 2, 3]], 5);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileObjectInFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
// "[object Object]"
|
|
|
|
|
testFirstArgument([{}], 15);
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-02-19 15:36:18 -05:00
|
|
|
|
function testSecondArgument(arg2: any, expectedFileName: string): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
const file = new File(["bits"], arg2);
|
|
|
|
|
assert(file instanceof File);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(file.name, expectedFileName);
|
2018-11-04 13:05:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("dummy", "dummy");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingNullFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(null, "null");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingNumberFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(1, "1");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingEmptyStringFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("", "");
|
|
|
|
|
});
|
2021-04-12 07:33:05 -04:00
|
|
|
|
|
2021-04-12 08:32:58 -04:00
|
|
|
|
unitTest(
|
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
|
function fileTruncateSyncSuccess(): void {
|
|
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
|
async function fileTruncateSuccess(): Promise<void> {
|
|
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2021-04-12 07:33:05 -04:00
|
|
|
|
unitTest({ perms: { read: true } }, function fileStatSyncSuccess(): void {
|
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
unitTest({ perms: { read: true } }, async function fileStatSuccess(): Promise<
|
|
|
|
|
void
|
|
|
|
|
> {
|
|
|
|
|
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();
|
|
|
|
|
});
|