addon-sdk/source/test/test-require.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 'use strict';
michael@0 6
michael@0 7 const traceback = require('sdk/console/traceback');
michael@0 8 const REQUIRE_LINE_NO = 30;
michael@0 9
michael@0 10 exports.test_no_args = function(assert) {
michael@0 11 let passed = tryRequireModule(assert);
michael@0 12 assert.ok(passed, 'require() with no args should raise helpful error');
michael@0 13 };
michael@0 14
michael@0 15 exports.test_invalid_sdk_module = function (assert) {
michael@0 16 let passed = tryRequireModule(assert, 'sdk/does-not-exist');
michael@0 17 assert.ok(passed, 'require() with an invalid sdk module should raise');
michael@0 18 };
michael@0 19
michael@0 20 exports.test_invalid_relative_module = function (assert) {
michael@0 21 let passed = tryRequireModule(assert, './does-not-exist');
michael@0 22 assert.ok(passed, 'require() with an invalid relative module should raise');
michael@0 23 };
michael@0 24
michael@0 25
michael@0 26 function tryRequireModule(assert, module) {
michael@0 27 let passed = false;
michael@0 28 try {
michael@0 29 // This line number is important, referenced in REQUIRE_LINE_NO
michael@0 30 let doesNotExist = require(module);
michael@0 31 } catch(e) {
michael@0 32 checkError(assert, module, e);
michael@0 33 passed = true;
michael@0 34 }
michael@0 35 return passed;
michael@0 36 }
michael@0 37
michael@0 38 function checkError (assert, name, e) {
michael@0 39 let msg = e.toString();
michael@0 40 if (name) {
michael@0 41 assert.ok(/is not found at/.test(msg),
michael@0 42 'Error message indicates module not found');
michael@0 43 assert.ok(msg.indexOf(name.replace(/\./g,'')) > -1,
michael@0 44 'Error message has the invalid module name in the message');
michael@0 45 }
michael@0 46 else {
michael@0 47 assert.equal(msg.indexOf('Error: you must provide a module name when calling require() from '), 0);
michael@0 48 assert.ok(msg.indexOf("test-require") !== -1, msg);
michael@0 49 }
michael@0 50
michael@0 51 // we'd also like to assert that the right filename
michael@0 52 // and linenumber is in the stacktrace
michael@0 53 let tb = traceback.fromException(e);
michael@0 54 // Get the second to last frame, as the last frame is inside
michael@0 55 // toolkit/loader
michael@0 56 let lastFrame = tb[tb.length-2];
michael@0 57 assert.ok(lastFrame.fileName.indexOf("test-require.js") !== -1,
michael@0 58 'Filename found in stacktrace');
michael@0 59 assert.equal(lastFrame.lineNumber, REQUIRE_LINE_NO,
michael@0 60 'stacktrace has correct line number');
michael@0 61 }
michael@0 62
michael@0 63 require('test').run(exports);

mercurial