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

chore: fix circle-env for contributor PRs #21292

Merged
merged 2 commits into from
May 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion scripts/circle-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?')
Expand All @@ -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) {
Expand Down
49 changes: 38 additions & 11 deletions scripts/unit/circle-env-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
})

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('Missing MAIN_CANARY')
}
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 () => {
Expand Down