git.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. let toRestore;
  8. const git = require( '../tasks/utils/git' );
  9. const chai = require( 'chai' );
  10. const sinon = require( 'sinon' );
  11. const tools = require( '../tasks/utils/tools' );
  12. const expect = chai.expect;
  13. describe( 'utils', () => {
  14. beforeEach( () => toRestore = [] );
  15. afterEach( () => {
  16. toRestore.forEach( item => item.restore() );
  17. } );
  18. describe( 'git', () => {
  19. describe( 'parseRepositoryUrl', () => {
  20. it( 'should be defined', () => expect( git.parseRepositoryUrl ).to.be.a( 'function' ) );
  21. it( 'should parse short GitHub URL ', () => {
  22. const urlInfo = git.parseRepositoryUrl( 'ckeditor/ckeditor5-core' );
  23. expect( urlInfo.server ).to.equal( 'https://github.com/' );
  24. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  25. expect( urlInfo.user ).to.equal( 'ckeditor' );
  26. expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
  27. expect( urlInfo.branch ).to.equal( 'master' );
  28. } );
  29. it( 'should parse short GitHub URL with provided branch ', () => {
  30. const urlInfo = git.parseRepositoryUrl( 'ckeditor/ckeditor5-core#experimental' );
  31. expect( urlInfo.server ).to.equal( 'https://github.com/' );
  32. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  33. expect( urlInfo.user ).to.equal( 'ckeditor' );
  34. expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
  35. expect( urlInfo.branch ).to.equal( 'experimental' );
  36. } );
  37. it( 'should parse full GitHub URL (http)', () => {
  38. const urlInfo = git.parseRepositoryUrl( 'http://github.com/ckeditor/ckeditor5-core.git' );
  39. expect( urlInfo.server ).to.equal( 'http://github.com/' );
  40. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
  41. expect( urlInfo.user ).to.equal( 'ckeditor' );
  42. expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
  43. expect( urlInfo.branch ).to.equal( 'master' );
  44. } );
  45. it( 'should parse full GitHub URL (http) with provided branch', () => {
  46. const urlInfo = git.parseRepositoryUrl( 'http://github.com/ckeditor/ckeditor5-core.git#experimental' );
  47. expect( urlInfo.server ).to.equal( 'http://github.com/' );
  48. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
  49. expect( urlInfo.user ).to.equal( 'ckeditor' );
  50. expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
  51. expect( urlInfo.branch ).to.equal( 'experimental' );
  52. } );
  53. it( 'should parse full GitHub URL (https)', () => {
  54. const urlInfo = git.parseRepositoryUrl( 'https://github.com/ckeditor/ckeditor5-core.git' );
  55. expect( urlInfo.server ).to.equal( 'https://github.com/' );
  56. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
  57. expect( urlInfo.user ).to.equal( 'ckeditor' );
  58. expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
  59. expect( urlInfo.branch ).to.equal( 'master' );
  60. } );
  61. it( 'should parse full GitHub URL (https) with provided branch', () => {
  62. const urlInfo = git.parseRepositoryUrl( 'https://github.com/ckeditor/ckeditor5-core.git#t/122' );
  63. expect( urlInfo.server ).to.equal( 'https://github.com/' );
  64. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
  65. expect( urlInfo.user ).to.equal( 'ckeditor' );
  66. expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
  67. expect( urlInfo.branch ).to.equal( 't/122' );
  68. } );
  69. it( 'should parse full GitHub URL (git)', () => {
  70. const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core.git' );
  71. expect( urlInfo.server ).to.equal( 'git@github.com:' );
  72. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
  73. expect( urlInfo.user ).to.equal( 'ckeditor' );
  74. expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
  75. expect( urlInfo.branch ).to.equal( 'master' );
  76. } );
  77. it( 'should parse full GitHub URL (git) with provided branch', () => {
  78. const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core.git#new-feature' );
  79. expect( urlInfo.server ).to.equal( 'git@github.com:' );
  80. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core.git' );
  81. expect( urlInfo.user ).to.equal( 'ckeditor' );
  82. expect( urlInfo.name ).to.equal( 'ckeditor5-core' );
  83. expect( urlInfo.branch ).to.equal( 'new-feature' );
  84. } );
  85. it( 'should return null if GitHub URL is not valid', () => {
  86. let urlInfo = git.parseRepositoryUrl( 'https://ckeditor.com' );
  87. expect( urlInfo ).to.equal( null );
  88. urlInfo = git.parseRepositoryUrl( 'https://github.com/info.html' );
  89. expect( urlInfo ).to.equal( null );
  90. } );
  91. } );
  92. describe( 'cloneRepository', () => {
  93. it( 'should be defined', () => expect( git.cloneRepository ).to.be.a( 'function' ) );
  94. it( 'should call clone commands', () => {
  95. const shExecStub = sinon.stub( tools, 'shExec' );
  96. const workspacePath = '/path/to/workspace/';
  97. const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core#new-feature' );
  98. const cloneCommands = `cd ${ workspacePath } && git clone ${ urlInfo.server + urlInfo.repository }`;
  99. toRestore.push( shExecStub );
  100. git.cloneRepository( urlInfo, workspacePath );
  101. expect( shExecStub.calledOnce ).to.equal( true );
  102. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( cloneCommands );
  103. } );
  104. } );
  105. describe( 'checkout', () => {
  106. it( 'should be defined', () => expect( git.checkout ).to.be.a( 'function' ) );
  107. it( 'should call checkout commands', () => {
  108. const shExecStub = sinon.stub( tools, 'shExec' );
  109. const repositoryLocation = 'path/to/repository';
  110. const branchName = 'branch-to-checkout';
  111. const checkoutCommands = `cd ${ repositoryLocation } && git checkout ${ branchName }`;
  112. toRestore.push( shExecStub );
  113. git.checkout( repositoryLocation, branchName );
  114. expect( shExecStub.calledOnce ).to.equal( true );
  115. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( checkoutCommands );
  116. } );
  117. } );
  118. describe( 'pull', () => {
  119. it( 'should be defined', () => expect( git.pull ).to.be.a( 'function' ) );
  120. it( 'should call pull commands', () => {
  121. const shExecStub = sinon.stub( tools, 'shExec' );
  122. const repositoryLocation = 'path/to/repository';
  123. const branchName = 'branch-to-pull';
  124. const pullCommands = `cd ${ repositoryLocation } && git pull origin ${ branchName }`;
  125. toRestore.push( shExecStub );
  126. git.pull( repositoryLocation, branchName );
  127. expect( shExecStub.calledOnce ).to.equal( true );
  128. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( pullCommands );
  129. } );
  130. } );
  131. describe( 'initializeRepository', () => {
  132. it( 'should be defined', () => expect( git.initializeRepository ).to.be.a( 'function' ) );
  133. it( 'should call initialize commands', () => {
  134. const shExecStub = sinon.stub( tools, 'shExec' );
  135. const repositoryLocation = 'path/to/repository';
  136. const initializeCommands = [
  137. `git init ${ repositoryLocation }`,
  138. `cd ${ repositoryLocation }`,
  139. `git remote add boilerplate ${ git.BOILERPLATE_REPOSITORY }`,
  140. `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
  141. `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
  142. ];
  143. toRestore.push( shExecStub );
  144. git.initializeRepository( repositoryLocation );
  145. expect( shExecStub.calledOnce ).to.equal( true );
  146. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( initializeCommands.join( ' && ' ) );
  147. } );
  148. } );
  149. describe( 'getStatus', () => {
  150. it( 'should be defined', () => expect( git.getStatus ).to.be.a( 'function' ) );
  151. it( 'should call status command', () => {
  152. const shExecStub = sinon.stub( tools, 'shExec' );
  153. const repositoryLocation = 'path/to/repository';
  154. const statusCommands = `cd ${ repositoryLocation } && git status --porcelain -sb`;
  155. toRestore.push( shExecStub );
  156. git.getStatus( repositoryLocation );
  157. expect( shExecStub.calledOnce ).to.equal( true );
  158. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( statusCommands );
  159. } );
  160. } );
  161. describe( 'updateBoilerplate', () => {
  162. it( 'should be defined', () => expect( git.updateBoilerplate ).to.be.a( 'function' ) );
  163. it( 'should fetch and merge boilerplate if remote already exists', () => {
  164. const shExecStub = sinon.stub( tools, 'shExec' );
  165. const repositoryLocation = 'path/to/repository';
  166. const updateCommands = [
  167. `cd ${ repositoryLocation }`,
  168. `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
  169. `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
  170. ];
  171. shExecStub.onCall( 0 ).returns( 'origin\nboilerplate' );
  172. toRestore.push( shExecStub );
  173. git.updateBoilerplate( repositoryLocation );
  174. expect( shExecStub.calledTwice ).to.equal( true );
  175. expect( shExecStub.secondCall.args[ 0 ] ).to.equal( updateCommands.join( ' && ' ) );
  176. } );
  177. it( 'should add boilerplate remote if one not exists', () => {
  178. const shExecStub = sinon.stub( tools, 'shExec' );
  179. const repositoryLocation = 'path/to/repository';
  180. const addRemoteCommands = `cd ${ repositoryLocation } && git remote add boilerplate ${ git.BOILERPLATE_REPOSITORY }`;
  181. const updateCommands = [
  182. `cd ${ repositoryLocation }`,
  183. `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
  184. `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
  185. ];
  186. shExecStub.onCall( 0 ).returns( 'origin\nnew' );
  187. toRestore.push( shExecStub );
  188. git.updateBoilerplate( repositoryLocation );
  189. expect( shExecStub.calledThrice ).to.equal( true );
  190. expect( shExecStub.secondCall.args[ 0 ] ).to.equal( addRemoteCommands );
  191. expect( shExecStub.thirdCall.args[ 0 ] ).to.equal( updateCommands.join( ' && ' ) );
  192. } );
  193. } );
  194. describe( 'initialCommit', () => {
  195. it( 'should be defined', () => expect( git.initialCommit ).to.be.a( 'function' ) );
  196. it( 'should execute commit commands', () => {
  197. const shExecStub = sinon.stub( tools, 'shExec' );
  198. const pluginName = 'ckeditor5-plugin-name';
  199. const repositoryPath = '/path/to/repo';
  200. const commitCommands = [
  201. `cd ${ repositoryPath }`,
  202. `git add .`,
  203. `git commit -m "Initial commit for ${ pluginName }."`
  204. ];
  205. toRestore.push( shExecStub );
  206. git.initialCommit( pluginName, repositoryPath );
  207. expect( shExecStub.calledOnce ).to.equal( true );
  208. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( commitCommands.join( ' && ' ) );
  209. } );
  210. } );
  211. } );
  212. } );