2020-11-01 16:50:07 +00:00
|
|
|
'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);
|
2020-11-17 19:57:33 +00:00
|
|
|
// Reading only one character, hence buffer of one byte is enough.
|
2020-11-01 16:50:07 +00:00
|
|
|
|
2022-05-02 18:01:27 +00:00
|
|
|
// Tests are done by making sure the first letter in buffer is
|
|
|
|
// same as first letter in file.
|
|
|
|
// 120 is the ascii code of letter x.
|
|
|
|
|
|
|
|
// Tests for callback API.
|
2020-11-01 16:50:07 +00:00
|
|
|
fs.open(filepath, 'r', common.mustSucceed((fd) => {
|
|
|
|
fs.read(fd, { offset: null, buffer: buf },
|
|
|
|
common.mustSucceed((bytesRead, buffer) => {
|
2022-05-02 18:01:27 +00:00
|
|
|
assert.strictEqual(buffer[0], 120);
|
|
|
|
fs.close(fd, common.mustSucceed(() => {}));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
fs.open(filepath, 'r', common.mustSucceed((fd) => {
|
|
|
|
fs.read(fd, buf, { offset: null },
|
|
|
|
common.mustSucceed((bytesRead, buffer) => {
|
2020-11-17 19:57:33 +00:00
|
|
|
assert.strictEqual(buffer[0], 120);
|
2020-11-01 16:50:07 +00:00
|
|
|
fs.close(fd, common.mustSucceed(() => {}));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
let filehandle = null;
|
|
|
|
|
2022-05-02 18:01:27 +00:00
|
|
|
// Tests for promises api
|
|
|
|
(async () => {
|
|
|
|
filehandle = await fsPromises.open(filepath, 'r');
|
|
|
|
const readObject = await filehandle.read(buf, { offset: null });
|
|
|
|
assert.strictEqual(readObject.buffer[0], 120);
|
|
|
|
})()
|
|
|
|
.finally(() => filehandle?.close())
|
|
|
|
.then(common.mustCall());
|
|
|
|
|
|
|
|
// Undocumented: omitted position works the same as position === null
|
2020-11-01 16:50:07 +00:00
|
|
|
(async () => {
|
|
|
|
filehandle = await fsPromises.open(filepath, 'r');
|
|
|
|
const readObject = await filehandle.read(buf, null, buf.length);
|
|
|
|
assert.strictEqual(readObject.buffer[0], 120);
|
|
|
|
})()
|
2022-05-02 18:01:27 +00:00
|
|
|
.finally(() => filehandle?.close())
|
|
|
|
.then(common.mustCall());
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
filehandle = await fsPromises.open(filepath, 'r');
|
|
|
|
const readObject = await filehandle.read(buf, null, buf.length, 0);
|
|
|
|
assert.strictEqual(readObject.buffer[0], 120);
|
|
|
|
})()
|
|
|
|
.finally(() => filehandle?.close())
|
|
|
|
.then(common.mustCall());
|