2013-03-06 03:36:36 +08:00
|
|
|
require('./source-map-support').install();
|
2013-01-18 15:56:20 +08:00
|
|
|
|
|
|
|
var SourceMapGenerator = require('source-map').SourceMapGenerator;
|
2013-03-06 03:36:36 +08:00
|
|
|
var child_process = require('child_process');
|
2013-01-18 15:56:20 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
2013-04-25 08:56:13 +08:00
|
|
|
function compareLines(actual, expected) {
|
|
|
|
assert(actual.length >= expected.length, 'got ' + actual.length + ' lines but expected at least ' + expected.length + ' lines');
|
|
|
|
for (var i = 0; i < expected.length; i++) {
|
|
|
|
// Some tests are regular expressions because the output format changed slightly between node v0.9.2 and v0.9.3
|
|
|
|
if (expected[i] instanceof RegExp) {
|
|
|
|
assert(expected[i].test(actual[i]), JSON.stringify(actual[i]) + ' does not match ' + expected[i]);
|
|
|
|
} else {
|
|
|
|
assert.equal(actual[i], expected[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 15:56:20 +08:00
|
|
|
function compareStackTrace(source, expected) {
|
2013-03-06 03:36:36 +08:00
|
|
|
var sourceMap = new SourceMapGenerator({
|
|
|
|
file: '.generated.js',
|
|
|
|
sourceRoot: '.'
|
|
|
|
});
|
|
|
|
for (var i = 1; i <= 100; i++) {
|
|
|
|
sourceMap.addMapping({
|
|
|
|
generated: { line: i, column: 1 },
|
|
|
|
original: { line: 1000 + i, column: 100 + i },
|
|
|
|
source: 'line' + i + '.js'
|
|
|
|
});
|
|
|
|
}
|
2013-01-18 15:56:20 +08:00
|
|
|
fs.writeFileSync('.generated.js.map', sourceMap);
|
|
|
|
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
|
|
|
|
source.join('\n') + '};//@ sourceMappingURL=.generated.js.map');
|
|
|
|
try {
|
|
|
|
delete require.cache[require.resolve('./.generated')];
|
|
|
|
require('./.generated').test();
|
|
|
|
} catch (e) {
|
2013-04-25 08:56:13 +08:00
|
|
|
compareLines(e.stack.split('\n'), expected);
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
fs.unlinkSync('.generated.js');
|
|
|
|
fs.unlinkSync('.generated.js.map');
|
|
|
|
}
|
|
|
|
|
2013-03-06 03:36:36 +08:00
|
|
|
function compareStdout(done, source, expected) {
|
|
|
|
var sourceMap = new SourceMapGenerator({
|
|
|
|
file: '.generated.js',
|
|
|
|
sourceRoot: '.'
|
|
|
|
});
|
|
|
|
sourceMap.addMapping({
|
|
|
|
generated: { line: 1, column: 1 },
|
|
|
|
original: { line: 1, column: 1 },
|
|
|
|
source: '.original.js'
|
|
|
|
});
|
|
|
|
fs.writeFileSync('.original.js', 'this is the original code');
|
|
|
|
fs.writeFileSync('.generated.js.map', sourceMap);
|
|
|
|
fs.writeFileSync('.generated.js', source.join('\n') +
|
|
|
|
'//@ sourceMappingURL=.generated.js.map');
|
|
|
|
child_process.exec('node ./.generated', function(error, stdout, stderr) {
|
|
|
|
try {
|
2013-04-25 08:56:13 +08:00
|
|
|
compareLines((stdout + stderr).trim().split('\n'), expected);
|
2013-03-06 03:36:36 +08:00
|
|
|
} catch (e) {
|
|
|
|
return done(e);
|
|
|
|
}
|
|
|
|
fs.unlinkSync('.generated.js');
|
|
|
|
fs.unlinkSync('.generated.js.map');
|
|
|
|
fs.unlinkSync('.original.js');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-18 15:56:20 +08:00
|
|
|
it('normal throw', function() {
|
|
|
|
compareStackTrace([
|
|
|
|
'throw new Error("test");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throw inside function', function() {
|
|
|
|
compareStackTrace([
|
|
|
|
'function foo() {',
|
|
|
|
' throw new Error("test");',
|
|
|
|
'}',
|
|
|
|
'foo();'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at foo \(.*\/line2\.js:1002:102\)$/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line4\.js:1004:104\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throw inside function inside function', function() {
|
|
|
|
compareStackTrace([
|
|
|
|
'function foo() {',
|
|
|
|
' function bar() {',
|
|
|
|
' throw new Error("test");',
|
|
|
|
' }',
|
|
|
|
' bar();',
|
|
|
|
'}',
|
|
|
|
'foo();'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at bar \(.*\/line3\.js:1003:103\)$/,
|
|
|
|
/^ at foo \(.*\/line5\.js:1005:105\)$/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line7\.js:1007:107\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval', function() {
|
|
|
|
compareStackTrace([
|
|
|
|
'eval("throw new Error(\'test\')");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.eval \(eval at <anonymous> \(.*\/line1\.js:1001:101\)/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval inside eval', function() {
|
|
|
|
compareStackTrace([
|
|
|
|
'eval("eval(\'throw new Error(\\"test\\")\')");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.eval \(eval at <anonymous> \(eval at <anonymous> \(.*\/line1\.js:1001:101\)/,
|
|
|
|
/^ at Object\.eval \(eval at <anonymous> \(.*\/line1\.js:1001:101\)/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval inside function', function() {
|
|
|
|
compareStackTrace([
|
|
|
|
'function foo() {',
|
|
|
|
' eval("throw new Error(\'test\')");',
|
|
|
|
'}',
|
|
|
|
'foo();'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at eval \(eval at foo \(.*\/line2\.js:1002:102\)/,
|
|
|
|
/^ at foo \(.*\/line2\.js:1002:102\)/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line4\.js:1004:104\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval with sourceURL', function() {
|
|
|
|
compareStackTrace([
|
|
|
|
'eval("throw new Error(\'test\')//@ sourceURL=sourceURL.js");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
|
|
|
' at Object.eval (sourceURL.js:1:7)',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval with sourceURL inside eval', function() {
|
|
|
|
compareStackTrace([
|
|
|
|
'eval("eval(\'throw new Error(\\"test\\")//@ sourceURL=sourceURL.js\')");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
|
|
|
' at Object.eval (sourceURL.js:1:7)',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.eval \(eval at <anonymous> \(.*\/line1\.js:1001:101\)/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
2013-03-06 03:36:36 +08:00
|
|
|
|
|
|
|
it('default options', function(done) {
|
|
|
|
compareStdout(done, [
|
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install();',
|
|
|
|
'process.nextTick(foo);',
|
|
|
|
'process.nextTick(function() { process.exit(1); });'
|
|
|
|
], [
|
2013-04-25 10:16:25 +08:00
|
|
|
/\/.original\.js:1$/,
|
2013-03-06 03:36:36 +08:00
|
|
|
'this is the original code',
|
|
|
|
'^',
|
|
|
|
'Error: this is the error',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at foo \(.*\/.original\.js:1:1\)$/
|
2013-03-06 03:36:36 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleUncaughtExceptions is true', function(done) {
|
|
|
|
compareStdout(done, [
|
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install({ handleUncaughtExceptions: true });',
|
|
|
|
'process.nextTick(foo);'
|
|
|
|
], [
|
2013-04-25 10:16:25 +08:00
|
|
|
/\/.original\.js:1$/,
|
2013-03-06 03:36:36 +08:00
|
|
|
'this is the original code',
|
|
|
|
'^',
|
|
|
|
'Error: this is the error',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at foo \(.*\/.original\.js:1:1\)$/
|
2013-03-06 03:36:36 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleUncaughtExceptions is false', function(done) {
|
|
|
|
compareStdout(done, [
|
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install({ handleUncaughtExceptions: false });',
|
|
|
|
'process.nextTick(foo);'
|
|
|
|
], [
|
2013-04-25 08:56:13 +08:00
|
|
|
/\/.generated.js:2$/,
|
2013-03-06 03:36:36 +08:00
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
' ^',
|
|
|
|
'Error: this is the error',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at foo \(.*\/.original\.js:1:1\)$/
|
2013-03-06 03:36:36 +08:00
|
|
|
]);
|
|
|
|
});
|