8
0

git.js 9.9 KB

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