2020-02-05 05:34:19 +00:00
|
|
|
// import * as main from '../src/main';
|
|
|
|
import {Inputs} from '../src/interfaces';
|
2020-03-06 15:41:30 +00:00
|
|
|
import {showInputs, getInputs} from '../src/get-inputs';
|
|
|
|
import os from 'os';
|
2020-03-06 20:30:51 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import yaml from 'js-yaml';
|
2020-02-05 05:34:19 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetModules();
|
2020-03-06 20:30:51 +00:00
|
|
|
process.stdout.write = jest.fn();
|
|
|
|
|
2020-06-21 20:40:26 +00:00
|
|
|
const doc = yaml.safeLoad(fs.readFileSync(__dirname + '/../action.yml', 'utf8'));
|
2020-03-06 20:30:51 +00:00
|
|
|
Object.keys(doc.inputs).forEach(name => {
|
|
|
|
const envVar = `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
|
|
|
process.env[envVar] = doc.inputs[name]['default'];
|
|
|
|
});
|
2020-02-05 05:34:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-06-21 20:40:26 +00:00
|
|
|
const doc = yaml.safeLoad(fs.readFileSync(__dirname + '/../action.yml', 'utf8'));
|
2020-03-06 20:30:51 +00:00
|
|
|
Object.keys(doc.inputs).forEach(name => {
|
|
|
|
const envVar = `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
|
|
|
console.debug(`delete ${envVar}\t${process.env[envVar]}`);
|
|
|
|
delete process.env[envVar];
|
|
|
|
});
|
2020-02-05 05:34:19 +00:00
|
|
|
});
|
|
|
|
|
2020-03-06 15:41:30 +00:00
|
|
|
// Assert that process.stdout.write calls called only with the given arguments.
|
|
|
|
// cf. https://github.com/actions/toolkit/blob/8b0300129f08728419263b016de8630f1d426d5f/packages/core/__tests__/core.test.ts
|
|
|
|
function assertWriteCalls(calls: string[]): void {
|
|
|
|
expect(process.stdout.write).toHaveBeenCalledTimes(calls.length);
|
|
|
|
|
|
|
|
for (let i = 0; i < calls.length; i++) {
|
|
|
|
expect(process.stdout.write).toHaveBeenNthCalledWith(i + 1, calls[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInputsLog(authMethod: string, inps: Inputs): string {
|
|
|
|
return `\
|
|
|
|
[INFO] ${authMethod}: true
|
|
|
|
[INFO] PublishBranch: ${inps.PublishBranch}
|
|
|
|
[INFO] PublishDir: ${inps.PublishDir}
|
2020-07-21 02:15:53 +00:00
|
|
|
[INFO] DestinationDir: ${inps.DestinationDir}
|
2020-03-06 15:41:30 +00:00
|
|
|
[INFO] ExternalRepository: ${inps.ExternalRepository}
|
|
|
|
[INFO] AllowEmptyCommit: ${inps.AllowEmptyCommit}
|
|
|
|
[INFO] KeepFiles: ${inps.KeepFiles}
|
|
|
|
[INFO] ForceOrphan: ${inps.ForceOrphan}
|
|
|
|
[INFO] UserName: ${inps.UserName}
|
|
|
|
[INFO] UserEmail: ${inps.UserEmail}
|
|
|
|
[INFO] CommitMessage: ${inps.CommitMessage}
|
2020-05-04 00:50:38 +00:00
|
|
|
[INFO] FullCommitMessage: ${inps.FullCommitMessage}
|
2020-03-06 15:41:30 +00:00
|
|
|
[INFO] TagName: ${inps.TagName}
|
|
|
|
[INFO] TagMessage: ${inps.TagMessage}
|
2020-03-06 20:30:51 +00:00
|
|
|
[INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll}
|
2020-03-06 15:41:30 +00:00
|
|
|
[INFO] CNAME: ${inps.CNAME}
|
2020-07-25 11:52:35 +00:00
|
|
|
[INFO] ExcludeAssets ${inps.ExcludeAssets}
|
2020-03-06 15:41:30 +00:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('showInputs()', () => {
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
|
|
test('print all inputs DeployKey', () => {
|
|
|
|
process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key';
|
|
|
|
|
|
|
|
const inps: Inputs = getInputs();
|
|
|
|
showInputs(inps);
|
|
|
|
|
|
|
|
const authMethod = 'DeployKey';
|
|
|
|
const test = getInputsLog(authMethod, inps);
|
|
|
|
assertWriteCalls([`${test}${os.EOL}`]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
|
|
test('print all inputs GithubToken', () => {
|
2020-03-06 20:30:51 +00:00
|
|
|
delete process.env['INPUT_DEPLOY_KEY'];
|
2020-03-06 15:41:30 +00:00
|
|
|
process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token';
|
|
|
|
|
|
|
|
const inps: Inputs = getInputs();
|
|
|
|
showInputs(inps);
|
|
|
|
|
|
|
|
const authMethod = 'GithubToken';
|
|
|
|
const test = getInputsLog(authMethod, inps);
|
|
|
|
assertWriteCalls([`${test}${os.EOL}`]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
|
|
test('print all inputs PersonalToken', () => {
|
2020-03-06 20:30:51 +00:00
|
|
|
delete process.env['INPUT_DEPLOY_KEY'];
|
|
|
|
delete process.env['INPUT_GITHUB_TOKEN'];
|
2020-03-06 15:41:30 +00:00
|
|
|
process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token';
|
|
|
|
|
|
|
|
const inps: Inputs = getInputs();
|
|
|
|
showInputs(inps);
|
|
|
|
|
|
|
|
const authMethod = 'PersonalToken';
|
|
|
|
const test = getInputsLog(authMethod, inps);
|
|
|
|
assertWriteCalls([`${test}${os.EOL}`]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-05 05:34:19 +00:00
|
|
|
describe('getInputs()', () => {
|
|
|
|
test('get default inputs', () => {
|
|
|
|
process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key';
|
|
|
|
|
|
|
|
const inps: Inputs = getInputs();
|
|
|
|
|
|
|
|
expect(inps.DeployKey).toMatch('test_deploy_key');
|
|
|
|
expect(inps.GithubToken).toMatch('');
|
|
|
|
expect(inps.PersonalToken).toMatch('');
|
|
|
|
expect(inps.PublishBranch).toMatch('gh-pages');
|
|
|
|
expect(inps.PublishDir).toMatch('public');
|
2020-07-21 02:15:53 +00:00
|
|
|
expect(inps.DestinationDir).toMatch('');
|
2020-02-05 05:34:19 +00:00
|
|
|
expect(inps.ExternalRepository).toMatch('');
|
|
|
|
expect(inps.AllowEmptyCommit).toBe(false);
|
|
|
|
expect(inps.KeepFiles).toBe(false);
|
|
|
|
expect(inps.ForceOrphan).toBe(false);
|
|
|
|
expect(inps.UserName).toMatch('');
|
|
|
|
expect(inps.UserEmail).toMatch('');
|
|
|
|
expect(inps.CommitMessage).toMatch('');
|
2020-05-04 00:50:38 +00:00
|
|
|
expect(inps.FullCommitMessage).toMatch('');
|
2020-02-05 05:34:19 +00:00
|
|
|
expect(inps.TagName).toMatch('');
|
|
|
|
expect(inps.TagMessage).toMatch('');
|
2020-02-24 09:49:55 +00:00
|
|
|
expect(inps.DisableNoJekyll).toBe(false);
|
|
|
|
expect(inps.CNAME).toMatch('');
|
2020-07-25 11:52:35 +00:00
|
|
|
expect(inps.ExcludeAssets).toMatch('.github');
|
2020-02-05 05:34:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('get spec inputs', () => {
|
|
|
|
// process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key';
|
|
|
|
process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token';
|
|
|
|
process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token';
|
|
|
|
process.env['INPUT_PUBLISH_BRANCH'] = 'master';
|
|
|
|
process.env['INPUT_PUBLISH_DIR'] = 'out';
|
2020-07-21 02:15:53 +00:00
|
|
|
process.env['INPUT_DESTINATION_DIR'] = 'subdir';
|
2020-02-05 05:34:19 +00:00
|
|
|
process.env['INPUT_EXTERNAL_REPOSITORY'] = 'user/repo';
|
|
|
|
process.env['INPUT_ALLOW_EMPTY_COMMIT'] = 'true';
|
|
|
|
process.env['INPUT_KEEP_FILES'] = 'true';
|
|
|
|
process.env['INPUT_FORCE_ORPHAN'] = 'true';
|
|
|
|
process.env['INPUT_USER_NAME'] = 'username';
|
|
|
|
process.env['INPUT_USER_EMAIL'] = 'github@github.com';
|
|
|
|
process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature';
|
2020-05-04 00:50:38 +00:00
|
|
|
process.env['INPUT_FULL_COMMIT_MESSAGE'] = 'feat: Add new feature';
|
2020-02-05 05:34:19 +00:00
|
|
|
process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
|
|
|
|
process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3';
|
2020-02-24 09:49:55 +00:00
|
|
|
process.env['INPUT_DISABLE_NOJEKYLL'] = 'true';
|
|
|
|
process.env['INPUT_CNAME'] = 'github.com';
|
2020-07-25 11:52:35 +00:00
|
|
|
process.env['INPUT_EXCLUDE_ASSETS'] = '.github';
|
2020-02-05 05:34:19 +00:00
|
|
|
|
|
|
|
const inps: Inputs = getInputs();
|
|
|
|
|
|
|
|
expect(inps.DeployKey).toMatch('');
|
|
|
|
expect(inps.GithubToken).toMatch('test_github_token');
|
|
|
|
expect(inps.PersonalToken).toMatch('test_personal_token');
|
|
|
|
expect(inps.PublishBranch).toMatch('master');
|
|
|
|
expect(inps.PublishDir).toMatch('out');
|
2020-07-21 02:15:53 +00:00
|
|
|
expect(inps.DestinationDir).toMatch('subdir');
|
2020-02-05 05:34:19 +00:00
|
|
|
expect(inps.ExternalRepository).toMatch('user/repo');
|
|
|
|
expect(inps.AllowEmptyCommit).toBe(true);
|
|
|
|
expect(inps.KeepFiles).toBe(true);
|
|
|
|
expect(inps.ForceOrphan).toBe(true);
|
|
|
|
expect(inps.UserName).toMatch('username');
|
|
|
|
expect(inps.UserEmail).toMatch('github@github.com');
|
|
|
|
expect(inps.CommitMessage).toMatch('feat: Add new feature');
|
2020-05-04 00:50:38 +00:00
|
|
|
expect(inps.FullCommitMessage).toMatch('feat: Add new feature');
|
2020-02-05 05:34:19 +00:00
|
|
|
expect(inps.TagName).toMatch('deploy-v1.2.3');
|
|
|
|
expect(inps.TagMessage).toMatch('Deployment v1.2.3');
|
2020-02-24 09:49:55 +00:00
|
|
|
expect(inps.DisableNoJekyll).toBe(true);
|
|
|
|
expect(inps.CNAME).toMatch('github.com');
|
2020-07-25 11:52:35 +00:00
|
|
|
expect(inps.ExcludeAssets).toMatch('.github');
|
2020-02-05 05:34:19 +00:00
|
|
|
});
|
2020-03-06 20:30:51 +00:00
|
|
|
|
2020-05-04 00:50:38 +00:00
|
|
|
test('get spec inputs enable_jekyll', () => {
|
|
|
|
process.env['INPUT_ENABLE_JEKYLL'] = 'true';
|
|
|
|
const inps: Inputs = getInputs();
|
|
|
|
expect(inps.DisableNoJekyll).toBe(true);
|
|
|
|
});
|
|
|
|
|
2020-03-06 20:30:51 +00:00
|
|
|
test('throw error enable_jekyll or disable_nojekyll', () => {
|
|
|
|
process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key';
|
|
|
|
process.env['INPUT_ENABLE_JEKYLL'] = 'true';
|
|
|
|
process.env['INPUT_DISABLE_NOJEKYLL'] = 'true';
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
getInputs();
|
|
|
|
}).toThrowError('Use either of enable_jekyll or disable_nojekyll');
|
|
|
|
});
|
2020-02-05 05:34:19 +00:00
|
|
|
});
|