Skip to content

Commit 18e8e13

Browse files
authoredApr 30, 2024
feat: add usePwsh helper for PowerShell v7+ (google#790)
* feat: add `usePwsh` for PowerShell v7+ Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * test: add usePwsh smoke test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * test: add `usePwsh` unit test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * fixup! test: add usePwsh smoke test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * fixup! test: add `usePwsh` unit test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * fixup! test: add `usePwsh` unit test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * fixup! test: add usePwsh smoke test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> --------- Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
1 parent eda722c commit 18e8e13

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed
 

‎src/core.ts

+7
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ export function usePowerShell() {
117117
$.quote = quotePowerShell
118118
}
119119

120+
export function usePwsh() {
121+
$.shell = which.sync('pwsh')
122+
$.prefix = ''
123+
$.postfix = '; exit $LastExitCode'
124+
$.quote = quotePowerShell
125+
}
126+
120127
export function useBash() {
121128
$.shell = which.sync('bash')
122129
$.prefix = 'set -euo pipefail;'

‎src/globals.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ declare global {
4242
var quotePowerShell: typeof _.quotePowerShell
4343
var retry: typeof _.retry
4444
var usePowerShell: typeof _.usePowerShell
45+
var usePwsh: typeof _.usePwsh
4546
var useBash: typeof _.useBash
4647
var sleep: typeof _.sleep
4748
var spinner: typeof _.spinner

‎test/core.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,16 @@ describe('core', () => {
583583
}
584584
assert.ok(ok, 'Expected failure!')
585585
})
586+
587+
test('usePwsh() sets proper defaults', () => {
588+
const originalWhichSync = which.sync
589+
which.sync = (bin) => (bin === 'pwsh' ? 'pwsh' : originalWhichSync(bin))
590+
usePwsh()
591+
assert.equal($.shell, 'pwsh')
592+
assert.equal($.prefix, '')
593+
assert.equal($.postfix, '; exit $LastExitCode')
594+
assert.equal($.quote, quotePowerShell)
595+
which.sync = originalWhichSync
596+
useBash()
597+
})
586598
})

‎test/index.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
cd,
2323
syncProcessCwd,
2424
usePowerShell,
25+
usePwsh,
2526
useBash,
2627
kill,
2728
ProcessOutput,
@@ -69,6 +70,7 @@ describe('index', () => {
6970
assert(defaults)
7071
assert(within)
7172
assert(usePowerShell)
73+
assert(usePwsh)
7274
assert(useBash)
7375

7476
// goods

‎test/smoke/win32.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import '../../build/globals.js'
1818

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

21+
const _testPwsh = which.sync('pwsh', { nothrow: true }) ? test : test.skip
22+
2123
_describe('win32', () => {
2224
test('should work with windows-specific commands', async () => {
2325
const p = await $`echo $0` // Bash is first by default.
@@ -38,4 +40,13 @@ _describe('win32', () => {
3840
assert.match(p.stdout, /Windows 'rulez!'/)
3941
})
4042
})
43+
44+
_testPwsh('should work with pwsh when it is available', async () => {
45+
await within(async () => {
46+
usePwsh()
47+
assert.match($.shell, /pwsh/i)
48+
const p = await $`echo 'Hello,' && echo ${`new 'PowerShell'!`}`
49+
assert.match(p.stdout, /Hello,\s+new 'PowerShell'!/)
50+
})
51+
})
4152
})

0 commit comments

Comments
 (0)