diff --git a/Makefile b/Makefile index 5b941e4d4..744b090a5 100644 --- a/Makefile +++ b/Makefile @@ -123,13 +123,13 @@ test-list: test/test-list.js test/test-list.js: force @git ls-files -o -c test/feature | node build/build-test-list.js > $@ -test-interpret: test/unit/runtime/traceur-interpreter.js +test-interpret: test/unit/node/traceur-interpreter.js ./traceur $^ test-interpret-throw: test/unit/runtime/resources/throwsError.js ./traceur $^ 2>&1 | grep 'ModuleEvaluationError' | wc -l | grep '1' -test-interpret-absolute: $(CURDIR)/test/unit/runtime/traceur-interpreter.js +test-interpret-absolute: $(CURDIR)/test/unit/node/traceur-interpreter.js ./traceur $^ test-inline-module-error: diff --git a/demo/expressServer.js b/demo/expressServer.js index 64fbe40b6..e5890a3c6 100644 --- a/demo/expressServer.js +++ b/demo/expressServer.js @@ -1,18 +1,48 @@ -var express = require('express'); -var http = require('http'); -var serveIndex = require('serve-index'); +// Copyright 2015 Traceur Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import {globPatterns} from '../test/modular/NodeTraceurTestRunner.js'; + +let express = require('express'); +let http = require('http'); +let serveIndex = require('serve-index'); function servePathAtPort(path, port) { - var app = express(); - app.use(express.static(path)); // before directory to allow index.html to work + let app = express(); + // serveIndex must precede static to allow index.html to work app.use(serveIndex(path)); - var server = http.createServer(app); + app.use(express.static(path)); + // Expand the test list based on the file system. + app.get('/traceurService/testGlobs', function(req, res) { + let patterns = JSON.parse(req.query.patterns); + return globPatterns(patterns).then((files) => { + let nodeless = []; + files.forEach((file) => { + if (file.indexOf('/node/') === -1) { + nodeless.push(file); + } + }); + return res.send(nodeless); + }); + }); + let server = http.createServer(app); server.on('error', function(e) { - console.log('Port ' + port + ' did not work out'); + console.error('Port ' + port + ' did not work out'); }); server.listen.apply(server, [port]); console.log('serving ' + path + ' at ' + port); } -servePathAtPort(__dirname + '/..', 8099); -servePathAtPort(__dirname + '/..', 80); +servePathAtPort(System.dirname(__moduleName) + '/..', 8099); +servePathAtPort(System.dirname(__moduleName) + '/..', 80); diff --git a/src/runtime/ModuleStore.js b/src/runtime/ModuleStore.js index 777d5a86f..f217f029b 100644 --- a/src/runtime/ModuleStore.js +++ b/src/runtime/ModuleStore.js @@ -136,7 +136,7 @@ var lines = this.func.toString().split('\n') var evaled = []; - ex.stack.split('\n').some(function(frame) { + ex.stack.split('\n').some((frame, index) => { // End when we find ourselves on the stack. if (frame.indexOf('UncoatedModuleInstantiator.getUncoatedModule') > 0) return true; @@ -146,7 +146,12 @@ if (m) { var line = parseInt(m[2], 10); evaled = evaled.concat(beforeLines(lines, line)); - evaled.push(columnSpacing(m[3]) + '^'); + // The first evaled frame should be the one from 'this.func' + if (index === 1) { + evaled.push(columnSpacing(m[3]) + '^ ' + this.url); + } else { + evaled.push(columnSpacing(m[3]) + '^'); + } evaled = evaled.concat(afterLines(lines, line)); evaled.push('= = = = = = = = ='); } else { diff --git a/test/modular/BrowserTraceurTestRunner.js b/test/modular/BrowserTraceurTestRunner.js new file mode 100644 index 000000000..40848288b --- /dev/null +++ b/test/modular/BrowserTraceurTestRunner.js @@ -0,0 +1,50 @@ +// Copyright 2015 Traceur Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @fileoverview Configure mocha for Traceur testing.in browsers */ + +import {TraceurTestRunner} from './TraceurTestRunner.js'; +import {webLoader} from '../../src/runtime/webLoader.js'; + +export class BrowserTraceurTestRunner extends TraceurTestRunner { + + constructor(traceurTestOptions) { + super({ + reporter: 'html', + ui: 'tdd', + importMetadata: { + traceurOptions: { + sourceMaps: 'inline' + } + } + }, traceurTestOptions); + } + + expandPatterns() { + let url = './traceurService/testGlobs?patterns='; + url += encodeURIComponent(JSON.stringify(this.patterns_)); + return new Promise((resolve, reject) => { + webLoader.load(url, (files) => { + resolve(JSON.parse(files).forEach((file) => this.addFile(file))); + }, (ex) => { + console.error(url + ' FAILED ', ex.stack || ex); + }); + }); + } + + getOptions() { + return this.defaultOptions(); + } + +}; diff --git a/test/modular/Mocha6.js b/test/modular/Mocha6.js index 1db99a7c5..287146407 100644 --- a/test/modular/Mocha6.js +++ b/test/modular/Mocha6.js @@ -14,10 +14,7 @@ /** @fileoverview Wrap Mocha in es6 layer */ -var Mocha = require('mocha'); -var path = require('path'); -var Runner = require('mocha/lib/runner'); -var reporters = require('mocha/lib/reporters'); +import {Mocha, Runner, reporters} from './MochaDependencies.js'; export class Mocha6 extends Mocha { @@ -43,7 +40,7 @@ export class Mocha6 extends Mocha { importFiles() { var promiseImports = this.files.map((file) => { - file = path.resolve(file).replace(/\\/g, '/'); + file = './' + file.replace(/\\/g, '/');; this.suite.emit('pre-require', global, file, this); return System.import(file, {metadata: this.options.importMetadata}). then(() => { diff --git a/test/modular/MochaDependencies.js b/test/modular/MochaDependencies.js new file mode 100644 index 000000000..ce9f77652 --- /dev/null +++ b/test/modular/MochaDependencies.js @@ -0,0 +1,29 @@ +// Copyright 2015 Traceur Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @fileoverview Mocha dependencies from Node */ + + export let Mocha; + export let Runner; + export let reporters; + + if (typeof window === 'undefined') { + Mocha = require('mocha'); + Runner = require('mocha/lib/runner'); + reporters = require('mocha/lib/reporters'); + } else { + Mocha = window.Mocha; + Runner = Mocha.Runner; + reporters = Mocha.reporters; + } diff --git a/test/modular/NodeTraceurTestRunner.js b/test/modular/NodeTraceurTestRunner.js new file mode 100644 index 000000000..e4d989427 --- /dev/null +++ b/test/modular/NodeTraceurTestRunner.js @@ -0,0 +1,96 @@ +// Copyright 2015 Traceur Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @fileoverview Configure mocha for Traceur testing on node. */ + +import {TraceurTestRunner} from './TraceurTestRunner.js'; + +export function globPatterns(patterns) { + // This require will cause a compile error if at module scope. + // TOOD async load the class. + let glob = require('glob'); + return Promise.all( + patterns.map((pattern) => { + return new Promise((resolve, reject) => { + glob(pattern, {}, (err, files) => { + if (err) { + reject(err); + } else { + resolve(files); + } + }); + }); + })).then((arrayOfFiles) => { + let allFiles = []; + arrayOfFiles.forEach((files) => { + allFiles.push(...files); + }); + return allFiles; + }); +} + +export class NodeTraceurTestRunner extends TraceurTestRunner { + constructor(traceurTestOptions) { + super({ + ui: 'tdd', + ignoreLeaks: true, + importMetadata: { + traceurOptions: { + sourceMaps: 'memory', + require: true // Some unit tests use require() + } + } + }, traceurTestOptions); + } + + // Reads process arguments, merges defaults options, returns options object. + getOptions() { + let {Command} = require('commander'); + let testOptions = this.defaultOptions(); + let commandLine = + new Command(process.argv[0] + ' ' + process.argv[1]); + + Object.keys(testOptions).forEach((prop) => { + commandLine[prop] = testOptions[prop]; + }); + + commandLine.option('-?, --help', 'Show this help text', () => { + commandLine.help(); + }).usage('[options] [files]') + + // Selected mocha options supported. + commandLine.option('-g, --grep ', 'only run tests matching '). + option('-i, --invert', 'inverts --grep matches'). + option('-b, --bail', 'bail after first test failure'); + + commandLine.command('only [files...]'). + description('only test these [files] ').action( + (file, files) => { + commandLine.patterns = [file]; + if (files) + commandLine.patterns = commandLine.patterns.concat(files); + }); + + commandLine.parse(process.argv); + + return commandLine; + } + + expandPatterns() { + return globPatterns(this.patterns_).then((files) => { + files.forEach((file) => this.addFile(file)); + }); + } + +}; diff --git a/test/modular/TraceurTestRunner.js b/test/modular/TraceurTestRunner.js index 4941c78c9..fd11ffe47 100644 --- a/test/modular/TraceurTestRunner.js +++ b/test/modular/TraceurTestRunner.js @@ -15,22 +15,12 @@ /* @fileoverview Configure mocha for Traceur testing. */ import {Mocha6} from './Mocha6.js'; -let {Command} = require('commander'); -let glob = require('glob'); export class TraceurTestRunner extends Mocha6 { - constructor(defaultOptions) { - super({ - ui: 'tdd', - ignoreLeaks: true, - importMetadata: { - traceurOptions: { - sourceMaps: 'memory', - require: true // Some unit tests use require() - } - } - }); - this.defaultOptions_ = defaultOptions; + constructor(mochaOptions, traceurTestOptions) { + super(mochaOptions); + this.defaultOptions_ = traceurTestOptions || {}; + this.patterns_ = []; } // For derived classes to override. @@ -38,70 +28,34 @@ export class TraceurTestRunner extends Mocha6 { return this.defaultOptions_; } - // Reads process arguments, merges defaults options, returns options object. - parseCommandLine() { - let testOptions = this.defaultOptions(); - let commandLine = - new Command(process.argv[0] + ' ' + process.argv[1]); - - Object.getOwnPropertyNames(testOptions).forEach((prop) => { - commandLine[prop] = testOptions[prop]; - }); - - commandLine.option('-?, --help', 'Show this help text', () => { - commandLine.help(); - }).usage('[options] [files]') - - // Selected mocha options supported. - commandLine.option('-g, --grep ', 'only run tests matching '). - option('-i, --invert', 'inverts --grep matches'). - option('-b, --bail', 'bail after first test failure'); - - commandLine.command('only [files...]'). - description('only test these [files] ').action( - (file, files) => { - commandLine.patterns = [file]; - if (files) - commandLine.patterns = commandLine.patterns.concat(files); - }); - - commandLine.parse(process.argv); - - return commandLine; - } - - applyOptions(testOptions) { + applyOptions(patterns) { // Apply the mocha options + var testOptions = this.getOptions(); if (testOptions.grep) this.grep(new RegExp(testOptions.grep)); if (testOptions.invert) this.invert(); if (testOptions.bail) this.bail(); - - testOptions.patterns.forEach((pattern) => { - let files = glob.sync(pattern, {}); - files.forEach((file) => this.addFile(file)); - }); + this.patterns_ = patterns; } run() { - let failed = 0; - super.run().then((runner) => { - runner.on('fail', (err) => { - failed++; - }); - runner.on('end', () => { - process.exit(failed); + let numberOfFailures = 0; + return this.expandPatterns().then(() => { + return super.run().then((runner) => { + return new Promise((resolve, reject) => { + runner.on('fail', (err) => { + numberOfFailures++; + }); + runner.on('end', () => { + resolve(numberOfFailures); + }); + runner.on('error', (ex) => { + reject(ex); + }); + }); }); - }, (ex) => { - console.log('Test setup FAILED ', ex.stack || ex); - process.exit(failed); }); } - - parseOptionsAndRun() { - this.applyOptions(this.parseCommandLine()); - this.run(); - } }; diff --git a/test/runUnitTests.js b/test/runUnitTests.js index 9a1771333..5714263bc 100644 --- a/test/runUnitTests.js +++ b/test/runUnitTests.js @@ -16,4 +16,10 @@ import {unitTestRunner} from './unit/unitTestRunner.js'; -unitTestRunner.parseOptionsAndRun(); \ No newline at end of file +unitTestRunner.run().then((failures) => { + process.exit(failures); + },(ex) => { + console.log('unitTestRunner FAILED', ex.stack || ex); + process.exit(-1); + } +); diff --git a/test/unit/node/checkRequire.js b/test/unit/node/resources/checkRequire.js similarity index 100% rename from test/unit/node/checkRequire.js rename to test/unit/node/resources/checkRequire.js diff --git a/test/unit/runtime/traceur-interpreter.js b/test/unit/node/traceur-interpreter.js similarity index 96% rename from test/unit/runtime/traceur-interpreter.js rename to test/unit/node/traceur-interpreter.js index 4ef8d085a..b92f3f6b8 100644 --- a/test/unit/runtime/traceur-interpreter.js +++ b/test/unit/node/traceur-interpreter.js @@ -14,7 +14,7 @@ import {suite, test, assert} from '../../unit/unitTestRunner.js'; -suite('interpreter', function(){ +suite('node-only: interpreter', function(){ var exec = require('child_process').exec; var debug = false; diff --git a/test/unit/system/ScriptTypeTextTraceur.html b/test/unit/system/ScriptTypeTextTraceur.html index e229a5262..a975d8436 100644 --- a/test/unit/system/ScriptTypeTextTraceur.html +++ b/test/unit/system/ScriptTypeTextTraceur.html @@ -4,9 +4,6 @@ Hello, World! - - - - - - - - - - - - - - - - - - - - - - + +
-