From 449361a52779c5945ea4d521581e54ec1c567bfe Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Tue, 20 Jun 2023 17:52:43 -0700 Subject: [PATCH] fixup: add tests and prefix for ps1 scripts --- bin/npm.ps1 | 17 +++++++++++++---- bin/npx.ps1 | 17 +++++++++++++---- test/bin/windows-shims.js | 20 +++++++++++++++++++- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/bin/npm.ps1 b/bin/npm.ps1 index 419992b62c36f..f2f236adc23db 100644 --- a/bin/npm.ps1 +++ b/bin/npm.ps1 @@ -9,18 +9,27 @@ if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) { } $ret=0 -$nodebin = $(Get-Command "node$exe" -ErrorAction SilentlyContinue -ErrorVariable F).Source +$nodeexe = "node$exe" +$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source if ($nodebin -eq $null) { - Write-Host "node$exe not found." + Write-Host "$nodeexe not found." exit 1 } $nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path +$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js" +$npmprefix=(& $nodeexe $npmclijs prefix -g) +if ($LASTEXITCODE -ne 0) { + Write-Host "Could not determine Node.js install directory" + exit 1 +} +$npmprefixclijs="$npmprefix/node_modules/npm/bin/npm-cli.js" + # Support pipeline input if ($MyInvocation.ExpectingInput) { - $input | & "node$exe" "$nodedir/node_modules/npm/bin/npm-cli.js" $args + $input | & $nodeexe $npmprefixclijs $args } else { - & "node$exe" "$nodedir/node_modules/npm/bin/npm-cli.js" $args + & $nodeexe $npmprefixclijs $args } $ret=$LASTEXITCODE exit $ret diff --git a/bin/npx.ps1 b/bin/npx.ps1 index e6038695c5a92..437e2a7b74c3a 100644 --- a/bin/npx.ps1 +++ b/bin/npx.ps1 @@ -9,18 +9,27 @@ if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) { } $ret=0 -$nodebin = $(Get-Command "node$exe" -ErrorAction SilentlyContinue -ErrorVariable F).Source +$nodeexe = "node$exe" +$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source if ($nodebin -eq $null) { - Write-Host "node$exe not found." + Write-Host "$nodeexe not found." exit 1 } $nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path +$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js" +$npmprefix=(& $nodeexe $npmclijs prefix -g) +if ($LASTEXITCODE -ne 0) { + Write-Host "Could not determine Node.js install directory" + exit 1 +} +$npmprefixclijs="$npmprefix/node_modules/npm/bin/npx-cli.js" + # Support pipeline input if ($MyInvocation.ExpectingInput) { - $input | & "node$exe" "$nodedir/node_modules/npm/bin/npx-cli.js" $args + $input | & $nodeexe $npmprefixclijs $args } else { - & "node$exe" "$nodedir/node_modules/npm/bin/npx-cli.js" $args + & $nodeexe $npmprefixclijs $args } $ret=$LASTEXITCODE exit $ret diff --git a/test/bin/windows-shims.js b/test/bin/windows-shims.js index 6cd859001f2ed..c13a0ec5913f6 100644 --- a/test/bin/windows-shims.js +++ b/test/bin/windows-shims.js @@ -1,6 +1,6 @@ const t = require('tap') const { spawnSync } = require('child_process') -const { resolve, join, extname, basename } = require('path') +const { resolve, join, extname, basename, sep } = require('path') const { readFileSync, chmodSync, readdirSync } = require('fs') const Diff = require('diff') const { sync: which } = require('which') @@ -18,6 +18,12 @@ const SHIMS = readdirSync(BIN).reduce((acc, shim) => { const SHIM_EXTS = [...new Set(Object.keys(SHIMS).map(p => extname(p)))] +// windows requires each segment of a command path to be quoted when using shell: true +const quotePath = (cmd) => cmd + .split(sep) + .map(p => p.includes(' ') ? `"${p}"` : p) + .join(sep) + t.test('shim contents', t => { // these scripts should be kept in sync so this tests the contents of each // and does a diff to ensure the only differences between them are necessary @@ -49,6 +55,13 @@ t.test('shim contents', t => { t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x') t.end() }) + + t.test('pwsh', t => { + const { diff, letters } = diffFiles(SHIMS['npm.ps1'], SHIMS['npx.ps1']) + t.equal(diff.length, 0) + t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x') + t.end() + }) }) t.test('run shims', t => { @@ -197,6 +210,11 @@ t.test('run shims', t => { case 'bash.exe': args.push(bin) break + case 'pwsh.exe': + cmd = quotePath(cmd) + args.push(`${bin}.ps1`) + opts.shell = true + break default: throw new Error('unknown shell') }