Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/cli-kit/src/public/node/context/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
macAddress,
getThemeKitAccessDomain,
opentelemetryDomain,
_resetCIPlatformCache,
} from './local.js'
import {fileExists} from '../fs.js'
import {exec} from '../system.js'
Expand Down Expand Up @@ -252,6 +253,10 @@ describe('cloudEnvironment', () => {
})

describe('ciPlatform', () => {
afterEach(() => {
_resetCIPlatformCache()
})

test('should return isCI false for non-CI environment', () => {
// Given
const nonCIResult = ciPlatform({})
Expand Down Expand Up @@ -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', () => {
Expand Down
33 changes: 29 additions & 4 deletions packages/cli-kit/src/public/node/context/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -246,6 +254,14 @@ export async function hasGit(): Promise<boolean> {
}
}

/**
* 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.
*
Expand All @@ -255,6 +271,10 @@ export async function hasGit(): Promise<boolean> {
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)) {
Expand All @@ -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
}

/**
Expand Down
Loading