summaryrefslogtreecommitdiff
path: root/test/simple/test-child-process-cwd.js
blob: ce4eb0133aa228dfef917f1ac3b79f9f97c3198e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var common = require('../common');
var assert = require('assert');
var spawn = require('child_process').spawn;
var path = require('path');

var returns = 0;

/*
  Spawns 'pwd' with given options, then test
  - whether the exit code equals forCode,
  - optionally whether the stdout result matches forData
    (after removing traling whitespace)
*/
function testCwd(options, forCode, forData) {
  var data = '';

  var child = spawn('pwd', [], options);
  child.stdout.setEncoding('utf8');

  child.stdout.addListener('data', function(chunk) {
    data += chunk;
  });

  child.addListener('exit', function(code, signal) {
    forData && assert.strictEqual(forData, data.replace(/[\s\r\n]+$/, ''));
    assert.strictEqual(forCode, code);
    returns--;
  });

  returns++;
}

// Assume these exist, and 'pwd' gives us the right directory back
testCwd({cwd: '/dev'}, 0, '/dev');
testCwd({cwd: '/'}, 0, '/');

// Assume this doesn't exist, we expect exitcode=127
testCwd({cwd: 'does-not-exist'}, 127);

// Spawn() shouldn't try to chdir() so this should just work
testCwd(undefined, 0);
testCwd({}, 0);
testCwd({cwd: ''}, 0);
testCwd({cwd: undefined}, 0);
testCwd({cwd: null}, 0);

// Check whether all tests actually returned
assert.notEqual(0, returns);
process.addListener('exit', function() {
  assert.equal(0, returns);
});