Skip to content

Commit

Permalink
Add prelude option
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed Oct 21, 2016
1 parent 330f05f commit e589e19
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
16 changes: 12 additions & 4 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let hostType, hostPath;
if (argv.hostType) {
hostType = argv.hostType;

if (argv.hostPath) {
if (!argv.hostPath) {
console.error('Missing host path. Pass --hostPath with a path to the host executable you want to test.');
process.exit(1);
}
Expand Down Expand Up @@ -87,7 +87,15 @@ function pathToTestFile(path) {
return { file: path, contents: fs.readFileSync(path, 'utf-8')};
}

function compileFile(contents) {
contents = preludeContents + contents;
return compile(contents, { test262Dir: test262Dir, includesDir: includesDir });
const endFrontmatterRe = /---\*\/\r?\n/g;

This comment has been minimized.

Copy link
@hax

hax Mar 13, 2017

Member

Why need this? It seems make compileFile run in different behavior for odd/even files!! Bug!

This comment has been minimized.

Copy link
@bterlson

bterlson Mar 15, 2017

Author Member

Can you clarify what an odd/even file is? I guess there's a bug related to this but I'm not seeing it.

This comment has been minimized.

Copy link
@hax

hax Mar 17, 2017

Member

See my pr: #72

function compileFile(test) {
const match = endFrontmatterRe.exec(test.contents);
if (match) {
test.contents = test.contents.slice(0, endFrontmatterRe.lastIndex)
+ preludeContents
+ test.contents.slice(endFrontmatterRe.lastIndex);
} else {
test.contents = preludeContents + test.contents;
}
return compile(test, { test262Dir: test262Dir, includesDir: includesDir });
}
1 change: 1 addition & 0 deletions test/test-prelude.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('prelude!');
80 changes: 71 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,55 @@ var fs = require('fs');
var path = require('path');
var cp = require('child_process');

run()
Promise.all([
run(),
runPrelude()
])
.then(validate)
.catch(reportRunError)
.catch(reportRunError);

function reportRunError(e) {
console.error("Error running tests", e.stack);
process.exit(1);
}

function run() {
return new Promise((resolve, reject) => {
var stdout = '';
var stderr = '';

var child = cp.fork('bin/run.js', [
'--hostType', 'node',
'--hostPath', process.execPath,
'-r', 'json',
'--includesDir', './test/test-includes',
'test/collateral/**/*.js'], {silent: true});

child.stdout.on('data', function(d) { stdout += d });
child.stderr.on('data', function(d) { stderr += d });
child.on('exit', function() {
if (stderr) {
return reject(new Error("Got stderr: " + stderr));
}

try {
resolve(JSON.parse(stdout));
} catch(e) {
reject(e);
}
})
});
}

function validate(records) {
const normal = records[0];
const prelude = records[1];

validateNormal(normal);
validatePrelude(prelude);
}

function validateNormal(records) {
records.forEach(record => {
test(record.attrs.description, function (t) {
t.assert(record.attrs.expected, 'Test has an `expected` frontmatter');
Expand All @@ -28,12 +72,7 @@ function validate(records) {
});
}

function reportRunError(e) {
console.error("Error running tests", e.stack);
process.exit(1);
}

function run() {
function runPrelude() {
return new Promise((resolve, reject) => {
var stdout = '';
var stderr = '';
Expand All @@ -43,7 +82,8 @@ function run() {
'--hostPath', process.execPath,
'-r', 'json',
'--includesDir', './test/test-includes',
'test/collateral/**/*.js'], {silent: true});
'--prelude', './test/test-prelude.js',
'test/collateral/bothStrict.js'], {silent: true});

child.stdout.on('data', function(d) { stdout += d });
child.stderr.on('data', function(d) { stderr += d });
Expand All @@ -58,6 +98,28 @@ function run() {
reject(e);
}
})
})
}

function validatePrelude(records) {
records.forEach(record => {
test(record.attrs.description + ' with prelude', function (t) {
t.assert(record.attrs.expected, 'Test has an `expected` frontmatter');
if (!record.attrs.expected) {
// can't do anything else
t.end();
return;
}

t.equal(record.result.pass, record.attrs.expected.pass, 'Test passes or fails as expected');

if (record.attrs.expected.message) {
t.equal(record.result.message, record.attrs.expected.message, 'Test fails with appropriate message');
}

t.assert(record.rawResult.stdout.indexOf("prelude!") > -1, 'Has prelude content');
t.end();
});
});
}

0 comments on commit e589e19

Please sign in to comment.