inside-ci/test.mjs
Jonatan Nilsson 853f9b8f78
All checks were successful
/ deploy (push) Successful in 21s
Implement basic ci detection
2024-11-18 23:58:24 +00:00

71 lines
1.6 KiB
JavaScript

import { exec } from 'child_process'
import { Eltro as t, assert } from 'eltro'
import { insideCi } from './index.js'
t.describe('#insideCi()', function () {
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)
})
})