8
0

tasks.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. const getDevDirectoriesResult = [
  17. {
  18. repositoryPath: '/path/1',
  19. repositoryURL: 'ckeditor/test1'
  20. },
  21. {
  22. repositoryPath: '/path/2',
  23. repositoryURL: 'ckeditor/test2'
  24. }
  25. ];
  26. beforeEach( () => {
  27. mockery.enable( {
  28. useCleanCache: true,
  29. warnOnReplace: false,
  30. warnOnUnregistered: false
  31. } );
  32. sandbox = sinon.sandbox.create();
  33. } );
  34. afterEach( () => {
  35. mockery.disable();
  36. sandbox.restore();
  37. } );
  38. describe( 'execOnRepositories', () => {
  39. it( 'should throw error when there is no specified task', () => {
  40. const errorMessage = 'Missing task parameter: --task task-name';
  41. const log = require( '../../utils/log' );
  42. const logErrSpy = sandbox.stub( log, 'err' );
  43. mockery.registerMock( 'minimist', () => {
  44. return { };
  45. } );
  46. const tasks = require( '../../tasks/exec/tasks' )( config );
  47. tasks.execOnRepositories();
  48. sinon.assert.calledOnce( logErrSpy );
  49. expect( logErrSpy.firstCall.args[ 0 ] ).to.be.an( 'error' );
  50. expect( logErrSpy.firstCall.args[ 0 ].message ).to.equal( errorMessage );
  51. } );
  52. it( 'should throw error when task cannot be found', () => {
  53. const log = require( '../../utils/log' );
  54. const logErrSpy = sandbox.stub( log, 'err' );
  55. mockery.registerMock( 'minimist', () => {
  56. return { task: 'task-to-run' };
  57. } );
  58. const tasks = require( '../../tasks/exec/tasks' )( config );
  59. tasks.execOnRepositories();
  60. sinon.assert.calledOnce( logErrSpy );
  61. expect( logErrSpy.firstCall.args[ 0 ] ).to.be.an( 'error' );
  62. expect( logErrSpy.firstCall.args[ 0 ].code ).to.equal( 'MODULE_NOT_FOUND' );
  63. } );
  64. it( 'should load task module', () => {
  65. const ckeditor5Dirs = require( '../../utils/ckeditor5-dirs' );
  66. const log = require( '../../utils/log' );
  67. const logErrSpy = sandbox.stub( log, 'err' );
  68. sandbox.stub( ckeditor5Dirs, 'getDevDirectories' ).returns( [] );
  69. mockery.registerMock( 'minimist', () => {
  70. return { task: 'task-to-run' };
  71. } );
  72. mockery.registerMock( './functions/task-to-run', () => {} );
  73. const tasks = require( '../../tasks/exec/tasks' )( config );
  74. tasks.execOnRepositories();
  75. sinon.assert.notCalled( logErrSpy );
  76. } );
  77. it( 'should log error when task is throwing exceptions', () => {
  78. const ckeditor5Dirs = require( '../../utils/ckeditor5-dirs' );
  79. const taskStub = sinon.stub();
  80. const log = require( '../../utils/log' );
  81. const logErrSpy = sandbox.stub( log, 'err' );
  82. taskStub.onSecondCall().throws();
  83. mockery.registerMock( 'minimist', () => {
  84. return { task: 'task-to-run' };
  85. } );
  86. sandbox.stub( ckeditor5Dirs, 'getDevDirectories' ).returns( getDevDirectoriesResult );
  87. mockery.registerMock( './functions/task-to-run', taskStub );
  88. const tasks = require( '../../tasks/exec/tasks' )( config );
  89. tasks.execOnRepositories();
  90. sinon.assert.calledOnce( logErrSpy );
  91. expect( logErrSpy.firstCall.args[ 0 ] ).to.be.an( 'error' );
  92. sinon.assert.calledTwice( taskStub );
  93. sinon.assert.calledWith( taskStub, '/path/1', { task: 'task-to-run' } );
  94. sinon.assert.calledWith( taskStub, '/path/2', { task: 'task-to-run' } );
  95. } );
  96. it( 'should execute task over directories', () => {
  97. const ckeditor5Dirs = require( '../../utils/ckeditor5-dirs' );
  98. const taskStub = sinon.stub();
  99. mockery.registerMock( 'minimist', () => {
  100. return { task: 'task-to-run' };
  101. } );
  102. sandbox.stub( ckeditor5Dirs, 'getDevDirectories' ).returns( getDevDirectoriesResult );
  103. mockery.registerMock( './functions/task-to-run', taskStub );
  104. const tasks = require( '../../tasks/exec/tasks' )( config );
  105. tasks.execOnRepositories();
  106. sinon.assert.calledTwice( taskStub );
  107. sinon.assert.calledWith( taskStub, '/path/1', { task: 'task-to-run' } );
  108. sinon.assert.calledWith( taskStub, '/path/2', { task: 'task-to-run' } );
  109. } );
  110. it( 'should execute task over specific directory', () => {
  111. const Stream = require( 'stream' );
  112. const ckeditor5Dirs = require( '../../utils/ckeditor5-dirs' );
  113. const taskStub = sinon.stub().returns( new Stream() );
  114. mockery.registerMock( 'minimist', () => {
  115. return {
  116. task: 'task-to-run',
  117. repository: 'test1'
  118. };
  119. } );
  120. sandbox.stub( ckeditor5Dirs, 'getDevDirectories' ).returns( getDevDirectoriesResult );
  121. mockery.registerMock( './functions/task-to-run', taskStub );
  122. const tasks = require( '../../tasks/exec/tasks' )( config );
  123. tasks.execOnRepositories();
  124. sinon.assert.calledOnce( taskStub );
  125. sinon.assert.calledWith( taskStub, '/path/1', { task: 'task-to-run', repository: 'test1' } );
  126. sinon.assert.neverCalledWith( taskStub, '/path/2', { task: 'task-to-run', repository: 'test1' } );
  127. } );
  128. } );
  129. } );