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