8
0

tools.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. var chai = require( 'chai' );
  8. var sinon = require( 'sinon' );
  9. var expect = chai.expect;
  10. var tools = require( '../utils/tools' );
  11. var toRestore;
  12. describe( 'utils', function() {
  13. beforeEach( function() {
  14. toRestore = [];
  15. } );
  16. afterEach( function() {
  17. toRestore.forEach( function( item ) {
  18. item.restore();
  19. } );
  20. } );
  21. describe( 'tools', function() {
  22. describe( 'cloneRepository', function() {
  23. it( 'should be defined', function() {
  24. expect( tools.cloneRepository ).to.be.a( 'function' );
  25. } );
  26. it( 'should run clone repository commands', function( ) {
  27. var shExecStub = sinon.stub( tools, 'shExec' );
  28. var name = 'test';
  29. var gitHubUrl = 'ckeditor/test';
  30. var destination = '/destination/dir';
  31. toRestore.push( shExecStub );
  32. tools.cloneRepository( name, gitHubUrl, destination );
  33. expect( shExecStub.calledOnce ).to.equal( true );
  34. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( 'cd ' + destination + ' && git clone git@github.com:' + gitHubUrl );
  35. } );
  36. it( 'should checkout proper commit/branch if provided', function() {
  37. var shExecStub = sinon.stub( tools, 'shExec' );
  38. var name = 'test';
  39. var url = 'ckeditor/test';
  40. var branch = 'branch';
  41. var gitHubUrl = url + '#' + branch;
  42. var destination = '/destination/dir';
  43. toRestore.push( shExecStub );
  44. tools.cloneRepository( name, gitHubUrl, destination );
  45. expect( shExecStub.calledOnce ).to.equal( true );
  46. expect( shExecStub.firstCall.args[ 0 ] ).to.equal(
  47. 'cd ' + destination + ' && ' +
  48. 'git clone git@github.com:' + url + ' && ' +
  49. 'cd ' + name + ' && ' +
  50. 'git checkout ' + branch
  51. );
  52. } );
  53. } );
  54. describe( 'npmLink', function() {
  55. it( 'should be defined', function() {
  56. expect( tools.cloneRepository ).to.be.a( 'function' );
  57. } );
  58. it( 'should run npm link commands', function( ) {
  59. var shExecStub = sinon.stub( tools, 'shExec' );
  60. var source = '/source/dir';
  61. var destination = '/destination/dir';
  62. var pluginName = 'ckeditor5-plugin-name';
  63. var isWin = process.platform == 'win32';
  64. var linkCommands = [
  65. 'cd ' + source,
  66. ( !isWin ? 'sudo ' : '' ) + 'npm link',
  67. 'cd ' + destination,
  68. 'npm link ' + pluginName
  69. ];
  70. toRestore.push( shExecStub );
  71. tools.npmLink( source, destination, pluginName );
  72. expect( shExecStub.calledOnce ).to.equal( true );
  73. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( linkCommands.join( ' && ' ) );
  74. } );
  75. } );
  76. describe( 'getCKEditorDependencies', function() {
  77. it( 'should be defined', function() {
  78. expect( tools.getCKEditorDependencies ).to.be.a( 'function' );
  79. } );
  80. it( 'should return null if no CKEditor5 repository is found', function() {
  81. var dependencies = {
  82. 'plugin1': '',
  83. 'plugin2': '',
  84. 'plugin3': ''
  85. };
  86. expect( tools.getCKEditorDependencies( dependencies ) ).to.equal( null );
  87. } );
  88. it( 'should return only ckeditor5- dependencies', function() {
  89. var dependencies = {
  90. 'plugin1': '',
  91. 'ckeditor5-plugin-image': 'ckeditor/ckeditor5-plugin-image',
  92. 'plugin2': '',
  93. 'ckeditor5-core': 'ckeditor/ckeditor5-core'
  94. };
  95. var ckeditorDependencies = tools.getCKEditorDependencies( dependencies );
  96. expect( ckeditorDependencies ).to.be.an( 'object' );
  97. expect( ckeditorDependencies.plugin1 ).to.be.a( 'undefined' );
  98. expect( ckeditorDependencies.plugin2 ).to.be.a( 'undefined' );
  99. expect( ckeditorDependencies[ 'ckeditor5-plugin-image' ] ).to.be.a( 'string' );
  100. expect( ckeditorDependencies[ 'ckeditor5-core' ] ).to.be.a( 'string' );
  101. } );
  102. } );
  103. describe( 'getCKE5Directories', function() {
  104. it( 'should return only ckeditor5 directories', function() {
  105. var workspacePath = '/workspace/path';
  106. var getDirectoriesStub = sinon.stub( tools, 'getDirectories', function() {
  107. return [ 'tools', 'ckeditor5', 'ckeditor5-core', '.bin', 'ckeditor5-plugin-image' ];
  108. } );
  109. toRestore.push( getDirectoriesStub );
  110. var directories = tools.getCKE5Directories( workspacePath );
  111. expect( directories.length ).equal( 3 );
  112. expect( directories[ 0 ] ).equal( 'ckeditor5' );
  113. expect( directories[ 1 ] ).equal( 'ckeditor5-core' );
  114. expect( directories[ 2 ] ).equal( 'ckeditor5-plugin-image' );
  115. } );
  116. } );
  117. describe( 'initDevWorkspace', function() {
  118. it( 'should get ckeditor5- dependencies, clone repositories and link them', function() {
  119. var path = require( 'path' );
  120. var getDependenciesSpy = sinon.spy( tools, 'getCKEditorDependencies' );
  121. var cloneRepositoryStub = sinon.stub( tools, 'cloneRepository' );
  122. var npmLinkStub = sinon.stub( tools, 'npmLink' );
  123. var ckeditor5Path = process.cwd();
  124. var workspacePath = path.join( ckeditor5Path, '..' );
  125. var dependencies, keys;
  126. toRestore.push( getDependenciesSpy, cloneRepositoryStub, npmLinkStub );
  127. tools.initDevWorkspace( workspacePath, ckeditor5Path, function() {} );
  128. expect( getDependenciesSpy.calledOnce ).to.equal( true );
  129. dependencies = getDependenciesSpy.firstCall.returnValue;
  130. if ( dependencies ) {
  131. keys = Object.keys( dependencies );
  132. // All repositories were cloned.
  133. expect( cloneRepositoryStub.callCount ).to.equal( keys.length );
  134. // All repositories were linked.
  135. expect( npmLinkStub.callCount ).to.equal( keys.length );
  136. // Check clone and link parameters.
  137. keys.forEach( function( key, i ) {
  138. expect( cloneRepositoryStub.getCall( i ).args[0] ).equal( key );
  139. expect( cloneRepositoryStub.getCall( i ).args[1] ).equal( dependencies[ key ] );
  140. expect( cloneRepositoryStub.getCall( i ).args[2] ).equal( workspacePath );
  141. expect( npmLinkStub.getCall( i ).args[0] ).equal( path.join( workspacePath, key ) );
  142. expect( npmLinkStub.getCall( i ).args[1] ).equal( ckeditor5Path );
  143. expect( npmLinkStub.getCall( i ).args[2] ).equal( key );
  144. } );
  145. }
  146. } );
  147. } );
  148. describe( 'getWorkspaceStatus', function() {
  149. it( 'should get all repositories status', function() {
  150. var path = require( 'path' );
  151. var workspacePath = '/workspace/path/';
  152. var directories = [ 'ckeditor5', 'ckeditor5-core' ];
  153. var log = sinon.spy();
  154. // Stub methods for test purposes.
  155. var getCKE5DirectoriesStub = sinon.stub( tools, 'getCKE5Directories', function() {
  156. return directories;
  157. } );
  158. var getGitStatusStub = sinon.stub( tools, 'getGitStatus', function() {
  159. return 'status';
  160. } );
  161. toRestore.push( getCKE5DirectoriesStub, getGitStatusStub );
  162. tools.getWorkspaceStatus( workspacePath, log );
  163. expect( getCKE5DirectoriesStub.calledOnce ).equal( true );
  164. expect( log.callCount ).equal( directories.length );
  165. expect( getGitStatusStub.callCount ).equal( directories.length );
  166. // Check if status was called for proper directory.
  167. for ( var i = 0; i < getGitStatusStub.callCount; i++ ) {
  168. expect( getGitStatusStub.getCall( i ).args[0] ).equals( path.join( workspacePath, directories[ i ] ) );
  169. }
  170. } );
  171. } );
  172. } );
  173. } );