UNPKG

@buddy-works/unit-tests

Version:

Universal test results collector for Jest, Jasmine, Mocha, Cypress, Playwright, and Vitest that sends results to Buddy Works API in real-time

114 lines (113 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CI_PROVIDER = exports.environmentConfig = exports.environmentError = void 0; exports.setEnvironmentVariable = setEnvironmentVariable; exports.detectCIProvider = detectCIProvider; const environmentConfig = { BUDDY_UT_TOKEN: { type: 'string', required: true, secret: true }, CI: { type: 'boolean' }, BUDDY: { type: 'boolean' }, BUDDY_LOGGER_DEBUG: { type: 'boolean' }, BUDDY_API_URL: { type: 'string' }, BUDDY_SESSION_ID: { type: 'string' }, BUDDY_API_FAILURE: { type: 'boolean' }, BUDDY_RUN_HASH: { type: 'string' }, BUDDY_RUN_REF_NAME: { type: 'string' }, BUDDY_RUN_REF_TYPE: { type: 'string' }, BUDDY_RUN_COMMIT: { type: 'string' }, BUDDY_RUN_PRE_COMMIT: { type: 'string' }, BUDDY_RUN_BRANCH: { type: 'string' }, BUDDY_RUN_URL: { type: 'string' }, BUDDY_TRIGGERING_ACTOR_ID: { type: 'string' }, GITHUB_REPOSITORY: { type: 'string' }, GITHUB_SHA: { type: 'string' }, GITHUB_REF: { type: 'string' }, GITHUB_REF_NAME: { type: 'string' }, GITHUB_REF_TYPE: { type: 'string' }, GITHUB_WORKFLOW: { type: 'string' }, GITHUB_RUN_ID: { type: 'string' }, GITHUB_RUN_NUMBER: { type: 'string' }, GITHUB_ACTOR: { type: 'string' }, GITHUB_ACTOR_ID: { type: 'string' }, GITHUB_SERVER_URL: { type: 'string' }, GITHUB_API_URL: { type: 'string' }, GITHUB_ACTIONS: { type: 'boolean' }, }; exports.environmentConfig = environmentConfig; function processConfigEntry(key, config) { if (config.type === 'boolean') { return getEnvironmentFlag(key, config.defaultValue ?? false); } else { const stringConfig = config; return stringConfig.required === true ? getEnvironment(key, true) : getEnvironment(key, false); } } function loadEnvironment() { const entries = []; for (const key of Object.keys(environmentConfig)) { try { const value = processConfigEntry(key, environmentConfig[key]); entries.push([key, value]); } catch (error) { return { error, variables: {}, }; } } return { variables: Object.fromEntries(entries), }; } function setEnvironmentVariable(key, value) { const config = environmentConfig[key]; if (config.type === 'boolean') { process.env[key] = value ? '1' : '0'; } else { if (value === undefined) { process.env[key] = ''; } else { process.env[key] = value; } } } function getEnvironment(key, required = false) { const value = process.env[key]; if (value === undefined && required) { throw new Error(`Missing required configuration. Please set the ${key} environment variable.`); } return value; } function getEnvironmentFlag(key, defaultValue = false) { const value = process.env[key]; if (value === undefined) { return defaultValue; } const falseValues = ['0', 'false', 'no', 'off', '']; return !falseValues.includes(value.toLowerCase().trim()); } var CI_PROVIDER; (function (CI_PROVIDER) { CI_PROVIDER["BUDDY"] = "BUDDY"; CI_PROVIDER["GITHUB_ACTION"] = "GITHUB_ACTION"; CI_PROVIDER["NONE"] = "NONE"; })(CI_PROVIDER || (exports.CI_PROVIDER = CI_PROVIDER = {})); function detectCIProvider() { const result = loadEnvironment(); if (result.variables.BUDDY) { return CI_PROVIDER.BUDDY; } if (result.variables.GITHUB_ACTIONS) { return CI_PROVIDER.GITHUB_ACTION; } return CI_PROVIDER.NONE; } const environmentResult = loadEnvironment(); exports.default = environmentResult.variables; exports.environmentError = environmentResult.error;