git-command-manager.test.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.25'))
  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.25'))
  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.25'))
  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. '--no-recurse-submodules',
  114. '--filter=filterValue',
  115. 'origin',
  116. 'refspec1',
  117. 'refspec2'
  118. ],
  119. expect.any(Object)
  120. )
  121. })
  122. it('should call execGit with the correct arguments when fetchDepth is 0 and fetchTags is false', async () => {
  123. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  124. const workingDirectory = 'test'
  125. const lfs = false
  126. const doSparseCheckout = false
  127. git = await commandManager.createCommandManager(
  128. workingDirectory,
  129. lfs,
  130. doSparseCheckout
  131. )
  132. const refSpec = ['refspec1', 'refspec2']
  133. const options = {
  134. filter: 'filterValue',
  135. fetchDepth: 0,
  136. fetchTags: false
  137. }
  138. await git.fetch(refSpec, options)
  139. expect(mockExec).toHaveBeenCalledWith(
  140. expect.any(String),
  141. [
  142. '-c',
  143. 'protocol.version=2',
  144. 'fetch',
  145. '--no-tags',
  146. '--prune',
  147. '--no-recurse-submodules',
  148. '--filter=filterValue',
  149. 'origin',
  150. 'refspec1',
  151. 'refspec2'
  152. ],
  153. expect.any(Object)
  154. )
  155. })
  156. it('should call execGit with the correct arguments when fetchDepth is 1 and fetchTags is false', async () => {
  157. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  158. const workingDirectory = 'test'
  159. const lfs = false
  160. const doSparseCheckout = false
  161. git = await commandManager.createCommandManager(
  162. workingDirectory,
  163. lfs,
  164. doSparseCheckout
  165. )
  166. const refSpec = ['refspec1', 'refspec2']
  167. const options = {
  168. filter: 'filterValue',
  169. fetchDepth: 1,
  170. fetchTags: false
  171. }
  172. await git.fetch(refSpec, options)
  173. expect(mockExec).toHaveBeenCalledWith(
  174. expect.any(String),
  175. [
  176. '-c',
  177. 'protocol.version=2',
  178. 'fetch',
  179. '--no-tags',
  180. '--prune',
  181. '--no-recurse-submodules',
  182. '--filter=filterValue',
  183. '--depth=1',
  184. 'origin',
  185. 'refspec1',
  186. 'refspec2'
  187. ],
  188. expect.any(Object)
  189. )
  190. })
  191. it('should call execGit with the correct arguments when fetchDepth is 1 and fetchTags is true', async () => {
  192. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  193. const workingDirectory = 'test'
  194. const lfs = false
  195. const doSparseCheckout = false
  196. git = await commandManager.createCommandManager(
  197. workingDirectory,
  198. lfs,
  199. doSparseCheckout
  200. )
  201. const refSpec = ['refspec1', 'refspec2']
  202. const options = {
  203. filter: 'filterValue',
  204. fetchDepth: 1,
  205. fetchTags: true
  206. }
  207. await git.fetch(refSpec, options)
  208. expect(mockExec).toHaveBeenCalledWith(
  209. expect.any(String),
  210. [
  211. '-c',
  212. 'protocol.version=2',
  213. 'fetch',
  214. '--prune',
  215. '--no-recurse-submodules',
  216. '--filter=filterValue',
  217. '--depth=1',
  218. 'origin',
  219. 'refspec1',
  220. 'refspec2'
  221. ],
  222. expect.any(Object)
  223. )
  224. })
  225. it('should call execGit with the correct arguments when showProgress is true', async () => {
  226. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  227. const workingDirectory = 'test'
  228. const lfs = false
  229. const doSparseCheckout = false
  230. git = await commandManager.createCommandManager(
  231. workingDirectory,
  232. lfs,
  233. doSparseCheckout
  234. )
  235. const refSpec = ['refspec1', 'refspec2']
  236. const options = {
  237. filter: 'filterValue',
  238. showProgress: true
  239. }
  240. await git.fetch(refSpec, options)
  241. expect(mockExec).toHaveBeenCalledWith(
  242. expect.any(String),
  243. [
  244. '-c',
  245. 'protocol.version=2',
  246. 'fetch',
  247. '--no-tags',
  248. '--prune',
  249. '--no-recurse-submodules',
  250. '--progress',
  251. '--filter=filterValue',
  252. 'origin',
  253. 'refspec1',
  254. 'refspec2'
  255. ],
  256. expect.any(Object)
  257. )
  258. })
  259. it('should call execGit with the correct arguments when fetchDepth is 42 and showProgress is true', async () => {
  260. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  261. const workingDirectory = 'test'
  262. const lfs = false
  263. const doSparseCheckout = false
  264. git = await commandManager.createCommandManager(
  265. workingDirectory,
  266. lfs,
  267. doSparseCheckout
  268. )
  269. const refSpec = ['refspec1', 'refspec2']
  270. const options = {
  271. filter: 'filterValue',
  272. fetchDepth: 42,
  273. showProgress: true
  274. }
  275. await git.fetch(refSpec, options)
  276. expect(mockExec).toHaveBeenCalledWith(
  277. expect.any(String),
  278. [
  279. '-c',
  280. 'protocol.version=2',
  281. 'fetch',
  282. '--no-tags',
  283. '--prune',
  284. '--no-recurse-submodules',
  285. '--progress',
  286. '--filter=filterValue',
  287. '--depth=42',
  288. 'origin',
  289. 'refspec1',
  290. 'refspec2'
  291. ],
  292. expect.any(Object)
  293. )
  294. })
  295. it('should call execGit with the correct arguments when fetchTags is true and showProgress is true', async () => {
  296. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  297. const workingDirectory = 'test'
  298. const lfs = false
  299. const doSparseCheckout = false
  300. git = await commandManager.createCommandManager(
  301. workingDirectory,
  302. lfs,
  303. doSparseCheckout
  304. )
  305. const refSpec = ['refspec1', 'refspec2']
  306. const options = {
  307. filter: 'filterValue',
  308. fetchTags: true,
  309. showProgress: true
  310. }
  311. await git.fetch(refSpec, options)
  312. expect(mockExec).toHaveBeenCalledWith(
  313. expect.any(String),
  314. [
  315. '-c',
  316. 'protocol.version=2',
  317. 'fetch',
  318. '--prune',
  319. '--no-recurse-submodules',
  320. '--progress',
  321. '--filter=filterValue',
  322. 'origin',
  323. 'refspec1',
  324. 'refspec2'
  325. ],
  326. expect.any(Object)
  327. )
  328. })
  329. })