ref-helper.test.ts
1 import * as assert from 'assert' 2 import * as refHelper from '../lib/ref-helper' 3 import {IGitCommandManager} from '../lib/git-command-manager' 4 5 const commit = '1234567890123456789012345678901234567890' 6 let git: IGitCommandManager 7 8 describe('ref-helper tests', () => { 9 beforeEach(() => { 10 git = ({} as unknown) as IGitCommandManager 11 }) 12 13 it('getCheckoutInfo requires git', async () => { 14 const git = (null as unknown) as IGitCommandManager 15 try { 16 await refHelper.getCheckoutInfo(git, 'refs/heads/my/branch', commit) 17 throw new Error('Should not reach here') 18 } catch (err) { 19 expect(err.message).toBe('Arg git cannot be empty') 20 } 21 }) 22 23 it('getCheckoutInfo requires ref or commit', async () => { 24 try { 25 await refHelper.getCheckoutInfo(git, '', '') 26 throw new Error('Should not reach here') 27 } catch (err) { 28 expect(err.message).toBe('Args ref and commit cannot both be empty') 29 } 30 }) 31 32 it('getCheckoutInfo sha only', async () => { 33 const checkoutInfo = await refHelper.getCheckoutInfo(git, '', commit) 34 expect(checkoutInfo.ref).toBe(commit) 35 expect(checkoutInfo.startPoint).toBeFalsy() 36 }) 37 38 it('getCheckoutInfo refs/heads/', async () => { 39 const checkoutInfo = await refHelper.getCheckoutInfo( 40 git, 41 'refs/heads/my/branch', 42 commit 43 ) 44 expect(checkoutInfo.ref).toBe('my/branch') 45 expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch') 46 }) 47 48 it('getCheckoutInfo refs/pull/', async () => { 49 const checkoutInfo = await refHelper.getCheckoutInfo( 50 git, 51 'refs/pull/123/merge', 52 commit 53 ) 54 expect(checkoutInfo.ref).toBe('refs/remotes/pull/123/merge') 55 expect(checkoutInfo.startPoint).toBeFalsy() 56 }) 57 58 it('getCheckoutInfo refs/tags/', async () => { 59 const checkoutInfo = await refHelper.getCheckoutInfo( 60 git, 61 'refs/tags/my-tag', 62 commit 63 ) 64 expect(checkoutInfo.ref).toBe('refs/tags/my-tag') 65 expect(checkoutInfo.startPoint).toBeFalsy() 66 }) 67 68 it('getCheckoutInfo unqualified branch only', async () => { 69 git.branchExists = jest.fn(async (remote: boolean, pattern: string) => { 70 return true 71 }) 72 73 const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my/branch', '') 74 75 expect(checkoutInfo.ref).toBe('my/branch') 76 expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch') 77 }) 78 79 it('getCheckoutInfo unqualified tag only', async () => { 80 git.branchExists = jest.fn(async (remote: boolean, pattern: string) => { 81 return false 82 }) 83 git.tagExists = jest.fn(async (pattern: string) => { 84 return true 85 }) 86 87 const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my-tag', '') 88 89 expect(checkoutInfo.ref).toBe('refs/tags/my-tag') 90 expect(checkoutInfo.startPoint).toBeFalsy() 91 }) 92 93 it('getCheckoutInfo unqualified ref only, not a branch or tag', async () => { 94 git.branchExists = jest.fn(async (remote: boolean, pattern: string) => { 95 return false 96 }) 97 git.tagExists = jest.fn(async (pattern: string) => { 98 return false 99 }) 100 101 try { 102 await refHelper.getCheckoutInfo(git, 'my-ref', '') 103 throw new Error('Should not reach here') 104 } catch (err) { 105 expect(err.message).toBe( 106 "A branch or tag with the name 'my-ref' could not be found" 107 ) 108 } 109 }) 110 111 it('getRefSpec requires ref or commit', async () => { 112 assert.throws( 113 () => refHelper.getRefSpec('', ''), 114 /Args ref and commit cannot both be empty/ 115 ) 116 }) 117 118 it('getRefSpec sha + refs/heads/', async () => { 119 const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit) 120 expect(refSpec.length).toBe(1) 121 expect(refSpec[0]).toBe(`+${commit}:refs/remotes/origin/my/branch`) 122 }) 123 124 it('getRefSpec sha + refs/pull/', async () => { 125 const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit) 126 expect(refSpec.length).toBe(1) 127 expect(refSpec[0]).toBe(`+${commit}:refs/remotes/pull/123/merge`) 128 }) 129 130 it('getRefSpec sha + refs/tags/', async () => { 131 const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit) 132 expect(refSpec.length).toBe(1) 133 expect(refSpec[0]).toBe(`+${commit}:refs/tags/my-tag`) 134 }) 135 136 it('getRefSpec sha only', async () => { 137 const refSpec = refHelper.getRefSpec('', commit) 138 expect(refSpec.length).toBe(1) 139 expect(refSpec[0]).toBe(commit) 140 }) 141 142 it('getRefSpec unqualified ref only', async () => { 143 const refSpec = refHelper.getRefSpec('my-ref', '') 144 expect(refSpec.length).toBe(2) 145 expect(refSpec[0]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*') 146 expect(refSpec[1]).toBe('+refs/tags/my-ref*:refs/tags/my-ref*') 147 }) 148 149 it('getRefSpec refs/heads/ only', async () => { 150 const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '') 151 expect(refSpec.length).toBe(1) 152 expect(refSpec[0]).toBe( 153 '+refs/heads/my/branch:refs/remotes/origin/my/branch' 154 ) 155 }) 156 157 it('getRefSpec refs/pull/ only', async () => { 158 const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '') 159 expect(refSpec.length).toBe(1) 160 expect(refSpec[0]).toBe('+refs/pull/123/merge:refs/remotes/pull/123/merge') 161 }) 162 163 it('getRefSpec refs/tags/ only', async () => { 164 const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '') 165 expect(refSpec.length).toBe(1) 166 expect(refSpec[0]).toBe('+refs/tags/my-tag:refs/tags/my-tag') 167 }) 168 })