• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const tmpdir = require('../common/tmpdir');
25tmpdir.refresh();
26
27const assert = require('assert');
28const { spawn } = require('child_process');
29
30/*
31  Spawns 'pwd' with given options, then test
32  - whether the exit code equals expectCode,
33  - optionally whether the trimmed stdout result matches expectData
34*/
35function testCwd(options, expectCode = 0, expectData) {
36  const child = spawn(...common.pwdCommand, options);
37
38  child.stdout.setEncoding('utf8');
39
40  // No need to assert callback since `data` is asserted.
41  let data = '';
42  child.stdout.on('data', function(chunk) {
43    data += chunk;
44  });
45
46  // Can't assert callback, as stayed in to API:
47  // _The 'exit' event may or may not fire after an error has occurred._
48  child.on('exit', function(code, signal) {
49    assert.strictEqual(code, expectCode);
50  });
51
52  child.on('close', common.mustCall(function() {
53    expectData && assert.strictEqual(data.trim(), expectData);
54  }));
55
56  return child;
57}
58
59
60// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
61{
62  testCwd({ cwd: 'does-not-exist' }, -1)
63    .on('error', common.mustCall(function(e) {
64      assert.strictEqual(e.code, 'ENOENT');
65    }));
66}
67
68// Assume these exist, and 'pwd' gives us the right directory back
69testCwd({ cwd: tmpdir.path }, 0, tmpdir.path);
70const shouldExistDir = common.isWindows ? process.env.windir : '/dev';
71testCwd({ cwd: shouldExistDir }, 0, shouldExistDir);
72
73// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
74testCwd({ cwd: '' });
75testCwd({ cwd: undefined });
76testCwd({ cwd: null });
77