Skip to content

Commit

Permalink
test: implement common.log
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gibfahn committed Mar 9, 2018
1 parent 06e09b6 commit 1084e7c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ IP of `localhost`.

Array of IPV6 representations for `localhost`.

### log([data])
* return [<Function>]

Configurable console logging.

### mustCall([fn][, exact])
* `fn` [<Function>] default = () => {}
* `exact` [<number>] default = 1
Expand Down
8 changes: 8 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ exports.rootDir = exports.isWindows ? 'c:\\' : '/';

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

if (process.env.DEBUG) {
const { Console } = require('console');
const logger = new Console(process.stdout, process.stderr);
exports.log = logger.log;
} else {
exports.log = () => {};
}

// If env var is set then enable async_hook hooks for all tests.
if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
const destroydIdsList = {};
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-buffer-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

common.log('foo');

common.expectsError(() => new Buffer(42, 'utf8'), {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-stream2-readable-empty-buffer-no-eof.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

const Readable = require('stream').Readable;
Expand Down Expand Up @@ -85,7 +85,7 @@ function test1() {

process.on('exit', function() {
assert.deepStrictEqual(results, [ 'xxxxx', 'xxxxx', 'EOF' ]);
console.log('ok');
common.log('ok');
});
}

Expand Down Expand Up @@ -113,6 +113,6 @@ function test2() {

process.on('exit', function() {
assert.deepStrictEqual(results, [ 'eHh4', 'eHg=', 'EOF' ]);
console.log('ok');
common.log('ok');
});
}
5 changes: 5 additions & 0 deletions tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,8 @@ def BuildOptions():
result = optparse.OptionParser()
result.add_option("-m", "--mode", help="The test modes in which to run (comma-separated)",
default='release')
result.add_option("-l", "--log", help="Enable logging using common.log",
default=False, action="store_true")
result.add_option("-v", "--verbose", help="Verbose output",
default=False, action="store_true")
result.add_option('--logfile', dest='logfile',
Expand Down Expand Up @@ -1694,6 +1696,9 @@ def Main():
for rule in globally_unused_rules:
print "Rule for '%s' was not used." % '/'.join([str(s) for s in rule.path])

if options.log:
os.environ['DEBUG'] = 'true'

tempdir = os.environ.get('NODE_TEST_DIR') or options.temp_dir
if tempdir:
os.environ['NODE_TEST_DIR'] = tempdir
Expand Down

0 comments on commit 1084e7c

Please sign in to comment.