Skip to content

Commit

Permalink
Factor out the test environment setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkamarsik committed Aug 30, 2022
1 parent 585ceed commit 772ebdf
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 302 deletions.
28 changes: 10 additions & 18 deletions duktape.js
Expand Up @@ -8,7 +8,6 @@
* $ node duktape.js
*/

var fs = require('fs');
var child_process = require('child_process');
var runner_support = require('./runner_support');

Expand All @@ -28,23 +27,16 @@ var dukKey = (function () {
})();
console.log('Duktape result key is: test.res.' + dukKey);

function runTest(evalcode) {
var script = 'var evalcode = ' + JSON.stringify(evalcode) + ';\n' +
'try {\n' +
' var res = eval(evalcode);\n' +
' if (res !== true && res !== 1) { throw new Error("failed: " + res); }\n' +
' console.log("[SUCCESS]");\n' +
'} catch (e) {\n' +
' console.log("[FAILURE]", e);\n' +
' /*throw e;*/\n' +
'}\n';
function dukRunner(testFilename) {
try {
var stdout = child_process.execFileSync(dukCommand, [ testFilename ], {
encoding: 'utf-8'
});

fs.writeFileSync('duktest.js', script);
var stdout = child_process.execFileSync(dukCommand, [ 'duktest.js' ], {
encoding: 'utf-8'
});

return /^\[SUCCESS\]$/gm.test(stdout);
return /^\[SUCCESS\]$/m.test(stdout);
} catch (e) {
return false;
}
}

runner_support.runTests(runTest, dukKey, 'Duktape');
runner_support.runTests(dukRunner, dukKey, 'Duktape');
73 changes: 12 additions & 61 deletions graalvm.js
Expand Up @@ -16,47 +16,21 @@
* $ GRAALVM_NODE=<graalvm>/bin/node node graalvm.js
*/

var fs = require('fs');
var child_process = require('child_process');
var runner_support = require('./runner_support');

var jsCommand = 'js';
var jsArgs = [ '--js.intl-402' ];
var nodeCommand = process.env['GRAALVM_NODE'];
var nodeArgs = [];

var testScriptFilename = 'graalvmtest.js';

var flagsForSuite = {
'data-es5': [],
'data-es6': [ [ '--js.ecmascript-version=6' ] ],
'data-es2016plus': [ [ '--js.ecmascript-version=staging' ] ],
'data-esnext': [ [ '--js.ecmascript-version=staging' ], [ '--experimental-options', '--js.new-set-methods' ] ],
'data-esintl': [],
'data-non-standard': [ [ '--experimental-options', '--js.nashorn-compat' ], [ '--experimental-options', '--js.v8-compat' ], [ '--experimental-options', '--js.global-property' ] ]
'es5': [],
'es6': [ [ '--js.ecmascript-version=6' ] ],
'es2016plus': [ [ '--js.ecmascript-version=staging' ] ],
'esnext': [ [ '--js.ecmascript-version=staging' ], [ '--experimental-options', '--js.new-set-methods' ] ],
'esintl': [],
'non-standard': [ [ '--experimental-options', '--js.nashorn-compat' ], [ '--experimental-options', '--js.v8-compat' ], [ '--experimental-options', '--js.global-property' ] ]
};

var prelude =
'if (typeof global === "undefined") {\n' +
' this.lacksGlobal = true;\n' +
' global = this;\n' +
'}\n' +
'if (typeof globalThis === "undefined") {\n' +
' this.lacksGlobalThis = true;\n' +
' globalThis = this;\n' +
'}\n' +
'var __script_executed = {};\n' +
'global.__script_executed = __script_executed;\n' +
'global.test = function test(expression) {\n' +
' if (expression) {\n' +
' console.log("[SUCCESS]");\n' +
' }\n' +
'}\n' +
'global.asyncTestPassed = function asyncTestPassed() {\n' +
' console.log("[SUCCESS]");\n' +
'}\n' +
runner_support.createIterableHelper;

// Key for .res (e.g. test.res.graalvm), automatic based on GraalVM version.
var graalvmKey = (function () {
var stdout = child_process.execFileSync(jsCommand, [ '--version' ], {
Expand All @@ -69,9 +43,9 @@ var graalvmKey = (function () {
})();
console.log('GraalVM result key is: test.res.graalvm' + graalvmKey);

function exec(launcherCommand, launcherArgs, flags) {
function exec(flags, testFilename) {
try {
var stdout = child_process.execFileSync(launcherCommand, launcherArgs.concat(flags, [ testScriptFilename ]), {
var stdout = child_process.execFileSync(jsCommand, jsArgs.concat(flags, [ testFilename ]), {
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'ignore']
});
Expand All @@ -82,14 +56,14 @@ function exec(launcherCommand, launcherArgs, flags) {
}
}

function executeTestScript(launcherCommand, launcherArgs, suite) {
if (exec(launcherCommand, launcherArgs, [])) {
function graalvmRunner(testFilename, suite) {
if (exec([], testFilename)) {
return true;
}

for (var i = 0; i < flagsForSuite[suite].length; i++) {
var flags = flagsForSuite[suite][i];
if (exec(launcherCommand, launcherArgs, flags)) {
if (exec(flags, testFilename)) {
return {
val: 'flagged',
note_id: ('graalvm-' + flags.join('-')).replace(/[=\.]/g, '-').replace(/-+/g, '-'),
Expand All @@ -101,29 +75,6 @@ function executeTestScript(launcherCommand, launcherArgs, suite) {
return false;
}

function runTest(evalcode, suite, testPath) {
if (/\bsetTimeout\b/.test(evalcode) && !nodeCommand) {
console.log(testPath + ': could not run test using setTimeout (set GRAALVM_NODE to GraalVM\'s `node` binary and rerun to get results)');
return 'skip';
}

var script =
prelude + '\n' +
'var evalcode = ' + JSON.stringify(evalcode) + ';\n' +
'var result = eval(evalcode);\n' +
'if (result) {\n' +
' console.log("[SUCCESS]");\n' +
'}\n';

fs.writeFileSync(testScriptFilename, script);

if (evalcode.match(/\bsetTimeout\b/)) {
return executeTestScript(nodeCommand, nodeArgs, suite);
} else {
return executeTestScript(jsCommand, jsArgs, suite);
}
}

function resultsMatch(expect, actual) {
if (expect === actual) {
return true;
Expand All @@ -134,4 +85,4 @@ function resultsMatch(expect, actual) {
return false;
}

runner_support.runTests(runTest, graalvmKey, 'GraalVM', { resultsMatch: resultsMatch });
runner_support.runTests(graalvmRunner, graalvmKey, 'GraalVM', { resultsMatch: resultsMatch });
89 changes: 3 additions & 86 deletions hermes.js
Expand Up @@ -9,7 +9,6 @@
* suitename can be 'all'
*/

var fs = require('fs');
var child_process = require('child_process');
var console = require('console');
var runner_support = require('./runner_support');
Expand Down Expand Up @@ -47,7 +46,6 @@ var argv = require('yargs/yargs')(process.argv.slice(2))
var hermesCommand = argv.hermesBin;
var suites = argv.suite;
suites = suites === 'all' ? '' : suites;
var testName = argv.testName;

// Key for .res (e.g. test.res.hermes0_7_0), automatic based on `hermes -version`.
var hermesKey = (function () {
Expand All @@ -63,63 +61,6 @@ var hermesKey = (function () {
})();
console.log('Hermes result key is: test.res.' + hermesKey);

var asyncTestHelperHead =
'var asyncPassed = false;\n' +
'\n' +
'function asyncTestPassed() {\n' +
' asyncPassed = true;\n' +
'}\n' +
'\n' +
'function setTimeout(cb, time, cbarg) {\n' +
' if (!jobqueue[time]) {\n' +
' jobqueue[time] = [];\n' +
' }\n' +
' jobqueue[time].push({cb, cbarg, startTime: Date.now(), timeout: time});\n' +
'}\n' +
'\n' +
'var jobqueue = [];\n';

var asyncTestHelperTail =
'const thenCb = job => {\n' +
' job.cb(job.cbarg)\n' +
'}\n' +
'\n' +
'const catchCb = job => {\n' +
' jobRunner(job);\n' +
'}\n' +
'\n' +
'function jobRunner(job){\n' +
' return new Promise((resolve, reject) => {\n' +
' let diff = Date.now() - job.startTime;\n' +
' if (diff >= job.timeout) {\n' +
' if (!job.run) {\n' +
' job.run = true;\n' +
' resolve (job);\n' +
' }\n' +
' } else {\n' +
' reject (job)\n' +
' }\n' +
' })\n' +
' .then(thenCb)\n' +
' .catch(catchCb)\n' +
'}\n' +
'\n' +
'jobqueue.forEach(function(jobs, index) {\n' +
' for (var job of jobs) {\n' +
' jobRunner(job);\n' +
' }\n' +
'});\n' +
'\n' +
'function onCloseAsyncCheck() {\n' +
' if (!asyncPassed) {\n' +
' print("Async[FAILURE]");\n' +
' throw "Async check failed";\n' +
' }\n' +
' print("[SUCCESS]");\n' +
'}\n' +
'\n' +
'Promise.resolve().then(onCloseAsyncCheck);\n';

function getArgs(testFilename) {
var processArgs = [
/*'-enable-eval',*/
Expand All @@ -146,39 +87,15 @@ function getArgs(testFilename) {
return processArgs;
}

function runTest(evalcode) {
var testFilename = 'hermestest.js';
function hermesRunner(testFilename) {
var processArgs = getArgs(testFilename);
var script = '';

if (evalcode.includes('__createIterableObject')) {
script += runner_support.createIterableHelper;
} else if (evalcode.includes('global')) {
script += 'var global = this;\n';
}

if (evalcode.includes('asyncTestPassed')) {
script += asyncTestHelperHead + evalcode + asyncTestHelperTail;
} else {
script += 'var evalcode = ' + JSON.stringify(evalcode) + ';\n' +
'try {\n' +
' var res = eval(evalcode);\n' +
' if (!res) { throw new Error("failed: " + res); }\n' +
' print("[SUCCESS]");\n' +
'} catch (e) {\n' +
' print("[FAILURE]", e);\n' +
' /*throw e;*/\n' +
'}\n';
}

fs.writeFileSync(testFilename, script);

try {
var stdout = child_process.execFileSync(hermesCommand, processArgs, {
encoding: 'utf-8'
});

return /^\[SUCCESS\]$/gm.test(stdout);
return /^\[SUCCESS\]$/m.test(stdout);
} catch (e) {
// console.log(e);
return false;
Expand Down Expand Up @@ -206,4 +123,4 @@ function resultsMatch(expect, actual) {
return expect === actual;
}

runner_support.runTests(runTest, hermesKey, 'Hermes', { resultsMatch: resultsMatch, suites: suites, testName: testName, bail: argv.bail });
runner_support.runTests(hermesRunner, hermesKey, 'Hermes', { resultsMatch: resultsMatch, suites: suites, testName: argv.testName, bail: argv.bail });

0 comments on commit 772ebdf

Please sign in to comment.