ref-helper.test.ts 5.3 KB

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