From 41415f4d4e9070dad4a23911a240937f2163352d Mon Sep 17 00:00:00 2001 From: Julian Duque Date: Thu, 30 Apr 2020 22:25:55 -0400 Subject: [PATCH] test: add tests for options.fs in fs streams --- test/parallel/test-fs-stream-fs-options.js | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 test/parallel/test-fs-stream-fs-options.js diff --git a/test/parallel/test-fs-stream-fs-options.js b/test/parallel/test-fs-stream-fs-options.js new file mode 100644 index 00000000000000..65144c0d617e9b --- /dev/null +++ b/test/parallel/test-fs-stream-fs-options.js @@ -0,0 +1,73 @@ +'use strict'; + +require('../common'); +const fixtures = require('../common/fixtures'); +const path = require('path'); +const fs = require('fs'); +const assert = require('assert'); + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const streamOpts = ['open', 'close']; +const writeStreamOptions = [...streamOpts, 'write']; +const readStreamOptions = [...streamOpts, 'read']; +const originalFs = { fs }; + +{ + const file = path.join(tmpdir.path, 'write-end-test0.txt'); + + writeStreamOptions.forEach((fn) => { + const overrideFs = Object.assign({}, originalFs.fs, { [fn]: null }); + if (fn === 'write') overrideFs.writev = null; + + const opts = { + fs: overrideFs + }; + assert.throws( + () => fs.createWriteStream(file, opts), { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: `The "options.fs.${fn}" property must be of type function. ` + + 'Received null' + }, + `createWriteStream options.fs.${fn} should throw if isn't a function` + ); + }); +} + +{ + const file = path.join(tmpdir.path, 'write-end-test0.txt'); + const overrideFs = Object.assign({}, originalFs.fs, { writev: 'not a fn' }); + const opts = { + fs: overrideFs + }; + assert.throws( + () => fs.createWriteStream(file, opts), { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "options.fs.writev" property must be of type function. ' + + 'Received type string (\'not a fn\')' + }, + 'createWriteStream options.fs.writev should throw if isn\'t a function' + ); +} + +{ + const file = fixtures.path('x.txt'); + readStreamOptions.forEach((fn) => { + const overrideFs = Object.assign({}, originalFs.fs, { [fn]: null }); + const opts = { + fs: overrideFs + }; + assert.throws( + () => fs.createReadStream(file, opts), { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: `The "options.fs.${fn}" property must be of type function. ` + + 'Received null' + }, + `createReadStream options.fs.${fn} should throw if isn't a function` + ); + }); +}