input-helper.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import * as assert from 'assert'
  2. import * as core from '@actions/core'
  3. import * as fsHelper from '../lib/fs-helper'
  4. import * as github from '@actions/github'
  5. import * as inputHelper from '../lib/input-helper'
  6. import * as path from 'path'
  7. import {IGitSourceSettings} from '../lib/git-source-settings'
  8. const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
  9. const gitHubWorkspace = path.resolve('/checkout-tests/workspace')
  10. // Inputs for mock @actions/core
  11. let inputs = {} as any
  12. // Shallow clone original @actions/github context
  13. let originalContext = {...github.context}
  14. describe('input-helper tests', () => {
  15. beforeAll(() => {
  16. // Mock getInput
  17. jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
  18. return inputs[name]
  19. })
  20. // Mock error/warning/info/debug
  21. jest.spyOn(core, 'error').mockImplementation(jest.fn())
  22. jest.spyOn(core, 'warning').mockImplementation(jest.fn())
  23. jest.spyOn(core, 'info').mockImplementation(jest.fn())
  24. jest.spyOn(core, 'debug').mockImplementation(jest.fn())
  25. // Mock github context
  26. jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
  27. return {
  28. owner: 'some-owner',
  29. repo: 'some-repo'
  30. }
  31. })
  32. github.context.ref = 'refs/heads/some-ref'
  33. github.context.sha = '1234567890123456789012345678901234567890'
  34. // Mock ./fs-helper directoryExistsSync()
  35. jest
  36. .spyOn(fsHelper, 'directoryExistsSync')
  37. .mockImplementation((path: string) => path == gitHubWorkspace)
  38. // GitHub workspace
  39. process.env['GITHUB_WORKSPACE'] = gitHubWorkspace
  40. })
  41. beforeEach(() => {
  42. // Reset inputs
  43. inputs = {}
  44. })
  45. afterAll(() => {
  46. // Restore GitHub workspace
  47. delete process.env['GITHUB_WORKSPACE']
  48. if (originalGitHubWorkspace) {
  49. process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
  50. }
  51. // Restore @actions/github context
  52. github.context.ref = originalContext.ref
  53. github.context.sha = originalContext.sha
  54. // Restore
  55. jest.restoreAllMocks()
  56. })
  57. it('sets defaults', () => {
  58. const settings: IGitSourceSettings = inputHelper.getInputs()
  59. expect(settings).toBeTruthy()
  60. expect(settings.authToken).toBeFalsy()
  61. expect(settings.clean).toBe(true)
  62. expect(settings.commit).toBeTruthy()
  63. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  64. expect(settings.fetchDepth).toBe(1)
  65. expect(settings.lfs).toBe(false)
  66. expect(settings.ref).toBe('refs/heads/some-ref')
  67. expect(settings.repositoryName).toBe('some-repo')
  68. expect(settings.repositoryOwner).toBe('some-owner')
  69. expect(settings.repositoryPath).toBe(gitHubWorkspace)
  70. })
  71. it('qualifies ref', () => {
  72. let originalRef = github.context.ref
  73. try {
  74. github.context.ref = 'some-unqualified-ref'
  75. const settings: IGitSourceSettings = inputHelper.getInputs()
  76. expect(settings).toBeTruthy()
  77. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  78. expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
  79. } finally {
  80. github.context.ref = originalRef
  81. }
  82. })
  83. it('requires qualified repo', () => {
  84. inputs.repository = 'some-unqualified-repo'
  85. assert.throws(() => {
  86. inputHelper.getInputs()
  87. }, /Invalid repository 'some-unqualified-repo'/)
  88. })
  89. it('roots path', () => {
  90. inputs.path = 'some-directory/some-subdirectory'
  91. const settings: IGitSourceSettings = inputHelper.getInputs()
  92. expect(settings.repositoryPath).toBe(
  93. path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory')
  94. )
  95. })
  96. it('sets correct default ref/sha for other repo', () => {
  97. inputs.repository = 'some-owner/some-other-repo'
  98. const settings: IGitSourceSettings = inputHelper.getInputs()
  99. expect(settings.ref).toBe('refs/heads/master')
  100. expect(settings.commit).toBeFalsy()
  101. })
  102. it('sets ref to empty when explicit sha', () => {
  103. inputs.ref = '1111111111222222222233333333334444444444'
  104. const settings: IGitSourceSettings = inputHelper.getInputs()
  105. expect(settings.ref).toBeFalsy()
  106. expect(settings.commit).toBe('1111111111222222222233333333334444444444')
  107. })
  108. it('sets sha to empty when explicit ref', () => {
  109. inputs.ref = 'refs/heads/some-other-ref'
  110. const settings: IGitSourceSettings = inputHelper.getInputs()
  111. expect(settings.ref).toBe('refs/heads/some-other-ref')
  112. expect(settings.commit).toBeFalsy()
  113. })
  114. })