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