From bec56b0da5a6be9e8606c21f35f655dedbb2065d Mon Sep 17 00:00:00 2001 From: Radoslaw Krzemien Date: Mon, 27 Mar 2023 12:44:34 +0200 Subject: [PATCH] Add tests Added tests for `verifyPodfile` workflow See: https://github.com/Expensify/App/issues/13604 --- .github/workflows/verifyPodfile.yml | 9 +- .../assertions/verifyPodfileAssertions.js | 45 +++++ workflow_tests/mocks/verifyPodfileMocks.js | 33 ++++ workflow_tests/verifyPodfile.test.js | 177 ++++++++++++++++++ 4 files changed, 261 insertions(+), 3 deletions(-) create mode 100644 workflow_tests/assertions/verifyPodfileAssertions.js create mode 100644 workflow_tests/mocks/verifyPodfileMocks.js create mode 100644 workflow_tests/verifyPodfile.test.js diff --git a/.github/workflows/verifyPodfile.yml b/.github/workflows/verifyPodfile.yml index 892b7c03c2de..557df1d94867 100644 --- a/.github/workflows/verifyPodfile.yml +++ b/.github/workflows/verifyPodfile.yml @@ -15,10 +15,13 @@ jobs: runs-on: macos-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 - - run: ./.github/scripts/verifyPodfile.sh + - name: Verify podfile + run: ./.github/scripts/verifyPodfile.sh diff --git a/workflow_tests/assertions/verifyPodfileAssertions.js b/workflow_tests/assertions/verifyPodfileAssertions.js new file mode 100644 index 000000000000..499359d488b3 --- /dev/null +++ b/workflow_tests/assertions/verifyPodfileAssertions.js @@ -0,0 +1,45 @@ +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 podfile', + true, + null, + 'VERIFY', + 'Verify podfile', + [], + [], + ), + ]; + + for (const expectedStep of steps) { + if (didExecute) { + expect(workflowResult).toEqual(expect.arrayContaining([expectedStep])); + } else { + expect(workflowResult).not.toEqual(expect.arrayContaining([expectedStep])); + } + } +}; + +module.exports = { + assertVerifyJobExecuted, +}; diff --git a/workflow_tests/mocks/verifyPodfileMocks.js b/workflow_tests/mocks/verifyPodfileMocks.js new file mode 100644 index 000000000000..c44f51b7b784 --- /dev/null +++ b/workflow_tests/mocks/verifyPodfileMocks.js @@ -0,0 +1,33 @@ +const utils = require('../utils/utils'); + +// verify +const VERIFYPODFILE__VERIFY__CHECKOUT__STEP_MOCK = utils.getMockStep( + 'Checkout', + 'Checkout', + 'VERIFY', + ['fetch-depth'], + [], +); +const VERIFYPODFILE__VERIFY__SETUP_NODE__STEP_MOCK = utils.getMockStep( + 'Setup Node', + 'Setup Node', + 'VERIFY', + [], + [], +); +const VERIFYPODFILE__VERIFY__VERIFY_PODFILE__STEP_MOCK = utils.getMockStep( + 'Verify podfile', + 'Verify podfile', + 'VERIFY', + [], + [], +); +const VERIFYPODFILE__VERIFY__STEP_MOCKS = [ + VERIFYPODFILE__VERIFY__CHECKOUT__STEP_MOCK, + VERIFYPODFILE__VERIFY__SETUP_NODE__STEP_MOCK, + VERIFYPODFILE__VERIFY__VERIFY_PODFILE__STEP_MOCK, +]; + +module.exports = { + VERIFYPODFILE__VERIFY__STEP_MOCKS, +}; diff --git a/workflow_tests/verifyPodfile.test.js b/workflow_tests/verifyPodfile.test.js new file mode 100644 index 000000000000..7529f509787e --- /dev/null +++ b/workflow_tests/verifyPodfile.test.js @@ -0,0 +1,177 @@ +const path = require('path'); +const kieMockGithub = require('@kie/mock-github'); +const utils = require('./utils/utils'); +const assertions = require('./assertions/verifyPodfileAssertions'); +const mocks = require('./mocks/verifyPodfileMocks'); +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', 'verifyPodfile.yml'), + dest: '.github/workflows/verifyPodfile.yml', + }, +]; + +describe('test workflow verifyPodfile', () => { + const githubToken = 'dummy_github_token'; + const actor = 'Dummy Actor'; + const osbotifyActor = 'OSBotify'; + beforeEach(async () => { + // create a local repository and copy required files + mockGithub = new kieMockGithub.MockGithub({ + repo: { + testVerifyPodfileWorkflowRepo: { + 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 workflow', async () => { + const repoPath = mockGithub.repo.getPath('testVerifyPodfileWorkflowRepo') || ''; + const workflowPath = path.join(repoPath, '.github', 'workflows', 'verifyPodfile.yml'); + let act = new eAct.ExtendedAct(repoPath, workflowPath); + act = utils.setUpActParams( + act, + event, + eventOptions, + {}, + githubToken, + ); + act = utils.setJobRunners( + act, + {verify: 'ubuntu-latest'}, + workflowPath, + ); + const testMockSteps = { + verify: mocks.VERIFYPODFILE__VERIFY__STEP_MOCKS, + }; + const result = await act + .runEvent(event, { + workflowFile: path.join(repoPath, '.github', 'workflows'), + mockSteps: testMockSteps, + actor, + }); + + assertions.assertVerifyJobExecuted(result); + }); + describe('actor is OSBotify', () => { + it('does not execute workflow', async () => { + const repoPath = mockGithub.repo.getPath('testVerifyPodfileWorkflowRepo') || ''; + const workflowPath = path.join(repoPath, '.github', 'workflows', 'verifyPodfile.yml'); + let act = new eAct.ExtendedAct(repoPath, workflowPath); + act = utils.setUpActParams( + act, + event, + eventOptions, + {}, + githubToken, + ); + act = utils.setJobRunners( + act, + {verify: 'ubuntu-latest'}, + workflowPath, + ); + const testMockSteps = { + verify: mocks.VERIFYPODFILE__VERIFY__STEP_MOCKS, + }; + const result = await act + .runEvent(event, { + workflowFile: path.join(repoPath, '.github', 'workflows'), + mockSteps: testMockSteps, + actor: osbotifyActor, + }); + + assertions.assertVerifyJobExecuted(result, false); + }); + }); + }); + describe('pull request synchronized', () => { + const event = 'pull_request'; + const eventOptions = { + action: 'synchronize', + }; + it('executes workflow', async () => { + const repoPath = mockGithub.repo.getPath('testVerifyPodfileWorkflowRepo') || ''; + const workflowPath = path.join(repoPath, '.github', 'workflows', 'verifyPodfile.yml'); + let act = new eAct.ExtendedAct(repoPath, workflowPath); + act = utils.setUpActParams( + act, + event, + eventOptions, + {}, + githubToken, + ); + act = utils.setJobRunners( + act, + {verify: 'ubuntu-latest'}, + workflowPath, + ); + const testMockSteps = { + verify: mocks.VERIFYPODFILE__VERIFY__STEP_MOCKS, + }; + const result = await act + .runEvent(event, { + workflowFile: path.join(repoPath, '.github', 'workflows'), + mockSteps: testMockSteps, + actor, + }); + + assertions.assertVerifyJobExecuted(result); + }); + describe('actor is OSBotify', () => { + it('does not execute workflow', async () => { + const repoPath = mockGithub.repo.getPath('testVerifyPodfileWorkflowRepo') || ''; + const workflowPath = path.join(repoPath, '.github', 'workflows', 'verifyPodfile.yml'); + let act = new eAct.ExtendedAct(repoPath, workflowPath); + act = utils.setUpActParams( + act, + event, + eventOptions, + {}, + githubToken, + ); + act = utils.setJobRunners( + act, + {verify: 'ubuntu-latest'}, + workflowPath, + ); + const testMockSteps = { + verify: mocks.VERIFYPODFILE__VERIFY__STEP_MOCKS, + }; + const result = await act + .runEvent(event, { + workflowFile: path.join(repoPath, '.github', 'workflows'), + mockSteps: testMockSteps, + actor: osbotifyActor, + }); + + assertions.assertVerifyJobExecuted(result, false); + }); + }); + }); +});