Skip to content

Commit

Permalink
feat: add usePwsh helper for PowerShell v7+ (#790)
Browse files Browse the repository at this point in the history
* feat: add `usePwsh` for PowerShell v7+

Signed-off-by: Grigorii K. Shartsev <[email protected]>

* test: add usePwsh smoke test

Signed-off-by: Grigorii K. Shartsev <[email protected]>

* test: add `usePwsh` unit test

Signed-off-by: Grigorii K. Shartsev <[email protected]>

* fixup! test: add usePwsh smoke test

Signed-off-by: Grigorii K. Shartsev <[email protected]>

* fixup! test: add `usePwsh` unit test

Signed-off-by: Grigorii K. Shartsev <[email protected]>

* fixup! test: add `usePwsh` unit test

Signed-off-by: Grigorii K. Shartsev <[email protected]>

* fixup! test: add usePwsh smoke test

Signed-off-by: Grigorii K. Shartsev <[email protected]>

---------

Signed-off-by: Grigorii K. Shartsev <[email protected]>
  • Loading branch information
ShGKme authored Apr 30, 2024
1 parent eda722c commit 18e8e13
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ export function usePowerShell() {
$.quote = quotePowerShell
}

export function usePwsh() {
$.shell = which.sync('pwsh')
$.prefix = ''
$.postfix = '; exit $LastExitCode'
$.quote = quotePowerShell
}

export function useBash() {
$.shell = which.sync('bash')
$.prefix = 'set -euo pipefail;'
Expand Down
1 change: 1 addition & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ declare global {
var quotePowerShell: typeof _.quotePowerShell
var retry: typeof _.retry
var usePowerShell: typeof _.usePowerShell
var usePwsh: typeof _.usePwsh
var useBash: typeof _.useBash
var sleep: typeof _.sleep
var spinner: typeof _.spinner
Expand Down
12 changes: 12 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,16 @@ describe('core', () => {
}
assert.ok(ok, 'Expected failure!')
})

test('usePwsh() sets proper defaults', () => {
const originalWhichSync = which.sync
which.sync = (bin) => (bin === 'pwsh' ? 'pwsh' : originalWhichSync(bin))
usePwsh()
assert.equal($.shell, 'pwsh')
assert.equal($.prefix, '')
assert.equal($.postfix, '; exit $LastExitCode')
assert.equal($.quote, quotePowerShell)
which.sync = originalWhichSync
useBash()
})
})
2 changes: 2 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
cd,
syncProcessCwd,
usePowerShell,
usePwsh,
useBash,
kill,
ProcessOutput,
Expand Down Expand Up @@ -69,6 +70,7 @@ describe('index', () => {
assert(defaults)
assert(within)
assert(usePowerShell)
assert(usePwsh)
assert(useBash)

// goods
Expand Down
11 changes: 11 additions & 0 deletions test/smoke/win32.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import '../../build/globals.js'

const _describe = process.platform === 'win32' ? describe : describe.skip

const _testPwsh = which.sync('pwsh', { nothrow: true }) ? test : test.skip

_describe('win32', () => {
test('should work with windows-specific commands', async () => {
const p = await $`echo $0` // Bash is first by default.
Expand All @@ -38,4 +40,13 @@ _describe('win32', () => {
assert.match(p.stdout, /Windows 'rulez!'/)
})
})

_testPwsh('should work with pwsh when it is available', async () => {
await within(async () => {
usePwsh()
assert.match($.shell, /pwsh/i)
const p = await $`echo 'Hello,' && echo ${`new 'PowerShell'!`}`
assert.match(p.stdout, /Hello,\s+new 'PowerShell'!/)
})
})
})

0 comments on commit 18e8e13

Please sign in to comment.