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
43 changes: 43 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 @@ -5,6 +5,7 @@ import {
isShopify,
isTerminalInteractive,
isUnitTest,
_resetHasGit,
analyticsDisabled,
cloudEnvironment,
macAddress,
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 @@ -144,6 +149,44 @@ describe('hasGit', () => {
// Then
expect(got).toBeTruthy()
})

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

// When
await hasGit()
await hasGit()

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

test('returns the same promise instance', async () => {
// Given
vi.mocked(exec).mockResolvedValue(undefined)

// When
const promise1 = hasGit()
const promise2 = hasGit()

// Then
expect(promise1).toBe(promise2)
await expect(promise1).resolves.toBe(true)
})

test('clears the cache when _resetHasGit is called', async () => {
// Given
vi.mocked(exec).mockResolvedValue(undefined)

// When
await hasGit()
_resetHasGit()
await hasGit()

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

describe('analitycsDisabled', () => {
Expand Down
33 changes: 26 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 @@ -236,14 +241,28 @@ 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
export function hasGit(): Promise<boolean> {
if (memoizedHasGit !== undefined) {
return memoizedHasGit
}
memoizedHasGit = (async () => {
try {
await lazyExec('git', ['--version'])
return true
// eslint-disable-next-line no-catch-all/no-catch-all
} catch {
return false
}
})()
return memoizedHasGit
}

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

/**
Expand Down
Loading