diff --git a/packages/cli-kit/src/public/node/context/local.test.ts b/packages/cli-kit/src/public/node/context/local.test.ts index abe71bb52b7..0ceea6fa19a 100644 --- a/packages/cli-kit/src/public/node/context/local.test.ts +++ b/packages/cli-kit/src/public/node/context/local.test.ts @@ -10,6 +10,7 @@ import { macAddress, getThemeKitAccessDomain, opentelemetryDomain, + _resetCIPlatformCache, } from './local.js' import {fileExists} from '../fs.js' import {exec} from '../system.js' @@ -252,6 +253,10 @@ describe('cloudEnvironment', () => { }) describe('ciPlatform', () => { + afterEach(() => { + _resetCIPlatformCache() + }) + test('should return isCI false for non-CI environment', () => { // Given const nonCIResult = ciPlatform({}) @@ -339,6 +344,39 @@ describe('ciPlatform', () => { metadata: {}, }) }) + + test('should return memoized data when env is process.env', () => { + // Given + const originalEnv = process.env + try { + process.env = {CI: 'true', GITHUB_ACTION: '1'} as any + const firstResult = ciPlatform() + + // When + process.env = {CI: 'false'} as any + const secondResult = ciPlatform() + + // Then + expect(secondResult).toBe(firstResult) + expect(secondResult.isCI).toBe(true) + } finally { + process.env = originalEnv + } + }) + + test('should NOT return memoized data when env is NOT process.env', () => { + // Given + const env1 = {CI: 'true', GITHUB_ACTION: '1'} + const env2 = {CI: 'false'} + const firstResult = ciPlatform(env1) + + // When + const secondResult = ciPlatform(env2) + + // Then + expect(secondResult).not.toBe(firstResult) + expect(secondResult.isCI).toBe(false) + }) }) describe('getThemeKitAccessDomain', () => { diff --git a/packages/cli-kit/src/public/node/context/local.ts b/packages/cli-kit/src/public/node/context/local.ts index ad816399db9..909da4b6b54 100644 --- a/packages/cli-kit/src/public/node/context/local.ts +++ b/packages/cli-kit/src/public/node/context/local.ts @@ -44,6 +44,14 @@ let memoizedIsVerbose: boolean | undefined */ let memoizedIsUnitTest: boolean | undefined +/** + * Memoized value for the CI platform check. + */ +let memoizedCIPlatform: + | {isCI: true; name: string; metadata: Metadata} + | {isCI: false; name?: undefined; metadata?: undefined} + | undefined + /** * Returns true if the CLI is running in debug mode. * @@ -246,6 +254,14 @@ export async function hasGit(): Promise { } } +/** + * Resets the memoized CI platform value. + * This is useful for testing purposes. + */ +export function _resetCIPlatformCache(): void { + memoizedCIPlatform = undefined +} + /** * Gets info on the CI platform the CLI is running on, if applicable. * @@ -255,6 +271,10 @@ export async function hasGit(): Promise { export function ciPlatform( env = process.env, ): {isCI: true; name: string; metadata: Metadata} | {isCI: false; name?: undefined; metadata?: undefined} { + if (env === process.env && memoizedCIPlatform !== undefined) { + return memoizedCIPlatform + } + let result: {isCI: true; name: string; metadata: Metadata} | {isCI: false; name?: undefined; metadata?: undefined} if (isTruthy(env.CI)) { let name = 'unknown' if (isSet(env.BITBUCKET_BUILD_NUMBER)) { @@ -269,21 +289,26 @@ export function ciPlatform( name = 'buildkite' } - return { + result = { isCI: true, name, metadata: getCIMetadata(name, env), } } else if (isTruthy(env.TF_BUILD)) { - return { + result = { isCI: true, name: 'azure', metadata: getCIMetadata('azure', env), } + } else { + result = { + isCI: false, + } } - return { - isCI: false, + if (env === process.env) { + memoizedCIPlatform = result } + return result } /**