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
19 changes: 19 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 @@ -9,6 +9,7 @@ import {
cloudEnvironment,
macAddress,
getThemeKitAccessDomain,
_resetHasGit,
opentelemetryDomain,
} from './local.js'
import {fileExists} from '../fs.js'
Expand Down Expand Up @@ -123,6 +124,10 @@ describe('isShopify', () => {
})

describe('hasGit', () => {
afterEach(() => {
_resetHasGit()
})

test('returns false if git --version errors', async () => {
// Given
vi.mocked(exec).mockRejectedValue(new Error('git not found'))
Expand All @@ -132,6 +137,7 @@ describe('hasGit', () => {

// Then
expect(got).toBeFalsy()
expect(exec).toHaveBeenCalledWith('git', ['--version'])
})

test('returns true if git --version succeeds', async () => {
Expand All @@ -143,6 +149,19 @@ describe('hasGit', () => {

// Then
expect(got).toBeTruthy()
expect(exec).toHaveBeenCalledWith('git', ['--version'])
})

test('memoizes the result', async () => {
// Given
vi.mocked(exec).mockResolvedValue(undefined)

// When
await hasGit()
await hasGit()

// Then
expect(exec).toHaveBeenCalledTimes(1)
})
})

Expand Down
29 changes: 22 additions & 7 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,11 @@ let memoizedIsVerbose: boolean | undefined
*/
let memoizedIsUnitTest: boolean | undefined

/**
* Memoized value for the hasGit check.
*/
let memoizedHasGit: Promise<boolean> | undefined

/**
* Returns true if the CLI is running in debug mode.
*
Expand Down Expand Up @@ -237,13 +242,23 @@ export function cloudEnvironment(env: NodeJS.ProcessEnv = process.env): {
* @returns A promise that resolves with the value.
*/
export async function hasGit(): Promise<boolean> {
try {
await lazyExec('git', ['--version'])
return true
// eslint-disable-next-line no-catch-all/no-catch-all
} catch {
return false
}
return (memoizedHasGit ??= (async () => {
try {
await lazyExec('git', ['--version'])
return true
// eslint-disable-next-line no-catch-all/no-catch-all
} catch {
return false
}
})())
}

/**
* Resets the memoized hasGit value.
* This is useful for testing.
*/
export function _resetHasGit(): void {
memoizedHasGit = undefined
}

/**
Expand Down
Loading