From 7a76de10f88d3737fb0f46cfabe3b398af0a833f Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Mon, 2 May 2022 13:18:43 -0400 Subject: [PATCH] chore: fix circle-env for contributor PRs (#21292) --- scripts/circle-env.js | 11 +++++++- scripts/unit/circle-env-spec.js | 49 +++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/scripts/circle-env.js b/scripts/circle-env.js index 0787cf2cf6..70e7005e64 100644 --- a/scripts/circle-env.js +++ b/scripts/circle-env.js @@ -21,6 +21,10 @@ async function checkCanaries () { const circleEnv = await readCircleEnv() + if (Object.keys(circleEnv).length === 0) { + return console.warn('CircleCI env empty, assuming this is a contributor PR. Not checking for canary variables.') + } + if (!circleEnv.MAIN_CANARY) throw new Error('Missing MAIN_CANARY.') if (!circleEnv.CONTEXT_CANARY) throw new Error('Missing CONTEXT_CANARY. Does this job have the test-runner:env-canary context?') @@ -36,7 +40,12 @@ async function readCircleEnv () { // if this starts failing, try SSHing into a CircleCI job and see what changed in the $CIRCLE_INTERNAL_CONFIG file's schema const circleEnv = taskData['Dispatched']['TaskInfo']['Environment'] - if (!circleEnv || !Object.keys(circleEnv).length) throw new Error('An empty Environment object was found.') + if (!circleEnv) throw new Error('No Environment object was found.') + + // last-ditch effort to check that an empty circle env is accurately reflecting process.env (external PRs) + if (process.env.CACHE_VERSION && Object.keys(circleEnv).length === 0) { + throw new Error('CACHE_VERSION is set, but circleEnv is empty') + } return circleEnv } catch (err) { diff --git a/scripts/unit/circle-env-spec.js b/scripts/unit/circle-env-spec.js index e825f0111c..972067aeb6 100644 --- a/scripts/unit/circle-env-spec.js +++ b/scripts/unit/circle-env-spec.js @@ -12,22 +12,49 @@ describe('circle-env', () => { }) beforeEach(() => { + delete process.env.CACHE_VERSION process.env.CI = 'true' process.env.CIRCLE_INTERNAL_CONFIG = '/foo.json' }) - it('fails with missing canaries', async () => { - sinon.stub(fs, 'readFile') - .withArgs('/foo.json').resolves(JSON.stringify({ - Dispatched: { TaskInfo: { Environment: { somekey: 'someval' } } }, - })) + context('with missing canaries', () => { + it('fails', async () => { + sinon.stub(fs, 'readFile') + .withArgs('/foo.json').resolves(JSON.stringify({ + Dispatched: { TaskInfo: { Environment: { somekey: 'someval' } } }, + })) - try { - await _checkCanaries() - throw new Error('should not reach') - } catch (err) { - expect(err.message).to.include('Missing MAIN_CANARY') - } + try { + await _checkCanaries() + throw new Error('should not reach') + } catch (err) { + expect(err.message).to.include('Missing MAIN_CANARY') + } + }) + + context('with no circleEnv', () => { + beforeEach(() => { + sinon.stub(fs, 'readFile') + .withArgs('/foo.json').resolves(JSON.stringify({ + Dispatched: { TaskInfo: { Environment: {} } }, + })) + }) + + it('passes', async () => { + await _checkCanaries() + }) + + it('fails if CACHE_VERSION does exist', async () => { + process.env.CACHE_VERSION = 'foo' + + try { + await _checkCanaries() + throw new Error('should not reach') + } catch (err) { + expect(err.message).to.include('CACHE_VERSION is set, but circleEnv is empty') + } + }) + }) }) it('passes with canaries', async () => {