• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const path = require('path');
26
27const winPaths = [
28  // [path, root]
29  ['C:\\path\\dir\\index.html', 'C:\\'],
30  ['C:\\another_path\\DIR\\1\\2\\33\\\\index', 'C:\\'],
31  ['another_path\\DIR with spaces\\1\\2\\33\\index', ''],
32  ['\\', '\\'],
33  ['\\foo\\C:', '\\'],
34  ['file', ''],
35  ['file:stream', ''],
36  ['.\\file', ''],
37  ['C:', 'C:'],
38  ['C:.', 'C:'],
39  ['C:..', 'C:'],
40  ['C:abc', 'C:'],
41  ['C:\\', 'C:\\'],
42  ['C:\\abc', 'C:\\' ],
43  ['', ''],
44
45  // unc
46  ['\\\\server\\share\\file_path', '\\\\server\\share\\'],
47  ['\\\\server two\\shared folder\\file path.zip',
48   '\\\\server two\\shared folder\\'],
49  ['\\\\teela\\admin$\\system32', '\\\\teela\\admin$\\'],
50  ['\\\\?\\UNC\\server\\share', '\\\\?\\UNC\\']
51];
52
53const winSpecialCaseParseTests = [
54  ['t', { base: 't', name: 't', root: '', dir: '', ext: '' }],
55  ['/foo/bar', { root: '/', dir: '/foo', base: 'bar', ext: '', name: 'bar' }],
56];
57
58const winSpecialCaseFormatTests = [
59  [{ dir: 'some\\dir' }, 'some\\dir\\'],
60  [{ base: 'index.html' }, 'index.html'],
61  [{ root: 'C:\\' }, 'C:\\'],
62  [{ name: 'index', ext: '.html' }, 'index.html'],
63  [{ dir: 'some\\dir', name: 'index', ext: '.html' }, 'some\\dir\\index.html'],
64  [{ root: 'C:\\', name: 'index', ext: '.html' }, 'C:\\index.html'],
65  [{}, '']
66];
67
68const unixPaths = [
69  // [path, root]
70  ['/home/user/dir/file.txt', '/'],
71  ['/home/user/a dir/another File.zip', '/'],
72  ['/home/user/a dir//another&File.', '/'],
73  ['/home/user/a$$$dir//another File.zip', '/'],
74  ['user/dir/another File.zip', ''],
75  ['file', ''],
76  ['.\\file', ''],
77  ['./file', ''],
78  ['C:\\foo', ''],
79  ['/', '/'],
80  ['', ''],
81  ['.', ''],
82  ['..', ''],
83  ['/foo', '/'],
84  ['/foo.', '/'],
85  ['/foo.bar', '/'],
86  ['/.', '/'],
87  ['/.foo', '/'],
88  ['/.foo.bar', '/'],
89  ['/foo/bar.baz', '/']
90];
91
92const unixSpecialCaseFormatTests = [
93  [{ dir: 'some/dir' }, 'some/dir/'],
94  [{ base: 'index.html' }, 'index.html'],
95  [{ root: '/' }, '/'],
96  [{ name: 'index', ext: '.html' }, 'index.html'],
97  [{ dir: 'some/dir', name: 'index', ext: '.html' }, 'some/dir/index.html'],
98  [{ root: '/', name: 'index', ext: '.html' }, '/index.html'],
99  [{}, '']
100];
101
102const errors = [
103  { method: 'parse', input: [null] },
104  { method: 'parse', input: [{}] },
105  { method: 'parse', input: [true] },
106  { method: 'parse', input: [1] },
107  { method: 'parse', input: [] },
108  { method: 'format', input: [null] },
109  { method: 'format', input: [''] },
110  { method: 'format', input: [true] },
111  { method: 'format', input: [1] },
112];
113
114checkParseFormat(path.win32, winPaths);
115checkParseFormat(path.posix, unixPaths);
116checkSpecialCaseParseFormat(path.win32, winSpecialCaseParseTests);
117checkErrors(path.win32);
118checkErrors(path.posix);
119checkFormat(path.win32, winSpecialCaseFormatTests);
120checkFormat(path.posix, unixSpecialCaseFormatTests);
121
122// Test removal of trailing path separators
123const trailingTests = [
124  [ path.win32.parse,
125    [['.\\', { root: '', dir: '', base: '.', ext: '', name: '.' }],
126     ['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
127     ['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
128     ['c:\\foo\\\\\\',
129      { root: 'c:\\', dir: 'c:\\', base: 'foo', ext: '', name: 'foo' }],
130     ['D:\\foo\\\\\\bar.baz',
131      { root: 'D:\\',
132        dir: 'D:\\foo\\\\',
133        base: 'bar.baz',
134        ext: '.baz',
135        name: 'bar'
136      }
137     ]
138    ]
139  ],
140  [ path.posix.parse,
141    [['./', { root: '', dir: '', base: '.', ext: '', name: '.' }],
142     ['//', { root: '/', dir: '/', base: '', ext: '', name: '' }],
143     ['///', { root: '/', dir: '/', base: '', ext: '', name: '' }],
144     ['/foo///', { root: '/', dir: '/', base: 'foo', ext: '', name: 'foo' }],
145     ['/foo///bar.baz',
146      { root: '/', dir: '/foo//', base: 'bar.baz', ext: '.baz', name: 'bar' }
147     ]
148    ]
149  ]
150];
151const failures = [];
152trailingTests.forEach((test) => {
153  const parse = test[0];
154  const os = parse === path.win32.parse ? 'win32' : 'posix';
155  test[1].forEach((test) => {
156    const actual = parse(test[0]);
157    const expected = test[1];
158    const message = `path.${os}.parse(${JSON.stringify(test[0])})\n  expect=${
159      JSON.stringify(expected)}\n  actual=${JSON.stringify(actual)}`;
160    const actualKeys = Object.keys(actual);
161    const expectedKeys = Object.keys(expected);
162    let failed = (actualKeys.length !== expectedKeys.length);
163    if (!failed) {
164      for (let i = 0; i < actualKeys.length; ++i) {
165        const key = actualKeys[i];
166        if (!expectedKeys.includes(key) || actual[key] !== expected[key]) {
167          failed = true;
168          break;
169        }
170      }
171    }
172    if (failed)
173      failures.push(`\n${message}`);
174  });
175});
176assert.strictEqual(failures.length, 0, failures.join(''));
177
178function checkErrors(path) {
179  errors.forEach(({ method, input }) => {
180    assert.throws(() => {
181      path[method].apply(path, input);
182    }, {
183      code: 'ERR_INVALID_ARG_TYPE',
184      name: 'TypeError'
185    });
186  });
187}
188
189function checkParseFormat(path, paths) {
190  paths.forEach(([element, root]) => {
191    const output = path.parse(element);
192    assert.strictEqual(typeof output.root, 'string');
193    assert.strictEqual(typeof output.dir, 'string');
194    assert.strictEqual(typeof output.base, 'string');
195    assert.strictEqual(typeof output.ext, 'string');
196    assert.strictEqual(typeof output.name, 'string');
197    assert.strictEqual(path.format(output), element);
198    assert.strictEqual(output.root, root);
199    assert(output.dir.startsWith(output.root));
200    assert.strictEqual(output.dir, output.dir ? path.dirname(element) : '');
201    assert.strictEqual(output.base, path.basename(element));
202    assert.strictEqual(output.ext, path.extname(element));
203  });
204}
205
206function checkSpecialCaseParseFormat(path, testCases) {
207  testCases.forEach(([element, expect]) => {
208    assert.deepStrictEqual(path.parse(element), expect);
209  });
210}
211
212function checkFormat(path, testCases) {
213  testCases.forEach(([element, expect]) => {
214    assert.strictEqual(path.format(element), expect);
215  });
216
217  [null, undefined, 1, true, false, 'string'].forEach((pathObject) => {
218    assert.throws(() => {
219      path.format(pathObject);
220    }, {
221      code: 'ERR_INVALID_ARG_TYPE',
222      name: 'TypeError',
223      message: 'The "pathObject" argument must be of type object.' +
224               common.invalidArgTypeHelper(pathObject)
225    });
226  });
227}
228