node/test/parallel/test-fs-promises-file-handle-write.js
cjihrig 975f6c1f70
fs: move fs/promises to fs.promises
PR-URL: https://github.com/nodejs/node/pull/20504
Refs: https://github.com/nodejs/TSC/issues/389
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
2018-05-07 19:47:42 -04:00

53 lines
1.7 KiB
JavaScript

'use strict';
const common = require('../common');
// The following tests validate base functionality for the fs.promises
// FileHandle.read method.
const fs = require('fs');
const { open } = fs.promises;
const path = require('path');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;
tmpdir.refresh();
common.crashOnUnhandledRejection();
async function validateWrite() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}
async function validateEmptyWrite() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-empty-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from(''); // empty buffer
await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(buffer, readFileData);
}
async function validateNonUint8ArrayWrite() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-data-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Hello world', 'utf8').toString('base64');
await fileHandle.write(buffer, 0, buffer.length);
const readFileData = fs.readFileSync(filePathForHandle);
assert.deepStrictEqual(Buffer.from(buffer, 'utf8'), readFileData);
}
Promise.all([
validateWrite(),
validateEmptyWrite(),
validateNonUint8ArrayWrite()
]).then(common.mustCall());