11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst child = require('child_process');
61cb0ef41Sopenharmony_ciconst path = require('path');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst failures = [];
91cb0ef41Sopenharmony_ciconst slashRE = /\//g;
101cb0ef41Sopenharmony_ciconst backslashRE = /\\/g;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst posixyCwd = common.isWindows ?
131cb0ef41Sopenharmony_ci  (() => {
141cb0ef41Sopenharmony_ci    const _ = process.cwd()
151cb0ef41Sopenharmony_ci      .replaceAll(path.sep, path.posix.sep);
161cb0ef41Sopenharmony_ci    return _.slice(_.indexOf(path.posix.sep));
171cb0ef41Sopenharmony_ci  })() :
181cb0ef41Sopenharmony_ci  process.cwd();
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst resolveTests = [
211cb0ef41Sopenharmony_ci  [ path.win32.resolve,
221cb0ef41Sopenharmony_ci    // Arguments                               result
231cb0ef41Sopenharmony_ci    [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
241cb0ef41Sopenharmony_ci     [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
251cb0ef41Sopenharmony_ci     [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
261cb0ef41Sopenharmony_ci     [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
271cb0ef41Sopenharmony_ci     [['.'], process.cwd()],
281cb0ef41Sopenharmony_ci     [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
291cb0ef41Sopenharmony_ci     [['c:/', '//'], 'c:\\'],
301cb0ef41Sopenharmony_ci     [['c:/', '//dir'], 'c:\\dir'],
311cb0ef41Sopenharmony_ci     [['c:/', '//server/share'], '\\\\server\\share\\'],
321cb0ef41Sopenharmony_ci     [['c:/', '//server//share'], '\\\\server\\share\\'],
331cb0ef41Sopenharmony_ci     [['c:/', '///some//dir'], 'c:\\some\\dir'],
341cb0ef41Sopenharmony_ci     [['C:\\foo\\tmp.3\\', '..\\tmp.3\\cycles\\root.js'],
351cb0ef41Sopenharmony_ci      'C:\\foo\\tmp.3\\cycles\\root.js'],
361cb0ef41Sopenharmony_ci    ],
371cb0ef41Sopenharmony_ci  ],
381cb0ef41Sopenharmony_ci  [ path.posix.resolve,
391cb0ef41Sopenharmony_ci    // Arguments                    result
401cb0ef41Sopenharmony_ci    [[['/var/lib', '../', 'file/'], '/var/file'],
411cb0ef41Sopenharmony_ci     [['/var/lib', '/../', 'file/'], '/file'],
421cb0ef41Sopenharmony_ci     [['a/b/c/', '../../..'], posixyCwd],
431cb0ef41Sopenharmony_ci     [['.'], posixyCwd],
441cb0ef41Sopenharmony_ci     [['/some/dir', '.', '/absolute/'], '/absolute'],
451cb0ef41Sopenharmony_ci     [['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js'],
461cb0ef41Sopenharmony_ci    ],
471cb0ef41Sopenharmony_ci  ],
481cb0ef41Sopenharmony_ci];
491cb0ef41Sopenharmony_ciresolveTests.forEach(([resolve, tests]) => {
501cb0ef41Sopenharmony_ci  tests.forEach(([test, expected]) => {
511cb0ef41Sopenharmony_ci    const actual = resolve.apply(null, test);
521cb0ef41Sopenharmony_ci    let actualAlt;
531cb0ef41Sopenharmony_ci    const os = resolve === path.win32.resolve ? 'win32' : 'posix';
541cb0ef41Sopenharmony_ci    if (resolve === path.win32.resolve && !common.isWindows)
551cb0ef41Sopenharmony_ci      actualAlt = actual.replace(backslashRE, '/');
561cb0ef41Sopenharmony_ci    else if (resolve !== path.win32.resolve && common.isWindows)
571cb0ef41Sopenharmony_ci      actualAlt = actual.replace(slashRE, '\\');
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    const message =
601cb0ef41Sopenharmony_ci      `path.${os}.resolve(${test.map(JSON.stringify).join(',')})\n  expect=${
611cb0ef41Sopenharmony_ci        JSON.stringify(expected)}\n  actual=${JSON.stringify(actual)}`;
621cb0ef41Sopenharmony_ci    if (actual !== expected && actualAlt !== expected)
631cb0ef41Sopenharmony_ci      failures.push(message);
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci});
661cb0ef41Sopenharmony_ciassert.strictEqual(failures.length, 0, failures.join('\n'));
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciif (common.isWindows) {
691cb0ef41Sopenharmony_ci  // Test resolving the current Windows drive letter from a spawned process.
701cb0ef41Sopenharmony_ci  // See https://github.com/nodejs/node/issues/7215
711cb0ef41Sopenharmony_ci  const currentDriveLetter = path.parse(process.cwd()).root.substring(0, 2);
721cb0ef41Sopenharmony_ci  const resolveFixture = fixtures.path('path-resolve.js');
731cb0ef41Sopenharmony_ci  const spawnResult = child.spawnSync(
741cb0ef41Sopenharmony_ci    process.argv[0], [resolveFixture, currentDriveLetter]);
751cb0ef41Sopenharmony_ci  const resolvedPath = spawnResult.stdout.toString().trim();
761cb0ef41Sopenharmony_ci  assert.strictEqual(resolvedPath.toLowerCase(), process.cwd().toLowerCase());
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciif (!common.isWindows) {
801cb0ef41Sopenharmony_ci  // Test handling relative paths to be safe when process.cwd() fails.
811cb0ef41Sopenharmony_ci  process.cwd = () => '';
821cb0ef41Sopenharmony_ci  assert.strictEqual(process.cwd(), '');
831cb0ef41Sopenharmony_ci  const resolved = path.resolve();
841cb0ef41Sopenharmony_ci  const expected = '.';
851cb0ef41Sopenharmony_ci  assert.strictEqual(resolved, expected);
861cb0ef41Sopenharmony_ci}
87