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

Commit d391b04

Browse files
committed
test: add test for win32 path environ selection
1 parent 5523951 commit d391b04

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ exports = module.exports = lifecycle
44
exports.makeEnv = makeEnv
55
exports._incorrectWorkingDirectory = _incorrectWorkingDirectory
66

7+
// for testing
8+
const platform = process.env.__TESTING_FAKE_PLATFORM__ || process.platform
9+
const isWindows = platform === 'win32'
710
const spawn = require('./lib/spawn')
811
const path = require('path')
912
const Stream = require('stream').Stream
@@ -20,8 +23,8 @@ const hookStatCache = new Map()
2023

2124
let PATH = 'PATH'
2225

23-
// windows calls it's path 'Path' usually, but this is not guaranteed.
24-
if (process.platform === 'win32') {
26+
// windows calls its path 'Path' usually, but this is not guaranteed.
27+
if (isWindows) {
2528
PATH = 'Path'
2629
if (!process.env[PATH]) {
2730
Object.keys(process.env).forEach(function (e) {
@@ -32,6 +35,8 @@ if (process.platform === 'win32') {
3235
}
3336
}
3437

38+
exports._pathEnvName = PATH
39+
3540
function logid (pkg, stage) {
3641
return pkg._id + '~' + stage + ':'
3742
}
@@ -123,7 +128,7 @@ function lifecycle_ (pkg, stage, wd, opts, env, cb) {
123128
}
124129

125130
if (env[PATH]) pathArr.push(env[PATH])
126-
env[PATH] = pathArr.join(process.platform === 'win32' ? ';' : ':')
131+
env[PATH] = pathArr.join(isWindows ? ';' : ':')
127132

128133
var packageLifecycle = pkg.scripts && pkg.scripts.hasOwnProperty(stage)
129134

@@ -166,7 +171,6 @@ function shouldPrependCurrentNodeDirToPATH (opts) {
166171

167172
var isDifferentNodeInPath
168173

169-
var isWindows = process.platform === 'win32'
170174
var foundExecPath
171175
try {
172176
foundExecPath = which.sync(path.basename(process.execPath), { pathExt: isWindows ? ';' : ':' })
@@ -244,7 +248,7 @@ function runCmd (note, cmd, pkg, env, stage, wd, opts, cb) {
244248
}
245249
opts.log.verbose('lifecycle', logid(pkg, stage), 'unsafe-perm in lifecycle', unsafe)
246250

247-
if (process.platform === 'win32') {
251+
if (isWindows) {
248252
unsafe = true
249253
}
250254

@@ -282,7 +286,7 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
282286

283287
if (customShell) {
284288
sh = customShell
285-
} else if (process.platform === 'win32') {
289+
} else if (isWindows) {
286290
sh = process.env.comspec || 'cmd'
287291
shFlag = '/d /s /c'
288292
conf.windowsVerbatimArguments = true

test/win32-set-path.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// test that windows gets the expected path environ name
2+
// use Path if present, then PATH, then any /^PATH$/i
3+
const main = () => {
4+
const { spawn } = require('child_process')
5+
// [env keys, expect]
6+
const cases = [
7+
[['Path', 'PATH', 'pAtH'], 'Path'],
8+
[['path', 'PATH', 'pAtH'], 'pAtH'],
9+
[['Path', 'PATH', 'htap'], 'Path'],
10+
[['path', 'PATH', 'htap'], 'PATH']
11+
]
12+
13+
const t = require('tap')
14+
t.plan(cases.length)
15+
t.jobs = cases.length
16+
cases.forEach(c => {
17+
t.test(JSON.stringify(c), t => {
18+
t.plan(1)
19+
const proc = spawn(process.execPath, [__filename].concat(c[0]))
20+
proc.stderr.pipe(process.stderr)
21+
const out = []
22+
proc.stdout.on('data', d => out.push(d))
23+
proc.on('close', () => t.equal(Buffer.concat(out).toString().trim(), c[1]))
24+
})
25+
})
26+
}
27+
28+
const child = () => {
29+
process.env = {
30+
__TESTING_FAKE_PLATFORM__: 'win32',
31+
[process.argv[2]]: 1,
32+
[process.argv[3] || 'ignore']: 2,
33+
[process.argv[4] || 'blerp']: 3
34+
}
35+
36+
console.log(require('../')._pathEnvName)
37+
}
38+
39+
if (process.argv[2]) { child() } else { main() }

0 commit comments

Comments
 (0)