git-command-manager.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import * as exec from '@actions/exec'
  2. import * as fshelper from '../lib/fs-helper'
  3. import * as commandManager from '../lib/git-command-manager'
  4. let git: commandManager.IGitCommandManager
  5. let mockExec = jest.fn()
  6. describe('git-auth-helper tests', () => {
  7. beforeAll(async () => {})
  8. beforeEach(async () => {
  9. jest.spyOn(fshelper, 'fileExistsSync').mockImplementation(jest.fn())
  10. jest.spyOn(fshelper, 'directoryExistsSync').mockImplementation(jest.fn())
  11. })
  12. afterEach(() => {
  13. jest.restoreAllMocks()
  14. })
  15. afterAll(() => {})
  16. it('branch list matches', async () => {
  17. mockExec.mockImplementation((path, args, options) => {
  18. console.log(args, options.listeners.stdout)
  19. if (args.includes('version')) {
  20. options.listeners.stdout(Buffer.from('2.18'))
  21. return 0
  22. }
  23. if (args.includes('rev-parse')) {
  24. options.listeners.stdline(Buffer.from('refs/heads/foo'))
  25. options.listeners.stdline(Buffer.from('refs/heads/bar'))
  26. return 0
  27. }
  28. return 1
  29. })
  30. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  31. const workingDirectory = 'test'
  32. const lfs = false
  33. const doSparseCheckout = false
  34. git = await commandManager.createCommandManager(
  35. workingDirectory,
  36. lfs,
  37. doSparseCheckout
  38. )
  39. let branches = await git.branchList(false)
  40. expect(branches).toHaveLength(2)
  41. expect(branches.sort()).toEqual(['foo', 'bar'].sort())
  42. })
  43. it('ambiguous ref name output is captured', async () => {
  44. mockExec.mockImplementation((path, args, options) => {
  45. console.log(args, options.listeners.stdout)
  46. if (args.includes('version')) {
  47. options.listeners.stdout(Buffer.from('2.18'))
  48. return 0
  49. }
  50. if (args.includes('rev-parse')) {
  51. options.listeners.stdline(Buffer.from('refs/heads/foo'))
  52. // If refs/tags/v1 and refs/heads/tags/v1 existed on this repository
  53. options.listeners.errline(
  54. Buffer.from("error: refname 'tags/v1' is ambiguous")
  55. )
  56. return 0
  57. }
  58. return 1
  59. })
  60. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  61. const workingDirectory = 'test'
  62. const lfs = false
  63. const doSparseCheckout = false
  64. git = await commandManager.createCommandManager(
  65. workingDirectory,
  66. lfs,
  67. doSparseCheckout
  68. )
  69. let branches = await git.branchList(false)
  70. expect(branches).toHaveLength(1)
  71. expect(branches.sort()).toEqual(['foo'].sort())
  72. })
  73. })
  74. describe('Test fetchDepth and fetchTags options', () => {
  75. beforeEach(async () => {
  76. jest.spyOn(fshelper, 'fileExistsSync').mockImplementation(jest.fn())
  77. jest.spyOn(fshelper, 'directoryExistsSync').mockImplementation(jest.fn())
  78. mockExec.mockImplementation((path, args, options) => {
  79. console.log(args, options.listeners.stdout)
  80. if (args.includes('version')) {
  81. options.listeners.stdout(Buffer.from('2.18'))
  82. }
  83. return 0
  84. })
  85. })
  86. afterEach(() => {
  87. jest.restoreAllMocks()
  88. })
  89. it('should call execGit with the correct arguments when fetchDepth is 0 and fetchTags is true', async () => {
  90. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  91. const workingDirectory = 'test'
  92. const lfs = false
  93. const doSparseCheckout = false
  94. git = await commandManager.createCommandManager(
  95. workingDirectory,
  96. lfs,
  97. doSparseCheckout
  98. )
  99. const refSpec = ['refspec1', 'refspec2']
  100. const options = {
  101. filter: 'filterValue',
  102. fetchDepth: 0,
  103. fetchTags: true
  104. }
  105. await git.fetch(refSpec, options)
  106. expect(mockExec).toHaveBeenCalledWith(
  107. expect.any(String),
  108. [
  109. '-c',
  110. 'protocol.version=2',
  111. 'fetch',
  112. '--prune',
  113. '--progress',
  114. '--no-recurse-submodules',
  115. '--filter=filterValue',
  116. 'origin',
  117. 'refspec1',
  118. 'refspec2'
  119. ],
  120. expect.any(Object)
  121. )
  122. })
  123. it('should call execGit with the correct arguments when fetchDepth is 0 and fetchTags is false', async () => {
  124. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  125. const workingDirectory = 'test'
  126. const lfs = false
  127. const doSparseCheckout = false
  128. git = await commandManager.createCommandManager(
  129. workingDirectory,
  130. lfs,
  131. doSparseCheckout
  132. )
  133. const refSpec = ['refspec1', 'refspec2']
  134. const options = {
  135. filter: 'filterValue',
  136. fetchDepth: 0,
  137. fetchTags: false
  138. }
  139. await git.fetch(refSpec, options)
  140. expect(mockExec).toHaveBeenCalledWith(
  141. expect.any(String),
  142. [
  143. '-c',
  144. 'protocol.version=2',
  145. 'fetch',
  146. '--no-tags',
  147. '--prune',
  148. '--progress',
  149. '--no-recurse-submodules',
  150. '--filter=filterValue',
  151. 'origin',
  152. 'refspec1',
  153. 'refspec2'
  154. ],
  155. expect.any(Object)
  156. )
  157. })
  158. it('should call execGit with the correct arguments when fetchDepth is 1 and fetchTags is false', async () => {
  159. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  160. const workingDirectory = 'test'
  161. const lfs = false
  162. const doSparseCheckout = false
  163. git = await commandManager.createCommandManager(
  164. workingDirectory,
  165. lfs,
  166. doSparseCheckout
  167. )
  168. const refSpec = ['refspec1', 'refspec2']
  169. const options = {
  170. filter: 'filterValue',
  171. fetchDepth: 1,
  172. fetchTags: false
  173. }
  174. await git.fetch(refSpec, options)
  175. expect(mockExec).toHaveBeenCalledWith(
  176. expect.any(String),
  177. [
  178. '-c',
  179. 'protocol.version=2',
  180. 'fetch',
  181. '--no-tags',
  182. '--prune',
  183. '--progress',
  184. '--no-recurse-submodules',
  185. '--filter=filterValue',
  186. '--depth=1',
  187. 'origin',
  188. 'refspec1',
  189. 'refspec2'
  190. ],
  191. expect.any(Object)
  192. )
  193. })
  194. it('should call execGit with the correct arguments when fetchDepth is 1 and fetchTags is true', async () => {
  195. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  196. const workingDirectory = 'test'
  197. const lfs = false
  198. const doSparseCheckout = false
  199. git = await commandManager.createCommandManager(
  200. workingDirectory,
  201. lfs,
  202. doSparseCheckout
  203. )
  204. const refSpec = ['refspec1', 'refspec2']
  205. const options = {
  206. filter: 'filterValue',
  207. fetchDepth: 1,
  208. fetchTags: true
  209. }
  210. await git.fetch(refSpec, options)
  211. expect(mockExec).toHaveBeenCalledWith(
  212. expect.any(String),
  213. [
  214. '-c',
  215. 'protocol.version=2',
  216. 'fetch',
  217. '--prune',
  218. '--progress',
  219. '--no-recurse-submodules',
  220. '--filter=filterValue',
  221. '--depth=1',
  222. 'origin',
  223. 'refspec1',
  224. 'refspec2'
  225. ],
  226. expect.any(Object)
  227. )
  228. })
  229. })