From c884f24a77e2010d39f4c172432fb48fb66c3adc Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:08:28 -0700 Subject: [PATCH 01/10] test: improve test-vm-syntax-error-stderr --- test/parallel/test-vm-syntax-error-stderr.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/parallel/test-vm-syntax-error-stderr.js b/test/parallel/test-vm-syntax-error-stderr.js index 805bea0f874065..015a187489531e 100644 --- a/test/parallel/test-vm-syntax-error-stderr.js +++ b/test/parallel/test-vm-syntax-error-stderr.js @@ -12,18 +12,14 @@ const p = child_process.spawn(process.execPath, [ wrong_script ]); -p.stdout.on('data', function(data) { - assert.fail(`Unexpected stdout data: ${data}`); -}); +p.stdout.on('data', common.mustNotCall); let output = ''; -p.stderr.on('data', function(data) { - output += data; -}); +p.stderr.on('data', (data) => output += data); -process.on('exit', function() { +p.stderr.on('end', common.mustCall(() => { assert(/BEGIN CERT/.test(output)); assert(/^\s+\^/m.test(output)); assert(/Invalid left-hand side expression in prefix operation/.test(output)); -}); +})); From ae2d714dee0f3b7a8a67e4f09b92d0bcee44e13e Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:13:42 -0700 Subject: [PATCH 02/10] test: improve test-vm-run-in-new-context --- test/parallel/test-vm-syntax-error-message.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-vm-syntax-error-message.js b/test/parallel/test-vm-syntax-error-message.js index 89589c5cc9d881..72fdfafa163884 100644 --- a/test/parallel/test-vm-syntax-error-message.js +++ b/test/parallel/test-vm-syntax-error-message.js @@ -1,5 +1,5 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const child_process = require('child_process'); @@ -11,16 +11,12 @@ const p = child_process.spawn(process.execPath, [ 'catch (e) { console.log(e.message); }' ]); -p.stderr.on('data', function(data) { - assert.fail(`Unexpected stderr data: ${data}`); -}); +p.stderr.on('data', common.mustNotCall); let output = ''; -p.stdout.on('data', function(data) { - output += data; -}); +p.stdout.on('data', (data) => output += data); -process.on('exit', function() { +p.stdout.on('end', common.mustCall(() => { assert.strictEqual(output.replace(/[\r\n]+/g, ''), 'boo'); -}); +})); From 8e1ef69a1aaac78c172c19cc95524c16ba789542 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:13:56 -0700 Subject: [PATCH 03/10] test: improve test-vm-run-in-new-context --- test/parallel/test-vm-run-in-new-context.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-vm-run-in-new-context.js b/test/parallel/test-vm-run-in-new-context.js index 0da73ea6a90726..6157b2445dfccc 100644 --- a/test/parallel/test-vm-run-in-new-context.js +++ b/test/parallel/test-vm-run-in-new-context.js @@ -31,12 +31,12 @@ assert.strictEqual(typeof global.gc, 'function', common.globalCheck = false; -console.error('run a string'); +// Run a string const result = vm.runInNewContext('\'passed\';'); assert.strictEqual('passed', result); -console.error('thrown error'); -assert.throws(function() { +// Thrown error +assert.throws(() => { vm.runInNewContext('throw new Error(\'test\');'); }, /^Error: test$/); @@ -45,7 +45,7 @@ vm.runInNewContext('hello = 2'); assert.strictEqual(5, global.hello); -console.error('pass values in and out'); +// Pass values in and out global.code = 'foo = 1;' + 'bar = 2;' + 'if (baz !== 3) throw new Error(\'test fail\');'; @@ -58,17 +58,17 @@ assert.strictEqual(1, global.obj.foo); assert.strictEqual(2, global.obj.bar); assert.strictEqual(2, global.foo); -console.error('call a function by reference'); +// Call a function by reference function changeFoo() { global.foo = 100; } vm.runInNewContext('f()', { f: changeFoo }); assert.strictEqual(global.foo, 100); -console.error('modify an object by reference'); +// Modify an object by reference const f = { a: 1 }; vm.runInNewContext('f.a = 2', { f: f }); assert.strictEqual(f.a, 2); -console.error('use function in context without referencing context'); +// Use function in context without referencing context const fn = vm.runInNewContext('(function() { obj.p = {}; })', { obj: {} }); global.gc(); fn(); From fea0067700f9e48e1928fee2eccd835a5e0e1654 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:16:18 -0700 Subject: [PATCH 04/10] test: improve test-vm-new-script-this-context --- test/parallel/test-vm-new-script-this-context.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-vm-new-script-this-context.js b/test/parallel/test-vm-new-script-this-context.js index dfa9b1130b1f55..6da47e67249c2b 100644 --- a/test/parallel/test-vm-new-script-this-context.js +++ b/test/parallel/test-vm-new-script-this-context.js @@ -26,14 +26,14 @@ const Script = require('vm').Script; common.globalCheck = false; -console.error('run a string'); +// Run a string let script = new Script('\'passed\';'); const result = script.runInThisContext(script); assert.strictEqual('passed', result); -console.error('thrown error'); +// Thrown error script = new Script('throw new Error(\'test\');'); -assert.throws(function() { +assert.throws(() => { script.runInThisContext(script); }, /^Error: test$/); @@ -43,7 +43,7 @@ script.runInThisContext(script); assert.strictEqual(2, global.hello); -console.error('pass values'); +// Pass values global.code = 'foo = 1;' + 'bar = 2;' + 'if (typeof baz !== "undefined") throw new Error("test fail");'; @@ -55,7 +55,7 @@ assert.strictEqual(0, global.obj.foo); assert.strictEqual(2, global.bar); assert.strictEqual(1, global.foo); -console.error('call a function'); +// Call a function global.f = function() { global.foo = 100; }; script = new Script('f()'); script.runInThisContext(script); From 29e257672e1b351c80b030fb58dd4527202e56da Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:18:20 -0700 Subject: [PATCH 05/10] test: improve test-vm-new-script-new-context --- test/parallel/test-vm-new-script-new-context.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-vm-new-script-new-context.js b/test/parallel/test-vm-new-script-new-context.js index 045c37ce335199..7015499879959f 100644 --- a/test/parallel/test-vm-new-script-new-context.js +++ b/test/parallel/test-vm-new-script-new-context.js @@ -36,14 +36,14 @@ const Script = require('vm').Script; { const script = new Script('throw new Error(\'test\');'); - assert.throws(function() { + assert.throws(() => { script.runInNewContext(); }, /^Error: test$/); } { const script = new Script('foo.bar = 5;'); - assert.throws(function() { + assert.throws(() => { script.runInNewContext(); }, /^ReferenceError: foo is not defined$/); } @@ -94,14 +94,14 @@ const Script = require('vm').Script; script.runInNewContext({ f: f }); assert.strictEqual(f.a, 2); - assert.throws(function() { + assert.throws(() => { script.runInNewContext(); }, /^ReferenceError: f is not defined$/); } { const script = new Script(''); - assert.throws(function() { + assert.throws(() => { script.runInNewContext.call('\'hello\';'); }, /^TypeError: this\.runInContext is not a function$/); } From b8f2d889843b19d4e6705f59b5195ea15e51217b Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:21:12 -0700 Subject: [PATCH 06/10] test: improve test-vm-function-declaration --- test/parallel/test-vm-function-declaration.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/parallel/test-vm-function-declaration.js b/test/parallel/test-vm-function-declaration.js index d42a3a3f48058a..875ecef22a7851 100644 --- a/test/parallel/test-vm-function-declaration.js +++ b/test/parallel/test-vm-function-declaration.js @@ -42,5 +42,3 @@ assert.strictEqual(res.name, 'b', 'res should be named b'); assert.strictEqual(typeof o.a, 'function', 'a should be function'); assert.strictEqual(typeof o.b, 'function', 'b should be function'); assert.strictEqual(res, o.b, 'result should be global b function'); - -console.log('ok'); From 684c01ff2c278db5a71f87b14f7cac14c90766d8 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:23:46 -0700 Subject: [PATCH 07/10] test: test-vm-create-and-run-in-context --- test/parallel/test-vm-create-and-run-in-context.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-vm-create-and-run-in-context.js b/test/parallel/test-vm-create-and-run-in-context.js index 1adf85460d2aa9..09f643aa90df46 100644 --- a/test/parallel/test-vm-create-and-run-in-context.js +++ b/test/parallel/test-vm-create-and-run-in-context.js @@ -26,23 +26,23 @@ const assert = require('assert'); const vm = require('vm'); -console.error('run in a new empty context'); +// Run in a new empty context let context = vm.createContext(); let result = vm.runInContext('"passed";', context); assert.strictEqual('passed', result); -console.error('create a new pre-populated context'); +// Create a new pre-populated context context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' }); assert.strictEqual('bar', context.foo); assert.strictEqual('lala', context.thing); -console.error('test updating context'); +// Test updating context result = vm.runInContext('var foo = 3;', context); assert.strictEqual(3, context.foo); assert.strictEqual('lala', context.thing); // https://github.com/nodejs/node/issues/5768 -console.error('run in contextified sandbox without referencing the context'); +// Run in contextified sandbox without referencing the context const sandbox = { x: 1 }; vm.createContext(sandbox); global.gc(); From 1556d3b52b98492e67341509124cb52197cbb363 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:25:38 -0700 Subject: [PATCH 08/10] test: improve test-vm-context --- test/parallel/test-vm-context.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/parallel/test-vm-context.js b/test/parallel/test-vm-context.js index 8d47cb52ab994a..7e5404796e7533 100644 --- a/test/parallel/test-vm-context.js +++ b/test/parallel/test-vm-context.js @@ -27,29 +27,29 @@ const vm = require('vm'); const Script = vm.Script; let script = new Script('"passed";'); -console.error('run in a new empty context'); +// Run in a new empty context let context = vm.createContext(); let result = script.runInContext(context); assert.strictEqual('passed', result); -console.error('create a new pre-populated context'); +// Create a new pre-populated context context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' }); assert.strictEqual('bar', context.foo); assert.strictEqual('lala', context.thing); -console.error('test updating context'); +// Test updating context script = new Script('foo = 3;'); result = script.runInContext(context); assert.strictEqual(3, context.foo); assert.strictEqual('lala', context.thing); // Issue GH-227: -assert.throws(function() { +assert.throws(() => { vm.runInNewContext('', null, 'some.js'); }, /^TypeError: sandbox must be an object$/); // Issue GH-1140: -console.error('test runInContext signature'); +// Test runInContext signature let gh1140Exception; try { vm.runInContext('throw new Error()', context, 'expected-filename.js'); @@ -77,7 +77,7 @@ const contextifiedSandboxErrorMsg = }); // Issue GH-693: -console.error('test RegExp as argument to assert.throws'); +// Test RegExp as argument to assert.throws script = vm.createScript('const assert = require(\'assert\'); assert.throws(' + 'function() { throw "hello world"; }, /hello/);', 'some.js'); @@ -92,13 +92,13 @@ assert.strictEqual(script.runInContext(ctx), false); // Error on the first line of a module should // have the correct line and column number -assert.throws(function() { +assert.throws(() => { vm.runInContext('throw new Error()', context, { filename: 'expected-filename.js', lineOffset: 32, columnOffset: 123 }); -}, function(err) { +}, (err) => { return /expected-filename\.js:33:130/.test(err.stack); }, 'Expected appearance of proper offset in Error stack'); From 716a19c1dc475c83c1ad994ae0461f848c470a35 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 24 Jul 2017 12:27:46 -0700 Subject: [PATCH 09/10] test: improve test-vm-context-async-script --- test/parallel/test-vm-context-async-script.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-vm-context-async-script.js b/test/parallel/test-vm-context-async-script.js index 87b6f501111a7c..879315e37bb3e0 100644 --- a/test/parallel/test-vm-context-async-script.js +++ b/test/parallel/test-vm-context-async-script.js @@ -20,16 +20,16 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const vm = require('vm'); -const sandbox = { setTimeout: setTimeout }; +const sandbox = { setTimeout }; const ctx = vm.createContext(sandbox); vm.runInContext('setTimeout(function() { x = 3; }, 0);', ctx); -setTimeout(function() { +setTimeout(common.mustCall(() => { assert.strictEqual(sandbox.x, 3); assert.strictEqual(ctx.x, 3); -}, 1); +}), 1); From 3fc4543023e0d8335e9bb60e6d19afff54adc23a Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 31 Jul 2017 12:01:32 -0700 Subject: [PATCH 10/10] [Squash] Address nits --- test/parallel/test-vm-syntax-error-message.js | 2 +- test/parallel/test-vm-syntax-error-stderr.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-vm-syntax-error-message.js b/test/parallel/test-vm-syntax-error-message.js index 72fdfafa163884..5a16239f5653e9 100644 --- a/test/parallel/test-vm-syntax-error-message.js +++ b/test/parallel/test-vm-syntax-error-message.js @@ -11,7 +11,7 @@ const p = child_process.spawn(process.execPath, [ 'catch (e) { console.log(e.message); }' ]); -p.stderr.on('data', common.mustNotCall); +p.stderr.on('data', common.mustNotCall()); let output = ''; diff --git a/test/parallel/test-vm-syntax-error-stderr.js b/test/parallel/test-vm-syntax-error-stderr.js index 015a187489531e..e40edc907c6639 100644 --- a/test/parallel/test-vm-syntax-error-stderr.js +++ b/test/parallel/test-vm-syntax-error-stderr.js @@ -12,7 +12,7 @@ const p = child_process.spawn(process.execPath, [ wrong_script ]); -p.stdout.on('data', common.mustNotCall); +p.stdout.on('data', common.mustNotCall()); let output = '';