git.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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( '../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( () => 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.branch ).to.equal( 'master' );
  25. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  26. } );
  27. it( 'should parse short GitHub URL with provided branch ', () => {
  28. const urlInfo = git.parseRepositoryUrl( 'ckeditor/ckeditor5-core#experimental' );
  29. expect( urlInfo.server ).to.equal( 'https://github.com/' );
  30. expect( urlInfo.branch ).to.equal( 'experimental' );
  31. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  32. } );
  33. it( 'should parse full GitHub URL (http)', () => {
  34. const urlInfo = git.parseRepositoryUrl( 'http://github.com/ckeditor/ckeditor5-core' );
  35. expect( urlInfo.server ).to.equal( 'http://github.com/' );
  36. expect( urlInfo.branch ).to.equal( 'master' );
  37. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  38. } );
  39. it( 'should parse full GitHub URL (http) with provided branch', () => {
  40. const urlInfo = git.parseRepositoryUrl( 'http://github.com/ckeditor/ckeditor5-core#experimental' );
  41. expect( urlInfo.server ).to.equal( 'http://github.com/' );
  42. expect( urlInfo.branch ).to.equal( 'experimental' );
  43. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  44. } );
  45. it( 'should parse full GitHub URL (https)', () => {
  46. const urlInfo = git.parseRepositoryUrl( 'https://github.com/ckeditor/ckeditor5-core' );
  47. expect( urlInfo.server ).to.equal( 'https://github.com/' );
  48. expect( urlInfo.branch ).to.equal( 'master' );
  49. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  50. } );
  51. it( 'should parse full GitHub URL (https) with provided branch', () => {
  52. const urlInfo = git.parseRepositoryUrl( 'https://github.com/ckeditor/ckeditor5-core#t/122' );
  53. expect( urlInfo.server ).to.equal( 'https://github.com/' );
  54. expect( urlInfo.branch ).to.equal( 't/122' );
  55. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  56. } );
  57. it( 'should parse full GitHub URL (git)', () => {
  58. const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core' );
  59. expect( urlInfo.server ).to.equal( 'git@github.com:' );
  60. expect( urlInfo.branch ).to.equal( 'master' );
  61. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  62. } );
  63. it( 'should parse full GitHub URL (git) with provided branch', () => {
  64. const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core#new-feature' );
  65. expect( urlInfo.server ).to.equal( 'git@github.com:' );
  66. expect( urlInfo.branch ).to.equal( 'new-feature' );
  67. expect( urlInfo.repository ).to.equal( 'ckeditor/ckeditor5-core' );
  68. } );
  69. } );
  70. describe( 'cloneRepository', () => {
  71. it( 'should be defined', () => expect( git.cloneRepository ).to.be.a( 'function' ) );
  72. it( 'should call clone commands', () => {
  73. const shExecStub = sinon.stub( tools, 'shExec' );
  74. const workspacePath = '/path/to/workspace/';
  75. const urlInfo = git.parseRepositoryUrl( 'git@github.com:ckeditor/ckeditor5-core#new-feature' );
  76. const cloneCommands = `cd ${ workspacePath } && git clone ${ urlInfo.server + urlInfo.repository }`;
  77. toRestore.push( shExecStub );
  78. git.cloneRepository( urlInfo, workspacePath );
  79. expect( shExecStub.calledOnce ).to.equal( true );
  80. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( cloneCommands );
  81. } );
  82. } );
  83. describe( 'checkout', () => {
  84. it( 'should be defined', () => expect( git.checkout ).to.be.a( 'function' ) );
  85. it( 'should call checkout commands', () => {
  86. const shExecStub = sinon.stub( tools, 'shExec' );
  87. const repositoryLocation = 'path/to/repository';
  88. const branchName = 'branch-to-checkout';
  89. const checkoutCommands = `cd ${ repositoryLocation } && git checkout ${ branchName }`;
  90. toRestore.push( shExecStub );
  91. git.checkout( repositoryLocation, branchName );
  92. expect( shExecStub.calledOnce ).to.equal( true );
  93. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( checkoutCommands );
  94. } );
  95. } );
  96. describe( 'pull', () => {
  97. it( 'should be defined', () => expect( git.pull ).to.be.a( 'function' ) );
  98. it( 'should call pull commands', () => {
  99. const shExecStub = sinon.stub( tools, 'shExec' );
  100. const repositoryLocation = 'path/to/repository';
  101. const branchName = 'branch-to-pull';
  102. const pullCommands = `cd ${ repositoryLocation } && git pull origin ${ branchName }`;
  103. toRestore.push( shExecStub );
  104. git.pull( repositoryLocation, branchName );
  105. expect( shExecStub.calledOnce ).to.equal( true );
  106. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( pullCommands );
  107. } );
  108. } );
  109. describe( 'initializeRepository', () => {
  110. it( 'should be defined', () => expect( git.initializeRepository ).to.be.a( 'function' ) );
  111. it( 'should call initialize commands', () => {
  112. const shExecStub = sinon.stub( tools, 'shExec' );
  113. const repositoryLocation = 'path/to/repository';
  114. const initializeCommands = [
  115. `git init ${ repositoryLocation }`,
  116. `cd ${ repositoryLocation }`,
  117. `git remote add boilerplate ${ git.BOILERPLATE_REPOSITORY }`,
  118. `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
  119. `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
  120. ];
  121. toRestore.push( shExecStub );
  122. git.initializeRepository( repositoryLocation );
  123. expect( shExecStub.calledOnce ).to.equal( true );
  124. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( initializeCommands.join( ' && ' ) );
  125. } );
  126. } );
  127. describe( 'getStatus', () => {
  128. it( 'should be defined', () => expect( git.getStatus ).to.be.a( 'function' ) );
  129. it( 'should call status command', () => {
  130. const shExecStub = sinon.stub( tools, 'shExec' );
  131. const repositoryLocation = 'path/to/repository';
  132. const statusCommands = `cd ${ repositoryLocation } && git status --porcelain -sb`;
  133. toRestore.push( shExecStub );
  134. git.getStatus( repositoryLocation );
  135. expect( shExecStub.calledOnce ).to.equal( true );
  136. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( statusCommands );
  137. } );
  138. } );
  139. describe( 'updateBoilerplate', () => {
  140. it( 'should be defined', () => expect( git.updateBoilerplate ).to.be.a( 'function' ) );
  141. it( 'should call update boilerplate commands', () => {
  142. const shExecStub = sinon.stub( tools, 'shExec' );
  143. const repositoryLocation = 'path/to/repository';
  144. const addRemoteCommands = `cd ${ repositoryLocation } && git remote add boilerplate ${ git.BOILERPLATE_REPOSITORY }`;
  145. const updateCommands = [
  146. `cd ${ repositoryLocation }`,
  147. `git fetch boilerplate ${ git.BOILERPLATE_BRANCH }`,
  148. `git merge boilerplate/${ git.BOILERPLATE_BRANCH }`
  149. ];
  150. toRestore.push( shExecStub );
  151. git.updateBoilerplate( repositoryLocation );
  152. expect( shExecStub.calledTwice ).to.equal( true );
  153. expect( shExecStub.firstCall.args[ 0 ] ).to.equal( addRemoteCommands );
  154. expect( shExecStub.secondCall.args[ 0 ] ).to.equal( updateCommands.join( ' && ' ) );
  155. } );
  156. } );
  157. } );
  158. } );