Skip to content

Commit

Permalink
fixup: add tests and prefix for ps1 scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys authored and wraithgar committed Jun 26, 2023
1 parent 6069cce commit cca0e25
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
17 changes: 13 additions & 4 deletions bin/npm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 13 additions & 4 deletions bin/npx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 20 additions & 1 deletion test/bin/windows-shims.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -133,6 +146,7 @@ t.test('run shims', t => {

const shells = Object.entries({
cmd: 'cmd',
pwsh: 'pwsh',
git: join(ProgramFiles, 'Git', 'bin', 'bash.exe'),
'user git': join(ProgramFiles, 'Git', 'usr', 'bin', 'bash.exe'),
wsl: join(SystemRoot, 'System32', 'bash.exe'),
Expand Down Expand Up @@ -197,6 +211,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')
}
Expand Down

0 comments on commit cca0e25

Please sign in to comment.