Skip to content

Commit 1084e7c

Browse files
committed
test: implement common.log
Enable logs by setting the `DEBUG` environment variable, or by passing `--log` or `-l` to the script. Could probably be enhanced to send logs somewhere other than stdout, but I haven't found a need for that yet. Fixes: nodejs#9314
1 parent 06e09b6 commit 1084e7c

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

test/common/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ IP of `localhost`.
271271

272272
Array of IPV6 representations for `localhost`.
273273

274+
### log([data])
275+
* return [<Function>]
276+
277+
Configurable console logging.
278+
274279
### mustCall([fn][, exact])
275280
* `fn` [<Function>] default = () => {}
276281
* `exact` [<number>] default = 1

test/common/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ exports.rootDir = exports.isWindows ? 'c:\\' : '/';
6868

6969
exports.buildType = process.config.target_defaults.default_configuration;
7070

71+
if (process.env.DEBUG) {
72+
const { Console } = require('console');
73+
const logger = new Console(process.stdout, process.stderr);
74+
exports.log = logger.log;
75+
} else {
76+
exports.log = () => {};
77+
}
78+
7179
// If env var is set then enable async_hook hooks for all tests.
7280
if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
7381
const destroydIdsList = {};

test/parallel/test-buffer-new.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const common = require('../common');
44

5+
common.log('foo');
6+
57
common.expectsError(() => new Buffer(42, 'utf8'), {
68
code: 'ERR_INVALID_ARG_TYPE',
79
type: TypeError,

test/parallel/test-stream2-readable-empty-buffer-no-eof.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525

2626
const Readable = require('stream').Readable;
@@ -85,7 +85,7 @@ function test1() {
8585

8686
process.on('exit', function() {
8787
assert.deepStrictEqual(results, [ 'xxxxx', 'xxxxx', 'EOF' ]);
88-
console.log('ok');
88+
common.log('ok');
8989
});
9090
}
9191

@@ -113,6 +113,6 @@ function test2() {
113113

114114
process.on('exit', function() {
115115
assert.deepStrictEqual(results, [ 'eHh4', 'eHg=', 'EOF' ]);
116-
console.log('ok');
116+
common.log('ok');
117117
});
118118
}

tools/test.py

+5
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,8 @@ def BuildOptions():
13501350
result = optparse.OptionParser()
13511351
result.add_option("-m", "--mode", help="The test modes in which to run (comma-separated)",
13521352
default='release')
1353+
result.add_option("-l", "--log", help="Enable logging using common.log",
1354+
default=False, action="store_true")
13531355
result.add_option("-v", "--verbose", help="Verbose output",
13541356
default=False, action="store_true")
13551357
result.add_option('--logfile', dest='logfile',
@@ -1694,6 +1696,9 @@ def Main():
16941696
for rule in globally_unused_rules:
16951697
print "Rule for '%s' was not used." % '/'.join([str(s) for s in rule.path])
16961698

1699+
if options.log:
1700+
os.environ['DEBUG'] = 'true'
1701+
16971702
tempdir = os.environ.get('NODE_TEST_DIR') or options.temp_dir
16981703
if tempdir:
16991704
os.environ['NODE_TEST_DIR'] = tempdir

0 commit comments

Comments
 (0)