test-path-join.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. var tape = require('tape');
  3. var path = require('../');
  4. var backslashRE = /\\/g;
  5. var joinTests =
  6. // arguments result
  7. [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
  8. [[], '.'],
  9. [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
  10. [['/foo', '../../../bar'], '/bar'],
  11. [['foo', '../../../bar'], '../../bar'],
  12. [['foo/', '../../../bar'], '../../bar'],
  13. [['foo/x', '../../../bar'], '../bar'],
  14. [['foo/x', './bar'], 'foo/x/bar'],
  15. [['foo/x/', './bar'], 'foo/x/bar'],
  16. [['foo/x/', '.', 'bar'], 'foo/x/bar'],
  17. [['./'], './'],
  18. [['.', './'], './'],
  19. [['.', '.', '.'], '.'],
  20. [['.', './', '.'], '.'],
  21. [['.', '/./', '.'], '.'],
  22. [['.', '/////./', '.'], '.'],
  23. [['.'], '.'],
  24. [['', '.'], '.'],
  25. [['', 'foo'], 'foo'],
  26. [['foo', '/bar'], 'foo/bar'],
  27. [['', '/foo'], '/foo'],
  28. [['', '', '/foo'], '/foo'],
  29. [['', '', 'foo'], 'foo'],
  30. [['foo', ''], 'foo'],
  31. [['foo/', ''], 'foo/'],
  32. [['foo', '', '/bar'], 'foo/bar'],
  33. [['./', '..', '/foo'], '../foo'],
  34. [['./', '..', '..', '/foo'], '../../foo'],
  35. [['.', '..', '..', '/foo'], '../../foo'],
  36. [['', '..', '..', '/foo'], '../../foo'],
  37. [['/'], '/'],
  38. [['/', '.'], '/'],
  39. [['/', '..'], '/'],
  40. [['/', '..', '..'], '/'],
  41. [[''], '.'],
  42. [['', ''], '.'],
  43. [[' /foo'], ' /foo'],
  44. [[' ', 'foo'], ' /foo'],
  45. [[' ', '.'], ' '],
  46. [[' ', '/'], ' /'],
  47. [[' ', ''], ' '],
  48. [['/', 'foo'], '/foo'],
  49. [['/', '/foo'], '/foo'],
  50. [['/', '//foo'], '/foo'],
  51. [['/', '', '/foo'], '/foo'],
  52. [['', '/', 'foo'], '/foo'],
  53. [['', '/', '/foo'], '/foo']
  54. ];
  55. // Windows-specific join tests
  56. var windowsJoinTests =
  57. [// arguments result
  58. // UNC path expected
  59. [['//foo/bar'], '\\\\foo\\bar\\'],
  60. [['\\/foo/bar'], '\\\\foo\\bar\\'],
  61. [['\\\\foo/bar'], '\\\\foo\\bar\\'],
  62. // UNC path expected - server and share separate
  63. [['//foo', 'bar'], '\\\\foo\\bar\\'],
  64. [['//foo/', 'bar'], '\\\\foo\\bar\\'],
  65. [['//foo', '/bar'], '\\\\foo\\bar\\'],
  66. // UNC path expected - questionable
  67. [['//foo', '', 'bar'], '\\\\foo\\bar\\'],
  68. [['//foo/', '', 'bar'], '\\\\foo\\bar\\'],
  69. [['//foo/', '', '/bar'], '\\\\foo\\bar\\'],
  70. // UNC path expected - even more questionable
  71. [['', '//foo', 'bar'], '\\\\foo\\bar\\'],
  72. [['', '//foo/', 'bar'], '\\\\foo\\bar\\'],
  73. [['', '//foo/', '/bar'], '\\\\foo\\bar\\'],
  74. // No UNC path expected (no double slash in first component)
  75. [['\\', 'foo/bar'], '\\foo\\bar'],
  76. [['\\', '/foo/bar'], '\\foo\\bar'],
  77. [['', '/', '/foo/bar'], '\\foo\\bar'],
  78. // No UNC path expected (no non-slashes in first component -
  79. // questionable)
  80. [['//', 'foo/bar'], '\\foo\\bar'],
  81. [['//', '/foo/bar'], '\\foo\\bar'],
  82. [['\\\\', '/', '/foo/bar'], '\\foo\\bar'],
  83. [['//'], '/'],
  84. // No UNC path expected (share name missing - questionable).
  85. [['//foo'], '\\foo'],
  86. [['//foo/'], '\\foo\\'],
  87. [['//foo', '/'], '\\foo\\'],
  88. [['//foo', '', '/'], '\\foo\\'],
  89. // No UNC path expected (too many leading slashes - questionable)
  90. [['///foo/bar'], '\\foo\\bar'],
  91. [['////foo', 'bar'], '\\foo\\bar'],
  92. [['\\\\\\/foo/bar'], '\\foo\\bar'],
  93. // Drive-relative vs drive-absolute paths. This merely describes the
  94. // status quo, rather than being obviously right
  95. [['c:'], 'c:.'],
  96. [['c:.'], 'c:.'],
  97. [['c:', ''], 'c:.'],
  98. [['', 'c:'], 'c:.'],
  99. [['c:.', '/'], 'c:.\\'],
  100. [['c:.', 'file'], 'c:file'],
  101. [['c:', '/'], 'c:\\'],
  102. [['c:', 'file'], 'c:\\file']
  103. ];
  104. tape('path.posix.join', function (t) {
  105. joinTests.forEach(function (p) {
  106. var actual = path.posix.join.apply(null, p[0]);
  107. t.strictEqual(actual, p[1]);
  108. });
  109. t.end();
  110. });
  111. tape('path.win32.join', { skip: true }, function (t) {
  112. joinTests.forEach(function (p) {
  113. var actual = path.win32.join.apply(null, p[0]).replace(backslashRE, '/');
  114. t.strictEqual(actual, p[1]);
  115. });
  116. windowsJoinTests.forEach(function (p) {
  117. var actual = path.win32.join.apply(null, p[0]);
  118. t.strictEqual(actual, p[1]);
  119. });
  120. t.end();
  121. });