michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: const traceback = require('sdk/console/traceback'); michael@0: const REQUIRE_LINE_NO = 30; michael@0: michael@0: exports.test_no_args = function(assert) { michael@0: let passed = tryRequireModule(assert); michael@0: assert.ok(passed, 'require() with no args should raise helpful error'); michael@0: }; michael@0: michael@0: exports.test_invalid_sdk_module = function (assert) { michael@0: let passed = tryRequireModule(assert, 'sdk/does-not-exist'); michael@0: assert.ok(passed, 'require() with an invalid sdk module should raise'); michael@0: }; michael@0: michael@0: exports.test_invalid_relative_module = function (assert) { michael@0: let passed = tryRequireModule(assert, './does-not-exist'); michael@0: assert.ok(passed, 'require() with an invalid relative module should raise'); michael@0: }; michael@0: michael@0: michael@0: function tryRequireModule(assert, module) { michael@0: let passed = false; michael@0: try { michael@0: // This line number is important, referenced in REQUIRE_LINE_NO michael@0: let doesNotExist = require(module); michael@0: } catch(e) { michael@0: checkError(assert, module, e); michael@0: passed = true; michael@0: } michael@0: return passed; michael@0: } michael@0: michael@0: function checkError (assert, name, e) { michael@0: let msg = e.toString(); michael@0: if (name) { michael@0: assert.ok(/is not found at/.test(msg), michael@0: 'Error message indicates module not found'); michael@0: assert.ok(msg.indexOf(name.replace(/\./g,'')) > -1, michael@0: 'Error message has the invalid module name in the message'); michael@0: } michael@0: else { michael@0: assert.equal(msg.indexOf('Error: you must provide a module name when calling require() from '), 0); michael@0: assert.ok(msg.indexOf("test-require") !== -1, msg); michael@0: } michael@0: michael@0: // we'd also like to assert that the right filename michael@0: // and linenumber is in the stacktrace michael@0: let tb = traceback.fromException(e); michael@0: // Get the second to last frame, as the last frame is inside michael@0: // toolkit/loader michael@0: let lastFrame = tb[tb.length-2]; michael@0: assert.ok(lastFrame.fileName.indexOf("test-require.js") !== -1, michael@0: 'Filename found in stacktrace'); michael@0: assert.equal(lastFrame.lineNumber, REQUIRE_LINE_NO, michael@0: 'stacktrace has correct line number'); michael@0: } michael@0: michael@0: require('test').run(exports);