git.js 11 KB

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