8
0

tasks.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global describe, it, beforeEach, afterEach */
  6. 'use strict';
  7. const mockery = require( 'mockery' );
  8. const sinon = require( 'sinon' );
  9. const chai = require( 'chai' );
  10. const expect = chai.expect;
  11. describe( 'exec-tasks', () => {
  12. let sandbox;
  13. const config = {
  14. WORKSPACE_DIR: '/path/exec/'
  15. };
  16. beforeEach( () => {
  17. mockery.enable( {
  18. useCleanCache: true,
  19. warnOnReplace: false,
  20. warnOnUnregistered: false
  21. } );
  22. sandbox = sinon.sandbox.create();
  23. } );
  24. afterEach( () => {
  25. mockery.disable();
  26. sandbox.restore();
  27. } );
  28. describe( 'execOnRepositories', () => {
  29. it( 'should throw error when there is no specified task', () => {
  30. const errorMessage = 'Missing task parameter: --task task-name';
  31. const log = require( '../../utils/log' );
  32. const logErrSpy = sandbox.spy( log, 'err' );
  33. mockery.registerMock( 'minimist', () => {
  34. return { };
  35. } );
  36. const tasks = require( '../../tasks/exec/tasks' )( config );
  37. tasks.execOnRepositories();
  38. sinon.assert.calledOnce( logErrSpy );
  39. expect( logErrSpy.firstCall.args[ 0 ] ).to.be.an( 'error' );
  40. expect( logErrSpy.firstCall.args[ 0 ].message ).to.equal( errorMessage );
  41. } );
  42. it( 'should throw error when task cannot be found', () => {
  43. const log = require( '../../utils/log' );
  44. const logErrSpy = sandbox.spy( log, 'err' );
  45. mockery.registerMock( 'minimist', () => {
  46. return { task: 'task-to-run' };
  47. } );
  48. const tasks = require( '../../tasks/exec/tasks' )( config );
  49. tasks.execOnRepositories();
  50. sinon.assert.calledOnce( logErrSpy );
  51. expect( logErrSpy.firstCall.args[ 0 ] ).to.be.an( 'error' );
  52. expect( logErrSpy.firstCall.args[ 0 ].code ).to.equal( 'MODULE_NOT_FOUND' );
  53. } );
  54. it( 'should load task module', () => {
  55. const ckeditor5Dirs = require( '../../utils/ckeditor5-dirs' );
  56. const log = require( '../../utils/log' );
  57. const logErrSpy = sandbox.spy( log, 'err' );
  58. sandbox.stub( ckeditor5Dirs, 'getDevDirectories' ).returns( [] );
  59. mockery.registerMock( 'minimist', () => {
  60. return { task: 'task-to-run' };
  61. } );
  62. mockery.registerMock( './functions/task-to-run', () => {} );
  63. const tasks = require( '../../tasks/exec/tasks' )( config );
  64. tasks.execOnRepositories();
  65. sinon.assert.notCalled( logErrSpy );
  66. } );
  67. it( 'should execute task over directories', () => {
  68. const ckeditor5Dirs = require( '../../utils/ckeditor5-dirs' );
  69. const taskStub = sinon.stub();
  70. mockery.registerMock( 'minimist', () => {
  71. return { task: 'task-to-run' };
  72. } );
  73. sandbox.stub( ckeditor5Dirs, 'getDevDirectories' ).returns( [
  74. {
  75. repositoryPath: '/path/1',
  76. repositoryURL: 'repo/test1'
  77. },
  78. {
  79. repositoryPath: '/path/2',
  80. repositoryURL: 'repo/test2'
  81. }
  82. ] );
  83. mockery.registerMock( './functions/task-to-run', taskStub );
  84. const tasks = require( '../../tasks/exec/tasks' )( config );
  85. tasks.execOnRepositories();
  86. sinon.assert.calledTwice( taskStub );
  87. sinon.assert.calledWith( taskStub, '/path/1', { task: 'task-to-run' } );
  88. sinon.assert.calledWith( taskStub, '/path/2', { task: 'task-to-run' } );
  89. } );
  90. } );
  91. } );