input-helper.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import * as core from '@actions/core'
  2. import * as fsHelper from '../lib/fs-helper'
  3. import * as github from '@actions/github'
  4. import * as inputHelper from '../lib/input-helper'
  5. import * as path from 'path'
  6. import * as workflowContextHelper from '../lib/workflow-context-helper'
  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. // Mock ./workflowContextHelper getOrganizationId()
  39. jest
  40. .spyOn(workflowContextHelper, 'getOrganizationId')
  41. .mockImplementation(() => Promise.resolve(123456))
  42. // GitHub workspace
  43. process.env['GITHUB_WORKSPACE'] = gitHubWorkspace
  44. })
  45. beforeEach(() => {
  46. // Reset inputs
  47. inputs = {}
  48. })
  49. afterAll(() => {
  50. // Restore GitHub workspace
  51. delete process.env['GITHUB_WORKSPACE']
  52. if (originalGitHubWorkspace) {
  53. process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
  54. }
  55. // Restore @actions/github context
  56. github.context.ref = originalContext.ref
  57. github.context.sha = originalContext.sha
  58. // Restore
  59. jest.restoreAllMocks()
  60. })
  61. it('sets defaults', async () => {
  62. const settings: IGitSourceSettings = await inputHelper.getInputs()
  63. expect(settings).toBeTruthy()
  64. expect(settings.authToken).toBeFalsy()
  65. expect(settings.clean).toBe(true)
  66. expect(settings.commit).toBeTruthy()
  67. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  68. expect(settings.fetchDepth).toBe(1)
  69. expect(settings.lfs).toBe(false)
  70. expect(settings.ref).toBe('refs/heads/some-ref')
  71. expect(settings.repositoryName).toBe('some-repo')
  72. expect(settings.repositoryOwner).toBe('some-owner')
  73. expect(settings.repositoryPath).toBe(gitHubWorkspace)
  74. })
  75. it('qualifies ref', async () => {
  76. let originalRef = github.context.ref
  77. try {
  78. github.context.ref = 'some-unqualified-ref'
  79. const settings: IGitSourceSettings = await inputHelper.getInputs()
  80. expect(settings).toBeTruthy()
  81. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  82. expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
  83. } finally {
  84. github.context.ref = originalRef
  85. }
  86. })
  87. it('requires qualified repo', async () => {
  88. inputs.repository = 'some-unqualified-repo'
  89. try {
  90. await inputHelper.getInputs()
  91. throw 'should not reach here'
  92. } catch (err) {
  93. expect(`(${(err as any).message}`).toMatch(
  94. "Invalid repository 'some-unqualified-repo'"
  95. )
  96. }
  97. })
  98. it('roots path', async () => {
  99. inputs.path = 'some-directory/some-subdirectory'
  100. const settings: IGitSourceSettings = await inputHelper.getInputs()
  101. expect(settings.repositoryPath).toBe(
  102. path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory')
  103. )
  104. })
  105. it('sets ref to empty when explicit sha', async () => {
  106. inputs.ref = '1111111111222222222233333333334444444444'
  107. const settings: IGitSourceSettings = await inputHelper.getInputs()
  108. expect(settings.ref).toBeFalsy()
  109. expect(settings.commit).toBe('1111111111222222222233333333334444444444')
  110. })
  111. it('sets sha to empty when explicit ref', async () => {
  112. inputs.ref = 'refs/heads/some-other-ref'
  113. const settings: IGitSourceSettings = await inputHelper.getInputs()
  114. expect(settings.ref).toBe('refs/heads/some-other-ref')
  115. expect(settings.commit).toBeFalsy()
  116. })
  117. it('sets workflow organization ID', async () => {
  118. const settings: IGitSourceSettings = await inputHelper.getInputs()
  119. expect(settings.workflowOrganizationId).toBe(123456)
  120. })
  121. })