Last active
September 2, 2019 12:24
-
-
Save jamo/19eff8a3c304aa4489c9417118b5a14d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ciInfo = jest.genMockFromModule(`ci-info`) | |
ciInfo.isCI = true | |
ciInfo.name = `bad default` | |
function setName(name) { | |
ciInfo.name = name | |
} | |
ciInfo.setName = setName | |
module.exports = ciInfo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe(`CI detection`, () => { | |
beforeEach(() => { | |
jest.resetModules() | |
}) | |
it(`Detects CI when ci-info detects one`, () => { | |
const { isCI, getCIName } = require(`../ci`) | |
jest.mock(`ci-info`) | |
const ciInfo = require(`ci-info`) | |
ciInfo.isCI = true | |
ciInfo.setName(`testname`) | |
expect(isCI()).toBeTruthy() | |
expect(getCIName()).toEqual(`testname`) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ci = require(`ci-info`) | |
const CI_DEFINITIONS = [ | |
getEnvFromCIInfo, | |
getEnvDetect({ key: `NOW_BUILDER_ANNOTATE`, name: `ZEIT Now` }), | |
getEnvDetect({ key: "NOW_REGION", name: "ZEIT Now v1" }), | |
herokuDetect, | |
envFromCIandCIName, | |
] | |
console.log(CI_DEFINITIONS) | |
const CIName = CI_DEFINITIONS.find(fn => { | |
const res = fn() | |
if (res) { | |
console.log(`got res`, res) | |
return res | |
} | |
return false | |
}) | |
console.log(`CI NAME = `, CIName) | |
function isCI() { | |
return !!CIName | |
} | |
function getCIName() { | |
if (!isCI()) { | |
return | |
} | |
console.log(`returning `, CIName) | |
return CIName | |
} | |
module.exports = { isCI, getCIName } | |
// Helper functions | |
function getEnvFromCIInfo() { | |
if (ci.isCI) { | |
console.log(`JAMO OK returning `, ci.name) | |
return ci.name || `ci-info detected w/o name` | |
} | |
console.log(`JAMO FAIL`) | |
return false | |
} | |
function getEnvDetect({ key, name }) { | |
return function() { | |
console.log(`looking for ${key} in env to detect ${name}`) | |
if (process.env[key]) { | |
return name | |
} | |
return false | |
} | |
} | |
function herokuDetect() { | |
console.log(`looking for heroku in path to detect heroku`) | |
if (/\.heroku\/node\/bin\/node/.test(process.cwd())) { | |
return `Heroku` | |
} | |
return false | |
} | |
function envFromCIandCIName() { | |
if (process.env.CI_NAME) { | |
return process.env.CI_NAME | |
} else if (process.env.CI) { | |
return `CI detected without name` | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment