ref-helper.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.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.message).toBe('Args ref and commit cannot both be empty')
  25. }
  26. })
  27. it('getCheckoutInfo sha only', async () => {
  28. const checkoutInfo = await refHelper.getCheckoutInfo(git, '', commit)
  29. expect(checkoutInfo.ref).toBe(commit)
  30. expect(checkoutInfo.startPoint).toBeFalsy()
  31. })
  32. it('getCheckoutInfo refs/heads/', async () => {
  33. const checkoutInfo = await refHelper.getCheckoutInfo(
  34. git,
  35. 'refs/heads/my/branch',
  36. commit
  37. )
  38. expect(checkoutInfo.ref).toBe('my/branch')
  39. expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
  40. })
  41. it('getCheckoutInfo refs/pull/', async () => {
  42. const checkoutInfo = await refHelper.getCheckoutInfo(
  43. git,
  44. 'refs/pull/123/merge',
  45. commit
  46. )
  47. expect(checkoutInfo.ref).toBe('refs/remotes/pull/123/merge')
  48. expect(checkoutInfo.startPoint).toBeFalsy()
  49. })
  50. it('getCheckoutInfo refs/tags/', async () => {
  51. const checkoutInfo = await refHelper.getCheckoutInfo(
  52. git,
  53. 'refs/tags/my-tag',
  54. commit
  55. )
  56. expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
  57. expect(checkoutInfo.startPoint).toBeFalsy()
  58. })
  59. it('getCheckoutInfo unqualified branch only', async () => {
  60. git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
  61. return true
  62. })
  63. const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my/branch', '')
  64. expect(checkoutInfo.ref).toBe('my/branch')
  65. expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
  66. })
  67. it('getCheckoutInfo unqualified tag only', async () => {
  68. git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
  69. return false
  70. })
  71. git.tagExists = jest.fn(async (pattern: string) => {
  72. return true
  73. })
  74. const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my-tag', '')
  75. expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
  76. expect(checkoutInfo.startPoint).toBeFalsy()
  77. })
  78. it('getCheckoutInfo unqualified ref only, not a branch or tag', 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 false
  84. })
  85. try {
  86. await refHelper.getCheckoutInfo(git, 'my-ref', '')
  87. throw new Error('Should not reach here')
  88. } catch (err) {
  89. expect(err.message).toBe(
  90. "A branch or tag with the name 'my-ref' could not be found"
  91. )
  92. }
  93. })
  94. it('getRefSpec requires ref or commit', async () => {
  95. assert.throws(
  96. () => refHelper.getRefSpec('', ''),
  97. /Args ref and commit cannot both be empty/
  98. )
  99. })
  100. it('getRefSpec sha + refs/heads/', async () => {
  101. const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit)
  102. expect(refSpec.length).toBe(1)
  103. expect(refSpec[0]).toBe(`+${commit}:refs/remotes/origin/my/branch`)
  104. })
  105. it('getRefSpec sha + refs/pull/', async () => {
  106. const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit)
  107. expect(refSpec.length).toBe(1)
  108. expect(refSpec[0]).toBe(`+${commit}:refs/remotes/pull/123/merge`)
  109. })
  110. it('getRefSpec sha + refs/tags/', async () => {
  111. const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit)
  112. expect(refSpec.length).toBe(1)
  113. expect(refSpec[0]).toBe(`+${commit}:refs/tags/my-tag`)
  114. })
  115. it('getRefSpec sha only', async () => {
  116. const refSpec = refHelper.getRefSpec('', commit)
  117. expect(refSpec.length).toBe(1)
  118. expect(refSpec[0]).toBe(commit)
  119. })
  120. it('getRefSpec unqualified ref only', async () => {
  121. const refSpec = refHelper.getRefSpec('my-ref', '')
  122. expect(refSpec.length).toBe(2)
  123. expect(refSpec[0]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*')
  124. expect(refSpec[1]).toBe('+refs/tags/my-ref*:refs/tags/my-ref*')
  125. })
  126. it('getRefSpec refs/heads/ only', async () => {
  127. const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '')
  128. expect(refSpec.length).toBe(1)
  129. expect(refSpec[0]).toBe(
  130. '+refs/heads/my/branch:refs/remotes/origin/my/branch'
  131. )
  132. })
  133. it('getRefSpec refs/pull/ only', async () => {
  134. const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '')
  135. expect(refSpec.length).toBe(1)
  136. expect(refSpec[0]).toBe('+refs/pull/123/merge:refs/remotes/pull/123/merge')
  137. })
  138. it('getRefSpec refs/tags/ only', async () => {
  139. const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '')
  140. expect(refSpec.length).toBe(1)
  141. expect(refSpec[0]).toBe('+refs/tags/my-tag:refs/tags/my-tag')
  142. })
  143. })