unit-test-finder.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "deprecated"
  7. };
  8. const file = require("../io/file");
  9. const memory = require('./memory');
  10. const suites = require('@test/options').allTestModules;
  11. const { Loader } = require("sdk/test/loader");
  12. const cuddlefish = require("sdk/loader/cuddlefish");
  13. let loader = Loader(module);
  14. const NOT_TESTS = ['setup', 'teardown'];
  15. var TestFinder = exports.TestFinder = function TestFinder(options) {
  16. memory.track(this);
  17. this.filter = options.filter;
  18. this.testInProcess = options.testInProcess === false ? false : true;
  19. this.testOutOfProcess = options.testOutOfProcess === true ? true : false;
  20. };
  21. TestFinder.prototype = {
  22. findTests: function findTests(cb) {
  23. var self = this;
  24. var tests = [];
  25. var filter;
  26. // A filter string is {fileNameRegex}[:{testNameRegex}] - ie, a colon
  27. // optionally separates a regex for the test fileName from a regex for the
  28. // testName.
  29. if (this.filter) {
  30. var colonPos = this.filter.indexOf(':');
  31. var filterFileRegex, filterNameRegex;
  32. if (colonPos === -1) {
  33. filterFileRegex = new RegExp(self.filter);
  34. } else {
  35. filterFileRegex = new RegExp(self.filter.substr(0, colonPos));
  36. filterNameRegex = new RegExp(self.filter.substr(colonPos + 1));
  37. }
  38. // This function will first be called with just the filename; if
  39. // it returns true the module will be loaded then the function
  40. // called again with both the filename and the testname.
  41. filter = function(filename, testname) {
  42. return filterFileRegex.test(filename) &&
  43. ((testname && filterNameRegex) ? filterNameRegex.test(testname)
  44. : true);
  45. };
  46. } else
  47. filter = function() {return true};
  48. suites.forEach(function(suite) {
  49. // Load each test file as a main module in its own loader instance
  50. // `suite` is defined by cuddlefish/manifest.py:ManifestBuilder.build
  51. let suiteModule;
  52. try {
  53. suiteModule = cuddlefish.main(loader, suite);
  54. }
  55. catch (e) {
  56. if (!/^Unsupported Application/.test(e.message))
  57. throw e;
  58. // If `Unsupported Application` error thrown during test,
  59. // skip the test suite
  60. suiteModule = {
  61. 'test suite skipped': assert => assert.pass(e.message)
  62. };
  63. }
  64. if (self.testInProcess)
  65. for each (let name in Object.keys(suiteModule).sort()) {
  66. if(NOT_TESTS.indexOf(name) === -1 && filter(suite, name)) {
  67. tests.push({
  68. setup: suiteModule.setup,
  69. teardown: suiteModule.teardown,
  70. testFunction: suiteModule[name],
  71. name: suite + "." + name
  72. });
  73. }
  74. }
  75. });
  76. cb(tests);
  77. }
  78. };