test-path-parse-format.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. 'use strict';
  22. var tape = require('tape');
  23. var path = require('../');
  24. var winPaths = [
  25. // [path, root]
  26. ['C:\\path\\dir\\index.html', 'C:\\'],
  27. ['C:\\another_path\\DIR\\1\\2\\33\\\\index', 'C:\\'],
  28. ['another_path\\DIR with spaces\\1\\2\\33\\index', ''],
  29. ['\\', '\\'],
  30. ['\\foo\\C:', '\\'],
  31. ['file', ''],
  32. ['file:stream', ''],
  33. ['.\\file', ''],
  34. ['C:', 'C:'],
  35. ['C:.', 'C:'],
  36. ['C:..', 'C:'],
  37. ['C:abc', 'C:'],
  38. ['C:\\', 'C:\\'],
  39. ['C:\\abc', 'C:\\' ],
  40. ['', ''],
  41. // unc
  42. ['\\\\server\\share\\file_path', '\\\\server\\share\\'],
  43. ['\\\\server two\\shared folder\\file path.zip',
  44. '\\\\server two\\shared folder\\'],
  45. ['\\\\teela\\admin$\\system32', '\\\\teela\\admin$\\'],
  46. ['\\\\?\\UNC\\server\\share', '\\\\?\\UNC\\']
  47. ];
  48. var winSpecialCaseParseTests = [
  49. ['/foo/bar', { root: '/' }],
  50. ];
  51. var winSpecialCaseFormatTests = [
  52. [{ dir: 'some\\dir' }, 'some\\dir\\'],
  53. [{ base: 'index.html' }, 'index.html'],
  54. [{ root: 'C:\\' }, 'C:\\'],
  55. [{ name: 'index', ext: '.html' }, 'index.html'],
  56. [{ dir: 'some\\dir', name: 'index', ext: '.html' }, 'some\\dir\\index.html'],
  57. [{ root: 'C:\\', name: 'index', ext: '.html' }, 'C:\\index.html'],
  58. [{}, '']
  59. ];
  60. var unixPaths = [
  61. // [path, root]
  62. ['/home/user/dir/file.txt', '/'],
  63. ['/home/user/a dir/another File.zip', '/'],
  64. ['/home/user/a dir//another&File.', '/'],
  65. ['/home/user/a$$$dir//another File.zip', '/'],
  66. ['user/dir/another File.zip', ''],
  67. ['file', ''],
  68. ['.\\file', ''],
  69. ['./file', ''],
  70. ['C:\\foo', ''],
  71. ['/', '/'],
  72. ['', ''],
  73. ['.', ''],
  74. ['..', ''],
  75. ['/foo', '/'],
  76. ['/foo.', '/'],
  77. ['/foo.bar', '/'],
  78. ['/.', '/'],
  79. ['/.foo', '/'],
  80. ['/.foo.bar', '/'],
  81. ['/foo/bar.baz', '/']
  82. ];
  83. var unixSpecialCaseFormatTests = [
  84. [{ dir: 'some/dir' }, 'some/dir/'],
  85. [{ base: 'index.html' }, 'index.html'],
  86. [{ root: '/' }, '/'],
  87. [{ name: 'index', ext: '.html' }, 'index.html'],
  88. [{ dir: 'some/dir', name: 'index', ext: '.html' }, 'some/dir/index.html'],
  89. [{ root: '/', name: 'index', ext: '.html' }, '/index.html'],
  90. [{}, '']
  91. ];
  92. var errors = [
  93. { method: 'parse', input: [null], message: TypeError },
  94. { method: 'parse', input: [{}], message: TypeError },
  95. { method: 'parse', input: [true], message: TypeError },
  96. { method: 'parse', input: [1], message: TypeError },
  97. { method: 'parse', input: [], message: TypeError },
  98. { method: 'format', input: [null], message: TypeError },
  99. { method: 'format', input: [''], message: TypeError },
  100. { method: 'format', input: [true], message: TypeError },
  101. { method: 'format', input: [1], message: TypeError },
  102. ];
  103. tape('path.win32.parse', { skip: true }, function (t) {
  104. checkParseFormat(t, path.win32, winPaths);
  105. checkSpecialCaseParseFormat(t, path.win32, winSpecialCaseParseTests);
  106. t.end();
  107. });
  108. tape('path.posix.parse', function (t) {
  109. checkParseFormat(t, path.posix, unixPaths);
  110. t.end();
  111. });
  112. tape('path.win32.parse errors', { skip: true }, function (t) {
  113. checkErrors(t, path.win32);
  114. t.end();
  115. });
  116. tape('path.posix.parse errors', function (t) {
  117. checkErrors(t, path.posix);
  118. t.end();
  119. });
  120. tape('path.win32.format', { skip: true }, function (t) {
  121. checkFormat(t, path.win32, winSpecialCaseFormatTests);
  122. t.end();
  123. });
  124. tape('path.posix.format', function (t) {
  125. checkFormat(t, path.posix, unixSpecialCaseFormatTests);
  126. t.end();
  127. });
  128. // Test removal of trailing path separators
  129. var windowsTrailingTests =
  130. [['.\\', { root: '', dir: '', base: '.', ext: '', name: '.' }],
  131. ['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
  132. ['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
  133. ['c:\\foo\\\\\\',
  134. { root: 'c:\\', dir: 'c:\\', base: 'foo', ext: '', name: 'foo' }],
  135. ['D:\\foo\\\\\\bar.baz',
  136. { root: 'D:\\',
  137. dir: 'D:\\foo\\\\',
  138. base: 'bar.baz',
  139. ext: '.baz',
  140. name: 'bar'
  141. }
  142. ]
  143. ];
  144. var posixTrailingTests =
  145. [['./', { root: '', dir: '', base: '.', ext: '', name: '.' }],
  146. ['//', { root: '/', dir: '/', base: '', ext: '', name: '' }],
  147. ['///', { root: '/', dir: '/', base: '', ext: '', name: '' }],
  148. ['/foo///', { root: '/', dir: '/', base: 'foo', ext: '', name: 'foo' }],
  149. ['/foo///bar.baz',
  150. { root: '/', dir: '/foo//', base: 'bar.baz', ext: '.baz', name: 'bar' }
  151. ]
  152. ];
  153. tape('path.win32.parse trailing', { skip: true }, function (t) {
  154. windowsTrailingTests.forEach(function (p) {
  155. var actual = path.win32.parse(p[0]);
  156. var expected = p[1];
  157. t.deepEqual(actual, expected)
  158. });
  159. t.end();
  160. });
  161. tape('path.posix.parse trailing', function (t) {
  162. posixTrailingTests.forEach(function (p) {
  163. var actual = path.posix.parse(p[0]);
  164. var expected = p[1];
  165. t.deepEqual(actual, expected)
  166. });
  167. t.end();
  168. });
  169. function checkErrors(t, path) {
  170. errors.forEach(function(errorCase) {
  171. t.throws(function () {
  172. path[errorCase.method].apply(path, errorCase.input);
  173. }, errorCase.message);
  174. });
  175. }
  176. function checkParseFormat(t, path, paths) {
  177. paths.forEach(function(p) {
  178. var element = p[0];
  179. var root = p[1];
  180. var output = path.parse(element);
  181. t.strictEqual(typeof output.root, 'string');
  182. t.strictEqual(typeof output.dir, 'string');
  183. t.strictEqual(typeof output.base, 'string');
  184. t.strictEqual(typeof output.ext, 'string');
  185. t.strictEqual(typeof output.name, 'string');
  186. t.strictEqual(path.format(output), element);
  187. t.strictEqual(output.root, root);
  188. t.ok(output.dir.startsWith(output.root));
  189. t.strictEqual(output.dir, output.dir ? path.dirname(element) : '');
  190. t.strictEqual(output.base, path.basename(element));
  191. t.strictEqual(output.ext, path.extname(element));
  192. });
  193. }
  194. function checkSpecialCaseParseFormat(t, path, testCases) {
  195. testCases.forEach(function(testCase) {
  196. var element = testCase[0];
  197. var expect = testCase[1];
  198. var output = path.parse(element);
  199. Object.keys(expect).forEach(function(key) {
  200. t.strictEqual(output[key], expect[key]);
  201. });
  202. });
  203. }
  204. function checkFormat(t, path, testCases) {
  205. testCases.forEach(function(testCase) {
  206. t.strictEqual(path.format(testCase[0]), testCase[1]);
  207. });
  208. [null, undefined, 1, true, false, 'string'].forEach(function (pathObject) {
  209. t.throws(function() {
  210. path.format(pathObject);
  211. }, /The "pathObject" argument must be of type Object. Received type (\w+)/);
  212. });
  213. }