tools.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**
  2. * @license Copyright (c) 2003-2016, 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( 'isFile', () => {
  144. it( 'should be defined', () => expect( tools.isFile ).to.be.a( 'function' ) );
  145. it( 'should return true if path points to file', () => {
  146. const fs = require( 'fs' );
  147. const statSyncStub = sinon.stub( fs, 'statSync', () => ( { isFile: () => true } ) );
  148. const path = 'path';
  149. toRestore.push( statSyncStub );
  150. const result = tools.isFile( path );
  151. expect( statSyncStub.calledOnce ).to.equal( true );
  152. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  153. expect( result ).to.equal( true );
  154. } );
  155. it( 'should return false if path does not point to directory', () => {
  156. const fs = require( 'fs' );
  157. const statSyncStub = sinon.stub( fs, 'statSync', () => ( { isFile: () => false } ) );
  158. const path = 'path';
  159. toRestore.push( statSyncStub );
  160. const result = tools.isFile( path );
  161. expect( statSyncStub.calledOnce ).to.equal( true );
  162. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  163. expect( result ).to.equal( false );
  164. } );
  165. it( 'should return false if statSync method throws', () => {
  166. const fs = require( 'fs' );
  167. const statSyncStub = sinon.stub( fs, 'statSync' ).throws();
  168. const path = 'path';
  169. toRestore.push( statSyncStub );
  170. const result = tools.isFile( path );
  171. expect( statSyncStub.calledOnce ).to.equal( true );
  172. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  173. expect( result ).to.equal( false );
  174. } );
  175. } );
  176. describe( 'getCKE5Directories', () => {
  177. it( 'should be defined', () => expect( tools.getCKE5Directories ).to.be.a( 'function' ) );
  178. it( 'should return only ckeditor5 directories', () => {
  179. const workspacePath = '/workspace/path';
  180. const sourceDirectories = [ 'tools', 'ckeditor5', 'ckeditor5-core', '.bin', 'ckeditor5-plugin-image' ];
  181. const getDirectoriesStub = sinon.stub( tools, 'getDirectories', () => sourceDirectories );
  182. toRestore.push( getDirectoriesStub );
  183. const directories = tools.getCKE5Directories( workspacePath );
  184. expect( directories.length ).equal( 2 );
  185. expect( directories[ 0 ] ).equal( 'ckeditor5-core' );
  186. expect( directories[ 1 ] ).equal( 'ckeditor5-plugin-image' );
  187. } );
  188. } );
  189. describe( 'updateJSONFile', () => {
  190. it( 'should be defined', () => expect( tools.updateJSONFile ).to.be.a( 'function' ) );
  191. it( 'should read, update and save JSON file', () => {
  192. const path = 'path/to/file.json';
  193. const fs = require( 'fs' );
  194. const readFileStub = sinon.stub( fs, 'readFileSync', () => '{}' );
  195. const modifiedJSON = { modified: true };
  196. const writeFileStub = sinon.stub( fs, 'writeFileSync' );
  197. toRestore.push( readFileStub, writeFileStub );
  198. tools.updateJSONFile( path, () => {
  199. return modifiedJSON;
  200. } );
  201. expect( readFileStub.calledOnce ).to.equal( true );
  202. expect( readFileStub.firstCall.args[ 0 ] ).to.equal( path );
  203. expect( writeFileStub.calledOnce ).to.equal( true );
  204. expect( writeFileStub.firstCall.args[ 0 ] ).to.equal( path );
  205. expect( writeFileStub.firstCall.args[ 1 ] ).to.equal( JSON.stringify( modifiedJSON, null, 2 ) );
  206. } );
  207. } );
  208. describe( 'readPackageName', () => {
  209. const modulePath = 'path/to/module';
  210. it( 'should read package name from NPM module', () => {
  211. const isFileStub = sinon.stub( tools, 'isFile' ).returns( true );
  212. const fs = require( 'fs' );
  213. const name = 'module-name';
  214. const readFileStub = sinon.stub( fs, 'readFileSync' ).returns( JSON.stringify( { name: name } ) );
  215. toRestore.push( isFileStub, readFileStub );
  216. const result = tools.readPackageName( modulePath );
  217. expect( result ).to.equal( name );
  218. } );
  219. it( 'should return null if no package.json is found', () => {
  220. const isFileStub = sinon.stub( tools, 'isFile' ).returns( false );
  221. toRestore.push( isFileStub );
  222. const result = tools.readPackageName( modulePath );
  223. expect( result ).to.equal( null );
  224. } );
  225. it( 'should return null if no name in package.json is provided', () => {
  226. const isFileStub = sinon.stub( tools, 'isFile' ).returns( true );
  227. const fs = require( 'fs' );
  228. const readFileStub = sinon.stub( fs, 'readFileSync' ).returns( JSON.stringify( { } ) );
  229. toRestore.push( isFileStub, readFileStub );
  230. const result = tools.readPackageName( modulePath );
  231. expect( result ).to.equal( null );
  232. } );
  233. } );
  234. describe( 'npmInstall', () => {
  235. it( 'should be defined', () => expect( tools.npmInstall ).to.be.a( 'function' ) );
  236. it( 'should execute npm install command', () => {
  237. const shExecStub = sinon.stub( tools, 'shExec' );
  238. const path = '/path/to/repository';
  239. toRestore.push( shExecStub );
  240. tools.npmInstall( path );
  241. expect( shExecStub.calledOnce ).to.equal( true );
  242. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm install` );
  243. } );
  244. } );
  245. describe( 'npmUpdate', () => {
  246. it( 'should be defined', () => expect( tools.npmUpdate ).to.be.a( 'function' ) );
  247. it( 'should execute npm update command', () => {
  248. const shExecStub = sinon.stub( tools, 'shExec' );
  249. const path = '/path/to/repository';
  250. toRestore.push( shExecStub );
  251. tools.npmUpdate( path );
  252. expect( shExecStub.calledOnce ).to.equal( true );
  253. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm update` );
  254. } );
  255. } );
  256. describe( 'npmUninstall', () => {
  257. it( 'should be defined', () => expect( tools.npmUninstall ).to.be.a( 'function' ) );
  258. it( 'should execute npm uninstall command', () => {
  259. const shExecStub = sinon.stub( tools, 'shExec' );
  260. const path = '/path/to/repository';
  261. const moduleName = 'module-name';
  262. toRestore.push( shExecStub );
  263. tools.npmUninstall( path, moduleName );
  264. expect( shExecStub.calledOnce ).to.equal( true );
  265. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm uninstall ${ moduleName }` );
  266. } );
  267. } );
  268. describe( 'installGitHooks', () => {
  269. it( 'should be defined', () => expect( tools.installGitHooks ).to.be.a( 'function' ) );
  270. it( 'should execute grunt githooks command', () => {
  271. const shExecStub = sinon.stub( tools, 'shExec' );
  272. const path = '/path/to/repository';
  273. toRestore.push( shExecStub );
  274. tools.installGitHooks( path );
  275. expect( shExecStub.calledOnce ).to.equal( true );
  276. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && grunt githooks` );
  277. } );
  278. } );
  279. describe( 'copyTemplateFiles', () => {
  280. it( 'should be defined', () => expect( tools.copyTemplateFiles ).to.be.a( 'function' ) );
  281. it( 'should copy template files', () => {
  282. const path = require( 'path' );
  283. const TEMPLATE_PATH = './dev/tasks/templates';
  284. const templatesPath = path.resolve( TEMPLATE_PATH );
  285. const shExecStub = sinon.stub( tools, 'shExec' );
  286. const repositoryPath = '/path/to/repository';
  287. toRestore.push( shExecStub );
  288. tools.copyTemplateFiles( repositoryPath );
  289. expect( shExecStub.calledOnce ).to.equal( true );
  290. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cp ${ path.join( templatesPath, '*.md' ) } ${ repositoryPath }` );
  291. } );
  292. } );
  293. describe( 'getGitUrlFromNpm', () => {
  294. const repository = {
  295. type: 'git',
  296. url: 'git@github.com:ckeditor/ckeditor5-core'
  297. };
  298. const moduleName = 'ckeditor5-core';
  299. it( 'should be defined', () => expect( tools.getGitUrlFromNpm ).to.be.a( 'function' ) );
  300. it( 'should call npm view command', () => {
  301. const shExecStub = sinon.stub( tools, 'shExec', () => {
  302. return JSON.stringify( repository );
  303. } );
  304. toRestore.push( shExecStub );
  305. const url = tools.getGitUrlFromNpm( moduleName );
  306. expect( shExecStub.calledOnce ).to.equal( true );
  307. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `npm view ${ moduleName } repository --json` );
  308. expect( url ).to.equal( repository.url );
  309. } );
  310. it( 'should return null if module is not found', () => {
  311. const shExecStub = sinon.stub( tools, 'shExec' ).throws( new Error( 'npm ERR! code E404' ) );
  312. toRestore.push( shExecStub );
  313. const url = tools.getGitUrlFromNpm( moduleName );
  314. expect( url ).to.equal( null );
  315. } );
  316. it( 'should return null if module has no repository information', () => {
  317. const shExecStub = sinon.stub( tools, 'shExec' ).returns( JSON.stringify( {} ) );
  318. toRestore.push( shExecStub );
  319. const url = tools.getGitUrlFromNpm( moduleName );
  320. expect( url ).to.equal( null );
  321. } );
  322. it( 'should throw on other errors', () => {
  323. const error = new Error( 'Random error.' );
  324. const shExecStub = sinon.stub( tools, 'shExec' ).throws( error );
  325. const getUrlSpy = sinon.spy( tools, 'getGitUrlFromNpm' );
  326. toRestore.push( shExecStub );
  327. toRestore.push( getUrlSpy );
  328. try {
  329. tools.getGitUrlFromNpm( moduleName );
  330. } catch ( e ) {}
  331. expect( getUrlSpy.threw( error ) ).to.equal( true );
  332. } );
  333. } );
  334. } );
  335. } );