1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const path = require('path'); 26const fs = require('fs'); 27 28const tmpdir = require('../common/tmpdir'); 29 30const file = path.join(tmpdir.path, 'write.txt'); 31 32tmpdir.refresh(); 33 34{ 35 const stream = fs.WriteStream(file); 36 const _fs_close = fs.close; 37 38 fs.close = function(fd) { 39 assert.ok(fd, 'fs.close must not be called without an undefined fd.'); 40 fs.close = _fs_close; 41 fs.closeSync(fd); 42 }; 43 stream.destroy(); 44} 45 46{ 47 const stream = fs.createWriteStream(file); 48 49 stream.on('drain', function() { 50 assert.fail('\'drain\' event must not be emitted before ' + 51 'stream.write() has been called at least once.'); 52 }); 53 stream.destroy(); 54} 55 56// Throws if data is not of type Buffer. 57{ 58 const stream = fs.createWriteStream(file); 59 stream.on('error', common.mustNotCall()); 60 assert.throws(() => { 61 stream.write(42); 62 }, { 63 code: 'ERR_INVALID_ARG_TYPE', 64 name: 'TypeError' 65 }); 66 stream.destroy(); 67} 68