Expose cwd option to child_process.exec()
authorBert Belder <bertbelder@gmail.com>
Thu, 5 Aug 2010 04:06:46 +0000 (06:06 +0200)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 6 Aug 2010 20:38:41 +0000 (13:38 -0700)
doc/api.markdown
lib/child_process.js
test/simple/test-child-process-exec-cwd.js [new file with mode: 0644]

index 403bc66..4e7b011 100644 (file)
@@ -1054,6 +1054,7 @@ There is a second optional argument to specify several options. The default opti
     , timeout: 0
     , maxBuffer: 200*1024
     , killSignal: 'SIGKILL'
+    , cwd: null
     , env: null
     }
 
index 54bb667..d19a0c0 100644 (file)
@@ -28,6 +28,7 @@ exports.execFile = function (file /* args, options, callback */) {
                 , timeout: 0
                 , maxBuffer: 200*1024
                 , killSignal: 'SIGKILL'
+                , cwd: null
                 , env: null
                 };
   var args, optionArg, callback;
@@ -55,7 +56,7 @@ exports.execFile = function (file /* args, options, callback */) {
     }
   }
 
-  var child = spawn(file, args, {env: options.env});
+  var child = spawn(file, args, {cwd: options.cwd, env: options.env});
   var stdout = "";
   var stderr = "";
   var killed = false;
diff --git a/test/simple/test-child-process-exec-cwd.js b/test/simple/test-child-process-exec-cwd.js
new file mode 100644 (file)
index 0000000..20d73b2
--- /dev/null
@@ -0,0 +1,24 @@
+require('../common');
+var assert = require('assert');
+var exec   = require('child_process').exec;
+
+var success_count = 0;
+var error_count = 0;
+
+child = exec('pwd', {cwd: "/dev"}, function (err, stdout, stderr) {
+  if (err) {
+    error_count++;
+    console.log('error!: ' + err.code);
+    console.log('stdout: ' + JSON.stringify(stdout));
+    console.log('stderr: ' + JSON.stringify(stderr));
+    assert.equal(false, err.killed);
+  } else {
+    success_count++;
+    assert.equal(true, /^\/dev\b/.test(stdout));
+  }
+});
+
+process.addListener('exit', function () {
+  assert.equal(1, success_count);
+  assert.equal(0, error_count);
+});
\ No newline at end of file