Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
squash: fix up tests
  • Loading branch information
lholmquist committed Mar 30, 2020
commit c2bb51bd281941d947d29ccfdb743dbd9651e0b5
4 changes: 1 addition & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,7 @@ function readSync(fd, buffer, offset, length, position) {
// Assume fs.read(fd, buffer, options)
const options = offset || {};

offset = options.offset || 0;
length = options.length || buffer.length;
position = options.position;
({ offset = 0, length = buffer.length, position } = options);
}

validateBuffer(buffer);
Expand Down
29 changes: 11 additions & 18 deletions test/parallel/test-fs-readSync-optional-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,20 @@ const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');

const expected = Buffer.from('xyz\n');
const defaultBuffer1 = Buffer.allocUnsafe(expected.length);
const defaultBuffer2 = Buffer.allocUnsafe(expected.length);
const defaultBuffer3 = Buffer.allocUnsafe(expected.length);

function runTest(defaultBuffer, options) {
const result = fs.readSync(fd, defaultBuffer, options);
assert.strictEqual(result, expected.length);
assert.deepStrictEqual(defaultBuffer, expected);
}

// Test passing in an empty options object
const result1 = fs.readSync(fd, defaultBuffer1, { position: 0 });
assert.strictEqual(result1, expected.length);
assert.deepStrictEqual(defaultBuffer1, expected);
runTest(Buffer.allocUnsafe(expected.length), { position: 0 });

// Test not passing in any options object
const result2 = fs.readSync(fd, defaultBuffer2);
assert.strictEqual(result2, expected.length);
assert.deepStrictEqual(defaultBuffer2, expected);

runTest(Buffer.allocUnsafe(expected.length));

// Test passing in options
const result3 = fs.readSync(fd, defaultBuffer3, {
offset: 0,
length: defaultBuffer3.length,
position: 0
});

assert.strictEqual(result3, expected.length);
assert.deepStrictEqual(defaultBuffer3, expected);
runTest(Buffer.allocUnsafe(expected.length), { offset: 0,
length: expected.length,
position: 0 });