dev-tasks.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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( '../utils/tools' );
  11. const inquiries = require( '../utils/inquiries' );
  12. const git = require( '../utils/git' );
  13. const path = require( 'path' );
  14. const emptyFn = () => { };
  15. let spies;
  16. describe( 'dev-tasks', () => {
  17. const mainRepositoryPath = '/path/to/repository';
  18. const workspaceRoot = '..';
  19. const workspacePath = path.join( mainRepositoryPath, workspaceRoot );
  20. const pluginName = 'plugin-name';
  21. const repositoryPath = path.join( workspacePath, pluginName );
  22. const pluginVersion = '0.0.1';
  23. const gitHubUrl = 'ckeditor5/plugin-name';
  24. beforeEach( () => createSpies() );
  25. afterEach( () => restoreSpies() );
  26. function createSpies() {
  27. spies = {
  28. getDependencies: sinon.spy( tools, 'getCKEditorDependencies' ),
  29. getDirectories: sinon.stub( tools, 'getCKE5Directories', () => [] ),
  30. parseRepositoryUrl: sinon.spy( git, 'parseRepositoryUrl' ),
  31. cloneRepository: sinon.stub( git, 'cloneRepository' ),
  32. linkDirectories: sinon.stub( tools, 'linkDirectories' ),
  33. pull: sinon.stub( git, 'pull' ),
  34. checkout: sinon.stub( git, 'checkout' ),
  35. npmInstall: sinon.stub( tools, 'npmInstall' ),
  36. installGitHooks: sinon.stub( tools, 'installGitHooks' ),
  37. getPluginName: sinon.stub( inquiries, 'getPluginName' ).returns( new Promise( ( r ) => r( pluginName ) ) ),
  38. getPluginVersion: sinon.stub( inquiries, 'getPluginVersion' ).returns( new Promise( ( r ) => r( pluginVersion ) ) ),
  39. getPluginGitHubUrl: sinon.stub( inquiries, 'getPluginGitHubUrl' ).returns( new Promise( ( r ) => r( gitHubUrl ) ) ),
  40. initializeRepository: sinon.stub( git, 'initializeRepository' ),
  41. updateJSONFile: sinon.stub( tools, 'updateJSONFile' ),
  42. getStatus: sinon.stub( git, 'getStatus' ),
  43. updateBoilerplate: sinon.stub( git, 'updateBoilerplate' ),
  44. copyTemplateFiles: sinon.stub( tools, 'copyTemplateFiles' ),
  45. initialCommit: sinon.stub( git, 'initialCommit' )
  46. };
  47. }
  48. function restoreSpies() {
  49. for ( let spy in spies ) {
  50. spies[ spy ].restore();
  51. }
  52. }
  53. describe( 'dev-init', () => {
  54. const initTask = require( '../utils/dev-init' );
  55. it( 'task should exist', () => expect( initTask ).to.be.a( 'function' ) );
  56. it( 'performs no action when no ckeditor dependencies are found', () => {
  57. const packageJSON = {
  58. dependencies: {
  59. 'non-ckeditor-plugin': 'other/plugin'
  60. }
  61. };
  62. initTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  63. expect( spies.getDependencies.calledOnce ).to.equal( true );
  64. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  65. expect( spies.getDirectories.called ).to.equal( false );
  66. expect( spies.parseRepositoryUrl.called ).to.equal( false );
  67. expect( spies.cloneRepository.called ).to.equal( false );
  68. expect( spies.checkout.called ).to.equal( false );
  69. expect( spies.pull.called ).to.equal( false );
  70. expect( spies.npmInstall.called ).to.equal( false );
  71. expect( spies.installGitHooks.called ).to.equal( false );
  72. } );
  73. it( 'clones repositories if no directories are found', () => {
  74. const packageJSON = {
  75. dependencies: {
  76. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  77. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  78. 'non-ckeditor-plugin': 'other/plugin'
  79. }
  80. };
  81. initTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  82. expect( spies.getDependencies.calledOnce ).to.equal( true );
  83. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  84. expect( spies.getDirectories.calledOnce ).to.equal( true );
  85. expect( spies.getDirectories.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, workspaceRoot ) );
  86. expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
  87. expect( spies.cloneRepository.calledTwice ).to.equal( true );
  88. expect( spies.cloneRepository.firstCall.args[ 0 ] ).to.equal( spies.parseRepositoryUrl.firstCall.returnValue );
  89. expect( spies.cloneRepository.firstCall.args[ 1 ] ).to.equal( workspacePath );
  90. expect( spies.cloneRepository.secondCall.args[ 0 ] ).to.equal( spies.parseRepositoryUrl.secondCall.returnValue );
  91. expect( spies.cloneRepository.secondCall.args[ 1 ] ).to.equal( workspacePath );
  92. expect( spies.checkout.calledTwice ).to.equal( true );
  93. expect( spies.pull.calledTwice ).to.equal( true );
  94. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  95. expect( spies.npmInstall.calledTwice ).to.equal( true );
  96. expect( spies.installGitHooks.calledTwice ).to.equal( true );
  97. } );
  98. it( 'only checks out repositories if directories are found', () => {
  99. const packageJSON = {
  100. dependencies: {
  101. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  102. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  103. 'non-ckeditor-plugin': 'other/plugin'
  104. }
  105. };
  106. spies.getDirectories.restore();
  107. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ] );
  108. initTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  109. expect( spies.getDependencies.calledOnce ).to.equal( true );
  110. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  111. expect( spies.getDirectories.calledOnce ).to.equal( true );
  112. expect( spies.getDirectories.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, workspaceRoot ) );
  113. expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
  114. expect( spies.cloneRepository.called ).to.equal( false );
  115. expect( spies.checkout.calledTwice ).to.equal( true );
  116. expect( spies.pull.calledTwice ).to.equal( true );
  117. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  118. expect( spies.npmInstall.calledTwice ).to.equal( true );
  119. expect( spies.installGitHooks.calledTwice ).to.equal( true );
  120. } );
  121. } );
  122. describe( 'dev-plugin-create', () => {
  123. const pluginCreateTask = require( '../utils/dev-plugin-create' );
  124. const repositoryPath = path.join( workspacePath, pluginName );
  125. it( 'should exist', () => expect( pluginCreateTask ).to.be.a( 'function' ) );
  126. it( 'should create a plugin', () => {
  127. return pluginCreateTask( mainRepositoryPath, workspaceRoot, emptyFn ).then( () => {
  128. expect( spies.getPluginName.calledOnce ).to.equal( true );
  129. expect( spies.getPluginVersion.calledOnce ).to.equal( true );
  130. expect( spies.getPluginGitHubUrl.calledOnce ).to.equal( true );
  131. expect( spies.initializeRepository.calledOnce ).to.equal( true );
  132. expect( spies.initializeRepository.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  133. expect( spies.copyTemplateFiles.calledOnce ).to.equal( true );
  134. expect( spies.copyTemplateFiles.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  135. expect( spies.updateJSONFile.calledTwice ).to.equal( true );
  136. expect( spies.updateJSONFile.firstCall.args[ 0 ] ).to.equal( path.join( repositoryPath, 'package.json' ) );
  137. expect( spies.updateJSONFile.secondCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, 'package.json' ) );
  138. expect( spies.initialCommit.calledOnce ).to.equal( true );
  139. expect( spies.initialCommit.firstCall.args[ 0 ] ).to.equal( pluginName );
  140. expect( spies.initialCommit.firstCall.args[ 1 ] ).to.equal( repositoryPath );
  141. expect( spies.linkDirectories.calledOnce ).to.equal( true );
  142. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  143. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', pluginName ) );
  144. expect( spies.npmInstall.calledOnce ).to.equal( true );
  145. expect( spies.npmInstall.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  146. expect( spies.installGitHooks.calledOnce ).to.equal( true );
  147. expect( spies.installGitHooks.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  148. } );
  149. } );
  150. } );
  151. describe( 'dev-plugin-install', () => {
  152. const pluginInstallTask = require( '../utils/dev-plugin-install' );
  153. it( 'should exist', () => expect( pluginInstallTask ).to.be.a( 'function' ) );
  154. it( 'should install a plugin', () => {
  155. return pluginInstallTask( mainRepositoryPath, workspaceRoot, emptyFn ).then( () => {
  156. expect( spies.getPluginName.calledOnce ).to.equal( true );
  157. expect( spies.getPluginGitHubUrl.calledOnce ).to.equal( true );
  158. expect( spies.parseRepositoryUrl.calledOnce ).to.equal( true );
  159. const urlInfo = spies.parseRepositoryUrl.firstCall.returnValue;
  160. expect( spies.parseRepositoryUrl.firstCall.args[ 0 ] ).to.equal( gitHubUrl );
  161. expect( spies.cloneRepository.calledOnce ).to.equal( true );
  162. expect( spies.cloneRepository.firstCall.args[ 0 ] ).to.equal( urlInfo );
  163. expect( spies.cloneRepository.firstCall.args[ 1 ] ).to.equal( workspacePath );
  164. expect( spies.checkout.calledOnce ).to.equal( true );
  165. expect( spies.checkout.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  166. expect( spies.checkout.firstCall.args[ 1 ] ).to.equal( urlInfo.branch );
  167. expect( spies.updateJSONFile.calledOnce ).to.equal( true );
  168. expect( spies.updateJSONFile.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, 'package.json' ) );
  169. expect( spies.linkDirectories.calledOnce ).to.equal( true );
  170. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  171. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', pluginName ) );
  172. expect( spies.npmInstall.calledOnce ).to.equal( true );
  173. expect( spies.npmInstall.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  174. expect( spies.installGitHooks.calledOnce ).to.equal( true );
  175. expect( spies.installGitHooks.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  176. } );
  177. } );
  178. } );
  179. describe( 'dev-relink', () => {
  180. const devRelinkTask = require( '../utils/dev-relink' );
  181. it( 'should exist', () => expect( devRelinkTask ).to.be.a( 'function' ) );
  182. it( 'should relink repositories', () => {
  183. const packageJSON = {
  184. dependencies: {
  185. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  186. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  187. 'non-ckeditor-plugin': 'other/plugin'
  188. }
  189. };
  190. spies.getDirectories.restore();
  191. const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
  192. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
  193. devRelinkTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  194. expect( spies.getDependencies.calledOnce ).to.equal( true );
  195. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  196. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  197. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
  198. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', dirs[ 0 ] ) );
  199. expect( spies.linkDirectories.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
  200. expect( spies.linkDirectories.secondCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', dirs[ 1 ] ) );
  201. } );
  202. } );
  203. describe( 'dev-status', () => {
  204. const devStatusTask = require( '../utils/dev-status' );
  205. it( 'should exist', () => expect( devStatusTask ).to.be.a( 'function' ) );
  206. it( 'should show repositories status', () => {
  207. const packageJSON = {
  208. dependencies: {
  209. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  210. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  211. 'non-ckeditor-plugin': 'other/plugin'
  212. }
  213. };
  214. const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
  215. spies.getDirectories.restore();
  216. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
  217. devStatusTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  218. expect( spies.getDependencies.calledOnce ).to.equal( true );
  219. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  220. expect( spies.getStatus.calledTwice ).to.equal( true );
  221. expect( spies.getStatus.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
  222. expect( spies.getStatus.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
  223. } );
  224. } );
  225. describe( 'dev-update', () => {
  226. const devUpdateTask = require( '../utils/dev-update' );
  227. it( 'should exist', () => expect( devUpdateTask ).to.be.a( 'function' ) );
  228. it( 'should show repositories status', () => {
  229. const packageJSON = {
  230. dependencies: {
  231. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  232. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  233. 'non-ckeditor-plugin': 'other/plugin'
  234. }
  235. };
  236. const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
  237. spies.getDirectories.restore();
  238. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
  239. devUpdateTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  240. expect( spies.getDependencies.calledOnce ).to.equal( true );
  241. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  242. expect( spies.parseRepositoryUrl.calledTwice ).to.equal( true );
  243. expect( spies.pull.calledTwice ).to.equal( true );
  244. let urlInfo = spies.parseRepositoryUrl.firstCall.returnValue;
  245. expect( spies.pull.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
  246. expect( spies.pull.firstCall.args[ 1 ] ).to.equal( urlInfo.branch );
  247. urlInfo = spies.parseRepositoryUrl.secondCall.returnValue;
  248. expect( spies.pull.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
  249. expect( spies.pull.secondCall.args[ 1 ] ).to.equal( urlInfo.branch );
  250. } );
  251. } );
  252. describe( 'dev-boilerplate-update', () => {
  253. const devBoilerplateTask = require( '../utils/dev-boilerplate-update' );
  254. it( 'should exist', () => expect( devBoilerplateTask ).to.be.a( 'function' ) );
  255. it( 'should update boilerplate in repositories', () => {
  256. const packageJSON = {
  257. dependencies: {
  258. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  259. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  260. 'non-ckeditor-plugin': 'other/plugin'
  261. }
  262. };
  263. const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
  264. spies.getDirectories.restore();
  265. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
  266. devBoilerplateTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  267. expect( spies.getDependencies.calledOnce ).to.equal( true );
  268. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  269. expect( spies.updateBoilerplate.calledTwice ).to.equal( true );
  270. expect( spies.updateBoilerplate.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
  271. expect( spies.updateBoilerplate.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
  272. } );
  273. } );
  274. } );