11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst path = require('path');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst failures = [];
71cb0ef41Sopenharmony_ciconst backslashRE = /\\/g;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst joinTests = [
101cb0ef41Sopenharmony_ci  [ [path.posix.join, path.win32.join],
111cb0ef41Sopenharmony_ci    // Arguments                     result
121cb0ef41Sopenharmony_ci    [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
131cb0ef41Sopenharmony_ci     [[], '.'],
141cb0ef41Sopenharmony_ci     [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
151cb0ef41Sopenharmony_ci     [['/foo', '../../../bar'], '/bar'],
161cb0ef41Sopenharmony_ci     [['foo', '../../../bar'], '../../bar'],
171cb0ef41Sopenharmony_ci     [['foo/', '../../../bar'], '../../bar'],
181cb0ef41Sopenharmony_ci     [['foo/x', '../../../bar'], '../bar'],
191cb0ef41Sopenharmony_ci     [['foo/x', './bar'], 'foo/x/bar'],
201cb0ef41Sopenharmony_ci     [['foo/x/', './bar'], 'foo/x/bar'],
211cb0ef41Sopenharmony_ci     [['foo/x/', '.', 'bar'], 'foo/x/bar'],
221cb0ef41Sopenharmony_ci     [['./'], './'],
231cb0ef41Sopenharmony_ci     [['.', './'], './'],
241cb0ef41Sopenharmony_ci     [['.', '.', '.'], '.'],
251cb0ef41Sopenharmony_ci     [['.', './', '.'], '.'],
261cb0ef41Sopenharmony_ci     [['.', '/./', '.'], '.'],
271cb0ef41Sopenharmony_ci     [['.', '/////./', '.'], '.'],
281cb0ef41Sopenharmony_ci     [['.'], '.'],
291cb0ef41Sopenharmony_ci     [['', '.'], '.'],
301cb0ef41Sopenharmony_ci     [['', 'foo'], 'foo'],
311cb0ef41Sopenharmony_ci     [['foo', '/bar'], 'foo/bar'],
321cb0ef41Sopenharmony_ci     [['', '/foo'], '/foo'],
331cb0ef41Sopenharmony_ci     [['', '', '/foo'], '/foo'],
341cb0ef41Sopenharmony_ci     [['', '', 'foo'], 'foo'],
351cb0ef41Sopenharmony_ci     [['foo', ''], 'foo'],
361cb0ef41Sopenharmony_ci     [['foo/', ''], 'foo/'],
371cb0ef41Sopenharmony_ci     [['foo', '', '/bar'], 'foo/bar'],
381cb0ef41Sopenharmony_ci     [['./', '..', '/foo'], '../foo'],
391cb0ef41Sopenharmony_ci     [['./', '..', '..', '/foo'], '../../foo'],
401cb0ef41Sopenharmony_ci     [['.', '..', '..', '/foo'], '../../foo'],
411cb0ef41Sopenharmony_ci     [['', '..', '..', '/foo'], '../../foo'],
421cb0ef41Sopenharmony_ci     [['/'], '/'],
431cb0ef41Sopenharmony_ci     [['/', '.'], '/'],
441cb0ef41Sopenharmony_ci     [['/', '..'], '/'],
451cb0ef41Sopenharmony_ci     [['/', '..', '..'], '/'],
461cb0ef41Sopenharmony_ci     [[''], '.'],
471cb0ef41Sopenharmony_ci     [['', ''], '.'],
481cb0ef41Sopenharmony_ci     [[' /foo'], ' /foo'],
491cb0ef41Sopenharmony_ci     [[' ', 'foo'], ' /foo'],
501cb0ef41Sopenharmony_ci     [[' ', '.'], ' '],
511cb0ef41Sopenharmony_ci     [[' ', '/'], ' /'],
521cb0ef41Sopenharmony_ci     [[' ', ''], ' '],
531cb0ef41Sopenharmony_ci     [['/', 'foo'], '/foo'],
541cb0ef41Sopenharmony_ci     [['/', '/foo'], '/foo'],
551cb0ef41Sopenharmony_ci     [['/', '//foo'], '/foo'],
561cb0ef41Sopenharmony_ci     [['/', '', '/foo'], '/foo'],
571cb0ef41Sopenharmony_ci     [['', '/', 'foo'], '/foo'],
581cb0ef41Sopenharmony_ci     [['', '/', '/foo'], '/foo'],
591cb0ef41Sopenharmony_ci    ],
601cb0ef41Sopenharmony_ci  ],
611cb0ef41Sopenharmony_ci];
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci// Windows-specific join tests
641cb0ef41Sopenharmony_cijoinTests.push([
651cb0ef41Sopenharmony_ci  path.win32.join,
661cb0ef41Sopenharmony_ci  joinTests[0][1].slice(0).concat(
671cb0ef41Sopenharmony_ci    [// Arguments                     result
681cb0ef41Sopenharmony_ci      // UNC path expected
691cb0ef41Sopenharmony_ci      [['//foo/bar'], '\\\\foo\\bar\\'],
701cb0ef41Sopenharmony_ci      [['\\/foo/bar'], '\\\\foo\\bar\\'],
711cb0ef41Sopenharmony_ci      [['\\\\foo/bar'], '\\\\foo\\bar\\'],
721cb0ef41Sopenharmony_ci      // UNC path expected - server and share separate
731cb0ef41Sopenharmony_ci      [['//foo', 'bar'], '\\\\foo\\bar\\'],
741cb0ef41Sopenharmony_ci      [['//foo/', 'bar'], '\\\\foo\\bar\\'],
751cb0ef41Sopenharmony_ci      [['//foo', '/bar'], '\\\\foo\\bar\\'],
761cb0ef41Sopenharmony_ci      // UNC path expected - questionable
771cb0ef41Sopenharmony_ci      [['//foo', '', 'bar'], '\\\\foo\\bar\\'],
781cb0ef41Sopenharmony_ci      [['//foo/', '', 'bar'], '\\\\foo\\bar\\'],
791cb0ef41Sopenharmony_ci      [['//foo/', '', '/bar'], '\\\\foo\\bar\\'],
801cb0ef41Sopenharmony_ci      // UNC path expected - even more questionable
811cb0ef41Sopenharmony_ci      [['', '//foo', 'bar'], '\\\\foo\\bar\\'],
821cb0ef41Sopenharmony_ci      [['', '//foo/', 'bar'], '\\\\foo\\bar\\'],
831cb0ef41Sopenharmony_ci      [['', '//foo/', '/bar'], '\\\\foo\\bar\\'],
841cb0ef41Sopenharmony_ci      // No UNC path expected (no double slash in first component)
851cb0ef41Sopenharmony_ci      [['\\', 'foo/bar'], '\\foo\\bar'],
861cb0ef41Sopenharmony_ci      [['\\', '/foo/bar'], '\\foo\\bar'],
871cb0ef41Sopenharmony_ci      [['', '/', '/foo/bar'], '\\foo\\bar'],
881cb0ef41Sopenharmony_ci      // No UNC path expected (no non-slashes in first component -
891cb0ef41Sopenharmony_ci      // questionable)
901cb0ef41Sopenharmony_ci      [['//', 'foo/bar'], '\\foo\\bar'],
911cb0ef41Sopenharmony_ci      [['//', '/foo/bar'], '\\foo\\bar'],
921cb0ef41Sopenharmony_ci      [['\\\\', '/', '/foo/bar'], '\\foo\\bar'],
931cb0ef41Sopenharmony_ci      [['//'], '\\'],
941cb0ef41Sopenharmony_ci      // No UNC path expected (share name missing - questionable).
951cb0ef41Sopenharmony_ci      [['//foo'], '\\foo'],
961cb0ef41Sopenharmony_ci      [['//foo/'], '\\foo\\'],
971cb0ef41Sopenharmony_ci      [['//foo', '/'], '\\foo\\'],
981cb0ef41Sopenharmony_ci      [['//foo', '', '/'], '\\foo\\'],
991cb0ef41Sopenharmony_ci      // No UNC path expected (too many leading slashes - questionable)
1001cb0ef41Sopenharmony_ci      [['///foo/bar'], '\\foo\\bar'],
1011cb0ef41Sopenharmony_ci      [['////foo', 'bar'], '\\foo\\bar'],
1021cb0ef41Sopenharmony_ci      [['\\\\\\/foo/bar'], '\\foo\\bar'],
1031cb0ef41Sopenharmony_ci      // Drive-relative vs drive-absolute paths. This merely describes the
1041cb0ef41Sopenharmony_ci      // status quo, rather than being obviously right
1051cb0ef41Sopenharmony_ci      [['c:'], 'c:.'],
1061cb0ef41Sopenharmony_ci      [['c:.'], 'c:.'],
1071cb0ef41Sopenharmony_ci      [['c:', ''], 'c:.'],
1081cb0ef41Sopenharmony_ci      [['', 'c:'], 'c:.'],
1091cb0ef41Sopenharmony_ci      [['c:.', '/'], 'c:.\\'],
1101cb0ef41Sopenharmony_ci      [['c:.', 'file'], 'c:file'],
1111cb0ef41Sopenharmony_ci      [['c:', '/'], 'c:\\'],
1121cb0ef41Sopenharmony_ci      [['c:', 'file'], 'c:\\file'],
1131cb0ef41Sopenharmony_ci    ]
1141cb0ef41Sopenharmony_ci  ),
1151cb0ef41Sopenharmony_ci]);
1161cb0ef41Sopenharmony_cijoinTests.forEach((test) => {
1171cb0ef41Sopenharmony_ci  if (!Array.isArray(test[0]))
1181cb0ef41Sopenharmony_ci    test[0] = [test[0]];
1191cb0ef41Sopenharmony_ci  test[0].forEach((join) => {
1201cb0ef41Sopenharmony_ci    test[1].forEach((test) => {
1211cb0ef41Sopenharmony_ci      const actual = join.apply(null, test[0]);
1221cb0ef41Sopenharmony_ci      const expected = test[1];
1231cb0ef41Sopenharmony_ci      // For non-Windows specific tests with the Windows join(), we need to try
1241cb0ef41Sopenharmony_ci      // replacing the slashes since the non-Windows specific tests' `expected`
1251cb0ef41Sopenharmony_ci      // use forward slashes
1261cb0ef41Sopenharmony_ci      let actualAlt;
1271cb0ef41Sopenharmony_ci      let os;
1281cb0ef41Sopenharmony_ci      if (join === path.win32.join) {
1291cb0ef41Sopenharmony_ci        actualAlt = actual.replace(backslashRE, '/');
1301cb0ef41Sopenharmony_ci        os = 'win32';
1311cb0ef41Sopenharmony_ci      } else {
1321cb0ef41Sopenharmony_ci        os = 'posix';
1331cb0ef41Sopenharmony_ci      }
1341cb0ef41Sopenharmony_ci      if (actual !== expected && actualAlt !== expected) {
1351cb0ef41Sopenharmony_ci        const delimiter = test[0].map(JSON.stringify).join(',');
1361cb0ef41Sopenharmony_ci        const message = `path.${os}.join(${delimiter})\n  expect=${
1371cb0ef41Sopenharmony_ci          JSON.stringify(expected)}\n  actual=${JSON.stringify(actual)}`;
1381cb0ef41Sopenharmony_ci        failures.push(`\n${message}`);
1391cb0ef41Sopenharmony_ci      }
1401cb0ef41Sopenharmony_ci    });
1411cb0ef41Sopenharmony_ci  });
1421cb0ef41Sopenharmony_ci});
1431cb0ef41Sopenharmony_ciassert.strictEqual(failures.length, 0, failures.join(''));
144