ref-helper.test.ts 5.8 KB

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