test-path.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. // Adapted version of:
  22. // https://github.com/joyent/node/blob/v0.9.1/test/simple/test-path.js
  23. exports['test path'] = function(assert) {
  24. var system = require('sdk/system');
  25. var path = require('sdk/fs/path');
  26. // Shim process global from node.
  27. var process = Object.create(require('sdk/system'));
  28. process.cwd = process.pathFor.bind(process, 'CurProcD');
  29. var isWindows = require('sdk/system').platform.indexOf('win') === 0;
  30. assert.equal(path.basename(''), '');
  31. assert.equal(path.basename('/dir/basename.ext'), 'basename.ext');
  32. assert.equal(path.basename('/basename.ext'), 'basename.ext');
  33. assert.equal(path.basename('basename.ext'), 'basename.ext');
  34. assert.equal(path.basename('basename.ext/'), 'basename.ext');
  35. assert.equal(path.basename('basename.ext//'), 'basename.ext');
  36. if (isWindows) {
  37. // On Windows a backslash acts as a path separator.
  38. assert.equal(path.basename('\\dir\\basename.ext'), 'basename.ext');
  39. assert.equal(path.basename('\\basename.ext'), 'basename.ext');
  40. assert.equal(path.basename('basename.ext'), 'basename.ext');
  41. assert.equal(path.basename('basename.ext\\'), 'basename.ext');
  42. assert.equal(path.basename('basename.ext\\\\'), 'basename.ext');
  43. } else {
  44. // On unix a backslash is just treated as any other character.
  45. assert.equal(path.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
  46. assert.equal(path.basename('\\basename.ext'), '\\basename.ext');
  47. assert.equal(path.basename('basename.ext'), 'basename.ext');
  48. assert.equal(path.basename('basename.ext\\'), 'basename.ext\\');
  49. assert.equal(path.basename('basename.ext\\\\'), 'basename.ext\\\\');
  50. }
  51. // POSIX filenames may include control characters
  52. // c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
  53. if (!isWindows) {
  54. var controlCharFilename = 'Icon' + String.fromCharCode(13);
  55. assert.equal(path.basename('/a/b/' + controlCharFilename),
  56. controlCharFilename);
  57. }
  58. assert.equal(path.dirname('/a/b/'), '/a');
  59. assert.equal(path.dirname('/a/b'), '/a');
  60. assert.equal(path.dirname('/a'), '/');
  61. assert.equal(path.dirname(''), '.');
  62. assert.equal(path.dirname('/'), '/');
  63. assert.equal(path.dirname('////'), '/');
  64. if (isWindows) {
  65. assert.equal(path.dirname('c:\\'), 'c:\\');
  66. assert.equal(path.dirname('c:\\foo'), 'c:\\');
  67. assert.equal(path.dirname('c:\\foo\\'), 'c:\\');
  68. assert.equal(path.dirname('c:\\foo\\bar'), 'c:\\foo');
  69. assert.equal(path.dirname('c:\\foo\\bar\\'), 'c:\\foo');
  70. assert.equal(path.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar');
  71. assert.equal(path.dirname('\\'), '\\');
  72. assert.equal(path.dirname('\\foo'), '\\');
  73. assert.equal(path.dirname('\\foo\\'), '\\');
  74. assert.equal(path.dirname('\\foo\\bar'), '\\foo');
  75. assert.equal(path.dirname('\\foo\\bar\\'), '\\foo');
  76. assert.equal(path.dirname('\\foo\\bar\\baz'), '\\foo\\bar');
  77. assert.equal(path.dirname('c:'), 'c:');
  78. assert.equal(path.dirname('c:foo'), 'c:');
  79. assert.equal(path.dirname('c:foo\\'), 'c:');
  80. assert.equal(path.dirname('c:foo\\bar'), 'c:foo');
  81. assert.equal(path.dirname('c:foo\\bar\\'), 'c:foo');
  82. assert.equal(path.dirname('c:foo\\bar\\baz'), 'c:foo\\bar');
  83. assert.equal(path.dirname('\\\\unc\\share'), '\\\\unc\\share');
  84. assert.equal(path.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\');
  85. assert.equal(path.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\');
  86. assert.equal(path.dirname('\\\\unc\\share\\foo\\bar'),
  87. '\\\\unc\\share\\foo');
  88. assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\'),
  89. '\\\\unc\\share\\foo');
  90. assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\baz'),
  91. '\\\\unc\\share\\foo\\bar');
  92. }
  93. assert.equal(path.extname(''), '');
  94. assert.equal(path.extname('/path/to/file'), '');
  95. assert.equal(path.extname('/path/to/file.ext'), '.ext');
  96. assert.equal(path.extname('/path.to/file.ext'), '.ext');
  97. assert.equal(path.extname('/path.to/file'), '');
  98. assert.equal(path.extname('/path.to/.file'), '');
  99. assert.equal(path.extname('/path.to/.file.ext'), '.ext');
  100. assert.equal(path.extname('/path/to/f.ext'), '.ext');
  101. assert.equal(path.extname('/path/to/..ext'), '.ext');
  102. assert.equal(path.extname('file'), '');
  103. assert.equal(path.extname('file.ext'), '.ext');
  104. assert.equal(path.extname('.file'), '');
  105. assert.equal(path.extname('.file.ext'), '.ext');
  106. assert.equal(path.extname('/file'), '');
  107. assert.equal(path.extname('/file.ext'), '.ext');
  108. assert.equal(path.extname('/.file'), '');
  109. assert.equal(path.extname('/.file.ext'), '.ext');
  110. assert.equal(path.extname('.path/file.ext'), '.ext');
  111. assert.equal(path.extname('file.ext.ext'), '.ext');
  112. assert.equal(path.extname('file.'), '.');
  113. assert.equal(path.extname('.'), '');
  114. assert.equal(path.extname('./'), '');
  115. assert.equal(path.extname('.file.ext'), '.ext');
  116. assert.equal(path.extname('.file'), '');
  117. assert.equal(path.extname('.file.'), '.');
  118. assert.equal(path.extname('.file..'), '.');
  119. assert.equal(path.extname('..'), '');
  120. assert.equal(path.extname('../'), '');
  121. assert.equal(path.extname('..file.ext'), '.ext');
  122. assert.equal(path.extname('..file'), '.file');
  123. assert.equal(path.extname('..file.'), '.');
  124. assert.equal(path.extname('..file..'), '.');
  125. assert.equal(path.extname('...'), '.');
  126. assert.equal(path.extname('...ext'), '.ext');
  127. assert.equal(path.extname('....'), '.');
  128. assert.equal(path.extname('file.ext/'), '.ext');
  129. assert.equal(path.extname('file.ext//'), '.ext');
  130. assert.equal(path.extname('file/'), '');
  131. assert.equal(path.extname('file//'), '');
  132. assert.equal(path.extname('file./'), '.');
  133. assert.equal(path.extname('file.//'), '.');
  134. if (isWindows) {
  135. // On windows, backspace is a path separator.
  136. assert.equal(path.extname('.\\'), '');
  137. assert.equal(path.extname('..\\'), '');
  138. assert.equal(path.extname('file.ext\\'), '.ext');
  139. assert.equal(path.extname('file.ext\\\\'), '.ext');
  140. assert.equal(path.extname('file\\'), '');
  141. assert.equal(path.extname('file\\\\'), '');
  142. assert.equal(path.extname('file.\\'), '.');
  143. assert.equal(path.extname('file.\\\\'), '.');
  144. } else {
  145. // On unix, backspace is a valid name component like any other character.
  146. assert.equal(path.extname('.\\'), '');
  147. assert.equal(path.extname('..\\'), '.\\');
  148. assert.equal(path.extname('file.ext\\'), '.ext\\');
  149. assert.equal(path.extname('file.ext\\\\'), '.ext\\\\');
  150. assert.equal(path.extname('file\\'), '');
  151. assert.equal(path.extname('file\\\\'), '');
  152. assert.equal(path.extname('file.\\'), '.\\');
  153. assert.equal(path.extname('file.\\\\'), '.\\\\');
  154. }
  155. // path.join tests
  156. var failures = [];
  157. var joinTests =
  158. // arguments result
  159. [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
  160. [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
  161. [['/foo', '../../../bar'], '/bar'],
  162. [['foo', '../../../bar'], '../../bar'],
  163. [['foo/', '../../../bar'], '../../bar'],
  164. [['foo/x', '../../../bar'], '../bar'],
  165. [['foo/x', './bar'], 'foo/x/bar'],
  166. [['foo/x/', './bar'], 'foo/x/bar'],
  167. [['foo/x/', '.', 'bar'], 'foo/x/bar'],
  168. [['./'], './'],
  169. [['.', './'], './'],
  170. [['.', '.', '.'], '.'],
  171. [['.', './', '.'], '.'],
  172. [['.', '/./', '.'], '.'],
  173. [['.', '/////./', '.'], '.'],
  174. [['.'], '.'],
  175. [['', '.'], '.'],
  176. [['', 'foo'], 'foo'],
  177. [['foo', '/bar'], 'foo/bar'],
  178. [['', '/foo'], '/foo'],
  179. [['', '', '/foo'], '/foo'],
  180. [['', '', 'foo'], 'foo'],
  181. [['foo', ''], 'foo'],
  182. [['foo/', ''], 'foo/'],
  183. [['foo', '', '/bar'], 'foo/bar'],
  184. [['./', '..', '/foo'], '../foo'],
  185. [['./', '..', '..', '/foo'], '../../foo'],
  186. [['.', '..', '..', '/foo'], '../../foo'],
  187. [['', '..', '..', '/foo'], '../../foo'],
  188. [['/'], '/'],
  189. [['/', '.'], '/'],
  190. [['/', '..'], '/'],
  191. [['/', '..', '..'], '/'],
  192. [[''], '.'],
  193. [['', ''], '.'],
  194. [[' /foo'], ' /foo'],
  195. [[' ', 'foo'], ' /foo'],
  196. [[' ', '.'], ' '],
  197. [[' ', '/'], ' /'],
  198. [[' ', ''], ' '],
  199. [['/', 'foo'], '/foo'],
  200. [['/', '/foo'], '/foo'],
  201. [['/', '//foo'], '/foo'],
  202. [['/', '', '/foo'], '/foo'],
  203. [['', '/', 'foo'], '/foo'],
  204. [['', '/', '/foo'], '/foo']
  205. ];
  206. // Windows-specific join tests
  207. if (isWindows) {
  208. joinTests = joinTests.concat(
  209. [// UNC path expected
  210. [['//foo/bar'], '//foo/bar/'],
  211. [['\\/foo/bar'], '//foo/bar/'],
  212. [['\\\\foo/bar'], '//foo/bar/'],
  213. // UNC path expected - server and share separate
  214. [['//foo', 'bar'], '//foo/bar/'],
  215. [['//foo/', 'bar'], '//foo/bar/'],
  216. [['//foo', '/bar'], '//foo/bar/'],
  217. // UNC path expected - questionable
  218. [['//foo', '', 'bar'], '//foo/bar/'],
  219. [['//foo/', '', 'bar'], '//foo/bar/'],
  220. [['//foo/', '', '/bar'], '//foo/bar/'],
  221. // UNC path expected - even more questionable
  222. [['', '//foo', 'bar'], '//foo/bar/'],
  223. [['', '//foo/', 'bar'], '//foo/bar/'],
  224. [['', '//foo/', '/bar'], '//foo/bar/'],
  225. // No UNC path expected (no double slash in first component)
  226. [['\\', 'foo/bar'], '/foo/bar'],
  227. [['\\', '/foo/bar'], '/foo/bar'],
  228. [['', '/', '/foo/bar'], '/foo/bar'],
  229. // No UNC path expected (no non-slashes in first component - questionable)
  230. [['//', 'foo/bar'], '/foo/bar'],
  231. [['//', '/foo/bar'], '/foo/bar'],
  232. [['\\\\', '/', '/foo/bar'], '/foo/bar'],
  233. [['//'], '/'],
  234. // No UNC path expected (share name missing - questionable).
  235. [['//foo'], '/foo'],
  236. [['//foo/'], '/foo/'],
  237. [['//foo', '/'], '/foo/'],
  238. [['//foo', '', '/'], '/foo/'],
  239. // No UNC path expected (too many leading slashes - questionable)
  240. [['///foo/bar'], '/foo/bar'],
  241. [['////foo', 'bar'], '/foo/bar'],
  242. [['\\\\\\/foo/bar'], '/foo/bar'],
  243. // Drive-relative vs drive-absolute paths. This merely describes the
  244. // status quo, rather than being obviously right
  245. [['c:'], 'c:.'],
  246. [['c:.'], 'c:.'],
  247. [['c:', ''], 'c:.'],
  248. [['', 'c:'], 'c:.'],
  249. [['c:.', '/'], 'c:./'],
  250. [['c:.', 'file'], 'c:file'],
  251. [['c:', '/'], 'c:/'],
  252. [['c:', 'file'], 'c:/file']
  253. ]);
  254. }
  255. // Run the join tests.
  256. joinTests.forEach(function(test) {
  257. var actual = path.join.apply(path, test[0]);
  258. var expected = isWindows ? test[1].replace(/\//g, '\\') : test[1];
  259. var message = 'path.join(' + test[0].map(JSON.stringify).join(',') + ')' +
  260. '\n expect=' + JSON.stringify(expected) +
  261. '\n actual=' + JSON.stringify(actual);
  262. if (actual !== expected) failures.push('\n' + message);
  263. // assert.equal(actual, expected, message);
  264. });
  265. assert.equal(failures.length, 0, failures.join(''));
  266. var joinThrowTests = [true, false, 7, null, {}, undefined, [], NaN];
  267. joinThrowTests.forEach(function(test) {
  268. assert.throws(function() {
  269. path.join(test);
  270. }, TypeError);
  271. assert.throws(function() {
  272. path.resolve(test);
  273. }, TypeError);
  274. });
  275. // path normalize tests
  276. if (isWindows) {
  277. assert.equal(path.normalize('./fixtures///b/../b/c.js'),
  278. 'fixtures\\b\\c.js');
  279. assert.equal(path.normalize('/foo/../../../bar'), '\\bar');
  280. assert.equal(path.normalize('a//b//../b'), 'a\\b');
  281. assert.equal(path.normalize('a//b//./c'), 'a\\b\\c');
  282. assert.equal(path.normalize('a//b//.'), 'a\\b');
  283. assert.equal(path.normalize('//server/share/dir/file.ext'),
  284. '\\\\server\\share\\dir\\file.ext');
  285. } else {
  286. assert.equal(path.normalize('./fixtures///b/../b/c.js'),
  287. 'fixtures/b/c.js');
  288. assert.equal(path.normalize('/foo/../../../bar'), '/bar');
  289. assert.equal(path.normalize('a//b//../b'), 'a/b');
  290. assert.equal(path.normalize('a//b//./c'), 'a/b/c');
  291. assert.equal(path.normalize('a//b//.'), 'a/b');
  292. }
  293. // path.resolve tests
  294. if (isWindows) {
  295. // windows
  296. var resolveTests =
  297. // arguments result
  298. [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
  299. [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
  300. [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
  301. [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
  302. [['.'], process.cwd()],
  303. [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
  304. [['c:/', '//'], 'c:\\'],
  305. [['c:/', '//dir'], 'c:\\dir'],
  306. [['c:/', '//server/share'], '\\\\server\\share\\'],
  307. [['c:/', '//server//share'], '\\\\server\\share\\'],
  308. [['c:/', '///some//dir'], 'c:\\some\\dir']
  309. ];
  310. } else {
  311. // Posix
  312. var resolveTests =
  313. // arguments result
  314. [[['/var/lib', '../', 'file/'], '/var/file'],
  315. [['/var/lib', '/../', 'file/'], '/file'],
  316. // For some mysterious reasons OSX debug builds resolve incorrectly
  317. // https://tbpl.mozilla.org/php/getParsedLog.php?id=25105489&tree=Mozilla-Inbound
  318. // Disable this tests until Bug 891698 is fixed.
  319. // [['a/b/c/', '../../..'], process.cwd()],
  320. // [['.'], process.cwd()],
  321. [['/some/dir', '.', '/absolute/'], '/absolute']];
  322. }
  323. var failures = [];
  324. resolveTests.forEach(function(test) {
  325. var actual = path.resolve.apply(path, test[0]);
  326. var expected = test[1];
  327. var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
  328. '\n expect=' + JSON.stringify(expected) +
  329. '\n actual=' + JSON.stringify(actual);
  330. if (actual !== expected) failures.push('\n' + message);
  331. // assert.equal(actual, expected, message);
  332. });
  333. assert.equal(failures.length, 0, failures.join(''));
  334. // path.isAbsolute tests
  335. if (isWindows) {
  336. assert.equal(path.isAbsolute('//server/file'), true);
  337. assert.equal(path.isAbsolute('\\\\server\\file'), true);
  338. assert.equal(path.isAbsolute('C:/Users/'), true);
  339. assert.equal(path.isAbsolute('C:\\Users\\'), true);
  340. assert.equal(path.isAbsolute('C:cwd/another'), false);
  341. assert.equal(path.isAbsolute('C:cwd\\another'), false);
  342. assert.equal(path.isAbsolute('directory/directory'), false);
  343. assert.equal(path.isAbsolute('directory\\directory'), false);
  344. } else {
  345. assert.equal(path.isAbsolute('/home/foo'), true);
  346. assert.equal(path.isAbsolute('/home/foo/..'), true);
  347. assert.equal(path.isAbsolute('bar/'), false);
  348. assert.equal(path.isAbsolute('./baz'), false);
  349. }
  350. // path.relative tests
  351. if (isWindows) {
  352. // windows
  353. var relativeTests =
  354. // arguments result
  355. [['c:/blah\\blah', 'd:/games', 'd:\\games'],
  356. ['c:/aaaa/bbbb', 'c:/aaaa', '..'],
  357. ['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'],
  358. ['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''],
  359. ['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'],
  360. ['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'],
  361. ['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'],
  362. ['c:/aaaa/bbbb', 'd:\\', 'd:\\']];
  363. } else {
  364. // posix
  365. var relativeTests =
  366. // arguments result
  367. [['/var/lib', '/var', '..'],
  368. ['/var/lib', '/bin', '../../bin'],
  369. ['/var/lib', '/var/lib', ''],
  370. ['/var/lib', '/var/apache', '../apache'],
  371. ['/var/', '/var/lib', 'lib'],
  372. ['/', '/var/lib', 'var/lib']];
  373. }
  374. var failures = [];
  375. relativeTests.forEach(function(test) {
  376. var actual = path.relative(test[0], test[1]);
  377. var expected = test[2];
  378. var message = 'path.relative(' +
  379. test.slice(0, 2).map(JSON.stringify).join(',') +
  380. ')' +
  381. '\n expect=' + JSON.stringify(expected) +
  382. '\n actual=' + JSON.stringify(actual);
  383. if (actual !== expected) failures.push('\n' + message);
  384. });
  385. assert.equal(failures.length, 0, failures.join(''));
  386. // path.sep tests
  387. if (isWindows) {
  388. // windows
  389. assert.equal(path.sep, '\\');
  390. } else {
  391. // posix
  392. assert.equal(path.sep, '/');
  393. }
  394. // path.delimiter tests
  395. if (isWindows) {
  396. // windows
  397. assert.equal(path.delimiter, ';');
  398. } else {
  399. // posix
  400. assert.equal(path.delimiter, ':');
  401. }
  402. };
  403. require('test').run(exports);