tools.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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( '../../utils/tools' );
  11. const path = require( 'path' );
  12. const fs = require( 'fs' );
  13. let sandbox;
  14. describe( 'utils', () => {
  15. beforeEach( () => {
  16. sandbox = sinon.sandbox.create();
  17. } );
  18. afterEach( () => {
  19. sandbox.restore();
  20. } );
  21. describe( 'tools', () => {
  22. describe( 'shExec', () => {
  23. it( 'should be defined', () => expect( tools.shExec ).to.be.a( 'function' ) );
  24. it( 'should execute command', () => {
  25. const sh = require( 'shelljs' );
  26. const execStub = sandbox.stub( sh, 'exec' ).returns( { code: 0 } );
  27. tools.shExec( 'command' );
  28. sinon.assert.calledOnce( execStub );
  29. } );
  30. it( 'should throw error on unsuccessful call', () => {
  31. const sh = require( 'shelljs' );
  32. const execStub = sandbox.stub( sh, 'exec' ).returns( { code: 1 } );
  33. expect( () => {
  34. tools.shExec( 'command' );
  35. } ).to.throw();
  36. sinon.assert.calledOnce( execStub );
  37. } );
  38. it( 'should output using log functions', () => {
  39. const sh = require( 'shelljs' );
  40. const execStub = sandbox.stub( sh, 'exec' ).returns( { code: 0, stdout: 'out', stderr: 'err' } );
  41. sandbox.stub( console, 'log' );
  42. tools.shExec( 'command' );
  43. sinon.assert.calledOnce( execStub );
  44. sinon.assert.calledTwice( console.log );
  45. } );
  46. it( 'should not log when in silent mode', () => {
  47. const sh = require( 'shelljs' );
  48. const execStub = sandbox.stub( sh, 'exec' ).returns( { code: 0, stdout: 'out', stderr: 'err' } );
  49. const log = require( '../../utils/log' );
  50. const outFn = sandbox.spy();
  51. const errFn = sandbox.spy();
  52. log.configure( outFn, errFn );
  53. tools.shExec( 'command', false );
  54. sinon.assert.calledOnce( execStub );
  55. sinon.assert.notCalled( outFn );
  56. sinon.assert.notCalled( errFn );
  57. } );
  58. it( 'should not log if no output from executed command', () => {
  59. const sh = require( 'shelljs' );
  60. const execStub = sandbox.stub( sh, 'exec' ).returns( { code: 0, stdout: '', stderr: '' } );
  61. const log = require( '../../utils/log' );
  62. const outFn = sandbox.spy();
  63. const errFn = sandbox.spy();
  64. log.configure( outFn, errFn );
  65. tools.shExec( 'command' );
  66. sinon.assert.calledOnce( execStub );
  67. sinon.assert.notCalled( outFn );
  68. sinon.assert.notCalled( errFn );
  69. } );
  70. } );
  71. describe( 'linkDirectories', () => {
  72. it( 'should be defined', () => expect( tools.linkDirectories ).to.be.a( 'function' ) );
  73. it( 'should link directories', () => {
  74. const isSymlinkStub = sandbox.stub( tools, 'isSymlink' ).returns( false );
  75. const isDirectoryStub = sandbox.stub( tools, 'isDirectory' ).returns( false );
  76. const symlinkStub = sandbox.stub( fs, 'symlinkSync' );
  77. const source = '/source/dir';
  78. const destination = '/destination/dir';
  79. tools.linkDirectories( source, destination );
  80. expect( isDirectoryStub.calledOnce ).to.equal( true );
  81. expect( isSymlinkStub.calledOnce ).to.equal( true );
  82. expect( symlinkStub.calledOnce ).to.equal( true );
  83. expect( symlinkStub.firstCall.args[ 0 ] ).to.equal( source );
  84. expect( symlinkStub.firstCall.args[ 1 ] ).to.equal( destination );
  85. } );
  86. it( 'should remove destination directory before linking', () => {
  87. const shExecStub = sandbox.stub( tools, 'shExec' );
  88. const isSymlinkStub = sandbox.stub( tools, 'isSymlink' ).returns( false );
  89. const isDirectoryStub = sandbox.stub( tools, 'isDirectory' ).returns( true );
  90. const symlinkStub = sandbox.stub( fs, 'symlinkSync' );
  91. const source = '/source/dir';
  92. const destination = '/destination/dir';
  93. tools.linkDirectories( source, destination );
  94. expect( isDirectoryStub.calledOnce ).to.equal( true );
  95. expect( isSymlinkStub.calledOnce ).to.equal( true );
  96. expect( shExecStub.calledOnce ).to.equal( true );
  97. expect( symlinkStub.firstCall.args[ 0 ] ).to.equal( source );
  98. expect( symlinkStub.firstCall.args[ 1 ] ).to.equal( destination );
  99. } );
  100. it( 'should unlink destination directory if symlink', () => {
  101. const shExecStub = sandbox.stub( tools, 'shExec' );
  102. const isSymlinkStub = sandbox.stub( tools, 'isSymlink' ).returns( true );
  103. const removeSymlinkStub = sandbox.stub( tools, 'removeSymlink' );
  104. const isDirectoryStub = sandbox.stub( tools, 'isDirectory' ).returns( true );
  105. const symlinkStub = sandbox.stub( fs, 'symlinkSync' );
  106. const source = '/source/dir';
  107. const destination = '/destination/dir';
  108. tools.linkDirectories( source, destination );
  109. expect( isDirectoryStub.notCalled ).to.equal( true );
  110. expect( isSymlinkStub.calledOnce ).to.equal( true );
  111. expect( shExecStub.notCalled ).to.equal( true );
  112. expect( removeSymlinkStub.calledOnce ).to.equal( true );
  113. expect( removeSymlinkStub.firstCall.args[ 0 ] ).to.equal( destination );
  114. expect( symlinkStub.firstCall.args[ 0 ] ).to.equal( source );
  115. expect( symlinkStub.firstCall.args[ 1 ] ).to.equal( destination );
  116. } );
  117. } );
  118. describe( 'getDirectories', () => {
  119. it( 'should be defined', () => expect( tools.getDirectories ).to.be.a( 'function' ) );
  120. it( 'should get directories in specified path', () => {
  121. const fs = require( 'fs' );
  122. const directories = [ 'dir1', 'dir2', 'dir3' ];
  123. const readdirSyncStub = sandbox.stub( fs, 'readdirSync', () => directories );
  124. const isDirectoryStub = sandbox.stub( tools, 'isDirectory' ).returns( true );
  125. const dirPath = 'path';
  126. tools.getDirectories( dirPath );
  127. expect( readdirSyncStub.calledOnce ).to.equal( true );
  128. expect( isDirectoryStub.calledThrice ).to.equal( true );
  129. expect( isDirectoryStub.firstCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 0 ] ) );
  130. expect( isDirectoryStub.secondCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 1 ] ) );
  131. expect( isDirectoryStub.thirdCall.args[ 0 ] ).to.equal( path.join( dirPath, directories[ 2 ] ) );
  132. } );
  133. } );
  134. describe( 'isDirectory', () => {
  135. it( 'should be defined', () => expect( tools.isDirectory ).to.be.a( 'function' ) );
  136. it( 'should return true if path points to directory', () => {
  137. const fs = require( 'fs' );
  138. const statSyncStub = sandbox.stub( fs, 'statSync', () => ( { isDirectory: () => true } ) );
  139. const path = 'path';
  140. const result = tools.isDirectory( path );
  141. expect( statSyncStub.calledOnce ).to.equal( true );
  142. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  143. expect( result ).to.equal( true );
  144. } );
  145. it( 'should return false if path does not point to directory', () => {
  146. const fs = require( 'fs' );
  147. const statSyncStub = sandbox.stub( fs, 'statSync', () => ( { isDirectory: () => false } ) );
  148. const path = 'path';
  149. const result = tools.isDirectory( path );
  150. expect( statSyncStub.calledOnce ).to.equal( true );
  151. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  152. expect( result ).to.equal( false );
  153. } );
  154. it( 'should return false if statSync method throws', () => {
  155. const fs = require( 'fs' );
  156. const statSyncStub = sandbox.stub( fs, 'statSync' ).throws();
  157. const path = 'path';
  158. const result = tools.isDirectory( path );
  159. expect( statSyncStub.calledOnce ).to.equal( true );
  160. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  161. expect( result ).to.equal( false );
  162. } );
  163. } );
  164. describe( 'isFile', () => {
  165. it( 'should be defined', () => expect( tools.isFile ).to.be.a( 'function' ) );
  166. it( 'should return true if path points to file', () => {
  167. const fs = require( 'fs' );
  168. const statSyncStub = sandbox.stub( fs, 'statSync', () => ( { isFile: () => true } ) );
  169. const path = 'path';
  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( true );
  174. } );
  175. it( 'should return false if path does not point to directory', () => {
  176. const fs = require( 'fs' );
  177. const statSyncStub = sandbox.stub( fs, 'statSync', () => ( { isFile: () => false } ) );
  178. const path = 'path';
  179. const result = tools.isFile( path );
  180. expect( statSyncStub.calledOnce ).to.equal( true );
  181. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  182. expect( result ).to.equal( false );
  183. } );
  184. it( 'should return false if statSync method throws', () => {
  185. const fs = require( 'fs' );
  186. const statSyncStub = sandbox.stub( fs, 'statSync' ).throws();
  187. const path = 'path';
  188. const result = tools.isFile( path );
  189. expect( statSyncStub.calledOnce ).to.equal( true );
  190. expect( statSyncStub.firstCall.args[ 0 ] ).to.equal( path );
  191. expect( result ).to.equal( false );
  192. } );
  193. } );
  194. describe( 'isSymlink', () => {
  195. it( 'should return true if path points to symbolic link', () => {
  196. const path = 'path/to/file';
  197. const fs = require( 'fs' );
  198. sandbox.stub( fs, 'lstatSync' ).returns( {
  199. isSymbolicLink: () => true
  200. } );
  201. expect( tools.isSymlink( path ) ).to.equal( true );
  202. } );
  203. it( 'should return false if lstatSync throws', () => {
  204. const path = 'path/to/file';
  205. const fs = require( 'fs' );
  206. sandbox.stub( fs, 'lstatSync' ).throws();
  207. expect( tools.isSymlink( path ) ).to.equal( false );
  208. } );
  209. } );
  210. describe( 'updateJSONFile', () => {
  211. it( 'should be defined', () => expect( tools.updateJSONFile ).to.be.a( 'function' ) );
  212. it( 'should read, update and save JSON file', () => {
  213. const path = 'path/to/file.json';
  214. const fs = require( 'fs' );
  215. const readFileStub = sandbox.stub( fs, 'readFileSync', () => '{}' );
  216. const modifiedJSON = { modified: true };
  217. const writeFileStub = sandbox.stub( fs, 'writeFileSync' );
  218. tools.updateJSONFile( path, () => {
  219. return modifiedJSON;
  220. } );
  221. expect( readFileStub.calledOnce ).to.equal( true );
  222. expect( readFileStub.firstCall.args[ 0 ] ).to.equal( path );
  223. expect( writeFileStub.calledOnce ).to.equal( true );
  224. expect( writeFileStub.firstCall.args[ 0 ] ).to.equal( path );
  225. expect( writeFileStub.firstCall.args[ 1 ] ).to.equal( JSON.stringify( modifiedJSON, null, 2 ) + '\n' );
  226. } );
  227. } );
  228. describe( 'sortObject', () => {
  229. it( 'should be defined', () => expect( tools.sortObject ).to.be.a( 'function' ) );
  230. it( 'should reinsert object properties in alphabetical order', () => {
  231. let obj = {
  232. c: '', d: '', a: '', z: ''
  233. };
  234. const sorted = {
  235. a: '', c: '', d: '', z: ''
  236. };
  237. obj = tools.sortObject( obj );
  238. expect( JSON.stringify( obj ) ).to.equal( JSON.stringify( sorted ) );
  239. } );
  240. } );
  241. describe( 'readPackageName', () => {
  242. const modulePath = 'path/to/module';
  243. it( 'should read package name from NPM module', () => {
  244. sandbox.stub( tools, 'isFile' ).returns( true );
  245. const fs = require( 'fs' );
  246. const name = 'module-name';
  247. sandbox.stub( fs, 'readFileSync' ).returns( JSON.stringify( { name: name } ) );
  248. const result = tools.readPackageName( modulePath );
  249. expect( result ).to.equal( name );
  250. } );
  251. it( 'should return null if no package.json is found', () => {
  252. sandbox.stub( tools, 'isFile' ).returns( false );
  253. const result = tools.readPackageName( modulePath );
  254. expect( result ).to.equal( null );
  255. } );
  256. it( 'should return null if no name in package.json is provided', () => {
  257. sandbox.stub( tools, 'isFile' ).returns( true );
  258. const fs = require( 'fs' );
  259. sandbox.stub( fs, 'readFileSync' ).returns( JSON.stringify( { } ) );
  260. const result = tools.readPackageName( modulePath );
  261. expect( result ).to.equal( null );
  262. } );
  263. } );
  264. describe( 'npmInstall', () => {
  265. it( 'should be defined', () => expect( tools.npmInstall ).to.be.a( 'function' ) );
  266. it( 'should execute npm install command', () => {
  267. const shExecStub = sandbox.stub( tools, 'shExec' );
  268. const path = '/path/to/repository';
  269. tools.npmInstall( path );
  270. expect( shExecStub.calledOnce ).to.equal( true );
  271. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm install` );
  272. } );
  273. } );
  274. describe( 'npmUpdate', () => {
  275. it( 'should be defined', () => expect( tools.npmUpdate ).to.be.a( 'function' ) );
  276. it( 'should execute npm update command', () => {
  277. const shExecStub = sandbox.stub( tools, 'shExec' );
  278. const path = '/path/to/repository';
  279. tools.npmUpdate( path );
  280. expect( shExecStub.calledOnce ).to.equal( true );
  281. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm update --dev` );
  282. } );
  283. } );
  284. describe( 'npmUninstall', () => {
  285. it( 'should be defined', () => expect( tools.npmUninstall ).to.be.a( 'function' ) );
  286. it( 'should execute npm uninstall command', () => {
  287. const shExecStub = sandbox.stub( tools, 'shExec' );
  288. const path = '/path/to/repository';
  289. const moduleName = 'module-name';
  290. tools.npmUninstall( path, moduleName );
  291. expect( shExecStub.calledOnce ).to.equal( true );
  292. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `cd ${ path } && npm uninstall ${ moduleName }` );
  293. } );
  294. } );
  295. describe( 'copyTemplateFiles', () => {
  296. it( 'should be defined', () => expect( tools.copyTemplateFiles ).to.be.a( 'function' ) );
  297. it( 'should copy files and replace provided texts', () => {
  298. const path = require( 'path' );
  299. const fs = require( 'fs-extra' );
  300. const readFileStub = sandbox.stub( fs, 'readFileSync' );
  301. readFileStub.onFirstCall().returns( 'file data {{var1}}, {{var2}}' );
  302. readFileStub.onSecondCall().returns( '{{var1}}, {{var2}}, {{var2}}{{var1}}' );
  303. const writeFileStub = sandbox.stub( fs, 'writeFileSync' );
  304. const ensureDirStub = sandbox.stub( fs, 'ensureDirSync' );
  305. const sources = [ '/path/to/file1.md', '/path/to/file2.md' ];
  306. const destination = '/destination/path';
  307. tools.copyTemplateFiles( sources, destination, {
  308. '{{var1}}': 'foo',
  309. '{{var2}}': 'bar'
  310. } );
  311. sinon.assert.calledWithExactly( ensureDirStub, destination );
  312. sinon.assert.calledTwice( readFileStub );
  313. sinon.assert.calledWithExactly( readFileStub.firstCall, sources[ 0 ], 'utf8' );
  314. sinon.assert.calledWithExactly( readFileStub.secondCall, sources[ 1 ], 'utf8' );
  315. sinon.assert.calledTwice( writeFileStub );
  316. let savePath = path.join( destination, path.basename( sources[ 0 ] ) );
  317. sinon.assert.calledWithExactly( writeFileStub.firstCall, savePath, 'file data foo, bar', 'utf8' );
  318. savePath = path.join( destination, path.basename( sources[ 1 ] ) );
  319. sinon.assert.calledWithExactly( writeFileStub.secondCall, savePath, 'foo, bar, barfoo', 'utf8' );
  320. } );
  321. } );
  322. describe( 'getGitUrlFromNpm', () => {
  323. const repository = {
  324. type: 'git',
  325. url: 'git@github.com:ckeditor/ckeditor5-core'
  326. };
  327. const moduleName = 'ckeditor5-core';
  328. it( 'should be defined', () => expect( tools.getGitUrlFromNpm ).to.be.a( 'function' ) );
  329. it( 'should call npm view command', () => {
  330. const shExecStub = sandbox.stub( tools, 'shExec', () => {
  331. return JSON.stringify( repository );
  332. } );
  333. const url = tools.getGitUrlFromNpm( moduleName );
  334. expect( shExecStub.calledOnce ).to.equal( true );
  335. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( `npm view ${ moduleName } repository --json` );
  336. expect( url ).to.equal( repository.url );
  337. } );
  338. it( 'should return null if module is not found', () => {
  339. sandbox.stub( tools, 'shExec' ).throws( new Error( 'npm ERR! code E404' ) );
  340. const url = tools.getGitUrlFromNpm( moduleName );
  341. expect( url ).to.equal( null );
  342. } );
  343. it( 'should return null if module has no repository information', () => {
  344. sandbox.stub( tools, 'shExec' ).returns( JSON.stringify( {} ) );
  345. const url = tools.getGitUrlFromNpm( moduleName );
  346. expect( url ).to.equal( null );
  347. } );
  348. it( 'should throw on other errors', () => {
  349. const error = new Error( 'Random error.' );
  350. sandbox.stub( tools, 'shExec' ).throws( error );
  351. const getUrlSpy = sandbox.spy( tools, 'getGitUrlFromNpm' );
  352. try {
  353. tools.getGitUrlFromNpm( moduleName );
  354. } catch ( e ) {}
  355. expect( getUrlSpy.threw( error ) ).to.equal( true );
  356. } );
  357. } );
  358. describe( 'removeSymlink', () => {
  359. it( 'should unlink provided symlink', () => {
  360. const fs = require( 'fs' );
  361. const unlinkStub = sandbox.stub( fs, 'unlinkSync' );
  362. const path = 'path/to/symlink';
  363. tools.removeSymlink( path );
  364. expect( unlinkStub.calledOnce ).to.equal( true );
  365. expect( unlinkStub.firstCall.args[ 0 ] ).to.equal( path );
  366. } );
  367. } );
  368. } );
  369. } );