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