run.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict';
  2. var _ = require('lodash');
  3. var debug = require('debug')('cypress:cli:run');
  4. var util = require('../util');
  5. var spawn = require('./spawn');
  6. var verify = require('../tasks/verify');
  7. var _require = require('../errors'),
  8. exitWithError = _require.exitWithError,
  9. errors = _require.errors;
  10. // maps options collected by the CLI
  11. // and forms list of CLI arguments to the server
  12. var processRunOptions = function processRunOptions() {
  13. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  14. debug('processing run options %o', options);
  15. var args = ['--run-project', options.project];
  16. if (options.browser) {
  17. args.push('--browser', options.browser);
  18. }
  19. if (options.ci) {
  20. // push to display the deprecation message
  21. args.push('--ci');
  22. // also automatically record
  23. args.push('--record', true);
  24. }
  25. if (options.ciBuildId) {
  26. args.push('--ci-build-id', options.ciBuildId);
  27. }
  28. if (options.config) {
  29. args.push('--config', options.config);
  30. }
  31. if (options.configFile !== undefined) {
  32. args.push('--config-file', options.configFile);
  33. }
  34. if (options.env) {
  35. args.push('--env', options.env);
  36. }
  37. if (options.exit === false) {
  38. args.push('--no-exit');
  39. }
  40. if (options.group) {
  41. args.push('--group', options.group);
  42. }
  43. if (options.headed) {
  44. args.push('--headed', options.headed);
  45. }
  46. if (options.headless) {
  47. if (options.headed) {
  48. // throw this error synchronously, it will be caught later on and
  49. // the details will be propagated to the promise chain
  50. var err = new Error();
  51. err.details = errors.incompatibleHeadlessFlags;
  52. throw err;
  53. }
  54. args.push('--headed', !options.headless);
  55. }
  56. // if key is set use that - else attempt to find it by environment variable
  57. if (options.key == null) {
  58. debug('--key is not set, looking up environment variable CYPRESS_RECORD_KEY');
  59. options.key = util.getEnv('CYPRESS_RECORD_KEY') || util.getEnv('CYPRESS_CI_KEY');
  60. }
  61. // if we have a key assume we're in record mode
  62. if (options.key) {
  63. args.push('--key', options.key);
  64. }
  65. if (options.outputPath) {
  66. args.push('--output-path', options.outputPath);
  67. }
  68. if (options.parallel) {
  69. args.push('--parallel');
  70. }
  71. if (options.port) {
  72. args.push('--port', options.port);
  73. }
  74. // if record is defined and we're not
  75. // already in ci mode, then send it up
  76. if (options.record != null && !options.ci) {
  77. args.push('--record', options.record);
  78. }
  79. // if we have a specific reporter push that into the args
  80. if (options.reporter) {
  81. args.push('--reporter', options.reporter);
  82. }
  83. // if we have a specific reporter push that into the args
  84. if (options.reporterOptions) {
  85. args.push('--reporter-options', options.reporterOptions);
  86. }
  87. // if we have specific spec(s) push that into the args
  88. if (options.spec) {
  89. args.push('--spec', options.spec);
  90. }
  91. if (options.tag) {
  92. args.push('--tag', options.tag);
  93. }
  94. return args;
  95. };
  96. module.exports = {
  97. processRunOptions: processRunOptions,
  98. // resolves with the number of failed tests
  99. start: function start() {
  100. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  101. _.defaults(options, {
  102. key: null,
  103. spec: null,
  104. reporter: null,
  105. reporterOptions: null,
  106. project: process.cwd()
  107. });
  108. function run() {
  109. var args = void 0;
  110. try {
  111. args = processRunOptions(options);
  112. } catch (err) {
  113. if (err.details) {
  114. return exitWithError(err.details)();
  115. }
  116. throw err;
  117. }
  118. debug('run to spawn.start args %j', args);
  119. return spawn.start(args, {
  120. dev: options.dev
  121. });
  122. }
  123. if (options.dev) {
  124. return run();
  125. }
  126. return verify.start().then(run);
  127. }
  128. };