Skip to content

Commit f35f06d

Browse files
jasnellMylesBorins
authored andcommitted
test: improve multiple vm tests
PR-URL: #14458 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent f8fbac7 commit f35f06d

9 files changed

+46
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const vm = require('vm');
55

6-
const sandbox = { setTimeout: setTimeout };
6+
const sandbox = { setTimeout };
77

88
const ctx = vm.createContext(sandbox);
99

1010
vm.runInContext('setTimeout(function() { x = 3; }, 0);', ctx);
11-
setTimeout(function() {
11+
setTimeout(common.mustCall(() => {
1212
assert.strictEqual(sandbox.x, 3);
1313
assert.strictEqual(ctx.x, 3);
14-
}, 1);
14+
}), 1);

test/parallel/test-vm-context.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ const vm = require('vm');
66
const Script = vm.Script;
77
let script = new Script('"passed";');
88

9-
console.error('run in a new empty context');
9+
// Run in a new empty context
1010
let context = vm.createContext();
1111
let result = script.runInContext(context);
1212
assert.strictEqual('passed', result);
1313

14-
console.error('create a new pre-populated context');
15-
context = vm.createContext({'foo': 'bar', 'thing': 'lala'});
14+
// Create a new pre-populated context
15+
context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' });
1616
assert.strictEqual('bar', context.foo);
1717
assert.strictEqual('lala', context.thing);
1818

19-
console.error('test updating context');
19+
// Test updating context
2020
script = new Script('foo = 3;');
2121
result = script.runInContext(context);
2222
assert.strictEqual(3, context.foo);
2323
assert.strictEqual('lala', context.thing);
2424

2525
// Issue GH-227:
26-
assert.throws(function() {
26+
assert.throws(() => {
2727
vm.runInNewContext('', null, 'some.js');
2828
}, /^TypeError: sandbox must be an object$/);
2929

3030
// Issue GH-1140:
31-
console.error('test runInContext signature');
31+
// Test runInContext signature
3232
let gh1140Exception;
3333
try {
3434
vm.runInContext('throw new Error()', context, 'expected-filename.js');
@@ -56,8 +56,8 @@ const contextifiedSandboxErrorMsg =
5656
});
5757

5858
// Issue GH-693:
59-
console.error('test RegExp as argument to assert.throws');
60-
script = vm.createScript('var assert = require(\'assert\'); assert.throws(' +
59+
// Test RegExp as argument to assert.throws
60+
script = vm.createScript('const assert = require(\'assert\'); assert.throws(' +
6161
'function() { throw "hello world"; }, /hello/);',
6262
'some.js');
6363
script.runInNewContext({ require: require });
@@ -71,14 +71,14 @@ assert.strictEqual(script.runInContext(ctx), false);
7171

7272
// Error on the first line of a module should
7373
// have the correct line and column number
74-
assert.throws(function() {
74+
assert.throws(() => {
7575
vm.runInContext('throw new Error()', context, {
7676
filename: 'expected-filename.js',
7777
lineOffset: 32,
7878
columnOffset: 123
7979
});
80-
}, function(err) {
81-
return /expected-filename.js:33:130/.test(err.stack);
80+
}, (err) => {
81+
return /expected-filename\.js:33:130/.test(err.stack);
8282
}, 'Expected appearance of proper offset in Error stack');
8383

8484
// https://github.com/nodejs/node/issues/6158

test/parallel/test-vm-create-and-run-in-context.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ const assert = require('assert');
55

66
const vm = require('vm');
77

8-
console.error('run in a new empty context');
8+
// Run in a new empty context
99
let context = vm.createContext();
1010
let result = vm.runInContext('"passed";', context);
1111
assert.strictEqual('passed', result);
1212

13-
console.error('create a new pre-populated context');
14-
context = vm.createContext({'foo': 'bar', 'thing': 'lala'});
13+
// Create a new pre-populated context
14+
context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' });
1515
assert.strictEqual('bar', context.foo);
1616
assert.strictEqual('lala', context.thing);
1717

18-
console.error('test updating context');
18+
// Test updating context
1919
result = vm.runInContext('var foo = 3;', context);
2020
assert.strictEqual(3, context.foo);
2121
assert.strictEqual('lala', context.thing);
2222

2323
// https://github.com/nodejs/node/issues/5768
24-
console.error('run in contextified sandbox without referencing the context');
25-
const sandbox = {x: 1};
24+
// Run in contextified sandbox without referencing the context
25+
const sandbox = { x: 1 };
2626
vm.createContext(sandbox);
2727
global.gc();
2828
vm.runInContext('x = 2', sandbox);

test/parallel/test-vm-function-declaration.js

-2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,3 @@ assert.strictEqual(res.name, 'b', 'res should be named b');
2121
assert.strictEqual(typeof o.a, 'function', 'a should be function');
2222
assert.strictEqual(typeof o.b, 'function', 'b should be function');
2323
assert.strictEqual(res, o.b, 'result should be global b function');
24-
25-
console.log('ok');

test/parallel/test-vm-new-script-new-context.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ const Script = require('vm').Script;
1515

1616
{
1717
const script = new Script('throw new Error(\'test\');');
18-
assert.throws(function() {
18+
assert.throws(() => {
1919
script.runInNewContext();
2020
}, /^Error: test$/);
2121
}
2222

2323
{
2424
const script = new Script('foo.bar = 5;');
25-
assert.throws(function() {
25+
assert.throws(() => {
2626
script.runInNewContext();
2727
}, /^ReferenceError: foo is not defined$/);
2828
}
@@ -73,14 +73,14 @@ const Script = require('vm').Script;
7373
script.runInNewContext({ f: f });
7474
assert.strictEqual(f.a, 2);
7575

76-
assert.throws(function() {
76+
assert.throws(() => {
7777
script.runInNewContext();
7878
}, /^ReferenceError: f is not defined$/);
7979
}
8080

8181
{
8282
const script = new Script('');
83-
assert.throws(function() {
83+
assert.throws(() => {
8484
script.runInNewContext.call('\'hello\';');
8585
}, /^TypeError: this\.runInContext is not a function$/);
8686
}

test/parallel/test-vm-new-script-this-context.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const Script = require('vm').Script;
55

66
common.globalCheck = false;
77

8-
console.error('run a string');
8+
// Run a string
99
let script = new Script('\'passed\';');
1010
const result = script.runInThisContext(script);
1111
assert.strictEqual('passed', result);
1212

13-
console.error('thrown error');
13+
// Thrown error
1414
script = new Script('throw new Error(\'test\');');
15-
assert.throws(function() {
15+
assert.throws(() => {
1616
script.runInThisContext(script);
1717
}, /^Error: test$/);
1818

@@ -22,7 +22,7 @@ script.runInThisContext(script);
2222
assert.strictEqual(2, global.hello);
2323

2424

25-
console.error('pass values');
25+
// Pass values
2626
global.code = 'foo = 1;' +
2727
'bar = 2;' +
2828
'if (typeof baz !== "undefined") throw new Error("test fail");';
@@ -34,7 +34,7 @@ assert.strictEqual(0, global.obj.foo);
3434
assert.strictEqual(2, global.bar);
3535
assert.strictEqual(1, global.foo);
3636

37-
console.error('call a function');
37+
// Call a function
3838
global.f = function() { global.foo = 100; };
3939
script = new Script('f()');
4040
script.runInThisContext(script);

test/parallel/test-vm-run-in-new-context.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ assert.strictEqual(typeof global.gc, 'function',
1010

1111
common.globalCheck = false;
1212

13-
console.error('run a string');
13+
// Run a string
1414
const result = vm.runInNewContext('\'passed\';');
1515
assert.strictEqual('passed', result);
1616

17-
console.error('thrown error');
18-
assert.throws(function() {
17+
// Thrown error
18+
assert.throws(() => {
1919
vm.runInNewContext('throw new Error(\'test\');');
2020
}, /^Error: test$/);
2121

@@ -24,7 +24,7 @@ vm.runInNewContext('hello = 2');
2424
assert.strictEqual(5, global.hello);
2525

2626

27-
console.error('pass values in and out');
27+
// Pass values in and out
2828
global.code = 'foo = 1;' +
2929
'bar = 2;' +
3030
'if (baz !== 3) throw new Error(\'test fail\');';
@@ -37,17 +37,17 @@ assert.strictEqual(1, global.obj.foo);
3737
assert.strictEqual(2, global.obj.bar);
3838
assert.strictEqual(2, global.foo);
3939

40-
console.error('call a function by reference');
40+
// Call a function by reference
4141
function changeFoo() { global.foo = 100; }
4242
vm.runInNewContext('f()', { f: changeFoo });
4343
assert.strictEqual(global.foo, 100);
4444

45-
console.error('modify an object by reference');
45+
// Modify an object by reference
4646
const f = { a: 1 };
4747
vm.runInNewContext('f.a = 2', { f: f });
4848
assert.strictEqual(f.a, 2);
4949

50-
console.error('use function in context without referencing context');
50+
// Use function in context without referencing context
5151
const fn = vm.runInNewContext('(function() { obj.p = {}; })', { obj: {} });
5252
global.gc();
5353
fn();
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const child_process = require('child_process');
55

@@ -11,16 +11,12 @@ const p = child_process.spawn(process.execPath, [
1111
'catch (e) { console.log(e.message); }'
1212
]);
1313

14-
p.stderr.on('data', function(data) {
15-
assert(false, 'Unexpected stderr data: ' + data);
16-
});
14+
p.stderr.on('data', common.mustNotCall());
1715

1816
let output = '';
1917

20-
p.stdout.on('data', function(data) {
21-
output += data;
22-
});
18+
p.stdout.on('data', (data) => output += data);
2319

24-
process.on('exit', function() {
20+
p.stdout.on('end', common.mustCall(() => {
2521
assert.strictEqual(output.replace(/[\r\n]+/g, ''), 'boo');
26-
});
22+
}));

test/parallel/test-vm-syntax-error-stderr.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@ const p = child_process.spawn(process.execPath, [
1212
wrong_script
1313
]);
1414

15-
p.stdout.on('data', function(data) {
16-
common.fail(`Unexpected stdout data: ${data}`);
17-
});
15+
p.stdout.on('data', common.mustNotCall());
1816

1917
let output = '';
2018

21-
p.stderr.on('data', function(data) {
22-
output += data;
23-
});
19+
p.stderr.on('data', (data) => output += data);
2420

25-
process.on('exit', function() {
21+
p.stderr.on('end', common.mustCall(() => {
2622
assert(/BEGIN CERT/.test(output));
2723
assert(/^\s+\^/m.test(output));
2824
assert(/Invalid left-hand side expression in prefix operation/.test(output));
29-
});
25+
}));

0 commit comments

Comments
 (0)