addon-sdk/source/lib/sdk/test/runner.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/test/runner.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +module.metadata = {
    1.10 +  "stability": "experimental"
    1.11 +};
    1.12 +
    1.13 +var { exit, stdout } = require("../system");
    1.14 +var cfxArgs = require("@test/options");
    1.15 +
    1.16 +function runTests(findAndRunTests) {
    1.17 +  var harness = require("./harness");
    1.18 +
    1.19 +  function onDone(tests) {
    1.20 +    stdout.write("\n");
    1.21 +    var total = tests.passed + tests.failed;
    1.22 +    stdout.write(tests.passed + " of " + total + " tests passed.\n");
    1.23 +
    1.24 +    if (tests.failed == 0) {
    1.25 +      if (tests.passed === 0)
    1.26 +        stdout.write("No tests were run\n");
    1.27 +      exit(0);
    1.28 +    } else {
    1.29 +      if (cfxArgs.verbose || cfxArgs.parseable)
    1.30 +        printFailedTests(tests, stdout.write);
    1.31 +      exit(1);
    1.32 +    }
    1.33 +  };
    1.34 +
    1.35 +  // We may have to run test on next cycle, otherwise XPCOM components
    1.36 +  // are not correctly updated.
    1.37 +  // For ex: nsIFocusManager.getFocusedElementForWindow may throw
    1.38 +  // NS_ERROR_ILLEGAL_VALUE exception.
    1.39 +  require("../timers").setTimeout(function () {
    1.40 +    harness.runTests({
    1.41 +      findAndRunTests: findAndRunTests,
    1.42 +      iterations: cfxArgs.iterations || 1,
    1.43 +      filter: cfxArgs.filter,
    1.44 +      profileMemory: cfxArgs.profileMemory,
    1.45 +      stopOnError: cfxArgs.stopOnError,
    1.46 +      verbose: cfxArgs.verbose,
    1.47 +      parseable: cfxArgs.parseable,
    1.48 +      print: stdout.write,
    1.49 +      onDone: onDone
    1.50 +    });
    1.51 +  }, 0);
    1.52 +}
    1.53 +
    1.54 +function printFailedTests(tests, print) {
    1.55 +  let iterationNumber = 0;
    1.56 +  let singleIteration = tests.testRuns.length == 1;
    1.57 +  let padding = singleIteration ? "" : "  ";
    1.58 +
    1.59 +  print("\nThe following tests failed:\n");
    1.60 +
    1.61 +  for each (let testRun in tests.testRuns) {
    1.62 +    iterationNumber++;
    1.63 +
    1.64 +    if (!singleIteration)
    1.65 +      print("  Iteration " + iterationNumber + ":\n");
    1.66 +
    1.67 +    for each (let test in testRun) {
    1.68 +      if (test.failed > 0) {
    1.69 +        print(padding + "  " + test.name + ": " + test.errors +"\n");
    1.70 +      }
    1.71 +    }
    1.72 +    print("\n");
    1.73 +  }
    1.74 +}
    1.75 +
    1.76 +function main() {
    1.77 +  var testsStarted = false;
    1.78 +
    1.79 +  if (!testsStarted) {
    1.80 +    testsStarted = true;
    1.81 +    runTests(function findAndRunTests(loader, nextIteration) {
    1.82 +      loader.require("../deprecated/unit-test").findAndRunTests({
    1.83 +        testOutOfProcess: false,
    1.84 +        testInProcess: true,
    1.85 +        stopOnError: cfxArgs.stopOnError,
    1.86 +        filter: cfxArgs.filter,
    1.87 +        onDone: nextIteration
    1.88 +      });
    1.89 +    });
    1.90 +  }
    1.91 +};
    1.92 +
    1.93 +if (require.main === module)
    1.94 +  main();
    1.95 +
    1.96 +exports.runTestsFromModule = function runTestsFromModule(module) {
    1.97 +  let id = module.id;
    1.98 +  // Make a copy of exports as it may already be frozen by module loader
    1.99 +  let exports = {};
   1.100 +  Object.keys(module.exports).forEach(function(key) {
   1.101 +    exports[key] = module.exports[key];
   1.102 +  });
   1.103 +
   1.104 +  runTests(function findAndRunTests(loader, nextIteration) {
   1.105 +    // Consider that all these tests are CommonJS ones
   1.106 +    loader.require('../../test').run(exports);
   1.107 +
   1.108 +    // Reproduce what is done in unit-test-finder.findTests()
   1.109 +    let tests = [];
   1.110 +    for each (let name in Object.keys(exports).sort()) {
   1.111 +      tests.push({
   1.112 +        setup: exports.setup,
   1.113 +        teardown: exports.teardown,
   1.114 +        testFunction: exports[name],
   1.115 +        name: id + "." + name
   1.116 +      });
   1.117 +    }
   1.118 +
   1.119 +    // Reproduce what is done by unit-test.findAndRunTests()
   1.120 +    var { TestRunner } = loader.require("../deprecated/unit-test");
   1.121 +    var runner = new TestRunner();
   1.122 +    runner.startMany({
   1.123 +      tests: tests,
   1.124 +      stopOnError: cfxArgs.stopOnError,
   1.125 +      onDone: nextIteration
   1.126 +    });
   1.127 +  });
   1.128 +}

mercurial