2021-01-11 12:13:41 -05:00
|
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
|
import { assert, assertEquals } 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
|
2021-08-05 07:08:58 -04:00
|
|
|
|
function testFirstArgument(arg1: any[], expectedSize: number) {
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileEmptyFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileStringFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["bits"], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUnicodeStringFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["𝓽𝓮𝔁𝓽"], 16);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileStringObjectFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new String("string object")], 13);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileEmptyBlobFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob()], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileBlobFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob(["bits"])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileEmptyFileFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File([], "world.txt")], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileFileFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File(["bits"], "world.txt")], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileArrayBufferFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new ArrayBuffer(8)], 8);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileTypedArrayFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileVariousFileBits() {
|
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
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileNumberInFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([12], 2);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileArrayInFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([[1, 2, 3]], 5);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileObjectInFileBits() {
|
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
|
2021-08-05 07:08:58 -04:00
|
|
|
|
function testSecondArgument(arg2: any, expectedFileName: string) {
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUsingFileName() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("dummy", "dummy");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUsingNullFileName() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(null, "null");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUsingNumberFileName() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(1, "1");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUsingEmptyStringFileName() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("", "");
|
|
|
|
|
});
|
2021-04-12 07:33:05 -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
|
|
|
|
function fileTruncateSyncSuccess() {
|
2021-04-12 08:32:58 -04:00
|
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
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 fileTruncateSuccess() {
|
2021-04-12 08:32:58 -04:00
|
|
|
|
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-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { read: true } }, function fileStatSyncSuccess() {
|
2021-04-12 07:33:05 -04:00
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { read: true } }, async function fileStatSuccess() {
|
2021-04-12 07:33:05 -04:00
|
|
|
|
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();
|
|
|
|
|
});
|