diff --git a/index.js b/index.js index a6cf4f5..e2549de 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,9 @@ exports = module.exports = lifecycle exports.makeEnv = makeEnv exports._incorrectWorkingDirectory = _incorrectWorkingDirectory +// for testing +const platform = process.env.__TESTING_FAKE_PLATFORM__ || process.platform +const isWindows = platform === 'win32' const spawn = require('./lib/spawn') const path = require('path') const Stream = require('stream').Stream @@ -20,8 +23,8 @@ const hookStatCache = new Map() let PATH = 'PATH' -// windows calls it's path 'Path' usually, but this is not guaranteed. -if (process.platform === 'win32') { +// windows calls its path 'Path' usually, but this is not guaranteed. +if (isWindows) { PATH = 'Path' if (!process.env[PATH]) { Object.keys(process.env).forEach(function (e) { @@ -32,6 +35,8 @@ if (process.platform === 'win32') { } } +exports._pathEnvName = PATH + function logid (pkg, stage) { return pkg._id + '~' + stage + ':' } @@ -123,7 +128,7 @@ function lifecycle_ (pkg, stage, wd, opts, env, cb) { } if (env[PATH]) pathArr.push(env[PATH]) - env[PATH] = pathArr.join(process.platform === 'win32' ? ';' : ':') + env[PATH] = pathArr.join(isWindows ? ';' : ':') var packageLifecycle = pkg.scripts && pkg.scripts.hasOwnProperty(stage) @@ -166,7 +171,6 @@ function shouldPrependCurrentNodeDirToPATH (opts) { var isDifferentNodeInPath - var isWindows = process.platform === 'win32' var foundExecPath try { foundExecPath = which.sync(path.basename(process.execPath), { pathExt: isWindows ? ';' : ':' }) @@ -244,7 +248,7 @@ function runCmd (note, cmd, pkg, env, stage, wd, opts, cb) { } opts.log.verbose('lifecycle', logid(pkg, stage), 'unsafe-perm in lifecycle', unsafe) - if (process.platform === 'win32') { + if (isWindows) { unsafe = true } @@ -282,7 +286,7 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) { if (customShell) { sh = customShell - } else if (process.platform === 'win32') { + } else if (isWindows) { sh = process.env.comspec || 'cmd' shFlag = '/d /s /c' conf.windowsVerbatimArguments = true diff --git a/test/win32-set-path.js b/test/win32-set-path.js new file mode 100644 index 0000000..36842eb --- /dev/null +++ b/test/win32-set-path.js @@ -0,0 +1,39 @@ +// test that windows gets the expected path environ name +// use Path if present, then PATH, then any /^PATH$/i +const main = () => { + const { spawn } = require('child_process') + // [env keys, expect] + const cases = [ + [['Path', 'PATH', 'pAtH'], 'Path'], + [['path', 'PATH', 'pAtH'], 'pAtH'], + [['Path', 'PATH', 'htap'], 'Path'], + [['path', 'PATH', 'htap'], 'PATH'] + ] + + const t = require('tap') + t.plan(cases.length) + t.jobs = cases.length + cases.forEach(c => { + t.test(JSON.stringify(c), t => { + t.plan(1) + const proc = spawn(process.execPath, [__filename].concat(c[0])) + proc.stderr.pipe(process.stderr) + const out = [] + proc.stdout.on('data', d => out.push(d)) + proc.on('close', () => t.equal(Buffer.concat(out).toString().trim(), c[1])) + }) + }) +} + +const child = () => { + process.env = { + __TESTING_FAKE_PLATFORM__: 'win32', + [process.argv[2]]: 1, + [process.argv[3] || 'ignore']: 2, + [process.argv[4] || 'blerp']: 3 + } + + console.log(require('../')._pathEnvName) +} + +if (process.argv[2]) { child() } else { main() }