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(([resolve, tests]) => { 42 tests.forEach(([test, expected]) => { 43 const actual = resolve.apply(null, test); 44 let actualAlt; 45 const os = resolve === path.win32.resolve ? 'win32' : 'posix'; 46 if (resolve === path.win32.resolve && !common.isWindows) 47 actualAlt = actual.replace(backslashRE, '/'); 48 else if (resolve !== path.win32.resolve && common.isWindows) 49 actualAlt = actual.replace(slashRE, '\\'); 50 51 const message = 52 `path.${os}.resolve(${test.map(JSON.stringify).join(',')})\n expect=${ 53 JSON.stringify(expected)}\n actual=${JSON.stringify(actual)}`; 54 if (actual !== expected && actualAlt !== expected) 55 failures.push(message); 56 }); 57}); 58assert.strictEqual(failures.length, 0, failures.join('\n')); 59 60if (common.isWindows) { 61 // Test resolving the current Windows drive letter from a spawned process. 62 // See https://github.com/nodejs/node/issues/7215 63 const currentDriveLetter = path.parse(process.cwd()).root.substring(0, 2); 64 const resolveFixture = fixtures.path('path-resolve.js'); 65 const spawnResult = child.spawnSync( 66 process.argv[0], [resolveFixture, currentDriveLetter]); 67 const resolvedPath = spawnResult.stdout.toString().trim(); 68 assert.strictEqual(resolvedPath.toLowerCase(), process.cwd().toLowerCase()); 69} 70 71if (!common.isWindows) { 72 // Test handling relative paths to be safe when process.cwd() fails. 73 process.cwd = () => ''; 74 assert.strictEqual(process.cwd(), ''); 75 const resolved = path.resolve(); 76 const expected = '.'; 77 assert.strictEqual(resolved, expected); 78} 79