8
0

dev-init.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 chai = require( 'chai' );
  8. const sinon = require( 'sinon' );
  9. const expect = chai.expect;
  10. const tools = require( '../utils/tools' );
  11. const git = require( '../utils/git' );
  12. const path = require( 'path' );
  13. const emptyFn = () => { };
  14. let spies;
  15. describe( 'dev-tasks', () => {
  16. describe( 'dev-init', () => {
  17. const initTask = require( '../utils/dev-init' );
  18. const mainRepositoryPath = '/path/to/repository';
  19. const options = {
  20. workspaceRoot: '..'
  21. };
  22. const workspacePath = path.join( mainRepositoryPath, options.workspaceRoot );
  23. beforeEach( () => createSpies() );
  24. afterEach( () => restoreSpies() );
  25. function createSpies() {
  26. spies = {
  27. getDependencies: sinon.spy( tools, 'getCKEditorDependencies' ),
  28. getDirectories: sinon.stub( tools, 'getCKE5Directories', () => [] ),
  29. parseRepositoryUrl: sinon.spy( git, 'parseRepositoryUrl' ),
  30. cloneRepository: sinon.stub( git, 'cloneRepository' ),
  31. linkDirectories: sinon.stub( tools, 'linkDirectories' ),
  32. pull: sinon.stub( git, 'pull' ),
  33. checkout: sinon.stub( git, 'checkout' ),
  34. npmInstall: sinon.stub( tools, 'npmInstall' ),
  35. installGitHooks: sinon.stub( tools, 'installGitHooks' )
  36. };
  37. }
  38. function restoreSpies() {
  39. for ( let spy in spies ) {
  40. spies[ spy ].restore();
  41. }
  42. }
  43. it( 'task should exists', () => expect( initTask ).to.be.a( 'function' ) );
  44. it( 'performs no action when no ckeditor dependencies are found', () => {
  45. const packageJSON = {
  46. dependencies: {
  47. 'non-ckeditor-plugin': 'other/plugin'
  48. }
  49. };
  50. initTask( mainRepositoryPath, packageJSON, options, emptyFn, emptyFn );
  51. expect( spies.getDependencies.calledOnce ).to.equal( true );
  52. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  53. expect( spies.getDirectories.called ).to.equal( false );
  54. expect( spies.parseRepositoryUrl.called ).to.equal( false );
  55. expect( spies.cloneRepository.called ).to.equal( false );
  56. expect( spies.checkout.called ).to.equal( false );
  57. expect( spies.pull.called ).to.equal( false );
  58. expect( spies.npmInstall.called ).to.equal( false );
  59. expect( spies.installGitHooks.called ).to.equal( false );
  60. } );
  61. it( 'clones repositories if no directories are found', () => {
  62. const packageJSON = {
  63. dependencies: {
  64. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  65. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  66. 'non-ckeditor-plugin': 'other/plugin'
  67. }
  68. };
  69. initTask( mainRepositoryPath, packageJSON, options, emptyFn, emptyFn );
  70. expect( spies.getDependencies.calledOnce ).to.equal( true );
  71. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  72. expect( spies.getDirectories.calledOnce ).to.equal( true );
  73. expect( spies.getDirectories.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, options.workspaceRoot ) );
  74. expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
  75. expect( spies.cloneRepository.calledTwice ).to.equal( true );
  76. expect( spies.cloneRepository.firstCall.args[ 0 ] ).to.equal( spies.parseRepositoryUrl.firstCall.returnValue );
  77. expect( spies.cloneRepository.firstCall.args[ 1 ] ).to.equal( workspacePath );
  78. expect( spies.cloneRepository.secondCall.args[ 0 ] ).to.equal( spies.parseRepositoryUrl.secondCall.returnValue );
  79. expect( spies.cloneRepository.secondCall.args[ 1 ] ).to.equal( workspacePath );
  80. expect( spies.checkout.calledTwice ).to.equal( true );
  81. expect( spies.pull.calledTwice ).to.equal( true );
  82. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  83. expect( spies.npmInstall.calledTwice ).to.equal( true );
  84. expect( spies.installGitHooks.calledTwice ).to.equal( true );
  85. } );
  86. it( 'only checks out repositories if directories are found', () => {
  87. const packageJSON = {
  88. dependencies: {
  89. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  90. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  91. 'non-ckeditor-plugin': 'other/plugin'
  92. }
  93. };
  94. spies.getDirectories.restore();
  95. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ] );
  96. initTask( mainRepositoryPath, packageJSON, options, emptyFn, emptyFn );
  97. expect( spies.getDependencies.calledOnce ).to.equal( true );
  98. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  99. expect( spies.getDirectories.calledOnce ).to.equal( true );
  100. expect( spies.getDirectories.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, options.workspaceRoot ) );
  101. expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
  102. expect( spies.cloneRepository.called ).to.equal( false );
  103. expect( spies.checkout.calledTwice ).to.equal( true );
  104. expect( spies.pull.calledTwice ).to.equal( true );
  105. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  106. expect( spies.npmInstall.calledTwice ).to.equal( true );
  107. expect( spies.installGitHooks.calledTwice ).to.equal( true );
  108. } );
  109. } );
  110. } );