deno.land / std@0.157.0 / node / _fs / _fs_read_test.ts

_fs_read_test.ts
نووسراو ببینە
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertMatch, assertStrictEquals,} from "../../testing/asserts.ts";import { read, readSync } from "./_fs_read.ts";import { open, openSync } from "./_fs_open.ts";import { Buffer } from "../buffer.ts";import * as path from "../../path/mod.ts";import { closeSync } from "./_fs_close.ts";
async function readTest( testData: string, buffer: Buffer | Uint8Array, offset: number, length: number, position: number | null = null, expected: ( fd: number, bytesRead: number | null, data: Buffer | undefined, ) => void,) { let fd1 = 0; await new Promise<{ fd: number; bytesRead: number | null; data: Buffer | undefined; }>((resolve, reject) => { open(testData, "r", (err, fd) => { if (err) reject(err); read(fd, buffer, offset, length, position, (err, bytesRead, data) => { if (err) reject(err); resolve({ fd, bytesRead, data }); }); }); }) .then(({ fd, bytesRead, data }) => { fd1 = fd; expected(fd, bytesRead, data); }) .finally(() => closeSync(fd1));}
Deno.test({ name: "readSuccess", async fn() { const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); const buf = Buffer.alloc(1024); await readTest( testData, buf, buf.byteOffset, buf.byteLength, null, (_fd, bytesRead, data) => { assertStrictEquals(bytesRead, 11); assertEquals(data instanceof Buffer, true); assertMatch((data as Buffer).toString(), /hello world/); }, ); },});
Deno.test({ name: "[std/node/fs] Read only five bytes, so that the position moves to five", async fn() { const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); const buf = Buffer.alloc(5); await readTest( testData, buf, buf.byteOffset, 5, null, (_fd, bytesRead, data) => { assertStrictEquals(bytesRead, 5); assertEquals(data instanceof Buffer, true); assertEquals((data as Buffer).toString(), "hello"); }, ); },});
Deno.test({ name: "[std/node/fs] position option of fs.read() specifies where to begin reading from in the file", async fn() { const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); const fd = openSync(testData); const buf = Buffer.alloc(5); const positions = [6, 0, -1, null]; const expected = [ [119, 111, 114, 108, 100], [104, 101, 108, 108, 111], [104, 101, 108, 108, 111], [32, 119, 111, 114, 108], ]; for (const [i, position] of positions.entries()) { await new Promise((resolve) => { read( fd, { buffer: buf, offset: buf.byteOffset, length: buf.byteLength, position, }, (err, bytesRead, data) => { assertEquals(err, null); assertStrictEquals(bytesRead, 5); assertEquals( data, Buffer.from(expected[i]), ); return resolve(true); }, ); }); } closeSync(fd); },});
Deno.test({ name: "[std/node/fs] Read fs.read(fd, options, cb) signature", async fn() { const file = Deno.makeTempFileSync(); Deno.writeTextFileSync(file, "hi there"); const fd = openSync(file, "r+"); const buf = Buffer.alloc(11); await read( fd, { buffer: buf, offset: buf.byteOffset, length: buf.byteLength, position: null, }, (err, bytesRead, data) => { assertEquals(err, null); assertStrictEquals(bytesRead, 8); assertEquals( data, Buffer.from([104, 105, 32, 116, 104, 101, 114, 101, 0, 0, 0]), ); }, ); closeSync(fd); },});
Deno.test({ name: "[std/node/fs] Read fs.read(fd, cb) signature", async fn() { const file = Deno.makeTempFileSync(); Deno.writeTextFileSync(file, "hi deno"); const fd = openSync(file, "r+"); await read(fd, (err, bytesRead, data) => { assertEquals(err, null); assertStrictEquals(bytesRead, 7); assertStrictEquals(data?.byteLength, 16384); }); closeSync(fd); },});
Deno.test({ name: "SYNC: readSuccess", fn() { const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); const buffer = Buffer.alloc(1024); const fd = openSync(testData); const bytesRead = readSync( fd, buffer, buffer.byteOffset, buffer.byteLength, null, ); assertStrictEquals(bytesRead, 11); closeSync(fd); },});
Deno.test({ name: "[std/node/fs] Read only two bytes, so that the position moves to two", fn() { const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); const buffer = Buffer.alloc(2); const fd = openSync(testData); const bytesRead = readSync(fd, buffer, buffer.byteOffset, 2, null); assertStrictEquals(bytesRead, 2); closeSync(fd); },});
Deno.test({ name: "[std/node/fs] position option of fs.readSync() specifies where to begin reading from in the file", fn() { const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); const fd = openSync(testData); const buf = Buffer.alloc(5); const positions = [6, 0, -1, null]; const expected = [ [119, 111, 114, 108, 100], [104, 101, 108, 108, 111], [104, 101, 108, 108, 111], [32, 119, 111, 114, 108], ]; for (const [i, position] of positions.entries()) { const bytesRead = readSync( fd, buf, buf.byteOffset, buf.byteLength, position, ); assertStrictEquals(bytesRead, 5); assertEquals( buf, Buffer.from(expected[i]), ); } closeSync(fd); },});
Deno.test({ name: "[std/node/fs] Read fs.readSync(fd, buffer[, options]) signature", fn() { const file = Deno.makeTempFileSync(); Deno.writeTextFileSync(file, "hello deno"); const buffer = Buffer.alloc(1024); const fd = openSync(file, "r+"); const bytesRead = readSync(fd, buffer, { length: buffer.byteLength, offset: buffer.byteOffset, position: null, }); assertStrictEquals(bytesRead, 10); closeSync(fd); },});
std

Version Info

Tagged at
a year ago