addon-sdk/source/lib/sdk/deprecated/unit-test-finder.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/deprecated/unit-test-finder.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     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 +
     1.8 +"use strict";
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "deprecated"
    1.12 +};
    1.13 +
    1.14 +const file = require("../io/file");
    1.15 +const memory = require('./memory');
    1.16 +const suites = require('@test/options').allTestModules;
    1.17 +const { Loader } = require("sdk/test/loader");
    1.18 +const cuddlefish = require("sdk/loader/cuddlefish");
    1.19 +
    1.20 +let loader = Loader(module);
    1.21 +const NOT_TESTS = ['setup', 'teardown'];
    1.22 +
    1.23 +var TestFinder = exports.TestFinder = function TestFinder(options) {
    1.24 +  memory.track(this);
    1.25 +  this.filter = options.filter;
    1.26 +  this.testInProcess = options.testInProcess === false ? false : true;
    1.27 +  this.testOutOfProcess = options.testOutOfProcess === true ? true : false;
    1.28 +};
    1.29 +
    1.30 +TestFinder.prototype = {
    1.31 +  findTests: function findTests(cb) {
    1.32 +    var self = this;
    1.33 +    var tests = [];
    1.34 +    var filter;
    1.35 +    // A filter string is {fileNameRegex}[:{testNameRegex}] - ie, a colon
    1.36 +    // optionally separates a regex for the test fileName from a regex for the
    1.37 +    // testName.
    1.38 +    if (this.filter) {
    1.39 +      var colonPos = this.filter.indexOf(':');
    1.40 +      var filterFileRegex, filterNameRegex;
    1.41 +      if (colonPos === -1) {
    1.42 +        filterFileRegex = new RegExp(self.filter);
    1.43 +      } else {
    1.44 +        filterFileRegex = new RegExp(self.filter.substr(0, colonPos));
    1.45 +        filterNameRegex = new RegExp(self.filter.substr(colonPos + 1));
    1.46 +      }
    1.47 +      // This function will first be called with just the filename; if
    1.48 +      // it returns true the module will be loaded then the function
    1.49 +      // called again with both the filename and the testname.
    1.50 +      filter = function(filename, testname) {
    1.51 +        return filterFileRegex.test(filename) &&
    1.52 +               ((testname && filterNameRegex) ? filterNameRegex.test(testname)
    1.53 +                                              : true);
    1.54 +      };
    1.55 +    } else
    1.56 +      filter = function() {return true};
    1.57 +
    1.58 +    suites.forEach(function(suite) {
    1.59 +        // Load each test file as a main module in its own loader instance
    1.60 +        // `suite` is defined by cuddlefish/manifest.py:ManifestBuilder.build
    1.61 +
    1.62 +        let suiteModule;
    1.63 +
    1.64 +        try {
    1.65 +          suiteModule = cuddlefish.main(loader, suite);
    1.66 +        }
    1.67 +        catch (e) {
    1.68 +          if (!/^Unsupported Application/.test(e.message))
    1.69 +            throw e;
    1.70 +          // If `Unsupported Application` error thrown during test,
    1.71 +          // skip the test suite
    1.72 +          suiteModule = {
    1.73 +            'test suite skipped': assert => assert.pass(e.message)
    1.74 +          };
    1.75 +        }
    1.76 +
    1.77 +        if (self.testInProcess)
    1.78 +          for each (let name in Object.keys(suiteModule).sort()) {
    1.79 +            if(NOT_TESTS.indexOf(name) === -1 && filter(suite, name)) {
    1.80 +              tests.push({
    1.81 +                           setup: suiteModule.setup,
    1.82 +                           teardown: suiteModule.teardown,
    1.83 +                           testFunction: suiteModule[name],
    1.84 +                           name: suite + "." + name
    1.85 +                         });
    1.86 +            }
    1.87 +          }
    1.88 +      });
    1.89 +
    1.90 +    cb(tests);
    1.91 +  }
    1.92 +};

mercurial