Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
test: add test for win32 path environ selection
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 17, 2019
1 parent 5523951 commit d391b04
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -32,6 +35,8 @@ if (process.platform === 'win32') {
}
}

exports._pathEnvName = PATH

function logid (pkg, stage) {
return pkg._id + '~' + stage + ':'
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 ? ';' : ':' })
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/win32-set-path.js
Original file line number Diff line number Diff line change
@@ -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() }

0 comments on commit d391b04

Please sign in to comment.