• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4const path = require('path');
5
6const failures = [];
7const slashRE = /\//g;
8
9[
10  [__filename, '.js'],
11  ['', ''],
12  ['/path/to/file', ''],
13  ['/path/to/file.ext', '.ext'],
14  ['/path.to/file.ext', '.ext'],
15  ['/path.to/file', ''],
16  ['/path.to/.file', ''],
17  ['/path.to/.file.ext', '.ext'],
18  ['/path/to/f.ext', '.ext'],
19  ['/path/to/..ext', '.ext'],
20  ['/path/to/..', ''],
21  ['file', ''],
22  ['file.ext', '.ext'],
23  ['.file', ''],
24  ['.file.ext', '.ext'],
25  ['/file', ''],
26  ['/file.ext', '.ext'],
27  ['/.file', ''],
28  ['/.file.ext', '.ext'],
29  ['.path/file.ext', '.ext'],
30  ['file.ext.ext', '.ext'],
31  ['file.', '.'],
32  ['.', ''],
33  ['./', ''],
34  ['.file.ext', '.ext'],
35  ['.file', ''],
36  ['.file.', '.'],
37  ['.file..', '.'],
38  ['..', ''],
39  ['../', ''],
40  ['..file.ext', '.ext'],
41  ['..file', '.file'],
42  ['..file.', '.'],
43  ['..file..', '.'],
44  ['...', '.'],
45  ['...ext', '.ext'],
46  ['....', '.'],
47  ['file.ext/', '.ext'],
48  ['file.ext//', '.ext'],
49  ['file/', ''],
50  ['file//', ''],
51  ['file./', '.'],
52  ['file.//', '.'],
53].forEach((test) => {
54  const expected = test[1];
55  [path.posix.extname, path.win32.extname].forEach((extname) => {
56    let input = test[0];
57    let os;
58    if (extname === path.win32.extname) {
59      input = input.replace(slashRE, '\\');
60      os = 'win32';
61    } else {
62      os = 'posix';
63    }
64    const actual = extname(input);
65    const message = `path.${os}.extname(${JSON.stringify(input)})\n  expect=${
66      JSON.stringify(expected)}\n  actual=${JSON.stringify(actual)}`;
67    if (actual !== expected)
68      failures.push(`\n${message}`);
69  });
70  {
71    const input = `C:${test[0].replace(slashRE, '\\')}`;
72    const actual = path.win32.extname(input);
73    const message = `path.win32.extname(${JSON.stringify(input)})\n  expect=${
74      JSON.stringify(expected)}\n  actual=${JSON.stringify(actual)}`;
75    if (actual !== expected)
76      failures.push(`\n${message}`);
77  }
78});
79assert.strictEqual(failures.length, 0, failures.join(''));
80
81// On Windows, backslash is a path separator.
82assert.strictEqual(path.win32.extname('.\\'), '');
83assert.strictEqual(path.win32.extname('..\\'), '');
84assert.strictEqual(path.win32.extname('file.ext\\'), '.ext');
85assert.strictEqual(path.win32.extname('file.ext\\\\'), '.ext');
86assert.strictEqual(path.win32.extname('file\\'), '');
87assert.strictEqual(path.win32.extname('file\\\\'), '');
88assert.strictEqual(path.win32.extname('file.\\'), '.');
89assert.strictEqual(path.win32.extname('file.\\\\'), '.');
90
91// On *nix, backslash is a valid name component like any other character.
92assert.strictEqual(path.posix.extname('.\\'), '');
93assert.strictEqual(path.posix.extname('..\\'), '.\\');
94assert.strictEqual(path.posix.extname('file.ext\\'), '.ext\\');
95assert.strictEqual(path.posix.extname('file.ext\\\\'), '.ext\\\\');
96assert.strictEqual(path.posix.extname('file\\'), '');
97assert.strictEqual(path.posix.extname('file\\\\'), '');
98assert.strictEqual(path.posix.extname('file.\\'), '.\\');
99assert.strictEqual(path.posix.extname('file.\\\\'), '.\\\\');
100