diff options
author | Michaël Zasso <targos@protonmail.com> | 2018-03-16 15:23:39 +0100 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2018-04-03 17:59:22 +0200 |
commit | 34d988f122c206da692b5c39ec5342fec22b60ab (patch) | |
tree | e1735f265ea1d0d936b5aab6985d9d48389af05b /test/parallel/test-vm-basic.js | |
parent | a820f4155b5e9a742fec750b437036dcd98d3271 (diff) | |
download | node-new-34d988f122c206da692b5c39ec5342fec22b60ab.tar.gz |
vm: move options checks from C++ to JS
Also introduces stronger type validations for options passed to vm
functions.
PR-URL: https://github.com/nodejs/node/pull/19398
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'test/parallel/test-vm-basic.js')
-rw-r--r-- | test/parallel/test-vm-basic.js | 111 |
1 files changed, 68 insertions, 43 deletions
diff --git a/test/parallel/test-vm-basic.js b/test/parallel/test-vm-basic.js index bf1532cacc..2dfc1e9cb7 100644 --- a/test/parallel/test-vm-basic.js +++ b/test/parallel/test-vm-basic.js @@ -24,60 +24,85 @@ const common = require('../common'); const assert = require('assert'); const vm = require('vm'); -// Test 1: vm.runInNewContext -const sandbox = {}; -let result = vm.runInNewContext( - 'foo = "bar"; this.typeofProcess = typeof process; typeof Object;', - sandbox -); -assert.deepStrictEqual(sandbox, { - foo: 'bar', - typeofProcess: 'undefined', -}); -assert.strictEqual(result, 'function'); +// vm.runInNewContext +{ + const sandbox = {}; + const result = vm.runInNewContext( + 'foo = "bar"; this.typeofProcess = typeof process; typeof Object;', + sandbox + ); + assert.deepStrictEqual(sandbox, { + foo: 'bar', + typeofProcess: 'undefined', + }); + assert.strictEqual(result, 'function'); +} -// Test 2: vm.runInContext -const sandbox2 = { foo: 'bar' }; -const context = vm.createContext(sandbox2); -result = vm.runInContext( - 'baz = foo; this.typeofProcess = typeof process; typeof Object;', - context -); -assert.deepStrictEqual(sandbox2, { - foo: 'bar', - baz: 'bar', - typeofProcess: 'undefined' -}); -assert.strictEqual(result, 'function'); +// vm.runInContext +{ + const sandbox = { foo: 'bar' }; + const context = vm.createContext(sandbox); + const result = vm.runInContext( + 'baz = foo; this.typeofProcess = typeof process; typeof Object;', + context + ); + assert.deepStrictEqual(sandbox, { + foo: 'bar', + baz: 'bar', + typeofProcess: 'undefined' + }); + assert.strictEqual(result, 'function'); +} + +// vm.runInThisContext +{ + const result = vm.runInThisContext( + 'vmResult = "foo"; Object.prototype.toString.call(process);' + ); + assert.strictEqual(global.vmResult, 'foo'); + assert.strictEqual(result, '[object process]'); + delete global.vmResult; +} + +// vm.runInNewContext +{ + const result = vm.runInNewContext( + 'vmResult = "foo"; typeof process;' + ); + assert.strictEqual(global.vmResult, undefined); + assert.strictEqual(result, 'undefined'); +} + +// vm.createContext +{ + const sandbox = {}; + const context = vm.createContext(sandbox); + assert.strictEqual(sandbox, context); +} -// Test 3: vm.runInThisContext -result = vm.runInThisContext( - 'vmResult = "foo"; Object.prototype.toString.call(process);' -); -assert.strictEqual(global.vmResult, 'foo'); -assert.strictEqual(result, '[object process]'); -delete global.vmResult; +// Run script with filename +{ + const script = 'throw new Error("boom")'; + const filename = 'test-boom-error'; + const context = vm.createContext(); -// Test 4: vm.runInNewContext -result = vm.runInNewContext( - 'vmResult = "foo"; typeof process;' -); -assert.strictEqual(global.vmResult, undefined); -assert.strictEqual(result, 'undefined'); + function checkErr(err) { + return err.stack.startsWith('test-boom-error:1'); + } -// Test 5: vm.createContext -const sandbox3 = {}; -const context2 = vm.createContext(sandbox3); -assert.strictEqual(sandbox3, context2); + assert.throws(() => vm.runInContext(script, context, filename), checkErr); + assert.throws(() => vm.runInNewContext(script, context, filename), checkErr); + assert.throws(() => vm.runInThisContext(script, filename), checkErr); +} -// Test 6: invalid arguments +// Invalid arguments [null, 'string'].forEach((input) => { common.expectsError(() => { vm.createContext({}, input); }, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: 'The "options" argument must be of type object. ' + + message: 'The "options" argument must be of type Object. ' + `Received type ${typeof input}` }); }); |