test-path-basename.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. var tape = require('tape');
  3. var path = require('../');
  4. tape('path.basename', function (t) {
  5. t.strictEqual(path.basename(__filename), 'test-path-basename.js');
  6. t.strictEqual(path.basename(__filename, '.js'), 'test-path-basename');
  7. t.strictEqual(path.basename('.js', '.js'), '');
  8. t.strictEqual(path.basename(''), '');
  9. t.strictEqual(path.basename('/dir/basename.ext'), 'basename.ext');
  10. t.strictEqual(path.basename('/basename.ext'), 'basename.ext');
  11. t.strictEqual(path.basename('basename.ext'), 'basename.ext');
  12. t.strictEqual(path.basename('basename.ext/'), 'basename.ext');
  13. t.strictEqual(path.basename('basename.ext//'), 'basename.ext');
  14. t.strictEqual(path.basename('aaa/bbb', '/bbb'), 'bbb');
  15. t.strictEqual(path.basename('aaa/bbb', 'a/bbb'), 'bbb');
  16. t.strictEqual(path.basename('aaa/bbb', 'bbb'), 'bbb');
  17. t.strictEqual(path.basename('aaa/bbb//', 'bbb'), 'bbb');
  18. t.strictEqual(path.basename('aaa/bbb', 'bb'), 'b');
  19. t.strictEqual(path.basename('aaa/bbb', 'b'), 'bb');
  20. t.strictEqual(path.basename('/aaa/bbb', '/bbb'), 'bbb');
  21. t.strictEqual(path.basename('/aaa/bbb', 'a/bbb'), 'bbb');
  22. t.strictEqual(path.basename('/aaa/bbb', 'bbb'), 'bbb');
  23. t.strictEqual(path.basename('/aaa/bbb//', 'bbb'), 'bbb');
  24. t.strictEqual(path.basename('/aaa/bbb', 'bb'), 'b');
  25. t.strictEqual(path.basename('/aaa/bbb', 'b'), 'bb');
  26. t.strictEqual(path.basename('/aaa/bbb'), 'bbb');
  27. t.strictEqual(path.basename('/aaa/'), 'aaa');
  28. t.strictEqual(path.basename('/aaa/b'), 'b');
  29. t.strictEqual(path.basename('/a/b'), 'b');
  30. t.strictEqual(path.basename('//a'), 'a');
  31. t.end();
  32. })
  33. tape('path.win32.basename', { skip: true }, function (t) {
  34. // On Windows a backslash acts as a path separator.
  35. t.strictEqual(path.win32.basename('\\dir\\basename.ext'), 'basename.ext');
  36. t.strictEqual(path.win32.basename('\\basename.ext'), 'basename.ext');
  37. t.strictEqual(path.win32.basename('basename.ext'), 'basename.ext');
  38. t.strictEqual(path.win32.basename('basename.ext\\'), 'basename.ext');
  39. t.strictEqual(path.win32.basename('basename.ext\\\\'), 'basename.ext');
  40. t.strictEqual(path.win32.basename('foo'), 'foo');
  41. t.strictEqual(path.win32.basename('aaa\\bbb', '\\bbb'), 'bbb');
  42. t.strictEqual(path.win32.basename('aaa\\bbb', 'a\\bbb'), 'bbb');
  43. t.strictEqual(path.win32.basename('aaa\\bbb', 'bbb'), 'bbb');
  44. t.strictEqual(path.win32.basename('aaa\\bbb\\\\\\\\', 'bbb'), 'bbb');
  45. t.strictEqual(path.win32.basename('aaa\\bbb', 'bb'), 'b');
  46. t.strictEqual(path.win32.basename('aaa\\bbb', 'b'), 'bb');
  47. t.strictEqual(path.win32.basename('C:'), '');
  48. t.strictEqual(path.win32.basename('C:.'), '.');
  49. t.strictEqual(path.win32.basename('C:\\'), '');
  50. t.strictEqual(path.win32.basename('C:\\dir\\base.ext'), 'base.ext');
  51. t.strictEqual(path.win32.basename('C:\\basename.ext'), 'basename.ext');
  52. t.strictEqual(path.win32.basename('C:basename.ext'), 'basename.ext');
  53. t.strictEqual(path.win32.basename('C:basename.ext\\'), 'basename.ext');
  54. t.strictEqual(path.win32.basename('C:basename.ext\\\\'), 'basename.ext');
  55. t.strictEqual(path.win32.basename('C:foo'), 'foo');
  56. t.strictEqual(path.win32.basename('file:stream'), 'file:stream');
  57. t.end();
  58. });
  59. tape('On unix a backslash is just treated as any other character.', function (t) {
  60. t.strictEqual(path.posix.basename('\\dir\\basename.ext'),
  61. '\\dir\\basename.ext');
  62. t.strictEqual(path.posix.basename('\\basename.ext'), '\\basename.ext');
  63. t.strictEqual(path.posix.basename('basename.ext'), 'basename.ext');
  64. t.strictEqual(path.posix.basename('basename.ext\\'), 'basename.ext\\');
  65. t.strictEqual(path.posix.basename('basename.ext\\\\'), 'basename.ext\\\\');
  66. t.strictEqual(path.posix.basename('foo'), 'foo');
  67. t.end();
  68. });
  69. tape('POSIX filenames may include control characters', function (t) {
  70. // c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
  71. var controlCharFilename = "Icon" + (String.fromCharCode(13));
  72. t.strictEqual(path.posix.basename(("/a/b/" + controlCharFilename)),
  73. controlCharFilename);
  74. t.end();
  75. });