• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const fixtures = require('../common/fixtures');
4const assert = require('assert');
5const child = require('child_process');
6const path = require('path');
7
8const failures = [];
9const slashRE = /\//g;
10const backslashRE = /\\/g;
11
12const resolveTests = [
13  [ path.win32.resolve,
14    // Arguments                               result
15    [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
16     [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
17     [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
18     [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
19     [['.'], process.cwd()],
20     [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
21     [['c:/', '//'], 'c:\\'],
22     [['c:/', '//dir'], 'c:\\dir'],
23     [['c:/', '//server/share'], '\\\\server\\share\\'],
24     [['c:/', '//server//share'], '\\\\server\\share\\'],
25     [['c:/', '///some//dir'], 'c:\\some\\dir'],
26     [['C:\\foo\\tmp.3\\', '..\\tmp.3\\cycles\\root.js'],
27      'C:\\foo\\tmp.3\\cycles\\root.js']
28    ]
29  ],
30  [ path.posix.resolve,
31    // Arguments                    result
32    [[['/var/lib', '../', 'file/'], '/var/file'],
33     [['/var/lib', '/../', 'file/'], '/file'],
34     [['a/b/c/', '../../..'], process.cwd()],
35     [['.'], process.cwd()],
36     [['/some/dir', '.', '/absolute/'], '/absolute'],
37     [['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js']
38    ]
39  ]
40];
41resolveTests.forEach((test) => {
42  const resolve = test[0];
43  test[1].forEach((test) => {
44    const actual = resolve.apply(null, test[0]);
45    let actualAlt;
46    const os = resolve === path.win32.resolve ? 'win32' : 'posix';
47    if (resolve === path.win32.resolve && !common.isWindows)
48      actualAlt = actual.replace(backslashRE, '/');
49    else if (resolve !== path.win32.resolve && common.isWindows)
50      actualAlt = actual.replace(slashRE, '\\');
51
52    const expected = test[1];
53    const message =
54      `path.${os}.resolve(${test[0].map(JSON.stringify).join(',')})\n  expect=${
55        JSON.stringify(expected)}\n  actual=${JSON.stringify(actual)}`;
56    if (actual !== expected && actualAlt !== expected)
57      failures.push(`\n${message}`);
58  });
59});
60assert.strictEqual(failures.length, 0, failures.join(''));
61
62if (common.isWindows) {
63  // Test resolving the current Windows drive letter from a spawned process.
64  // See https://github.com/nodejs/node/issues/7215
65  const currentDriveLetter = path.parse(process.cwd()).root.substring(0, 2);
66  const resolveFixture = fixtures.path('path-resolve.js');
67  const spawnResult = child.spawnSync(
68    process.argv[0], [resolveFixture, currentDriveLetter]);
69  const resolvedPath = spawnResult.stdout.toString().trim();
70  assert.strictEqual(resolvedPath.toLowerCase(), process.cwd().toLowerCase());
71}
72
73if (!common.isWindows) {
74  // Test handling relative paths to be safe when process.cwd() fails.
75  process.cwd = () => '';
76  assert.strictEqual(process.cwd(), '');
77  const resolved = path.resolve();
78  const expected = '.';
79  assert.strictEqual(resolved, expected);
80}
81