input-helper.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import {
  2. jest,
  3. describe,
  4. it,
  5. expect,
  6. beforeAll,
  7. beforeEach,
  8. afterAll
  9. } from '@jest/globals'
  10. import * as path from 'path'
  11. const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
  12. const gitHubWorkspace = path.resolve('/checkout-tests/workspace')
  13. // Inputs for mock @actions/core
  14. let inputs = {} as any
  15. // Mutable mock github context
  16. const mockGithubContext: any = {
  17. ref: 'refs/heads/some-ref',
  18. sha: '1234567890123456789012345678901234567890',
  19. repo: {owner: 'some-owner', repo: 'some-repo'},
  20. eventName: '',
  21. payload: {}
  22. }
  23. // Mock @actions/core before loading input-helper
  24. jest.unstable_mockModule('@actions/core', () => ({
  25. getInput: jest.fn((name: string) => inputs[name]),
  26. getBooleanInput: jest.fn((name: string) => inputs[name]),
  27. getMultilineInput: jest.fn((name: string) =>
  28. inputs[name] ? String(inputs[name]).split('\n').filter(Boolean) : []
  29. ),
  30. error: jest.fn(),
  31. warning: jest.fn(),
  32. info: jest.fn(),
  33. debug: jest.fn(),
  34. setFailed: jest.fn(),
  35. setOutput: jest.fn(),
  36. setSecret: jest.fn()
  37. }))
  38. // Mock @actions/github before loading input-helper
  39. jest.unstable_mockModule('@actions/github', () => ({
  40. context: mockGithubContext,
  41. getOctokit: jest.fn()
  42. }))
  43. // Mock fs-helper
  44. const mockDirectoryExistsSync = jest.fn((p: string) => p === gitHubWorkspace)
  45. jest.unstable_mockModule('../src/fs-helper.js', () => ({
  46. directoryExistsSync: mockDirectoryExistsSync,
  47. fileExistsSync: jest.fn()
  48. }))
  49. // Mock workflow-context-helper
  50. const mockGetOrganizationId = jest.fn(async () => 123456)
  51. jest.unstable_mockModule('../src/workflow-context-helper.js', () => ({
  52. getOrganizationId: mockGetOrganizationId
  53. }))
  54. // Dynamic imports after mocking
  55. const core = await import('@actions/core')
  56. const inputHelper = await import('../src/input-helper.js')
  57. type IGitSourceSettings =
  58. import('../src/git-source-settings.js').IGitSourceSettings
  59. describe('input-helper tests', () => {
  60. beforeAll(() => {
  61. // GitHub workspace
  62. process.env['GITHUB_WORKSPACE'] = gitHubWorkspace
  63. })
  64. beforeEach(() => {
  65. // Reset inputs
  66. inputs = {}
  67. jest.clearAllMocks()
  68. // Re-apply default mocks
  69. ;(core.getInput as jest.Mock<any>).mockImplementation(
  70. (name: string) => inputs[name]
  71. )
  72. mockDirectoryExistsSync.mockImplementation(
  73. (p: string) => p === gitHubWorkspace
  74. )
  75. mockGetOrganizationId.mockResolvedValue(123456)
  76. })
  77. afterAll(() => {
  78. // Restore GitHub workspace
  79. delete process.env['GITHUB_WORKSPACE']
  80. if (originalGitHubWorkspace) {
  81. process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
  82. }
  83. // Restore @actions/github context
  84. mockGithubContext.ref = 'refs/heads/some-ref'
  85. mockGithubContext.sha = '1234567890123456789012345678901234567890'
  86. })
  87. it('sets defaults', async () => {
  88. const settings: IGitSourceSettings = await inputHelper.getInputs()
  89. expect(settings).toBeTruthy()
  90. expect(settings.authToken).toBeFalsy()
  91. expect(settings.clean).toBe(true)
  92. expect(settings.commit).toBeTruthy()
  93. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  94. expect(settings.filter).toBe(undefined)
  95. expect(settings.sparseCheckout).toBe(undefined)
  96. expect(settings.sparseCheckoutConeMode).toBe(true)
  97. expect(settings.fetchDepth).toBe(1)
  98. expect(settings.fetchTags).toBe(false)
  99. expect(settings.showProgress).toBe(true)
  100. expect(settings.lfs).toBe(false)
  101. expect(settings.ref).toBe('refs/heads/some-ref')
  102. expect(settings.repositoryName).toBe('some-repo')
  103. expect(settings.repositoryOwner).toBe('some-owner')
  104. expect(settings.repositoryPath).toBe(gitHubWorkspace)
  105. expect(settings.setSafeDirectory).toBe(true)
  106. expect(settings.allowUnsafePrCheckout).toBe(false)
  107. })
  108. it('qualifies ref', async () => {
  109. let originalRef = mockGithubContext.ref
  110. try {
  111. mockGithubContext.ref = 'some-unqualified-ref'
  112. const settings: IGitSourceSettings = await inputHelper.getInputs()
  113. expect(settings).toBeTruthy()
  114. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  115. expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
  116. } finally {
  117. mockGithubContext.ref = originalRef
  118. }
  119. })
  120. it('requires qualified repo', async () => {
  121. inputs.repository = 'some-unqualified-repo'
  122. try {
  123. await inputHelper.getInputs()
  124. throw 'should not reach here'
  125. } catch (err) {
  126. expect(`(${(err as any).message}`).toMatch(
  127. "Invalid repository 'some-unqualified-repo'"
  128. )
  129. }
  130. })
  131. it('roots path', async () => {
  132. inputs.path = 'some-directory/some-subdirectory'
  133. const settings: IGitSourceSettings = await inputHelper.getInputs()
  134. expect(settings.repositoryPath).toBe(
  135. path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory')
  136. )
  137. })
  138. it('sets ref to empty when explicit sha', async () => {
  139. inputs.ref = '1111111111222222222233333333334444444444'
  140. const settings: IGitSourceSettings = await inputHelper.getInputs()
  141. expect(settings.ref).toBeFalsy()
  142. expect(settings.commit).toBe('1111111111222222222233333333334444444444')
  143. })
  144. it('sets ref to empty when explicit sha-256', async () => {
  145. inputs.ref =
  146. '1111111111222222222233333333334444444444555555555566666666667777'
  147. const settings: IGitSourceSettings = await inputHelper.getInputs()
  148. expect(settings.ref).toBeFalsy()
  149. expect(settings.commit).toBe(
  150. '1111111111222222222233333333334444444444555555555566666666667777'
  151. )
  152. })
  153. it('sets sha to empty when explicit ref', async () => {
  154. inputs.ref = 'refs/heads/some-other-ref'
  155. const settings: IGitSourceSettings = await inputHelper.getInputs()
  156. expect(settings.ref).toBe('refs/heads/some-other-ref')
  157. expect(settings.commit).toBeFalsy()
  158. })
  159. it('sets workflow organization ID', async () => {
  160. const settings: IGitSourceSettings = await inputHelper.getInputs()
  161. expect(settings.workflowOrganizationId).toBe(123456)
  162. })
  163. })