Skip to content

Commit 451c46e

Browse files
committed
fixup: add tests and prefix for ps1 scripts
1 parent bac4e02 commit 451c46e

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

bin/npm.ps1

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@ if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
99
}
1010
$ret=0
1111

12-
$nodebin = $(Get-Command "node$exe" -ErrorAction SilentlyContinue -ErrorVariable F).Source
12+
$nodeexe = "node$exe"
13+
$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source
1314
if ($nodebin -eq $null) {
14-
Write-Host "node$exe not found."
15+
Write-Host "$nodeexe not found."
1516
exit 1
1617
}
1718
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
1819

20+
$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
21+
$npmprefix=(& $nodeexe $npmclijs prefix -g)
22+
if ($LASTEXITCODE -ne 0) {
23+
Write-Host "Could not determine Node.js install directory"
24+
exit 1
25+
}
26+
$npmprefixclijs="$npmprefix/node_modules/npm/bin/npm-cli.js"
27+
1928
# Support pipeline input
2029
if ($MyInvocation.ExpectingInput) {
21-
$input | & "node$exe" "$nodedir/node_modules/npm/bin/npm-cli.js" $args
30+
$input | & $nodeexe $npmprefixclijs $args
2231
} else {
23-
& "node$exe" "$nodedir/node_modules/npm/bin/npm-cli.js" $args
32+
& $nodeexe $npmprefixclijs $args
2433
}
2534
$ret=$LASTEXITCODE
2635
exit $ret

bin/npx.ps1

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@ if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
99
}
1010
$ret=0
1111

12-
$nodebin = $(Get-Command "node$exe" -ErrorAction SilentlyContinue -ErrorVariable F).Source
12+
$nodeexe = "node$exe"
13+
$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source
1314
if ($nodebin -eq $null) {
14-
Write-Host "node$exe not found."
15+
Write-Host "$nodeexe not found."
1516
exit 1
1617
}
1718
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
1819

20+
$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
21+
$npmprefix=(& $nodeexe $npmclijs prefix -g)
22+
if ($LASTEXITCODE -ne 0) {
23+
Write-Host "Could not determine Node.js install directory"
24+
exit 1
25+
}
26+
$npmprefixclijs="$npmprefix/node_modules/npm/bin/npx-cli.js"
27+
1928
# Support pipeline input
2029
if ($MyInvocation.ExpectingInput) {
21-
$input | & "node$exe" "$nodedir/node_modules/npm/bin/npx-cli.js" $args
30+
$input | & $nodeexe $npmprefixclijs $args
2231
} else {
23-
& "node$exe" "$nodedir/node_modules/npm/bin/npx-cli.js" $args
32+
& $nodeexe $npmprefixclijs $args
2433
}
2534
$ret=$LASTEXITCODE
2635
exit $ret

test/bin/windows-shims.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const t = require('tap')
22
const { spawnSync } = require('child_process')
3-
const { resolve, join, extname, basename } = require('path')
3+
const { resolve, join, extname, basename, sep } = require('path')
44
const { readFileSync, chmodSync, readdirSync } = require('fs')
55
const Diff = require('diff')
66
const { sync: which } = require('which')
@@ -18,6 +18,12 @@ const SHIMS = readdirSync(BIN).reduce((acc, shim) => {
1818

1919
const SHIM_EXTS = [...new Set(Object.keys(SHIMS).map(p => extname(p)))]
2020

21+
// windows requires each segment of a command path to be quoted when using shell: true
22+
const quotePath = (cmd) => cmd
23+
.split(sep)
24+
.map(p => p.includes(' ') ? `"${p}"` : p)
25+
.join(sep)
26+
2127
t.test('shim contents', t => {
2228
// these scripts should be kept in sync so this tests the contents of each
2329
// and does a diff to ensure the only differences between them are necessary
@@ -49,6 +55,13 @@ t.test('shim contents', t => {
4955
t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x')
5056
t.end()
5157
})
58+
59+
t.test('pwsh', t => {
60+
const { diff, letters } = diffFiles(SHIMS['npm.ps1'], SHIMS['npx.ps1'])
61+
t.equal(diff.length, 0)
62+
t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x')
63+
t.end()
64+
})
5265
})
5366

5467
t.test('run shims', t => {
@@ -133,6 +146,7 @@ t.test('run shims', t => {
133146

134147
const shells = Object.entries({
135148
cmd: 'cmd',
149+
pwsh: 'pwsh',
136150
git: join(ProgramFiles, 'Git', 'bin', 'bash.exe'),
137151
'user git': join(ProgramFiles, 'Git', 'usr', 'bin', 'bash.exe'),
138152
wsl: join(SystemRoot, 'System32', 'bash.exe'),
@@ -197,6 +211,11 @@ t.test('run shims', t => {
197211
case 'bash.exe':
198212
args.push(bin)
199213
break
214+
case 'pwsh.exe':
215+
cmd = quotePath(cmd)
216+
args.push(`${bin}.ps1`)
217+
opts.shell = true
218+
break
200219
default:
201220
throw new Error('unknown shell')
202221
}

0 commit comments

Comments
 (0)