console: check that stderr is writable

`Console` constructor checks that `stdout.write()` is a function but
does not do an equivalent check for `stderr.write()`. If `stderr` is not
specified in the constructor, then `stderr` is set to be `stdout`.
However, if `stderr` is specified, but `stderr.write()` is not a
function, then an exception is not thrown until `console.error()` is
called.

This change adds the same check for 'stderr' in the constructor that is
there for `stdout`. If `stderr` fails the check, then a `TypeError` is
thrown.

Took the opportunity to copyedit the `console` doc a little too.

PR-URL: https://github.com/nodejs/node/pull/5635
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
This commit is contained in:
Rich Trott committed 2016-03-15 19:10:55 -07:00
1 parent 2a0b3daa4c
commit 6ba5af2c2c
3 files changed
+20 -10

No files matched your search

+13 -6
View File
@@ -1,10 +1,13 @@
'use strict';
require('../common');
var assert = require('assert');
var Stream = require('stream');
var Console = require('console').Console;
const assert = require('assert');
const Stream = require('stream');
const Console = require('console').Console;
var called = false;
const out = new Stream();
const err = new Stream();
// ensure the Console instance doesn't write to the
// process' "stdout" or "stderr" streams
process.stdout.write = process.stderr.write = function() {
@@ -20,9 +23,13 @@ assert.throws(function() {
new Console();
}, /Console expects a writable stream/);
var out = new Stream();
var err = new Stream();
out.writable = err.writable = true;
// Console constructor should throw if stderr exists but is not writable
assert.throws(function() {
out.write = function() {};
err.write = undefined;
new Console(out, err);
}, /Console expects writable stream instances/);
out.write = err.write = function(d) {};
var c = new Console(out, err);