Skip to content
Closed
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
34 changes: 33 additions & 1 deletion packages/cli-kit/src/public/node/system.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as system from './system.js'
import {execa} from 'execa'
import {describe, expect, test, vi} from 'vitest'
import {describe, expect, test, vi, beforeEach} from 'vitest'
import which from 'which'
import {Readable} from 'stream'

Expand Down Expand Up @@ -353,6 +353,38 @@ describe('isStdinPiped', () => {
})
})

describe('isWsl', () => {
beforeEach(() => {
system._resetIsWsl()
})

test('returns the value from is-wsl', async () => {
// Given
vi.doMock('is-wsl', () => ({
default: true,
}))

// When
const got = await system.isWsl()

// Then
expect(got).toBe(true)
})

test('memoizes the result', async () => {
// Given
const isWslModule = await import('is-wsl')
const spy = vi.spyOn(isWslModule, 'default', 'get').mockReturnValue(true)

// When
await system.isWsl()
await system.isWsl()

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

describe('readStdinString', () => {
test('returns undefined when stdin is not piped', async () => {
// Given
Expand Down
21 changes: 18 additions & 3 deletions packages/cli-kit/src/public/node/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,29 @@ export function isCI(): boolean {
return isTruthy(process.env.CI)
}

/**
* Memoized promise for the WSL check.
*/
let memoizedIsWsl: Promise<boolean> | undefined

/**
* Check if the current environment is a WSL environment.
*
* @returns True if the current environment is a WSL environment.
*/
export async function isWsl(): Promise<boolean> {
const wsl = await import('is-wsl')
return wsl.default
export function isWsl(): Promise<boolean> {
memoizedIsWsl ??= (async () => {
const wsl = await import('is-wsl')
return wsl.default
})()
return memoizedIsWsl
}

/**
* Resets the memoized WSL check.
*/
export function _resetIsWsl(): void {
memoizedIsWsl = undefined
}

/**
Expand Down
Loading