Skip to content

Commit

Permalink
npm-run: fix commands with whitespaces on windows
Browse files Browse the repository at this point in the history
Fixes npm#4403
  • Loading branch information
robertkowalski authored and domenic committed Jan 11, 2014
1 parent 10a2867 commit 80282ed
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/utils/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,6 @@ function runCmd_ (cmd, pkg, env, wd, stage, unsafe, uid, gid, cb_) {
process.nextTick(dequeue)
}

var sh = "sh"
var shFlag = "-c"

if (process.platform === "win32") {
sh = "cmd"
shFlag = "/c"
}

var conf = { cwd: wd
, env: env
, stdio: [ 0, 1, 2 ]
Expand All @@ -203,6 +195,15 @@ function runCmd_ (cmd, pkg, env, wd, stage, unsafe, uid, gid, cb_) {
conf.gid = gid ^ 0
}

var sh = "sh"
var shFlag = "-c"

if (process.platform === "win32") {
sh = "cmd"
shFlag = "/c"
conf.windowsVerbatimArguments = true
}

var proc = spawn(sh, [shFlag, cmd], conf)
proc.on("close", function (code, signal) {
if (signal) {
Expand Down
71 changes: 71 additions & 0 deletions test/tap/scripts-whitespace-windows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var test = require('tap').test
var path = require('path')
var npm = path.resolve(__dirname, '../../cli.js')
var pkg = __dirname + '/scripts-whitespace-windows'
var tmp = pkg + '/tmp'
var cache = pkg + '/cache'
var modules = pkg + '/node_modules'
var dep = pkg + '/dep'

var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var node = process.execPath
var spawn = require('child_process').spawn

test('setup', function (t) {
mkdirp.sync(cache)
mkdirp.sync(tmp)
rimraf.sync(modules)

var env = {
npm_config_cache: cache,
npm_config_tmp: tmp,
npm_config_prefix: pkg,
npm_config_global: 'false'
}

var child = spawn(node, [npm, 'i', dep], {
cwd: pkg,
env: env
})

child.stdout.setEncoding('utf8')
child.stderr.on('data', function(chunk) {
throw new Error('got stderr data: ' + JSON.stringify('' + chunk))
})
child.on('close', function () {
t.end()
})
})

test('test', function (t) {

var child = spawn(node, [npm, 'run', 'foo'], {
cwd: pkg,
env: process.env
})

child.stdout.setEncoding('utf8')
child.stderr.on('data', function(chunk) {
throw new Error('got stderr data: ' + JSON.stringify('' + chunk))
})
child.stdout.on('data', ondata)
child.on('close', onend)
var c = ''
function ondata (chunk) {
c += chunk
}
function onend () {
c = c.trim()

t.ok(/npm-test-fine/.test(c))
t.end()
}
})

test('cleanup', function (t) {
rimraf.sync(cache)
rimraf.sync(tmp)
rimraf.sync(modules)
t.end()
})
1 change: 1 addition & 0 deletions test/tap/scripts-whitespace-windows/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hi
1 change: 1 addition & 0 deletions test/tap/scripts-whitespace-windows/dep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hi!
4 changes: 4 additions & 0 deletions test/tap/scripts-whitespace-windows/dep/bin/foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

if (process.argv.length === 8)
console.log('npm-test-fine')
6 changes: 6 additions & 0 deletions test/tap/scripts-whitespace-windows/dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "scripts-whitespace-windows-dep",
"version": "0.0.1",
"bin": [ "bin/foo" ],
"repository": "git://github.com/robertkowalski/bogusfixture"
}
11 changes: 11 additions & 0 deletions test/tap/scripts-whitespace-windows/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "scripts-whitespace-windows",
"scripts": {
"foo": "foo --title \"Analysis of\" --recurse -d report src"
},
"description": "a test",
"repository": "git://github.com/robertkowalski/bogus",
"dependencies": {
"scripts-whitespace-windows-dep": "0.0.1"
}
}

0 comments on commit 80282ed

Please sign in to comment.