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