tools.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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( '../tasks/utils/tools' );
  11. const path = require( 'path' );
  12. const fs = require( 'fs' );
  13. let toRestore;
  14. describe( 'utils', () => {
  15. beforeEach( () => toRestore = [] );
  16. afterEach( () => {
  17. toRestore.forEach( item => item.restore() );
  18. } );
  19. describe( 'tools', () => {
  20. describe( 'linkDirectories', () => {
  21. it( 'should be defined', () => expect( tools.linkDirectories ).to.be.a( 'function' ) );
  22. it( 'should link directories', () => {
  23. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( false );
  24. const symlinkStub = sinon.stub( fs, 'symlinkSync' );
  25. const source = '/source/dir';
  26. const destination = '/destination/dir';
  27. toRestore.push( symlinkStub, isDirectoryStub );
  28. tools.linkDirectories( source, destination );
  29. expect( isDirectoryStub.calledOnce ).to.equal( true );
  30. expect( symlinkStub.calledOnce ).to.equal( true );
  31. expect( symlinkStub.firstCall.args[ 0 ] ).to.equal( source );
  32. expect( symlinkStub.firstCall.args[ 1 ] ).to.equal( destination );
  33. } );
  34. it( 'should remove destination directory before linking', () => {
  35. const shExecStub = sinon.stub( tools, 'shExec' );
  36. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( true );
  37. const symlinkStub = sinon.stub( fs, 'symlinkSync' );
  38. const source = '/source/dir';
  39. const destination = '/destination/dir';
  40. toRestore.push( symlinkStub, shExecStub, isDirectoryStub );
  41. tools.linkDirectories( source, destination );
  42. expect( isDirectoryStub.calledOnce ).to.equal( true );
  43. expect( shExecStub.calledOnce ).to.equal( true );
  44. expect( symlinkStub.firstCall.args[ 0 ] ).to.equal( source );
  45. expect( symlinkStub.firstCall.args[ 1 ] ).to.equal( destination );
  46. } );
  47. } );
  48. describe( 'getCKEditorDependencies', () => {
  49. it( 'should be defined', () => expect( tools.getCKEditorDependencies ).to.be.a( 'function' ) );
  50. it( 'should return null if no CKEditor5 repository is found', () => {
  51. const dependencies = {
  52. 'plugin1': '',
  53. 'plugin2': '',
  54. 'plugin3': ''
  55. };
  56. expect( tools.getCKEditorDependencies( dependencies ) ).to.equal( null );
  57. } );
  58. it( 'should return only ckeditor5- dependencies', () => {
  59. const dependencies = {
  60. 'plugin1': '',
  61. 'ckeditor5-plugin-image': 'ckeditor/ckeditor5-plugin-image',
  62. 'plugin2': '',
  63. 'ckeditor5-core': 'ckeditor/ckeditor5-core'
  64. };
  65. const ckeditorDependencies = tools.getCKEditorDependencies( dependencies );
  66. expect( ckeditorDependencies ).to.be.an( 'object' );
  67. expect( ckeditorDependencies.plugin1 ).to.be.a( 'undefined' );
  68. expect( ckeditorDependencies.plugin2 ).to.be.a( 'undefined' );
  69. expect( ckeditorDependencies[ 'ckeditor5-plugin-image' ] ).to.be.a( 'string' );
  70. expect( ckeditorDependencies[ 'ckeditor5-core' ] ).to.be.a( 'string' );
  71. } );
  72. } );
  73. describe( 'getDirectories', () => {
  74. it( 'should be defined', () => expect( tools.getDirectories ).to.be.a( 'function' ) );
  75. it( 'should get directories in specified path', () => {
  76. const fs = require( 'fs' );
  77. const directories = [ 'dir1', 'dir2', 'dir3' ];
  78. const readdirSyncStub = sinon.stub( fs, 'readdirSync', () => directories );
  79. const isDirectoryStub = sinon.stub( tools, 'isDirectory' ).returns( true );
  80. const dirPath = 'path';
  81. toRestore.push( readdirSyncStub, isDirectoryStub );
  82. tools.getDirectories( dirPath );
  83. expect( readdirSyncStub.calledOnce ).to.equal( true );
  84. expect( isDirectoryStub.calledThrice ).to.equal( true );
  85. expect( isDirectoryStub.firstCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 0 ] ) );
  86. expect( isDirectoryStub.secondCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 1 ] ) );
  87. expect( isDirectoryStub.thirdCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 2 ] ) );
  88. } );
  89. } );
  90. describe( 'isDirectory', () => {
  91. it( 'should be defined', () => expect( tools.isDirectory ).to.be.a( 'function' ) );
  92. it( 'should return true if path points to directory', () => {
  93. const fs = require( 'fs' );
  94. const statSyncStub = sinon.stub( fs, 'statSync', () => ( { isDirectory: () => true } ) );
  95. const path = 'path';
  96. toRestore.push( statSyncStub );
  97. const result = tools.isDirectory( path );
  98. expect( statSyncStub.calledOnce ).to.equal( true );
  99. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  100. expect( result ).to.equal( true );
  101. } );
  102. it( 'should return false if path does not point to directory', () => {
  103. const fs = require( 'fs' );
  104. const statSyncStub = sinon.stub( fs, 'statSync', () => ( { isDirectory: () => false } ) );
  105. const path = 'path';
  106. toRestore.push( statSyncStub );
  107. const result = tools.isDirectory( path );
  108. expect( statSyncStub.calledOnce ).to.equal( true );
  109. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  110. expect( result ).to.equal( false );
  111. } );
  112. it( 'should return false if statSync method throws', () => {
  113. const fs = require( 'fs' );
  114. const statSyncStub = sinon.stub( fs, 'statSync' ).throws();
  115. const path = 'path';
  116. toRestore.push( statSyncStub );
  117. const result = tools.isDirectory( path );
  118. expect( statSyncStub.calledOnce ).to.equal( true );
  119. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  120. expect( result ).to.equal( false );
  121. } );
  122. } );
  123. describe( 'getCKE5Directories', () => {
  124. it( 'should be defined', () => expect( tools.getCKE5Directories ).to.be.a( 'function' ) );
  125. it( 'should return only ckeditor5 directories', () => {
  126. const workspacePath = '/workspace/path';
  127. const sourceDirectories = [ 'tools', 'ckeditor5', 'ckeditor5-core', '.bin', 'ckeditor5-plugin-image' ];
  128. const getDirectoriesStub = sinon.stub( tools, 'getDirectories', () => sourceDirectories );
  129. toRestore.push( getDirectoriesStub );
  130. const directories = tools.getCKE5Directories( workspacePath );
  131. expect( directories.length ).equal( 2 );
  132. expect( directories[ 0 ] ).equal( 'ckeditor5-core' );
  133. expect( directories[ 1 ] ).equal( 'ckeditor5-plugin-image' );
  134. } );
  135. } );
  136. describe( 'updateJSONFile', () => {
  137. it( 'should be defined', () => expect( tools.updateJSONFile ).to.be.a( 'function' ) );
  138. it( 'should read, update and save JSON file', () => {
  139. const path = 'path/to/file.json';
  140. const fs = require( 'fs' );
  141. const readFileStub = sinon.stub( fs, 'readFileSync', () => '{}' );
  142. const modifiedJSON = { modified: true };
  143. const writeFileStub = sinon.stub( fs, 'writeFileSync' );
  144. toRestore.push( readFileStub, writeFileStub );
  145. tools.updateJSONFile( path, () => {
  146. return modifiedJSON;
  147. } );
  148. expect( readFileStub.calledOnce ).to.equal( true );
  149. expect( readFileStub.firstCall.args[ 0 ] ).to.equal( path );
  150. expect( writeFileStub.calledOnce ).to.equal( true );
  151. expect( writeFileStub.firstCall.args[ 0 ] ).to.equal( path );
  152. expect( writeFileStub.firstCall.args[ 1 ] ).to.equal( JSON.stringify( modifiedJSON, null, 2 ) );
  153. } );
  154. } );
  155. describe( 'npmInstall', () => {
  156. it( 'should be defined', () => expect( tools.npmInstall ).to.be.a( 'function' ) );
  157. it( 'should execute npm install command', () => {
  158. const shExecStub = sinon.stub( tools, 'shExec' );
  159. const path = '/path/to/repository';
  160. toRestore.push( shExecStub );
  161. tools.npmInstall( path );
  162. expect( shExecStub.calledOnce ).to.equal( true );
  163. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm install` );
  164. } );
  165. } );
  166. describe( 'installGitHooks', () => {
  167. it( 'should be defined', () => expect( tools.installGitHooks ).to.be.a( 'function' ) );
  168. it( 'should execute grunt githooks command', () => {
  169. const shExecStub = sinon.stub( tools, 'shExec' );
  170. const path = '/path/to/repository';
  171. toRestore.push( shExecStub );
  172. tools.installGitHooks( path );
  173. expect( shExecStub.calledOnce ).to.equal( true );
  174. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && grunt githooks` );
  175. } );
  176. } );
  177. describe( 'copyTemplateFiles', () => {
  178. it( 'should be defined', () => expect( tools.copyTemplateFiles ).to.be.a( 'function' ) );
  179. it( 'should copy template files', () => {
  180. const path = require( 'path' );
  181. const TEMPLATE_PATH = './dev/tasks/templates';
  182. const templatesPath = path.resolve( TEMPLATE_PATH );
  183. const shExecStub = sinon.stub( tools, 'shExec' );
  184. const repositoryPath = '/path/to/repository';
  185. toRestore.push( shExecStub );
  186. tools.copyTemplateFiles( repositoryPath );
  187. expect( shExecStub.calledOnce ).to.equal( true );
  188. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cp ${ path.join( templatesPath, '*.md' ) } ${ repositoryPath }` );
  189. } );
  190. } );
  191. describe( 'getGitUrlFromNpm', () => {
  192. const repository = {
  193. type: 'git',
  194. url: 'git@github.com:ckeditor/ckeditor5-core'
  195. };
  196. const moduleName = 'ckeditor5-core';
  197. it( 'should be defined', () => expect( tools.getGitUrlFromNpm ).to.be.a( 'function' ) );
  198. it( 'should call npm view command', () => {
  199. const shExecStub = sinon.stub( tools, 'shExec', () => {
  200. return JSON.stringify( repository );
  201. } );
  202. toRestore.push( shExecStub );
  203. const url = tools.getGitUrlFromNpm( moduleName );
  204. expect( shExecStub.calledOnce ).to.equal( true );
  205. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `npm view ${ moduleName } repository --json` );
  206. expect( url ).to.equal( repository.url );
  207. } );
  208. it( 'should return null if module is not found', () => {
  209. const shExecStub = sinon.stub( tools, 'shExec' ).throws( new Error( 'npm ERR! code E404' ) );
  210. toRestore.push( shExecStub );
  211. const url = tools.getGitUrlFromNpm( moduleName );
  212. expect( url ).to.equal( null );
  213. } );
  214. it( 'should throw on other errors', () => {
  215. const error = new Error( 'Random error.' );
  216. const shExecStub = sinon.stub( tools, 'shExec' ).throws( error );
  217. const getUrlSpy = sinon.spy( tools, 'getGitUrlFromNpm' );
  218. toRestore.push( shExecStub );
  219. toRestore.push( getUrlSpy );
  220. try {
  221. tools.getGitUrlFromNpm( moduleName );
  222. } catch ( e ) {}
  223. expect( getUrlSpy.threw( error ) ).to.equal( true );
  224. } );
  225. } );
  226. } );
  227. } );