Skip to content

Commit

Permalink
fix: safer regex
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Apr 5, 2022
1 parent dfb091d commit 470678a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const unlink = promisify(fs.unlink)
const { dirname, relative } = require('path')
const mkdir = require('mkdirp-infer-owner')
const toBatchSyntax = require('./to-batch-syntax')
const shebangExpr = /^#!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+=[^ \t]+\s+)*\s*([^ \t]+)(.*)$/
const shebangExpr = /^#!\s*(?:\/usr\/bin\/env\s*((?:[^ \t=]+=[^ \t=]+\s+)*))?([^ \t]+)(.*)$/

const cmdShimIfExists = (from, to) =>
stat(from).then(() => cmdShim(from, to), () => {})
Expand Down
71 changes: 71 additions & 0 deletions tap-snapshots/test/basic.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,77 @@ exec "$basedir/" "$@"
`

exports[`test/basic.js TAP multiple variables > cmd 1`] = `
@ECHO off\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
@SET key=value\\r
@SET key2=value2\\r
\\r
IF EXIST "%dp0%\\node.exe" (\\r
SET "_prog=%dp0%\\node.exe"\\r
) ELSE (\\r
SET "_prog=node"\\r
SET PATHEXT=%PATHEXT:;.JS;=;%\\r
)\\r
\\r
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" --flag-one --flag-two "%dp0%\\from.env.multiple.variables" %*\\r
`

exports[`test/basic.js TAP multiple variables > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" --flag-one --flag-two "$basedir/from.env.multiple.variables" $args
} else {
& "$basedir/node$exe" --flag-one --flag-two "$basedir/from.env.multiple.variables" $args
}
$ret=$LASTEXITCODE
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" --flag-one --flag-two "$basedir/from.env.multiple.variables" $args
} else {
& "node$exe" --flag-one --flag-two "$basedir/from.env.multiple.variables" $args
}
$ret=$LASTEXITCODE
}
exit $ret
`

exports[`test/basic.js TAP multiple variables > shell 1`] = `
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
case \`uname\` in
*CYGWIN*|*MINGW*|*MSYS*) basedir=\`cygpath -w "$basedir"\`;;
esac
if [ -x "$basedir/node" ]; then
exec key=value key2=value2 "$basedir/node" --flag-one --flag-two "$basedir/from.env.multiple.variables" "$@"
else
exec key=value key2=value2 node --flag-one --flag-two "$basedir/from.env.multiple.variables" "$@"
fi
`

exports[`test/basic.js TAP no shebang > cmd 1`] = `
@ECHO off\\r
GOTO start\\r
Expand Down
1 change: 1 addition & 0 deletions test/00-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const froms = {
'from.env.variables': '#!/usr/bin/env NODE_PATH=./lib:$NODE_PATH node',
'from.sh': '#!/usr/bin/sh\necho hi\n',
'from.sh.args': '#!/usr/bin/sh -x\necho hi\n',
'from.env.multiple.variables': '#!/usr/bin/env key=value key2=value2 node --flag-one --flag-two',
}

mkdirp.sync(fixtures)
Expand Down
10 changes: 10 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ test('explicit shebang with args', function (t) {
matchSnapshot(t, fs.readFileSync(to + '.ps1', 'utf8'), 'ps1')
})
})

test('multiple variables', function (t) {
var from = path.resolve(fixtures, 'from.env.multiple.variables')
var to = path.resolve(fixtures, 'sh.multiple.shim')
return cmdShim(from, to).then(() => {
matchSnapshot(t, fs.readFileSync(to, 'utf8'), 'shell')
matchSnapshot(t, fs.readFileSync(to + '.cmd', 'utf8'), 'cmd')
matchSnapshot(t, fs.readFileSync(to + '.ps1', 'utf8'), 'ps1')
})
})

0 comments on commit 470678a

Please sign in to comment.