inside-ci/test.mjs

97 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2024-11-18 23:57:35 +00:00
import { exec } from 'child_process'
import { Eltro as t, assert } from 'eltro'
import { insideCi } from './index.js'
t.describe('#insideCi()', function () {
// Most CI use env.CI (travis, Gitlab, etc.)
// There are some exceptions though:
// CI_APP_ID is used by Appflow: https://ionic.io/docs/appflow/package/environments#predefined-environments
// CI_NAME is used by Codeship:https://docs.cloudbees.com/docs/cloudbees-codeship/latest/pro-builds-and-configuration/environment-variables
// BUILD_NUMBER is used by TeamCity: https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Predefined+Server+Build+Parameters
// RUN_ID is used by Taskcluster: https://docs.taskcluster.net/docs/reference/workers/docker-worker/environment
2024-11-18 23:57:35 +00:00
const testVariables = [
'CI',
'CI_APP_ID',
'BUILD_NUMBER',
'CI_NAME',
'RUN_ID',
]
t.beforeEach(function () {
for (let name of testVariables) {
delete process.env[name]
}
})
testVariables.forEach(name => {
t.test(`env.${name} should return true if set`, function () {
process.env[name] = 'asdf'
assert.ok(insideCi())
})
})
t.test('should return false by default', function() {
assert.notOk(insideCi())
})
t.test('should return false if all are empty strings', function () {
for (let name of testVariables) {
process.env[name] = ''
}
assert.notOk(insideCi())
})
t.test('should return false if env.CI is specifically "false"', function () {
process.env.CI = 'false'
assert.notOk(insideCi())
})
})
function runCommand(command, options) {
return new Promise(function (res) {
exec(command, options, function (err, stdout, stderr) {
res({
err, stdout, stderr
})
})
})
}
2024-11-19 10:44:37 +00:00
t.describe('CLI in-ci', function() {
2024-11-18 23:57:35 +00:00
t.test('should return success/code 0 if CI is filled', async function () {
let result = await runCommand('node index.js', {
env: { CI: 'true' }
})
assert.notOk(result.err)
})
t.test('should return error code 1 if CI is false', async function () {
let result = await runCommand('node index.js', {
env: { CI: 'false' }
})
assert.ok(result.err)
assert.strictEqual(result.err.code, 1)
})
})
2024-11-19 10:44:37 +00:00
t.describe('CLI not-ci', function() {
t.test('should return error code 1 if CI is filled', async function () {
let result = await runCommand('node not.js', {
env: { CI: 'true' }
})
assert.ok(result.err)
assert.strictEqual(result.err.code, 1)
})
t.test('should return success/code 0 if CI is false', async function () {
let result = await runCommand('node not.js', {
env: { CI: 'false' }
})
assert.notOk(result.err)
})
})