taskRunner.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const child_process = require("child_process");
  4. const events_1 = require("events");
  5. const q = require("q");
  6. const configParser_1 = require("./configParser");
  7. const runner_1 = require("./runner");
  8. const taskLogger_1 = require("./taskLogger");
  9. /**
  10. * A runner for running a specified task (capabilities + specs).
  11. * The TaskRunner can either run the task from the current process (via
  12. * './runner.js') or from a new process (via './runnerCli.js').
  13. *
  14. * @constructor
  15. * @param {string} configFile Path of test configuration.
  16. * @param {object} additionalConfig Additional configuration.
  17. * @param {object} task Task to run.
  18. * @param {boolean} runInFork Whether to run test in a forked process.
  19. * @constructor
  20. */
  21. class TaskRunner extends events_1.EventEmitter {
  22. constructor(configFile, additionalConfig, task, runInFork) {
  23. super();
  24. this.configFile = configFile;
  25. this.additionalConfig = additionalConfig;
  26. this.task = task;
  27. this.runInFork = runInFork;
  28. }
  29. /**
  30. * Sends the run command.
  31. * @return {q.Promise} A promise that will resolve when the task finishes
  32. * running. The promise contains the following parameters representing the
  33. * result of the run:
  34. * taskId, specs, capabilities, failedCount, exitCode, specResults
  35. */
  36. run() {
  37. let runResults = {
  38. taskId: this.task.taskId,
  39. specs: this.task.specs,
  40. capabilities: this.task.capabilities,
  41. // The following are populated while running the test:
  42. failedCount: 0,
  43. exitCode: -1,
  44. specResults: []
  45. };
  46. let configParser = new configParser_1.ConfigParser();
  47. if (this.configFile) {
  48. configParser.addFileConfig(this.configFile);
  49. }
  50. if (this.additionalConfig) {
  51. configParser.addConfig(this.additionalConfig);
  52. }
  53. let config = configParser.getConfig();
  54. config.capabilities = this.task.capabilities;
  55. config.specs = this.task.specs;
  56. if (this.runInFork) {
  57. let deferred = q.defer();
  58. let childProcess = child_process.fork(__dirname + '/runnerCli.js', process.argv.slice(2), { cwd: process.cwd(), silent: true });
  59. let taskLogger = new taskLogger_1.TaskLogger(this.task, childProcess.pid);
  60. // stdout pipe
  61. childProcess.stdout.on('data', (data) => {
  62. taskLogger.log(data);
  63. });
  64. // stderr pipe
  65. childProcess.stderr.on('data', (data) => {
  66. taskLogger.log(data);
  67. });
  68. childProcess
  69. .on('message', (m) => {
  70. if (config.verboseMultiSessions) {
  71. taskLogger.peek();
  72. }
  73. switch (m.event) {
  74. case 'testPass':
  75. process.stdout.write('.');
  76. break;
  77. case 'testFail':
  78. process.stdout.write('F');
  79. break;
  80. case 'testsDone':
  81. runResults.failedCount = m.results.failedCount;
  82. runResults.specResults = m.results.specResults;
  83. break;
  84. }
  85. })
  86. .on('error', (err) => {
  87. taskLogger.flush();
  88. deferred.reject(err);
  89. })
  90. .on('exit', (code) => {
  91. taskLogger.flush();
  92. runResults.exitCode = code;
  93. deferred.resolve(runResults);
  94. });
  95. childProcess.send({
  96. command: 'run',
  97. configFile: this.configFile,
  98. additionalConfig: this.additionalConfig,
  99. capabilities: this.task.capabilities,
  100. specs: this.task.specs
  101. });
  102. return deferred.promise;
  103. }
  104. else {
  105. let runner = new runner_1.Runner(config);
  106. runner.on('testsDone', (results) => {
  107. runResults.failedCount = results.failedCount;
  108. runResults.specResults = results.specResults;
  109. });
  110. return runner.run().then((exitCode) => {
  111. runResults.exitCode = exitCode;
  112. return runResults;
  113. });
  114. }
  115. }
  116. }
  117. exports.TaskRunner = TaskRunner;
  118. //# sourceMappingURL=taskRunner.js.map