forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-fs-read-offset-null.js
More file actions
67 lines (58 loc) · 1.59 KB
/
test-fs-read-offset-null.js
File metadata and controls
67 lines (58 loc) · 1.59 KB
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
'use strict';
// Test to assert the desired functioning of fs.read
// when {offset:null} is passed as options parameter
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const fsPromises = fs.promises;
const fixtures = require('../common/fixtures');
const filepath = fixtures.path('x.txt');
const buf = Buffer.alloc(1);
// Reading only one character, hence buffer of one byte is enough.
// Test for callback API.
fs.open(filepath, 'r', (_, fd) => {
assert.throws(
() => fs.read(fd, { offset: null, buffer: buf }, () => {}),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);
assert.throws(
() => fs.read(fd, buf, { offset: null }, () => {}),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);
fs.close(fd, common.mustSucceed(() => {}));
});
let filehandle = null;
// Test for promise api
(async () => {
filehandle = await fsPromises.open(filepath, 'r');
assert.rejects(
async () => filehandle.read(buf, { offset: null }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);
})()
.then(common.mustCall())
.finally(async () => {
if (filehandle)
await filehandle.close();
});
(async () => {
filehandle = await fsPromises.open(filepath, 'r');
// In this test, null is interpreted as default options, not null offset.
// 120 is the ascii code of letter x.
const readObject = await filehandle.read(buf, null);
assert.strictEqual(readObject.buffer[0], 120);
})()
.then(common.mustCall())
.finally(async () => {
if (filehandle)
await filehandle.close();
});