8
0

tools.js 11 KB

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