inside-ci/test.mjs
Jonatan Nilsson afac31914a
Some checks failed
/ deploy (push) Failing after 15s
Trim the fat and fix typings
Clean the code a bit.
Move comments to test to decrease dependency size
Minify package.json for extra.
2024-11-19 00:11:46 +00:00

77 lines
2.2 KiB
JavaScript

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
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
})
})
})
}
t.describe('CLI', function() {
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)
})
})