ref-helper.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 unqualified branch only', async () => {
  71. git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
  72. return true
  73. })
  74. const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my/branch', '')
  75. expect(checkoutInfo.ref).toBe('my/branch')
  76. expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
  77. })
  78. it('getCheckoutInfo unqualified tag only', async () => {
  79. git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
  80. return false
  81. })
  82. git.tagExists = jest.fn(async (pattern: string) => {
  83. return true
  84. })
  85. const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my-tag', '')
  86. expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
  87. expect(checkoutInfo.startPoint).toBeFalsy()
  88. })
  89. it('getCheckoutInfo unqualified ref only, not a branch or tag', async () => {
  90. git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
  91. return false
  92. })
  93. git.tagExists = jest.fn(async (pattern: string) => {
  94. return false
  95. })
  96. try {
  97. await refHelper.getCheckoutInfo(git, 'my-ref', '')
  98. throw new Error('Should not reach here')
  99. } catch (err) {
  100. expect((err as any)?.message).toBe(
  101. "A branch or tag with the name 'my-ref' could not be found"
  102. )
  103. }
  104. })
  105. it('getRefSpec requires ref or commit', async () => {
  106. assert.throws(
  107. () => refHelper.getRefSpec('', ''),
  108. /Args ref and commit cannot both be empty/
  109. )
  110. })
  111. it('getRefSpec sha + refs/heads/', async () => {
  112. const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit)
  113. expect(refSpec.length).toBe(1)
  114. expect(refSpec[0]).toBe(`+${commit}:refs/remotes/origin/my/branch`)
  115. })
  116. it('getRefSpec sha + refs/pull/', async () => {
  117. const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit)
  118. expect(refSpec.length).toBe(1)
  119. expect(refSpec[0]).toBe(`+${commit}:refs/remotes/pull/123/merge`)
  120. })
  121. it('getRefSpec sha + refs/tags/', async () => {
  122. const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit)
  123. expect(refSpec.length).toBe(1)
  124. expect(refSpec[0]).toBe(`+${commit}:refs/tags/my-tag`)
  125. })
  126. it('getRefSpec sha only', async () => {
  127. const refSpec = refHelper.getRefSpec('', commit)
  128. expect(refSpec.length).toBe(1)
  129. expect(refSpec[0]).toBe(commit)
  130. })
  131. it('getRefSpec unqualified ref only', async () => {
  132. const refSpec = refHelper.getRefSpec('my-ref', '')
  133. expect(refSpec.length).toBe(2)
  134. expect(refSpec[0]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*')
  135. expect(refSpec[1]).toBe('+refs/tags/my-ref*:refs/tags/my-ref*')
  136. })
  137. it('getRefSpec refs/heads/ only', async () => {
  138. const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '')
  139. expect(refSpec.length).toBe(1)
  140. expect(refSpec[0]).toBe(
  141. '+refs/heads/my/branch:refs/remotes/origin/my/branch'
  142. )
  143. })
  144. it('getRefSpec refs/pull/ only', async () => {
  145. const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '')
  146. expect(refSpec.length).toBe(1)
  147. expect(refSpec[0]).toBe('+refs/pull/123/merge:refs/remotes/pull/123/merge')
  148. })
  149. it('getRefSpec refs/tags/ only', async () => {
  150. const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '')
  151. expect(refSpec.length).toBe(1)
  152. expect(refSpec[0]).toBe('+refs/tags/my-tag:refs/tags/my-tag')
  153. })
  154. })