2024-11-18 23:57:35 +00:00
|
|
|
import { exec } from 'child_process'
|
|
|
|
import { Eltro as t, assert } from 'eltro'
|
2024-11-22 05:58:44 +00:00
|
|
|
import { createRequire } from 'node:module';
|
|
|
|
const require = createRequire(import.meta.url);
|
2024-11-18 23:57:35 +00:00
|
|
|
|
2024-11-22 05:58:44 +00:00
|
|
|
t.describe('in.js', function () {
|
2024-11-19 00:11:46 +00:00
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-11-22 05:58:44 +00:00
|
|
|
let n = 1
|
|
|
|
function insideCi() {
|
|
|
|
Object.keys(require.cache).forEach(key => {
|
|
|
|
delete require.cache[key]
|
|
|
|
})
|
|
|
|
return import(`./in.js?${n++}`).then(x => x.default)
|
|
|
|
}
|
|
|
|
|
2024-11-18 23:57:35 +00:00
|
|
|
testVariables.forEach(name => {
|
2024-11-22 05:58:44 +00:00
|
|
|
t.test(`env.${name} should return true if set`, async function () {
|
2024-11-18 23:57:35 +00:00
|
|
|
process.env[name] = 'asdf'
|
2024-11-22 05:58:44 +00:00
|
|
|
assert.ok(await insideCi())
|
2024-11-18 23:57:35 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-11-22 05:58:44 +00:00
|
|
|
t.test('should return false by default', async function() {
|
|
|
|
assert.notOk(await insideCi())
|
2024-11-18 23:57:35 +00:00
|
|
|
})
|
|
|
|
|
2024-11-22 05:58:44 +00:00
|
|
|
t.test('should return false if all are empty strings', async function () {
|
2024-11-18 23:57:35 +00:00
|
|
|
for (let name of testVariables) {
|
|
|
|
process.env[name] = ''
|
|
|
|
}
|
2024-11-22 05:58:44 +00:00
|
|
|
assert.notOk(await insideCi())
|
2024-11-18 23:57:35 +00:00
|
|
|
})
|
|
|
|
|
2024-11-22 05:58:44 +00:00
|
|
|
t.test('should return false if env.CI is specifically "false"', async function () {
|
2024-11-18 23:57:35 +00:00
|
|
|
process.env.CI = 'false'
|
2024-11-22 05:58:44 +00:00
|
|
|
assert.notOk(await insideCi())
|
2024-11-18 23:57:35 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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 () {
|
2024-11-22 05:02:56 +00:00
|
|
|
let result = await runCommand('node in.js', {
|
2024-11-18 23:57:35 +00:00
|
|
|
env: { CI: 'true' }
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.notOk(result.err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should return error code 1 if CI is false', async function () {
|
2024-11-22 05:02:56 +00:00
|
|
|
let result = await runCommand('node in.js', {
|
2024-11-18 23:57:35 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|