Skip to content

Commit

Permalink
test: Add testing for set-tokens.ts (#126)
Browse files Browse the repository at this point in the history
* test: remove main
* test: add getPublishRepo()
* test: Add setPersonalToken()
* test: Add setGithubToken()
* test: add showInputs()
* test: .nojekyll already exists
* test: ignore jest/expect-expect
* refactor: squash inputs log
* fix: throw error message
  • Loading branch information
peaceiris committed Mar 6, 2020
1 parent acd0462 commit fd6e5fc
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 106 deletions.
93 changes: 92 additions & 1 deletion __tests__/get-inputs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// import * as main from '../src/main';
import {Inputs} from '../src/interfaces';
import {getInputs} from '../src/get-inputs';
import {showInputs, getInputs} from '../src/get-inputs';
import os from 'os';

beforeEach(() => {
jest.resetModules();
Expand All @@ -25,6 +26,96 @@ afterEach(() => {
delete process.env['INPUT_CNAME'];
});

// 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 setTestInputs(): void {
process.env['INPUT_PUBLISH_BRANCH'] = 'master';
process.env['INPUT_PUBLISH_DIR'] = 'out';
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';
process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3';
process.env['INPUT_DISABLE_NOJEKYLL'] = 'true';
process.env['INPUT_CNAME'] = 'github.com';
}

function getInputsLog(authMethod: string, inps: Inputs): string {
return `\
[INFO] ${authMethod}: true
[INFO] PublishBranch: ${inps.PublishBranch}
[INFO] PublishDir: ${inps.PublishDir}
[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}
[INFO] TagName: ${inps.TagName}
[INFO] TagMessage: ${inps.TagMessage}
[INFO] DisableNoJekyll: ${inps.DisableNoJekyll}
[INFO] CNAME: ${inps.CNAME}
`;
}

describe('showInputs()', () => {
beforeEach(() => {
process.stdout.write = jest.fn();
});

// eslint-disable-next-line jest/expect-expect
test('print all inputs DeployKey', () => {
process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key';
setTestInputs();

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', () => {
process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token';
setTestInputs();

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', () => {
process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token';
setTestInputs();

const inps: Inputs = getInputs();
showInputs(inps);

const authMethod = 'PersonalToken';
const test = getInputsLog(authMethod, inps);
assertWriteCalls([`${test}${os.EOL}`]);
});
});

describe('getInputs()', () => {
test('get default inputs', () => {
process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key';
Expand Down
32 changes: 0 additions & 32 deletions __tests__/main.test.ts

This file was deleted.

105 changes: 105 additions & 0 deletions __tests__/set-tokens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
getPublishRepo,
setPersonalToken,
setGithubToken
} from '../src/set-tokens';

beforeEach(() => {
jest.resetModules();
});

// afterEach(() => {

// });

describe('getPublishRepo()', () => {
test('return repository name', () => {
const test = getPublishRepo('', 'owner', 'repo');
expect(test).toMatch('owner/repo');
});

test('return external repository name', () => {
const test = getPublishRepo('extOwner/extRepo', 'owner', 'repo');
expect(test).toMatch('extOwner/extRepo');
});
});

describe('setGithubToken()', () => {
test('return remote url with GITHUB_TOKEN gh-pages', () => {
const expected =
'https://x-access-token:GITHUB_TOKEN@github.com/owner/repo.git';
const test = setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages',
'',
'refs/heads/master',
'push'
);
expect(test).toMatch(expected);
});

test('return remote url with GITHUB_TOKEN master', () => {
const expected =
'https://x-access-token:GITHUB_TOKEN@github.com/owner/repo.git';
const test = setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'master',
'',
'refs/heads/source',
'push'
);
expect(test).toMatch(expected);
});

test('throw error master to master', () => {
expect(() => {
setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'master',
'',
'refs/heads/master',
'push'
);
}).toThrowError('You deploy from master to master');
});

test('throw error external repository with GITHUB_TOKEN', () => {
expect(() => {
setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages',
'extOwner/extRepo',
'refs/heads/master',
'push'
);
}).toThrowError(
'GITHUB_TOKEN does not support to push to an external repository'
);
});

test('return remote url with GITHUB_TOKEN pull_request', () => {
const expected =
'https://x-access-token:GITHUB_TOKEN@github.com/owner/repo.git';
const test = setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages',
'',
'refs/pull/29/merge',
'pull_request'
);
expect(test).toMatch(expected);
});
});

describe('setPersonalToken()', () => {
test('return remote url with personal access token', () => {
const expected = 'https://x-access-token:pat@github.com/owner/repo.git';
const test = setPersonalToken('pat', 'owner/repo');
expect(test).toMatch(expected);
});
});
51 changes: 33 additions & 18 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ describe('addNoJekyll()', () => {
const filepath = path.join(workDir, '.nojekyll');

await addNoJekyll(workDir, false, 'gh-pages');
const test1 = fs.existsSync(filepath);
expect(test1).toBe(true);
const test = fs.existsSync(filepath);
expect(test).toBe(true);

fs.unlinkSync(filepath);
});
Expand All @@ -91,8 +91,23 @@ describe('addNoJekyll()', () => {
const filepath = path.join(workDir, '.nojekyll');

await addNoJekyll(workDir, false, 'master');
const test2 = fs.existsSync(filepath);
expect(test2).toBe(true);
const test = fs.existsSync(filepath);
expect(test).toBe(true);

fs.unlinkSync(filepath);
});

test('.nojekyll already exists', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, '.nojekyll');
fs.closeSync(fs.openSync(filepath, 'w'));

await addNoJekyll(workDir, false, 'master');
const test = fs.existsSync(filepath);
expect(test).toBe(true);

fs.unlinkSync(filepath);
});
Expand All @@ -105,8 +120,8 @@ describe('addNoJekyll()', () => {
const filepath = path.join(workDir, '.nojekyll');

await addNoJekyll(workDir, true, 'gh-pages');
const test3 = fs.existsSync(filepath);
expect(test3).toBe(false);
const test = fs.existsSync(filepath);
expect(test).toBe(false);
});

test('not add .nojekyll disable_nojekyll master', async () => {
Expand All @@ -117,8 +132,8 @@ describe('addNoJekyll()', () => {
const filepath = path.join(workDir, '.nojekyll');

await addNoJekyll(workDir, true, 'master');
const test4 = fs.existsSync(filepath);
expect(test4).toBe(false);
const test = fs.existsSync(filepath);
expect(test).toBe(false);
});

test('not add .nojekyll other-branch', async () => {
Expand All @@ -129,8 +144,8 @@ describe('addNoJekyll()', () => {
const filepath = path.join(workDir, '.nojekyll');

await addNoJekyll(workDir, false, 'other-branch');
const test5 = fs.existsSync(filepath);
expect(test5).toBe(false);
const test = fs.existsSync(filepath);
expect(test).toBe(false);
});

test('not add .nojekyll disable_nojekyll other-branch', async () => {
Expand All @@ -141,8 +156,8 @@ describe('addNoJekyll()', () => {
const filepath = path.join(workDir, '.nojekyll');

await addNoJekyll(workDir, true, 'other-branch');
const test6 = fs.existsSync(filepath);
expect(test6).toBe(false);
const test = fs.existsSync(filepath);
expect(test).toBe(false);
});
});

Expand All @@ -155,8 +170,8 @@ describe('addCNAME()', () => {
const filepath = path.join(workDir, 'CNAME');

await addCNAME(workDir, 'github.com');
const test1 = fs.readFileSync(filepath, 'utf8');
expect(test1).toMatch('github.com');
const test = fs.readFileSync(filepath, 'utf8');
expect(test).toMatch('github.com');

fs.unlinkSync(filepath);
});
Expand All @@ -169,8 +184,8 @@ describe('addCNAME()', () => {
const filepath = path.join(workDir, 'CNAME');

await addCNAME(workDir, '');
const test2 = fs.existsSync(filepath);
expect(test2).toBe(false);
const test = fs.existsSync(filepath);
expect(test).toBe(false);
});

test('CNAME already exists', async () => {
Expand All @@ -182,8 +197,8 @@ describe('addCNAME()', () => {

await addCNAME(workDir, 'github.io');
await addCNAME(workDir, 'github.com');
const test3 = fs.readFileSync(filepath, 'utf8');
expect(test3).toMatch('github.io');
const test = fs.readFileSync(filepath, 'utf8');
expect(test).toMatch('github.io');

fs.unlinkSync(filepath);
});
Expand Down
Loading

0 comments on commit fd6e5fc

Please sign in to comment.