get-inputs.test.ts
1 // import * as main from '../src/main'; 2 import {Inputs} from '../src/interfaces'; 3 import {showInputs, getInputs} from '../src/get-inputs'; 4 import os from 'os'; 5 import fs from 'fs'; 6 import yaml from 'js-yaml'; 7 8 beforeEach(() => { 9 jest.resetModules(); 10 process.stdout.write = jest.fn(); 11 12 // eslint-disable-next-line @typescript-eslint/no-explicit-any 13 const doc: any = yaml.load(fs.readFileSync(__dirname + '/../action.yml', 'utf8')); 14 Object.keys(doc.inputs).forEach(name => { 15 const envVar = `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; 16 process.env[envVar] = doc.inputs[name]['default']; 17 }); 18 }); 19 20 afterEach(() => { 21 // eslint-disable-next-line @typescript-eslint/no-explicit-any 22 const doc: any = yaml.load(fs.readFileSync(__dirname + '/../action.yml', 'utf8')); 23 Object.keys(doc.inputs).forEach(name => { 24 const envVar = `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; 25 console.debug(`delete ${envVar}\t${process.env[envVar]}`); 26 delete process.env[envVar]; 27 }); 28 }); 29 30 // Assert that process.stdout.write calls called only with the given arguments. 31 // cf. https://github.com/actions/toolkit/blob/8b0300129f08728419263b016de8630f1d426d5f/packages/core/__tests__/core.test.ts 32 function assertWriteCalls(calls: string[]): void { 33 expect(process.stdout.write).toHaveBeenCalledTimes(calls.length); 34 35 for (let i = 0; i < calls.length; i++) { 36 expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i]); 37 } 38 } 39 40 function getInputsLog(authMethod: string, inps: Inputs): string { 41 return `\ 42 [INFO] ${authMethod}: true 43 [INFO] PublishBranch: ${inps.PublishBranch} 44 [INFO] PublishDir: ${inps.PublishDir} 45 [INFO] DestinationDir: ${inps.DestinationDir} 46 [INFO] ExternalRepository: ${inps.ExternalRepository} 47 [INFO] AllowEmptyCommit: ${inps.AllowEmptyCommit} 48 [INFO] KeepFiles: ${inps.KeepFiles} 49 [INFO] ForceOrphan: ${inps.ForceOrphan} 50 [INFO] UserName: ${inps.UserName} 51 [INFO] UserEmail: ${inps.UserEmail} 52 [INFO] CommitMessage: ${inps.CommitMessage} 53 [INFO] FullCommitMessage: ${inps.FullCommitMessage} 54 [INFO] TagName: ${inps.TagName} 55 [INFO] TagMessage: ${inps.TagMessage} 56 [INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll} 57 [INFO] CNAME: ${inps.CNAME} 58 [INFO] ExcludeAssets ${inps.ExcludeAssets} 59 `; 60 } 61 62 describe('showInputs()', () => { 63 // eslint-disable-next-line jest/expect-expect 64 test('print all inputs DeployKey', () => { 65 process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key'; 66 67 const inps: Inputs = getInputs(); 68 showInputs(inps); 69 70 const authMethod = 'DeployKey'; 71 const test = getInputsLog(authMethod, inps); 72 assertWriteCalls([`${test}${os.EOL}`]); 73 }); 74 75 // eslint-disable-next-line jest/expect-expect 76 test('print all inputs GithubToken', () => { 77 delete process.env['INPUT_DEPLOY_KEY']; 78 process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token'; 79 80 const inps: Inputs = getInputs(); 81 showInputs(inps); 82 83 const authMethod = 'GithubToken'; 84 const test = getInputsLog(authMethod, inps); 85 assertWriteCalls([`${test}${os.EOL}`]); 86 }); 87 88 // eslint-disable-next-line jest/expect-expect 89 test('print all inputs PersonalToken', () => { 90 delete process.env['INPUT_DEPLOY_KEY']; 91 delete process.env['INPUT_GITHUB_TOKEN']; 92 process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token'; 93 94 const inps: Inputs = getInputs(); 95 showInputs(inps); 96 97 const authMethod = 'PersonalToken'; 98 const test = getInputsLog(authMethod, inps); 99 assertWriteCalls([`${test}${os.EOL}`]); 100 }); 101 }); 102 103 describe('getInputs()', () => { 104 test('get default inputs', () => { 105 process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key'; 106 107 const inps: Inputs = getInputs(); 108 109 expect(inps.DeployKey).toMatch('test_deploy_key'); 110 expect(inps.GithubToken).toMatch(''); 111 expect(inps.PersonalToken).toMatch(''); 112 expect(inps.PublishBranch).toMatch('gh-pages'); 113 expect(inps.PublishDir).toMatch('public'); 114 expect(inps.DestinationDir).toMatch(''); 115 expect(inps.ExternalRepository).toMatch(''); 116 expect(inps.AllowEmptyCommit).toBe(false); 117 expect(inps.KeepFiles).toBe(false); 118 expect(inps.ForceOrphan).toBe(false); 119 expect(inps.UserName).toMatch(''); 120 expect(inps.UserEmail).toMatch(''); 121 expect(inps.CommitMessage).toMatch(''); 122 expect(inps.FullCommitMessage).toMatch(''); 123 expect(inps.TagName).toMatch(''); 124 expect(inps.TagMessage).toMatch(''); 125 expect(inps.DisableNoJekyll).toBe(false); 126 expect(inps.CNAME).toMatch(''); 127 expect(inps.ExcludeAssets).toMatch('.github'); 128 }); 129 130 test('get spec inputs', () => { 131 // process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key'; 132 process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token'; 133 process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token'; 134 process.env['INPUT_PUBLISH_BRANCH'] = 'master'; 135 process.env['INPUT_PUBLISH_DIR'] = 'out'; 136 process.env['INPUT_DESTINATION_DIR'] = 'subdir'; 137 process.env['INPUT_EXTERNAL_REPOSITORY'] = 'user/repo'; 138 process.env['INPUT_ALLOW_EMPTY_COMMIT'] = 'true'; 139 process.env['INPUT_KEEP_FILES'] = 'true'; 140 process.env['INPUT_FORCE_ORPHAN'] = 'true'; 141 process.env['INPUT_USER_NAME'] = 'username'; 142 process.env['INPUT_USER_EMAIL'] = 'github@github.com'; 143 process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature'; 144 process.env['INPUT_FULL_COMMIT_MESSAGE'] = 'feat: Add new feature'; 145 process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3'; 146 process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3'; 147 process.env['INPUT_DISABLE_NOJEKYLL'] = 'true'; 148 process.env['INPUT_CNAME'] = 'github.com'; 149 process.env['INPUT_EXCLUDE_ASSETS'] = '.github'; 150 151 const inps: Inputs = getInputs(); 152 153 expect(inps.DeployKey).toMatch(''); 154 expect(inps.GithubToken).toMatch('test_github_token'); 155 expect(inps.PersonalToken).toMatch('test_personal_token'); 156 expect(inps.PublishBranch).toMatch('master'); 157 expect(inps.PublishDir).toMatch('out'); 158 expect(inps.DestinationDir).toMatch('subdir'); 159 expect(inps.ExternalRepository).toMatch('user/repo'); 160 expect(inps.AllowEmptyCommit).toBe(true); 161 expect(inps.KeepFiles).toBe(true); 162 expect(inps.ForceOrphan).toBe(true); 163 expect(inps.UserName).toMatch('username'); 164 expect(inps.UserEmail).toMatch('github@github.com'); 165 expect(inps.CommitMessage).toMatch('feat: Add new feature'); 166 expect(inps.FullCommitMessage).toMatch('feat: Add new feature'); 167 expect(inps.TagName).toMatch('deploy-v1.2.3'); 168 expect(inps.TagMessage).toMatch('Deployment v1.2.3'); 169 expect(inps.DisableNoJekyll).toBe(true); 170 expect(inps.CNAME).toMatch('github.com'); 171 expect(inps.ExcludeAssets).toMatch('.github'); 172 }); 173 174 test('get spec inputs enable_jekyll', () => { 175 process.env['INPUT_ENABLE_JEKYLL'] = 'true'; 176 const inps: Inputs = getInputs(); 177 expect(inps.DisableNoJekyll).toBe(true); 178 }); 179 180 test('throw error enable_jekyll or disable_nojekyll', () => { 181 process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key'; 182 process.env['INPUT_ENABLE_JEKYLL'] = 'true'; 183 process.env['INPUT_DISABLE_NOJEKYLL'] = 'true'; 184 185 expect(() => { 186 getInputs(); 187 }).toThrowError('Use either of enable_jekyll or disable_nojekyll'); 188 }); 189 });