tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_require.js (2944B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test require
      7 
      8 // Ensure that DevtoolsLoader.require doesn't spawn multiple
      9 // loader/modules when early cached
     10 function testBug1091706() {
     11  const loader = new DevToolsLoader();
     12  const require = loader.require;
     13 
     14  const indent1 = require("resource://devtools/shared/indentation.js");
     15  const indent2 = require("resource://devtools/shared/indentation.js");
     16 
     17  Assert.strictEqual(indent1, indent2);
     18 }
     19 
     20 function testInvalidModule() {
     21  const loader = new DevToolsLoader();
     22  const require = loader.require;
     23 
     24  try {
     25    // This will result in an invalid URL with no scheme and mae loadSubScript
     26    // throws "Error creating URI" error
     27    require("foo");
     28    Assert.ok(false, "require should throw");
     29  } catch (error) {
     30    Assert.equal(error.message, "Module `foo` is not found at foo.js");
     31    Assert.ok(
     32      error.stack.includes("testInvalidModule"),
     33      "Exception's stack includes the test function"
     34    );
     35  }
     36 
     37  try {
     38    // But when using devtools prefix, the URL is going to be correct but the file
     39    // doesn't exists, leading to "Error opening input stream (invalid filename?)" error
     40    require("resource://devtools/foo.js");
     41    Assert.ok(false, "require should throw");
     42  } catch (error) {
     43    Assert.equal(
     44      error.message,
     45      "Module `resource://devtools/foo.js` is not found at resource://devtools/foo.js"
     46    );
     47    Assert.ok(
     48      error.stack.includes("testInvalidModule"),
     49      "Exception's stack includes the test function"
     50    );
     51  }
     52 }
     53 
     54 function testThrowingModule() {
     55  const loader = new DevToolsLoader();
     56  const require = loader.require;
     57 
     58  try {
     59    // Require a test module that is throwing an Error object
     60    require("xpcshell-test/throwing-module-1.js");
     61    Assert.ok(false, "require should throw");
     62  } catch (error) {
     63    Assert.equal(error.message, "my-exception");
     64    Assert.ok(
     65      error.stack.includes("testThrowingModule"),
     66      "Exception's stack includes the test function"
     67    );
     68    Assert.ok(
     69      error.stack.includes("throwingMethod"),
     70      "Exception's stack also includes the module function that throws"
     71    );
     72  }
     73  try {
     74    // Require a test module that is throwing a string
     75    require("xpcshell-test/throwing-module-2.js");
     76    Assert.ok(false, "require should throw");
     77  } catch (error) {
     78    Assert.equal(
     79      error.message,
     80      "Error while loading module `xpcshell-test/throwing-module-2.js` at " +
     81        "resource://test/throwing-module-2.js:\nmy-exception"
     82    );
     83    Assert.ok(
     84      error.stack.includes("testThrowingModule"),
     85      "Exception's stack includes the test function"
     86    );
     87    Assert.ok(
     88      !error.stack.includes("throwingMethod"),
     89      "Exception's stack also includes the module function that throws"
     90    );
     91  }
     92 }
     93 
     94 function run_test() {
     95  testBug1091706();
     96 
     97  testInvalidModule();
     98 
     99  testThrowingModule();
    100 }