8
0

git.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. } );
  86. describe( 'cloneRepository', () => {
  87. it( 'should be defined', () => expect( git.cloneRepository ).to.be.a( 'function' ) );
  88. it( 'should call clone commands', () => {
  89. const shExecStub = sinon.stub( tools, 'shExec' );
  90. const workspacePath = '/path/to/workspace/';
  91. const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core#new-feature' );
  92. const cloneCommands = `cd ${ workspacePath } && git clone ${ urlInfo.server + urlInfo.repository }`;
  93. toRestore.push( shExecStub );
  94. git.cloneRepository( urlInfo, workspacePath );
  95. expect( shExecStub.calledOnce ).to.equal( true );
  96. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( cloneCommands );
  97. } );
  98. } );
  99. describe( 'checkout', () => {
  100. it( 'should be defined', () => expect( git.checkout ).to.be.a( 'function' ) );
  101. it( 'should call checkout commands', () => {
  102. const shExecStub = sinon.stub( tools, 'shExec' );
  103. const repositoryLocation = 'path/to/repository';
  104. const branchName = 'branch-to-checkout';
  105. const checkoutCommands = `cd ${ repositoryLocation } && git checkout ${ branchName }`;
  106. toRestore.push( shExecStub );
  107. git.checkout( repositoryLocation, branchName );
  108. expect( shExecStub.calledOnce ).to.equal( true );
  109. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( checkoutCommands );
  110. } );
  111. } );
  112. describe( 'pull', () => {
  113. it( 'should be defined', () => expect( git.pull ).to.be.a( 'function' ) );
  114. it( 'should call pull commands', () => {
  115. const shExecStub = sinon.stub( tools, 'shExec' );
  116. const repositoryLocation = 'path/to/repository';
  117. const branchName = 'branch-to-pull';
  118. const pullCommands = `cd ${ repositoryLocation } && git pull origin ${ branchName }`;
  119. toRestore.push( shExecStub );
  120. git.pull( repositoryLocation, branchName );
  121. expect( shExecStub.calledOnce ).to.equal( true );
  122. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( pullCommands );
  123. } );
  124. } );
  125. describe( 'initializeRepository', () => {
  126. it( 'should be defined', () => expect( git.initializeRepository ).to.be.a( 'function' ) );
  127. it( 'should call initialize commands', () => {
  128. const shExecStub = sinon.stub( tools, 'shExec' );
  129. const repositoryLocation = 'path/to/repository';
  130. const initializeCommands = [
  131. `git init ${ repositoryLocation }`,
  132. `cd ${ repositoryLocation }`,
  133. `git remote add boilerplate ${ git.BOILERPLATE_REPOSITORY }`,
  134. `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
  135. `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
  136. ];
  137. toRestore.push( shExecStub );
  138. git.initializeRepository( repositoryLocation );
  139. expect( shExecStub.calledOnce ).to.equal( true );
  140. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( initializeCommands.join( ' && ' ) );
  141. } );
  142. } );
  143. describe( 'getStatus', () => {
  144. it( 'should be defined', () => expect( git.getStatus ).to.be.a( 'function' ) );
  145. it( 'should call status command', () => {
  146. const shExecStub = sinon.stub( tools, 'shExec' );
  147. const repositoryLocation = 'path/to/repository';
  148. const statusCommands = `cd ${ repositoryLocation } && git status --porcelain -sb`;
  149. toRestore.push( shExecStub );
  150. git.getStatus( repositoryLocation );
  151. expect( shExecStub.calledOnce ).to.equal( true );
  152. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( statusCommands );
  153. } );
  154. } );
  155. describe( 'updateBoilerplate', () => {
  156. it( 'should be defined', () => expect( git.updateBoilerplate ).to.be.a( 'function' ) );
  157. it( 'should fetch and merge boilerplate if remote already exists', () => {
  158. const shExecStub = sinon.stub( tools, 'shExec' );
  159. const repositoryLocation = 'path/to/repository';
  160. const updateCommands = [
  161. `cd ${ repositoryLocation }`,
  162. `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
  163. `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
  164. ];
  165. shExecStub.onCall( 0 ).returns( 'origin\nboilerplate' );
  166. toRestore.push( shExecStub );
  167. git.updateBoilerplate( repositoryLocation );
  168. expect( shExecStub.calledTwice ).to.equal( true );
  169. expect( shExecStub.secondCall.args[ 0 ] ).to.equal( updateCommands.join( ' && ' ) );
  170. } );
  171. it( 'should add boilerplate remote if one not exists', () => {
  172. const shExecStub = sinon.stub( tools, 'shExec' );
  173. const repositoryLocation = 'path/to/repository';
  174. const addRemoteCommands = `cd ${ repositoryLocation } && git remote add boilerplate ${ git.BOILERPLATE_REPOSITORY }`;
  175. const updateCommands = [
  176. `cd ${ repositoryLocation }`,
  177. `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
  178. `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
  179. ];
  180. shExecStub.onCall( 0 ).returns( 'origin\nnew' );
  181. toRestore.push( shExecStub );
  182. git.updateBoilerplate( repositoryLocation );
  183. expect( shExecStub.calledThrice ).to.equal( true );
  184. expect( shExecStub.secondCall.args[ 0 ] ).to.equal( addRemoteCommands );
  185. expect( shExecStub.thirdCall.args[ 0 ] ).to.equal( updateCommands.join( ' && ' ) );
  186. } );
  187. } );
  188. describe( 'initialCommit', () => {
  189. it( 'should be defined', () => expect( git.initialCommit ).to.be.a( 'function' ) );
  190. it( 'should execute commit commands', () => {
  191. const shExecStub = sinon.stub( tools, 'shExec' );
  192. const pluginName = 'ckeditor5-plugin-name';
  193. const repositoryPath = '/path/to/repo';
  194. const commitCommands = [
  195. `cd ${ repositoryPath }`,
  196. `git add .`,
  197. `git commit -m "Initial commit for ${ pluginName }."`
  198. ];
  199. toRestore.push( shExecStub );
  200. git.initialCommit( pluginName, repositoryPath );
  201. expect( shExecStub.calledOnce ).to.equal( true );
  202. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( commitCommands.join( ' && ' ) );
  203. } );
  204. } );
  205. } );
  206. } );