API Docs for: 0.9.3
Show:

File: lib/abstractTask.js

  1. // Copyright 2014, Yahoo! Inc.
  2. // Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
  3.  
  4. var Base = require('preceptor-core').Base;
  5. var utils = require('preceptor-core').utils;
  6. var logger = require('log4js').getLogger(__filename);
  7. var Promise = require('promise');
  8. var _ = require('underscore');
  9. var uuid = require('uuid');
  10.  
  11. var defaultTask = require('./defaults/defaultTask');
  12.  
  13. /**
  14. * @class AbstractTask
  15. * @extends Base
  16. *
  17. * @property {ReportManager} _reportManager
  18. * @property {object} _plugins
  19. * @property {object} _options
  20. * @property {object} _config
  21. * @property {Collector} _coverageCollector
  22. */
  23. var AbstractTask = Base.extend(
  24.  
  25. /**
  26. * Abstract task constructor
  27. *
  28. * @param {object} config
  29. * @param {Collector} coverageCollector
  30. * @param {ReportManager} reportManager
  31. * @param {object} plugins
  32. * @param {object} plugins.taskDecorator
  33. * @param {object} plugins.clientDecorator
  34. * @param {object} plugins.task
  35. * @param {object} options
  36. * @constructor
  37. */
  38. function (config, coverageCollector, reportManager, plugins, options) {
  39. this.__super();
  40.  
  41. this._config = config;
  42. this._coverageCollector = coverageCollector;
  43. this._reportManager = reportManager;
  44. this._plugins = plugins;
  45. this._options = utils.deepExtend({}, [defaultTask, plugins.sharedOptions || {}, options || {}]);
  46.  
  47. this.initialize();
  48. },
  49.  
  50. {
  51. /**
  52. * Initializes the instance
  53. *
  54. * @method initialize
  55. */
  56. initialize: function () {
  57.  
  58. // Make sure the configuration has the correct structure
  59. this.validate();
  60.  
  61. // Augment options with outside data
  62. this.augment();
  63. },
  64.  
  65.  
  66. /**
  67. * Validates the given data
  68. *
  69. * @method validate
  70. */
  71. validate: function () {
  72. if (!_.isObject(this.getOptions())) {
  73. throw new Error('The options parameter is not an object.');
  74. }
  75. if (!_.isObject(this.getConfiguration())) {
  76. throw new Error('The "configuration" parameter is not an object.');
  77. }
  78. if (!_.isObject(this.getDecorators())) {
  79. throw new Error('The "decorators" parameter is not an object.');
  80. }
  81. if (!_.isString(this.getType())) {
  82. throw new Error('The "type" parameter is not a string.');
  83. }
  84. if (!_.isString(this.getTaskId())) {
  85. throw new Error('The "taskId" parameter is not a string.');
  86. }
  87. if (!_.isString(this.getName())) {
  88. throw new Error('The "name" parameter is not a string.');
  89. }
  90. if (!_.isString(this.getTitle())) {
  91. throw new Error('The "title" parameter is not a string.');
  92. }
  93. if (!_.isBoolean(this.isSuite())) {
  94. throw new Error('The "suite" parameter is not a boolean.');
  95. }
  96. if (!_.isBoolean(this.isActive())) {
  97. throw new Error('The "active" parameter is not a boolean.');
  98. }
  99. if (!_.isBoolean(this.isVerbose())) {
  100. throw new Error('The "verbose" parameter is not a boolean.');
  101. }
  102. if (!_.isBoolean(this.shouldReport())) {
  103. throw new Error('The "report" parameter is not a boolean.');
  104. }
  105. if (!_.isBoolean(this.shouldEchoStdOut())) {
  106. throw new Error('The "echoStdOut" parameter is not a boolean.');
  107. }
  108. if (!_.isBoolean(this.shouldEchoStdErr())) {
  109. throw new Error('The "echoStdErr" parameter is not a boolean.');
  110. }
  111. },
  112.  
  113. /**
  114. * Augments the data with default values
  115. *
  116. * @method augment
  117. */
  118. augment: function () {
  119. // Nothing yet
  120. },
  121.  
  122.  
  123. /**
  124. * Gets the options
  125. *
  126. * @method getOptions
  127. * @return {object}
  128. */
  129. getOptions: function () {
  130. return this._options;
  131. },
  132.  
  133. /**
  134. * Gets the client configuration
  135. *
  136. * @method getConfiguration
  137. * @return {object}
  138. */
  139. getConfiguration: function () {
  140. return this.getOptions().configuration;
  141. },
  142.  
  143. /**
  144. * Gets the decorator list
  145. *
  146. * @method getDecorators
  147. * @return {object[]}
  148. */
  149. getDecorators: function () {
  150. return this.getOptions().decorators;
  151. },
  152.  
  153. /**
  154. * Gets the global configuration
  155. *
  156. * @method getGlobalConfig
  157. * @returns {Object}
  158. */
  159. getGlobalConfig: function () {
  160. return this._config;
  161. },
  162.  
  163. /**
  164. * Gets the coverage collector
  165. *
  166. * @method getCoverageCollector
  167. * @returns {Collector}
  168. */
  169. getCoverageCollector: function () {
  170. return this._coverageCollector;
  171. },
  172.  
  173. /**
  174. * Gets the type of the preceptor task
  175. *
  176. * @method getType
  177. * @return {string}
  178. */
  179. getType: function () {
  180. return this.getOptions().type;
  181. },
  182.  
  183. /**
  184. * Gets a unique id for the task
  185. *
  186. * @method getTaskId
  187. * @return {string}
  188. */
  189. getTaskId: function () {
  190. return this.getOptions().taskId;
  191. },
  192.  
  193. /**
  194. * Gets the name of the preceptor task
  195. *
  196. * @method getName
  197. * @return {string}
  198. */
  199. getName: function () {
  200. return this.getOptions().name;
  201. },
  202.  
  203. /**
  204. * Gets the title of the preceptor task
  205. *
  206. * @method getTitle
  207. * @return {string}
  208. */
  209. getTitle: function () {
  210. return this.getOptions().title;
  211. },
  212.  
  213. /**
  214. * Run tasks in a suite?
  215. *
  216. * @method isSuite
  217. * @return {boolean}
  218. */
  219. isSuite: function () {
  220. return this.getOptions().suite;
  221. },
  222.  
  223. /**
  224. * Is the task in debug-mode?
  225. *
  226. * @method inDebug
  227. * @return {boolean}
  228. */
  229. inDebug: function () {
  230. return this.getOptions().debug;
  231. },
  232.  
  233. /**
  234. * Is the task active?
  235. *
  236. * @method isActive
  237. * @return {boolean}
  238. */
  239. isActive: function () {
  240. return this.getOptions().active;
  241. },
  242.  
  243. /**
  244. * Is the task verbose?
  245. *
  246. * @method isVerbose
  247. * @return {boolean}
  248. */
  249. isVerbose: function () {
  250. return this.getOptions().verbose;
  251. },
  252.  
  253. /**
  254. * Should report?
  255. *
  256. * @method shouldReport
  257. * @return {boolean}
  258. */
  259. shouldReport: function () {
  260. return this.getOptions().report;
  261. },
  262.  
  263. /**
  264. * Echo std-out output of child-process?
  265. *
  266. * @method shouldEchoStdOut
  267. * @return {boolean}
  268. */
  269. shouldEchoStdOut: function () {
  270. return this.getOptions().echoStdOut;
  271. },
  272.  
  273. /**
  274. * Echo std-err output of child-process?
  275. *
  276. * @method shouldEchoStdErr
  277. * @return {boolean}
  278. */
  279. shouldEchoStdErr: function () {
  280. return this.getOptions().echoStdErr;
  281. },
  282.  
  283.  
  284. /**
  285. * Gets all plugins
  286. *
  287. * @method getPlugins
  288. * @return {object}
  289. */
  290. getPlugins: function () {
  291. return this._plugins;
  292. },
  293.  
  294. /**
  295. * Gets all options-decorator plugins
  296. *
  297. * @method getTaskDecoratorPlugins
  298. * @return {AbstractTaskDecorator[]}
  299. */
  300. getTaskDecoratorPlugins: function () {
  301. return _.values(this.getPlugins().taskDecorator);
  302. },
  303.  
  304. /**
  305. * Gets all client-decorator plugins
  306. *
  307. * @method getClientDecoratorPlugins
  308. * @return {object[]}
  309. */
  310. getClientDecoratorPlugins: function () {
  311. return this.getPlugins().clientDecorator;
  312. },
  313.  
  314. /**
  315. * Gets a specific task plugin
  316. *
  317. * @method getTaskPlugin
  318. * @param {string} name
  319. * @return {AbstractTask}
  320. */
  321. getTaskPlugin: function (name) {
  322. return this.getPlugins().task[name.toLowerCase()];
  323. },
  324.  
  325.  
  326. /**
  327. * Gets the report manager
  328. *
  329. * @method getReportManager
  330. * @return {ReportManager}
  331. */
  332. getReportManager: function () {
  333. return this._reportManager;
  334. },
  335.  
  336.  
  337. /**
  338. * Gets the label of the task
  339. *
  340. * @method getLabel
  341. * @return {string}
  342. */
  343. getLabel: function () {
  344. return this.getName() + '-' + this.getTaskId();
  345. },
  346.  
  347.  
  348. /**
  349. * Run the task
  350. *
  351. * @method run
  352. * @param {string} parentId
  353. * @return {Promise}
  354. */
  355. run: function (parentId) {
  356.  
  357. var suiteId = parentId,
  358. promise;
  359.  
  360. if (this.isActive()) {
  361.  
  362. // Should task be wrapped in a suite? Start it
  363. if (this.isSuite()) {
  364. suiteId = 'group-' + uuid.v4(); // Generate unique-id
  365. this.getReportManager().message().suiteStart(suiteId, parentId, this.getTitle());
  366. }
  367.  
  368. // Run task
  369. promise = this._run(suiteId);
  370.  
  371. // Should tasks be wrapped in a suite? Finish it up
  372. if (this.isSuite()) {
  373. promise = promise.then(function () {
  374. this.getReportManager().message().suiteEnd(suiteId);
  375. }.bind(this), function (err) {
  376. this.getReportManager().message().suiteEnd(suiteId);
  377. throw err;
  378. }.bind(this));
  379. }
  380.  
  381. return promise;
  382.  
  383. } else {
  384. logger.debug('Skip task since it is inactive. ' + this.getLabel());
  385. return Promise.resolve();
  386. }
  387. },
  388.  
  389. /**
  390. * Run the task
  391. *
  392. * @method _run
  393. * @param {string} parentId
  394. * @return {Promise}
  395. * @private
  396. */
  397. _run: function (parentId) {
  398. throw new Error('Unimplemented task function "run".');
  399. }
  400. },
  401.  
  402. {
  403. /**
  404. * @property TYPE
  405. * @type {string}
  406. * @static
  407. */
  408. TYPE: 'AbstractTask'
  409. });
  410.  
  411. module.exports = AbstractTask;
  412.