input-helper.test.ts 4.3 KB

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