summaryrefslogtreecommitdiff
path: root/cli/tests/unit/file_test.ts
blob: 6401980347c65ba6427e6975e32eb6bd23fe79e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, unitTest } from "./test_util.ts";

// deno-lint-ignore no-explicit-any
function testFirstArgument(arg1: any[], expectedSize: number): void {
  const file = new File(arg1, "name");
  assert(file instanceof File);
  assertEquals(file.name, "name");
  assertEquals(file.size, expectedSize);
  assertEquals(file.type, "");
}

unitTest(function fileEmptyFileBits(): void {
  testFirstArgument([], 0);
});

unitTest(function fileStringFileBits(): void {
  testFirstArgument(["bits"], 4);
});

unitTest(function fileUnicodeStringFileBits(): void {
  testFirstArgument(["𝓽𝓮𝔁𝓽"], 16);
});

unitTest(function fileStringObjectFileBits(): void {
  testFirstArgument([new String("string object")], 13);
});

unitTest(function fileEmptyBlobFileBits(): void {
  testFirstArgument([new Blob()], 0);
});

unitTest(function fileBlobFileBits(): void {
  testFirstArgument([new Blob(["bits"])], 4);
});

unitTest(function fileEmptyFileFileBits(): void {
  testFirstArgument([new File([], "world.txt")], 0);
});

unitTest(function fileFileFileBits(): void {
  testFirstArgument([new File(["bits"], "world.txt")], 4);
});

unitTest(function fileArrayBufferFileBits(): void {
  testFirstArgument([new ArrayBuffer(8)], 8);
});

unitTest(function fileTypedArrayFileBits(): void {
  testFirstArgument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4);
});

unitTest(function fileVariousFileBits(): void {
  testFirstArgument(
    [
      "bits",
      new Blob(["bits"]),
      new Blob(),
      new Uint8Array([0x50, 0x41]),
      new Uint16Array([0x5353]),
      new Uint32Array([0x53534150]),
    ],
    16,
  );
});

unitTest(function fileNumberInFileBits(): void {
  testFirstArgument([12], 2);
});

unitTest(function fileArrayInFileBits(): void {
  testFirstArgument([[1, 2, 3]], 5);
});

unitTest(function fileObjectInFileBits(): void {
  // "[object Object]"
  testFirstArgument([{}], 15);
});

// deno-lint-ignore no-explicit-any
function testSecondArgument(arg2: any, expectedFileName: string): void {
  const file = new File(["bits"], arg2);
  assert(file instanceof File);
  assertEquals(file.name, expectedFileName);
}

unitTest(function fileUsingFileName(): void {
  testSecondArgument("dummy", "dummy");
});

unitTest(function fileUsingSpecialCharacterInFileName(): void {
  testSecondArgument("dummy/foo", "dummy:foo");
});

unitTest(function fileUsingNullFileName(): void {
  testSecondArgument(null, "null");
});

unitTest(function fileUsingNumberFileName(): void {
  testSecondArgument(1, "1");
});

unitTest(function fileUsingEmptyStringFileName(): void {
  testSecondArgument("", "");
});

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();
});