|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* eslint-env mocha */ |
| 4 | +const la = require('lazy-ass') |
| 5 | +const sinon = require('sinon') |
| 6 | +const fs = require('fs') |
| 7 | +const { |
| 8 | + resolvePullRequestCi, |
| 9 | + PROVIDER_GITHUB_ACTIONS, |
| 10 | + PROVIDER_AZURE_PIPELINES, |
| 11 | + readGithubActionsPullRequest |
| 12 | +} = require('./pull-request-ci') |
| 13 | + |
| 14 | +describe('pull-request-ci', () => { |
| 15 | + const githubEvent = { |
| 16 | + pull_request: { |
| 17 | + head: { ref: 'head-ref', sha: 'head-sha' }, |
| 18 | + base: { ref: 'base-ref', sha: 'base-sha' }, |
| 19 | + issue_url: 'issue', |
| 20 | + html_url: 'html', |
| 21 | + title: 'title' |
| 22 | + }, |
| 23 | + sender: { |
| 24 | + avatar_url: 'av', |
| 25 | + html_url: 'sender' |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + describe('resolvePullRequestCi', () => { |
| 30 | + let readStub |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + readStub = sinon |
| 34 | + .stub(fs, 'readFileSync') |
| 35 | + .returns(JSON.stringify(githubEvent)) |
| 36 | + }) |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + readStub.restore() |
| 40 | + }) |
| 41 | + |
| 42 | + it('auto prefers GitHub Actions when event file is present', () => { |
| 43 | + const r = resolvePullRequestCi({ |
| 44 | + env: { |
| 45 | + GITHUB_ACTIONS: 'true', |
| 46 | + GITHUB_EVENT_PATH: '/tmp/event.json', |
| 47 | + BUILD_REASON: 'PullRequest', |
| 48 | + SYSTEM_PULLREQUEST_PULLREQUESTID: '99' |
| 49 | + }, |
| 50 | + fs, |
| 51 | + provider: 'auto' |
| 52 | + }) |
| 53 | + |
| 54 | + la(r.provider === PROVIDER_GITHUB_ACTIONS, r) |
| 55 | + la(r.headRef === 'head-ref', r) |
| 56 | + }) |
| 57 | + |
| 58 | + it('auto falls back to Azure when GitHub is not active', () => { |
| 59 | + const r = resolvePullRequestCi({ |
| 60 | + env: { |
| 61 | + BUILD_REASON: 'PullRequest', |
| 62 | + SYSTEM_PULLREQUEST_PULLREQUESTID: '7', |
| 63 | + SYSTEM_PULLREQUEST_SOURCEBRANCH: 'refs/heads/f' |
| 64 | + }, |
| 65 | + fs, |
| 66 | + provider: 'auto' |
| 67 | + }) |
| 68 | + |
| 69 | + la(r.provider === PROVIDER_AZURE_PIPELINES, r) |
| 70 | + la(r.pullRequestId === '7', r) |
| 71 | + }) |
| 72 | + |
| 73 | + it('github-actions skips Azure even if ADO vars are set', () => { |
| 74 | + const r = resolvePullRequestCi({ |
| 75 | + env: { |
| 76 | + GITHUB_ACTIONS: 'true', |
| 77 | + GITHUB_EVENT_PATH: '/tmp/event.json', |
| 78 | + BUILD_REASON: 'PullRequest', |
| 79 | + SYSTEM_PULLREQUEST_PULLREQUESTID: '99' |
| 80 | + }, |
| 81 | + fs, |
| 82 | + provider: PROVIDER_GITHUB_ACTIONS |
| 83 | + }) |
| 84 | + |
| 85 | + la(r.provider === PROVIDER_GITHUB_ACTIONS, r) |
| 86 | + }) |
| 87 | + |
| 88 | + it('azure-pipelines ignores GitHub event file', () => { |
| 89 | + const r = resolvePullRequestCi({ |
| 90 | + env: { |
| 91 | + GITHUB_ACTIONS: 'true', |
| 92 | + GITHUB_EVENT_PATH: '/tmp/event.json', |
| 93 | + BUILD_REASON: 'PullRequest', |
| 94 | + SYSTEM_PULLREQUEST_PULLREQUESTID: '3' |
| 95 | + }, |
| 96 | + fs, |
| 97 | + provider: PROVIDER_AZURE_PIPELINES |
| 98 | + }) |
| 99 | + |
| 100 | + la(r.provider === PROVIDER_AZURE_PIPELINES, r) |
| 101 | + la(r.pullRequestId === '3', r) |
| 102 | + la(readStub.called === false, 'should not read GitHub event JSON') |
| 103 | + }) |
| 104 | + |
| 105 | + it('github-actions returns undefined when not a GHA run', () => { |
| 106 | + const r = resolvePullRequestCi({ |
| 107 | + env: {}, |
| 108 | + fs, |
| 109 | + provider: PROVIDER_GITHUB_ACTIONS |
| 110 | + }) |
| 111 | + |
| 112 | + la(r === undefined, r) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + describe('readGithubActionsPullRequest', () => { |
| 117 | + it('injects fs for tests without stubbing global fs', () => { |
| 118 | + const fakeFs = { |
| 119 | + readFileSync: () => JSON.stringify(githubEvent) |
| 120 | + } |
| 121 | + const r = readGithubActionsPullRequest( |
| 122 | + '/x', |
| 123 | + 'true', |
| 124 | + /** @type {any} */ (fakeFs) |
| 125 | + ) |
| 126 | + |
| 127 | + la(r.provider === PROVIDER_GITHUB_ACTIONS, r) |
| 128 | + }) |
| 129 | + }) |
| 130 | +}) |
0 commit comments