import { exec } from 'child_process' import { Eltro as t, assert } from 'eltro' import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); t.describe('in.js', 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] } }) let n = 1 function insideCi() { Object.keys(require.cache).forEach(key => { delete require.cache[key] }) return import(`./in.js?${n++}`).then(x => x.default) } testVariables.forEach(name => { t.test(`env.${name} should return true if set`, async function () { process.env[name] = 'asdf' assert.ok(await insideCi()) }) }) t.test('should return false by default', async function() { assert.notOk(await insideCi()) }) t.test('should return false if all are empty strings', async function () { for (let name of testVariables) { process.env[name] = '' } assert.notOk(await insideCi()) }) t.test('should return false if env.CI is specifically "false"', async function () { process.env.CI = 'false' assert.notOk(await insideCi()) }) }) function runCommand(command, options) { return new Promise(function (res) { exec(command, options, function (err, stdout, stderr) { res({ err, stdout, stderr }) }) }) } t.describe('CLI in-ci', function() { t.test('should return success/code 0 if CI is filled', async function () { let result = await runCommand('node in.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 in.js', { env: { CI: 'false' } }) assert.ok(result.err) assert.strictEqual(result.err.code, 1) }) }) 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) }) })