0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test/parallel/test-fs-readfile-fd.js
Jamie Davis dd2200ecf8
test: add test description to fs.readFile tests
PR-URL: https://github.com/nodejs/node/pull/17610
Refs: https://github.com/nodejs/node/pull/17054#discussion_r155406755
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2017-12-12 12:37:18 -02:00

49 lines
949 B
JavaScript

'use strict';
require('../common');
// Test fs.readFile using a file descriptor.
const fixtures = require('../common/fixtures');
const assert = require('assert');
const fs = require('fs');
const fn = fixtures.path('empty.txt');
tempFd(function(fd, close) {
fs.readFile(fd, function(err, data) {
assert.ok(data);
close();
});
});
tempFd(function(fd, close) {
fs.readFile(fd, 'utf8', function(err, data) {
assert.strictEqual('', data);
close();
});
});
tempFdSync(function(fd) {
assert.ok(fs.readFileSync(fd));
});
tempFdSync(function(fd) {
assert.strictEqual('', fs.readFileSync(fd, 'utf8'));
});
function tempFd(callback) {
fs.open(fn, 'r', function(err, fd) {
assert.ifError(err);
callback(fd, function() {
fs.close(fd, function(err) {
assert.ifError(err);
});
});
});
}
function tempFdSync(callback) {
const fd = fs.openSync(fn, 'r');
callback(fd);
fs.closeSync(fd);
}