Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Added tests for `validateGithubActions` workflow

See: #13604
  • Loading branch information
radoslawkrzemien committed Jun 30, 2023
1 parent f725614 commit 2fa6af3
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/validateGithubActions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ jobs:
runs-on: ubuntu-latest
steps:
# This action checks-out the repository, so the workflow can access it.
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
- name: Checkout
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
with:
fetch-depth: 0

- uses: Expensify/App/.github/actions/composite/setupNode@main
- name: Setup Node
uses: Expensify/App/.github/actions/composite/setupNode@main

# Rebuild all the actions on this branch and check for a diff. Fail if there is one,
# because that would be a sign that the PR author did not rebuild the Github Actions
Expand Down
54 changes: 54 additions & 0 deletions workflow_tests/assertions/validateGithubActionsAssertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const utils = require('../utils/utils');

const assertVerifyJobExecuted = (workflowResult, didExecute = true) => {
const steps = [
utils.getStepAssertion(
'Checkout',
true,
null,
'VERIFY',
'Checkout',
[{key: 'fetch-depth', value: '0'}],
[],
),
utils.getStepAssertion(
'Setup Node',
true,
null,
'VERIFY',
'Setup Node',
[],
[],
),
utils.getStepAssertion(
'Verify Javascript Action Builds',
true,
null,
'VERIFY',
'Verify Javascript Action Builds',
[],
[],
),
utils.getStepAssertion(
'Validate actions and workflows',
true,
null,
'VERIFY',
'Validate actions and workflows',
[],
[],
),
];

for (const expectedStep of steps) {
if (didExecute) {
expect(workflowResult).toEqual(expect.arrayContaining([expectedStep]));
} else {
expect(workflowResult).not.toEqual(expect.arrayContaining([expectedStep]));
}
}
};

module.exports = {
assertVerifyJobExecuted,
};
41 changes: 41 additions & 0 deletions workflow_tests/mocks/validateGithubActionsMocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const utils = require('../utils/utils');

// verify
const VALIDATEGITHUBACTIONS__VERIFY__CHECKOUT__STEP_MOCK = utils.getMockStep(
'Checkout',
'Checkout',
'VERIFY',
['fetch-depth'],
[],
);
const VALIDATEGITHUBACTIONS__VERIFY__SETUP_NODE__STEP_MOCK = utils.getMockStep(
'Setup Node',
'Setup Node',
'VERIFY',
[],
[],
);
const VALIDATEGITHUBACTIONS__VERIFY__VERIFY_JAVASCRIPT_ACTION_BUILDS__STEP_MOCK = utils.getMockStep(
'Verify Javascript Action Builds',
'Verify Javascript Action Builds',
'VERIFY',
[],
[],
);
const VALIDATEGITHUBACTIONS__VERIFY__VALIDATE_ACTIONS_AND_WORKFLOWS__STEP_MOCK = utils.getMockStep(
'Validate actions and workflows',
'Validate actions and workflows',
'VERIFY',
[],
[],
);
const VALIDATEGITHUBACTIONS__VERIFY__STEP_MOCKS = [
VALIDATEGITHUBACTIONS__VERIFY__CHECKOUT__STEP_MOCK,
VALIDATEGITHUBACTIONS__VERIFY__SETUP_NODE__STEP_MOCK,
VALIDATEGITHUBACTIONS__VERIFY__VERIFY_JAVASCRIPT_ACTION_BUILDS__STEP_MOCK,
VALIDATEGITHUBACTIONS__VERIFY__VALIDATE_ACTIONS_AND_WORKFLOWS__STEP_MOCK,
];

module.exports = {
VALIDATEGITHUBACTIONS__VERIFY__STEP_MOCKS,
};
106 changes: 106 additions & 0 deletions workflow_tests/validateGithubActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const path = require('path');
const kieMockGithub = require('@kie/mock-github');
const utils = require('./utils/utils');
const assertions = require('./assertions/validateGithubActionsAssertions');
const mocks = require('./mocks/validateGithubActionsMocks');
const eAct = require('./utils/ExtendedAct');

jest.setTimeout(60 * 1000);
let mockGithub;
const FILES_TO_COPY_INTO_TEST_REPO = [
{
src: path.resolve(__dirname, '..', '.github', 'actions'),
dest: '.github/actions',
},
{
src: path.resolve(__dirname, '..', '.github', 'libs'),
dest: '.github/libs',
},
{
src: path.resolve(__dirname, '..', '.github', 'scripts'),
dest: '.github/scripts',
},
{
src: path.resolve(__dirname, '..', '.github', 'workflows', 'validateGithubActions.yml'),
dest: '.github/workflows/validateGithubActions.yml',
},
];

describe('test workflow validateGithubActions', () => {
const githubToken = 'dummy_github_token';
const actor = 'Dummy Actor';
beforeEach(async () => {
// create a local repository and copy required files
mockGithub = new kieMockGithub.MockGithub({
repo: {
testValidateGithubActionsWorkflowRepo: {
files: FILES_TO_COPY_INTO_TEST_REPO,
},
},
});

await mockGithub.setup();
});

afterEach(async () => {
await mockGithub.teardown();
});
describe('pull request opened', () => {
const event = 'pull_request';
const eventOptions = {
action: 'opened',
};
it('executes verification', async () => {
const repoPath = mockGithub.repo.getPath('testValidateGithubActionsWorkflowRepo') || '';
const workflowPath = path.join(repoPath, '.github', 'workflows', 'validateGithubActions.yml');
let act = new eAct.ExtendedAct(repoPath, workflowPath);
act = utils.setUpActParams(
act,
event,
eventOptions,
{},
githubToken,
);
const testMockSteps = {
verify: mocks.VALIDATEGITHUBACTIONS__VERIFY__STEP_MOCKS,
};
const result = await act
.runEvent(event, {
workflowFile: path.join(repoPath, '.github', 'workflows'),
mockSteps: testMockSteps,
actor,
});

assertions.assertVerifyJobExecuted(result);
});
});
describe('pull request synchronized', () => {
const event = 'pull_request';
const eventOptions = {
action: 'synchronize',
};
it('executes verification', async () => {
const repoPath = mockGithub.repo.getPath('testValidateGithubActionsWorkflowRepo') || '';
const workflowPath = path.join(repoPath, '.github', 'workflows', 'validateGithubActions.yml');
let act = new eAct.ExtendedAct(repoPath, workflowPath);
act = utils.setUpActParams(
act,
event,
eventOptions,
{},
githubToken,
);
const testMockSteps = {
verify: mocks.VALIDATEGITHUBACTIONS__VERIFY__STEP_MOCKS,
};
const result = await act
.runEvent(event, {
workflowFile: path.join(repoPath, '.github', 'workflows'),
mockSteps: testMockSteps,
actor,
});

assertions.assertVerifyJobExecuted(result);
});
});
});

0 comments on commit 2fa6af3

Please sign in to comment.