Files
cypress/scripts/check-conditional-ci.js
Zach Panzarino 42a0a67c77 build: fail tests when the conditional CI script errors (#9141)
* fail tests if ci script fails

* exit with nonzero exit code on uncaught exeptions

* move check halt out of yarn so it doesn't check node version

* rename check halt to check conditional ci
2020-11-10 12:46:59 -05:00

69 lines
1.7 KiB
JavaScript

/* eslint-disable no-console */
const { execSync } = require('child_process')
const { getCurrentBranch, readPackageJson } = require('./utils')
const { getChangedPackagesAndDependents, getLernaPackages } = require('./changed-packages')
const skipTests = () => {
execSync('circleci-agent step halt')
}
const main = async (ciJob) => {
if (!ciJob) {
console.log(`Could not get current CI job`)
process.exit(1)
}
const currentBranch = await getCurrentBranch()
if (currentBranch === 'develop' || currentBranch === 'master') {
console.log(`Currently on ${currentBranch} - all tests run`)
return
}
const packages = await getLernaPackages()
const changed = await getChangedPackagesAndDependents()
const packageInfo = packages
.filter((pack) => !pack.private && !pack.name.includes('@packages'))
.find((p) => {
const packageJson = readPackageJson(p)
return packageJson.ciJobs && packageJson.ciJobs.includes(ciJob)
})
// default to binary if we don't find an independent package
const pack = packageInfo ? packageInfo.name : 'cypress'
console.log(`Found package ${pack} that corresponds to current job ${ciJob}.`)
if (Object.keys(changed).includes(pack)) {
console.log(`${pack} was directly changed, so tests run.`)
return
}
const dependenciesChanged = []
for (const c in changed) {
if (changed[c].includes(pack)) {
dependenciesChanged.push(c)
}
}
if (dependenciesChanged.length) {
console.log(`${pack} is listed as a dependant of ${dependenciesChanged.join(', ')}, so tests run.`)
return
}
console.log(`${pack} is unchanged and not dependent on any changed packages, so tests do not run.`)
return skipTests()
}
main(process.env.CIRCLE_JOB).catch(() => {
process.exit(1)
})