forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
console: implement minimal
console.group()
Node.js exposes `console.group()` and `console.groupEnd()` via the inspector. These functions have no apparent effect when called from Node.js without the inspector. We cannot easily hide them when Node.js is started without the inspector because we support opening the inspector during runtime via `inspector.port()`. Implement a minimal `console.group()`/`console.groupEnd()`. More sophisticated implementations are possible, but they can be done in userland and/or features can be added to this at a later time. (It lacks the `label` argument to `console.group()` right now, for example. How to handle `label`, or even whether to handle it, may become a bikeshed discussion. Landing a minimal implementation first avoids the pitfall of that discussion or a similar discussion delaying the implementation indefinitely.) Refs: nodejs#12675 Fixes: nodejs#1716
- Loading branch information
Showing
3 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
const assert = require('assert'); | ||
const Console = require('console').Console; | ||
|
||
let stdout, stderr; | ||
|
||
function setup() { | ||
stdout = ''; | ||
common.hijackStdout(function(data) { | ||
stdout += data; | ||
}); | ||
|
||
stderr = ''; | ||
common.hijackStderr(function(data) { | ||
stderr += data; | ||
}); | ||
} | ||
|
||
function teardown() { | ||
common.restoreStdout(); | ||
common.restoreStderr(); | ||
} | ||
|
||
{ | ||
setup(); | ||
const expectedOut = 'This is the outer level\n' + | ||
' Level 2\n' + | ||
' Level 3\n' + | ||
' Back to level 2\n' + | ||
'Back to the outer level\n' + | ||
'Still at the outer level\n'; | ||
|
||
|
||
const expectedErr = ' More of level 3\n'; | ||
|
||
console.log('This is the outer level'); | ||
console.group(); | ||
console.log('Level 2'); | ||
console.group(); | ||
console.log('Level 3'); | ||
console.warn('More of level 3'); | ||
console.groupEnd(); | ||
console.log('Back to level 2'); | ||
console.groupEnd(); | ||
console.log('Back to the outer level'); | ||
console.groupEnd(); | ||
console.log('Still at the outer level'); | ||
|
||
assert.strictEqual(stdout, expectedOut); | ||
assert.strictEqual(stderr, expectedErr); | ||
teardown(); | ||
} | ||
|
||
// Group indentation is tracked per Console instance. | ||
{ | ||
setup(); | ||
const expectedOut = 'No indentation\n' + | ||
'None here either\n' + | ||
' Now the first console is indenting\n' + | ||
'But the second one does not\n'; | ||
const expectedErr = ''; | ||
|
||
const c1 = new Console(process.stdout, process.stderr); | ||
const c2 = new Console(process.stdout, process.stderr); | ||
c1.log('No indentation'); | ||
c2.log('None here either'); | ||
c1.group(); | ||
c1.log('Now the first console is indenting'); | ||
c2.log('But the second one does not'); | ||
|
||
assert.strictEqual(stdout, expectedOut); | ||
assert.strictEqual(stderr, expectedErr); | ||
teardown(); | ||
} |