dev-init.js 4.9 KB

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