git-command-manager.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. })