Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove need for dummy file in unit tests #4

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ jobs:
aws-access-key-id: ${{secrets.AWS_ACCESS_KEY_ID}}
aws-secret-access-key: ${{secrets.AWS_SECRET_ACCESS_KEY}}
aws-session-token: ${{secrets.AWS_SESSION_TOKEN}}
report-path: ./test/data/d2l-test-report-empty.json
report-path: ./test/data/d2l-test-report.json
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions src/github.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { info, error, startGroup, endGroup } from '@actions/core';
import fs from 'fs';
import fs from 'fs/promises';
import { context as gitHubContext } from '@actions/github';
import { getInput, setFailed } from '@actions/core';
import { resolve } from 'path';

const { accessSync } = fs;

const makeLogger = () => ({ startGroup, endGroup, info, error });

const getContext = (logger) => {
Expand Down Expand Up @@ -74,7 +72,7 @@ const getStringInput = (name) => {
return input;
};

const getInputs = (logger) => {
const getInputs = async(logger) => {
logger.startGroup('Gather GitHub inputs');

const awsAccessKeyId = getStringInput('aws-access-key-id');
Expand All @@ -86,8 +84,8 @@ const getInputs = (logger) => {
const reportPath = resolve(getStringInput('report-path'));

try {
accessSync(reportPath);
} catch {
await fs.access(reportPath);
} catch (err) {
throw new Error('Report path must exists');
}

Expand All @@ -102,4 +100,4 @@ const getInputs = (logger) => {
};
};

export { getContext, getInputs, makeLogger, setFailed };
export { getContext as getContext, getInputs, makeLogger, setFailed };
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getContext, getInputs, makeLogger, setFailed } from './github.js';

try {
getContext(logger);
getInputs(logger);
await getInputs(logger);
} catch (err) {
logger.endGroup();

Expand Down
10 changes: 5 additions & 5 deletions test/.env
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
GITHUB_HEAD_REF=refs/heads/test/test
GITHUB_REF=refs/heads/test/test
GITHUB_REPOSITORY=Test/test
GITHUB_HEAD_REF=refs/heads/test/branch
GITHUB_REF=refs/heads/test/branch
GITHUB_REPOSITORY=TestOrganization/test-repository
GITHUB_RUN_ATTEMPT=1
GITHUB_RUN_ID=12345
GITHUB_SHA=0000000000000000000000000000000000000000
GITHUB_WORKFLOW_REF=Test/test/.github/workflows/test.yml@test/test
GITHUB_WORKFLOW_REF=TestOrganization/test-repository/.github/workflows/test-workflow.yml@test/branch
INPUT_AWS-ACCESS-KEY-ID=aws-access-key-id
INPUT_AWS-SECRET-ACCESS-KEY=aws-secret-access-key
INPUT_AWS-SESSION-TOKEN=aws-session-token
INPUT_REPORT-PATH=./test/data/d2l-test-report-empty.json
INPUT_REPORT-PATH=./test/data/d2l-test-report.json
File renamed without changes.
140 changes: 74 additions & 66 deletions test/github.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getContext, getInputs, makeLogger } from '../src/github.js';
import { expect } from 'chai';
import fs from 'fs/promises';
import { resolve } from 'path';
import { stub } from 'sinon';
import { createSandbox } from 'sinon';

const captureOutput = () => {
let output = '';
Expand All @@ -17,14 +18,20 @@ const captureOutput = () => {
};
};

const makeDummyLogger = () => ({
startGroup: stub(),
endGroup: stub(),
info: stub(),
error: stub()
});

describe('github', () => {
let sandbox;

before(() => sandbox = createSandbox());

afterEach(() => sandbox.restore());

const makeDummyLogger = () => ({
startGroup: sandbox.stub(),
endGroup: sandbox.stub(),
info: sandbox.stub(),
error: sandbox.stub()
});

it('logger logs', () => {
const output = captureOutput();
const logger = makeLogger();
Expand All @@ -50,66 +57,64 @@ describe('github', () => {
});

describe('get context', () => {
describe('succeeds', () => {
const expectedResult = {
githubOrganization: 'Test',
githubRepository: 'test',
githubWorkflow: 'test.yml',
githubRunId: 12345,
githubRunAttempt: 1,
gitBranch: 'test/test',
gitSha: '0000000000000000000000000000000000000000'
};
const expectedInfoLines = [
'GitHub Organization: Test',
'GitHub Repository: test',
'GitHub Workflow: test.yml',
'GitHub RunId: 12345',
'GitHub Run Attempt: 1',
'Git Branch: test/test',
'Git SHA: 0000000000000000000000000000000000000000'
];
let logger;

beforeEach(() => {
logger = makeDummyLogger();
});
const expectedResult = {
githubOrganization: 'TestOrganization',
githubRepository: 'test-repository',
githubWorkflow: 'test-workflow.yml',
githubRunId: 12345,
githubRunAttempt: 1,
gitBranch: 'test/branch',
gitSha: '0000000000000000000000000000000000000000'
};
const expectedInfoLines = [
'GitHub Organization: TestOrganization',
'GitHub Repository: test-repository',
'GitHub Workflow: test-workflow.yml',
'GitHub RunId: 12345',
'GitHub Run Attempt: 1',
'Git Branch: test/branch',
'Git SHA: 0000000000000000000000000000000000000000'
];
let logger;

it('pull request', () => {
const context = getContext(logger);
beforeEach(() => {
logger = makeDummyLogger();
});

expect(expectedResult).to.deep.eq(context);
expect(logger.startGroup.calledOnce).to.be.true;
expect(logger.endGroup.calledOnce).to.be.true;
expect(logger.info.callCount).to.eq(7);
expect(logger.error.notCalled).to.be.true;
expect(logger.startGroup.calledWith('Gather GitHub context')).to.be.true;
it('pull request', () => {
const context = getContext(logger);

for (const i in expectedInfoLines) {
expect(logger.info.calledWith(expectedInfoLines[i])).to.be.true;
}
});
expect(expectedResult).to.deep.eq(context);
expect(logger.startGroup.calledOnce).to.be.true;
expect(logger.endGroup.calledOnce).to.be.true;
expect(logger.info.callCount).to.eq(7);
expect(logger.error.notCalled).to.be.true;
expect(logger.startGroup.calledWith('Gather GitHub context')).to.be.true;

it('branch', () => {
const githubHeadRef = process.env['GITHUB_HEAD_REF'];
for (const i in expectedInfoLines) {
expect(logger.info.calledWith(expectedInfoLines[i])).to.be.true;
}
});

delete process.env['GITHUB_HEAD_REF'];
it('branch', () => {
const githubHeadRef = process.env['GITHUB_HEAD_REF'];

const context = getContext(logger);
delete process.env['GITHUB_HEAD_REF'];

process.env['GITHUB_HEAD_REF'] = githubHeadRef;
const context = getContext(logger);

expect(expectedResult).to.deep.eq(context);
expect(logger.startGroup.calledOnce).to.be.true;
expect(logger.endGroup.calledOnce).to.be.true;
expect(logger.info.callCount).to.eq(7);
expect(logger.error.notCalled).to.be.true;
expect(logger.startGroup.calledWith('Gather GitHub context')).to.be.true;
process.env['GITHUB_HEAD_REF'] = githubHeadRef;

for (const i in expectedInfoLines) {
expect(logger.info.calledWith(expectedInfoLines[i])).to.be.true;
}
});
expect(expectedResult).to.deep.eq(context);
expect(logger.startGroup.calledOnce).to.be.true;
expect(logger.endGroup.calledOnce).to.be.true;
expect(logger.info.callCount).to.eq(7);
expect(logger.error.notCalled).to.be.true;
expect(logger.startGroup.calledWith('Gather GitHub context')).to.be.true;

for (const i in expectedInfoLines) {
expect(logger.info.calledWith(expectedInfoLines[i])).to.be.true;
}
});

describe('fails', () => {
Expand All @@ -134,26 +139,29 @@ describe('github', () => {
});

describe('get inputs', () => {
it('succeeds', () => {
it('succeeds', async() => {
const logger = makeDummyLogger();
const inputs = getInputs(logger);

sandbox.stub(fs, 'access');

const inputs = await getInputs(logger);

expect(inputs.awsAccessKeyId).to.eq('aws-access-key-id');
expect(inputs.awsSecretAccessKey).to.eq('aws-secret-access-key');
expect(inputs.awsSessionToken).to.eq('aws-session-token');
expect(inputs.reportPath).to.eq(resolve('./test/data/d2l-test-report-empty.json'));
expect(inputs.reportPath).to.eq(resolve('./test/data/d2l-test-report.json'));
});

describe('fails', () => {
it('empty input', () => {
it('empty input', async() => {
const inputAwsAccessKeyId = process.env['INPUT_AWS-ACCESS-KEY-ID'];

process.env['INPUT_AWS-ACCESS-KEY-ID'] = ' ';

try {
const logger = makeDummyLogger();

getInputs(logger);
await getInputs(logger);
} catch {
return;
} finally {
Expand All @@ -163,15 +171,15 @@ describe('github', () => {
throw new Error('failed');
});

it('non-existent report path', () => {
it('non-existent report path', async() => {
const inputReportPath = process.env['INPUT_REPORT-PATH'];

process.env['INPUT_REPORT-PATH'] = 'not a file path';

try {
const logger = makeDummyLogger();

getInputs(logger);
await getInputs(logger);
} catch {
return;
} finally {
Expand Down