dev-tasks.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 inquiries = require( '../tasks/utils/inquiries' );
  12. const git = require( '../tasks/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-plugin-create', () => {
  54. const pluginCreateTask = require( '../tasks/utils/dev-plugin-create' );
  55. const repositoryPath = path.join( workspacePath, pluginName );
  56. it( 'should exist', () => expect( pluginCreateTask ).to.be.a( 'function' ) );
  57. it( 'should create a plugin', () => {
  58. return pluginCreateTask( mainRepositoryPath, workspaceRoot, emptyFn ).then( () => {
  59. expect( spies.getPluginName.calledOnce ).to.equal( true );
  60. expect( spies.getPluginVersion.calledOnce ).to.equal( true );
  61. expect( spies.getPluginGitHubUrl.calledOnce ).to.equal( true );
  62. expect( spies.initializeRepository.calledOnce ).to.equal( true );
  63. expect( spies.initializeRepository.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  64. expect( spies.copyTemplateFiles.calledOnce ).to.equal( true );
  65. expect( spies.copyTemplateFiles.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  66. expect( spies.updateJSONFile.calledTwice ).to.equal( true );
  67. expect( spies.updateJSONFile.firstCall.args[ 0 ] ).to.equal( path.join( repositoryPath, 'package.json' ) );
  68. expect( spies.updateJSONFile.secondCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, 'package.json' ) );
  69. expect( spies.initialCommit.calledOnce ).to.equal( true );
  70. expect( spies.initialCommit.firstCall.args[ 0 ] ).to.equal( pluginName );
  71. expect( spies.initialCommit.firstCall.args[ 1 ] ).to.equal( repositoryPath );
  72. expect( spies.linkDirectories.calledOnce ).to.equal( true );
  73. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  74. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', pluginName ) );
  75. expect( spies.npmInstall.calledOnce ).to.equal( true );
  76. expect( spies.npmInstall.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  77. expect( spies.installGitHooks.calledOnce ).to.equal( true );
  78. expect( spies.installGitHooks.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  79. } );
  80. } );
  81. } );
  82. describe( 'dev-plugin-install', () => {
  83. const pluginInstallTask = require( '../tasks/utils/dev-plugin-install' );
  84. it( 'should exist', () => expect( pluginInstallTask ).to.be.a( 'function' ) );
  85. it( 'should install a plugin', () => {
  86. return pluginInstallTask( mainRepositoryPath, workspaceRoot, emptyFn ).then( () => {
  87. expect( spies.getPluginName.calledOnce ).to.equal( true );
  88. expect( spies.getPluginGitHubUrl.calledOnce ).to.equal( true );
  89. expect( spies.parseRepositoryUrl.calledOnce ).to.equal( true );
  90. const urlInfo = spies.parseRepositoryUrl.firstCall.returnValue;
  91. expect( spies.parseRepositoryUrl.firstCall.args[ 0 ] ).to.equal( gitHubUrl );
  92. expect( spies.cloneRepository.calledOnce ).to.equal( true );
  93. expect( spies.cloneRepository.firstCall.args[ 0 ] ).to.equal( urlInfo );
  94. expect( spies.cloneRepository.firstCall.args[ 1 ] ).to.equal( workspacePath );
  95. expect( spies.checkout.calledOnce ).to.equal( true );
  96. expect( spies.checkout.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  97. expect( spies.checkout.firstCall.args[ 1 ] ).to.equal( urlInfo.branch );
  98. expect( spies.updateJSONFile.calledOnce ).to.equal( true );
  99. expect( spies.updateJSONFile.firstCall.args[ 0 ] ).to.equal( path.join( mainRepositoryPath, 'package.json' ) );
  100. expect( spies.linkDirectories.calledOnce ).to.equal( true );
  101. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  102. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', pluginName ) );
  103. expect( spies.npmInstall.calledOnce ).to.equal( true );
  104. expect( spies.npmInstall.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  105. expect( spies.installGitHooks.calledOnce ).to.equal( true );
  106. expect( spies.installGitHooks.firstCall.args[ 0 ] ).to.equal( repositoryPath );
  107. } );
  108. } );
  109. } );
  110. describe( 'dev-relink', () => {
  111. const devRelinkTask = require( '../tasks/utils/dev-relink' );
  112. it( 'should exist', () => expect( devRelinkTask ).to.be.a( 'function' ) );
  113. it( 'should relink repositories', () => {
  114. const packageJSON = {
  115. dependencies: {
  116. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  117. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  118. 'non-ckeditor-plugin': 'other/plugin'
  119. }
  120. };
  121. spies.getDirectories.restore();
  122. const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
  123. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
  124. devRelinkTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  125. expect( spies.getDependencies.calledOnce ).to.equal( true );
  126. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  127. expect( spies.linkDirectories.calledTwice ).to.equal( true );
  128. expect( spies.linkDirectories.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
  129. expect( spies.linkDirectories.firstCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', dirs[ 0 ] ) );
  130. expect( spies.linkDirectories.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
  131. expect( spies.linkDirectories.secondCall.args[ 1 ] ).to.equal( path.join( mainRepositoryPath, 'node_modules', dirs[ 1 ] ) );
  132. } );
  133. } );
  134. describe( 'dev-boilerplate-update', () => {
  135. const devBoilerplateTask = require( '../tasks/utils/dev-boilerplate-update' );
  136. it( 'should exist', () => expect( devBoilerplateTask ).to.be.a( 'function' ) );
  137. it( 'should update boilerplate in repositories', () => {
  138. const packageJSON = {
  139. dependencies: {
  140. 'ckeditor5-core': 'ckeditor/ckeditor5-core',
  141. 'ckeditor5-plugin-devtest': 'ckeditor/ckeditor5-plugin-devtest',
  142. 'non-ckeditor-plugin': 'other/plugin'
  143. }
  144. };
  145. const dirs = [ 'ckeditor5-core', 'ckeditor5-plugin-devtest' ];
  146. spies.getDirectories.restore();
  147. spies.getDirectories = sinon.stub( tools, 'getCKE5Directories', () => dirs );
  148. devBoilerplateTask( mainRepositoryPath, packageJSON, workspaceRoot, emptyFn, emptyFn );
  149. expect( spies.getDependencies.calledOnce ).to.equal( true );
  150. expect( spies.getDependencies.firstCall.args[ 0 ] ).to.equal( packageJSON.dependencies );
  151. expect( spies.updateBoilerplate.calledTwice ).to.equal( true );
  152. expect( spies.updateBoilerplate.firstCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 0 ] ) );
  153. expect( spies.updateBoilerplate.secondCall.args[ 0 ] ).to.equal( path.join( workspacePath, dirs[ 1 ] ) );
  154. } );
  155. } );
  156. } );