• 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';
23
24const common = require('../common');
25const tmpdir = require('../common/tmpdir');
26tmpdir.refresh();
27
28const assert = require('assert');
29const { spawn } = require('child_process');
30
31// Spawns 'pwd' with given options, then test
32// - whether the child pid is undefined or number,
33// - whether the exit code equals expectCode,
34// - optionally whether the trimmed stdout result matches expectData
35function testCwd(options, expectPidType, expectCode = 0, expectData) {
36  const child = spawn(...common.pwdCommand, options);
37
38  assert.strictEqual(typeof child.pid, expectPidType);
39
40  child.stdout.setEncoding('utf8');
41
42  // No need to assert callback since `data` is asserted.
43  let data = '';
44  child.stdout.on('data', function(chunk) {
45    data += chunk;
46  });
47
48  // Can't assert callback, as stayed in to API:
49  // _The 'exit' event may or may not fire after an error has occurred._
50  child.on('exit', function(code, signal) {
51    assert.strictEqual(code, expectCode);
52  });
53
54  child.on('close', common.mustCall(function() {
55    expectData && assert.strictEqual(data.trim(), expectData);
56  }));
57
58  return child;
59}
60
61
62// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
63{
64  testCwd({ cwd: 'does-not-exist' }, 'undefined', -1)
65    .on('error', common.mustCall(function(e) {
66      assert.strictEqual(e.code, 'ENOENT');
67    }));
68}
69
70{
71  assert.throws(() => {
72    testCwd({
73      cwd: new URL('http://example.com/'),
74    }, 'number', 0, tmpdir.path);
75  }, /The URL must be of scheme file/);
76
77  if (process.platform !== 'win32') {
78    assert.throws(() => {
79      testCwd({
80        cwd: new URL('file://host/dev/null'),
81      }, 'number', 0, tmpdir.path);
82    }, /File URL host must be "localhost" or empty on/);
83  }
84}
85
86// Assume these exist, and 'pwd' gives us the right directory back
87testCwd({ cwd: tmpdir.path }, 'number', 0, tmpdir.path);
88const shouldExistDir = common.isWindows ? process.env.windir : '/dev';
89testCwd({ cwd: shouldExistDir }, 'number', 0, shouldExistDir);
90testCwd({ cwd: tmpdir.fileURL() }, 'number', 0, tmpdir.path);
91
92// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
93testCwd({ cwd: '' }, 'number');
94testCwd({ cwd: undefined }, 'number');
95testCwd({ cwd: null }, 'number');
96