node/test/parallel/test-vm-cached-data.js
Rich Trott 150dc5c2e6 test: remove unneeded escaping in template strings
The no-useless-escape rule in ESLint did not previously flag certain
unnecessary escaping in template strings. These will be flagged in
ESLint 3.8.0.

PR-URL: https://github.com/nodejs/node/pull/9112
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
2016-10-18 15:28:21 -07:00

93 lines
2.1 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const vm = require('vm');
const spawnSync = require('child_process').spawnSync;
const Buffer = require('buffer').Buffer;
function getSource(tag) {
return `(function ${tag}() { return '${tag}'; })`;
}
function produce(source, count) {
if (!count)
count = 1;
const out = spawnSync(process.execPath, [ '-e', `
'use strict';
var assert = require('assert');
var vm = require('vm');
var data;
for (var i = 0; i < ${count}; i++) {
var script = new vm.Script(process.argv[1], {
produceCachedData: true
});
assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);
if (script.cachedDataProduced)
data = script.cachedData.toString('base64');
}
console.log(data);
`, source]);
assert.equal(out.status, 0, out.stderr + '');
return Buffer.from(out.stdout.toString(), 'base64');
}
function testProduceConsume() {
const source = getSource('original');
const data = produce(source);
// It should consume code cache
const script = new vm.Script(source, {
cachedData: data
});
assert(!script.cachedDataRejected);
assert.equal(script.runInThisContext()(), 'original');
}
testProduceConsume();
function testProduceMultiple() {
const source = getSource('original');
produce(source, 3);
}
testProduceMultiple();
function testRejectInvalid() {
const source = getSource('invalid');
const data = produce(source);
// It should reject invalid code cache
const script = new vm.Script(getSource('invalid_1'), {
cachedData: data
});
assert(script.cachedDataRejected);
assert.equal(script.runInThisContext()(), 'invalid_1');
}
testRejectInvalid();
function testRejectSlice() {
const source = getSource('slice');
const data = produce(source).slice(4);
const script = new vm.Script(source, {
cachedData: data
});
assert(script.cachedDataRejected);
}
testRejectSlice();
// It should throw on non-Buffer cachedData
assert.throws(() => {
new vm.Script('function abc() {}', {
cachedData: 'ohai'
});
});