dev-init.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /* global describe, it, beforeEach, afterEach */
  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. it( 'task should exists', () => expect( initTask ).to.be.a( 'function' ) );
  26. it( 'performs no action when no ckeditor dependencies are found', () => {
  27. const packageJSON = {
  28. dependencies: {
  29. 'non-ckeditor-plugin': 'other/plugin'
  30. }
  31. };
  32. initTask( mainRepositoryPath, packageJSON, options, emptyFn, emptyFn );
  33. expect( spies.getDependencies.calledOnce ).to.equal( true );
  34. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  35. expect( spies.getDirectories.called ).to.equal( false );
  36. expect( spies.parseRepositoryUrl.called ).to.equal( false );
  37. expect( spies.cloneRepository.called ).to.equal( false );
  38. expect( spies.checkout.called ).to.equal( false );
  39. expect( spies.npmInstall.called ).to.equal( false );
  40. expect( spies.installGitHooks.called ).to.equal( false );
  41. } );
  42. it( 'clones repositories if no directories are found', () => {
  43. const packageJSON = {
  44. dependencies: {
  45. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  46. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  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.calledOnce ).to.equal( true );
  54. expect( spies.getDirectories.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, options.workspaceRoot ) );
  55. expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
  56. expect( spies.cloneRepository.calledTwice ).to.equal( true );
  57. expect( spies.cloneRepository.firstCall.args[ 0 ] ).to.equal( spies.parseRepositoryUrl.firstCall.returnValue );
  58. expect( spies.cloneRepository.firstCall.args[ 1 ] ).to.equal( workspacePath );
  59. expect( spies.cloneRepository.secondCall.args[ 0 ] ).to.equal( spies.parseRepositoryUrl.secondCall.returnValue );
  60. expect( spies.cloneRepository.secondCall.args[ 1 ] ).to.equal( workspacePath );
  61. expect( spies.checkout.calledTwice ).to.equal( true );
  62. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  63. expect( spies.npmInstall.calledTwice ).to.equal( true );
  64. expect( spies.installGitHooks.calledTwice ).to.equal( true );
  65. } );
  66. it( 'only checks out repositories if directories are found', () => {
  67. const packageJSON = {
  68. dependencies: {
  69. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  70. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  71. 'non-ckeditor-plugin': 'other/plugin'
  72. }
  73. };
  74. spies.getDirectories.restore();
  75. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ] );
  76. initTask( mainRepositoryPath, packageJSON, options, emptyFn, emptyFn );
  77. expect( spies.getDependencies.calledOnce ).to.equal( true );
  78. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  79. expect( spies.getDirectories.calledOnce ).to.equal( true );
  80. expect( spies.getDirectories.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, options.workspaceRoot ) );
  81. expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
  82. expect( spies.cloneRepository.called ).to.equal( false );
  83. expect( spies.checkout.calledTwice ).to.equal( true );
  84. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  85. expect( spies.npmInstall.calledTwice ).to.equal( true );
  86. expect( spies.installGitHooks.calledTwice ).to.equal( true );
  87. } );
  88. } );
  89. function createSpies() {
  90. spies = {
  91. getDependencies: sinon.spy( tools, 'getCKEditorDependencies' ),
  92. getDirectories: sinon.stub( tools, 'getCKE5Directories', () => [] ),
  93. parseRepositoryUrl: sinon.spy( git, 'parseRepositoryUrl' ),
  94. cloneRepository: sinon.stub( git, 'cloneRepository' ),
  95. linkDirectories: sinon.stub( tools, 'linkDirectories' ),
  96. checkout: sinon.stub( git, 'checkout' ),
  97. npmInstall: sinon.stub( tools, 'npmInstall' ),
  98. installGitHooks: sinon.stub( tools, 'installGitHooks' )
  99. };
  100. }
  101. function restoreSpies() {
  102. for ( let spy in spies ) {
  103. spies[ spy ].restore();
  104. }
  105. }
  106. } );