git-command-manager.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. git = await commandManager.createCommandManager(workingDirectory, lfs)
  34. let branches = await git.branchList(false)
  35. expect(branches).toHaveLength(2)
  36. expect(branches.sort()).toEqual(['foo', 'bar'].sort())
  37. })
  38. it('ambiguous ref name output is captured', async () => {
  39. mockExec.mockImplementation((path, args, options) => {
  40. console.log(args, options.listeners.stdout)
  41. if (args.includes('version')) {
  42. options.listeners.stdout(Buffer.from('2.18'))
  43. return 0
  44. }
  45. if (args.includes('rev-parse')) {
  46. options.listeners.stdline(Buffer.from('refs/heads/foo'))
  47. // If refs/tags/v1 and refs/heads/tags/v1 existed on this repository
  48. options.listeners.errline(
  49. Buffer.from("error: refname 'tags/v1' is ambiguous")
  50. )
  51. return 0
  52. }
  53. return 1
  54. })
  55. jest.spyOn(exec, 'exec').mockImplementation(mockExec)
  56. const workingDirectory = 'test'
  57. const lfs = false
  58. git = await commandManager.createCommandManager(workingDirectory, lfs)
  59. let branches = await git.branchList(false)
  60. expect(branches).toHaveLength(1)
  61. expect(branches.sort()).toEqual(['foo'].sort())
  62. })
  63. })