Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit da7d854

Browse files
committed
path: Remove dead code in favor of unit tests
Remove dead code paths that are created by assertions that will never trigger. They may only trigger if either the `splitDeviceRe` or `splitPathRe` regular expressions are modified. If at some point they are modified, current unit tests will catch most of the resulting errors and this commit adds extra tests to catch the remaining errors.
1 parent ebbb356 commit da7d854

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

lib/path.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function win32SplitPath(filename) {
9090
// Separate device+slash from tail
9191
var result = splitDeviceRe.exec(filename),
9292
device = (result[1] || '') + (result[2] || ''),
93-
tail = result[3] || '';
93+
tail = result[3];
9494
// Split the tail into dir, basename and extension
9595
var result2 = splitTailRe.exec(tail),
9696
dir = result2[1],
@@ -392,13 +392,10 @@ win32.format = function(pathObject) {
392392
win32.parse = function(pathString) {
393393
if (!util.isString(pathString)) {
394394
throw new TypeError(
395-
"Parameter 'pathString' must be a string, not " + typeof pathString
395+
"Parameter 'pathString' must be a string, not " + typeof pathString
396396
);
397397
}
398398
var allParts = win32SplitPath(pathString);
399-
if (!allParts || allParts.length !== 4) {
400-
throw new TypeError("Invalid path '" + pathString + "'");
401-
}
402399
return {
403400
root: allParts[0],
404401
dir: allParts[0] + allParts[1].slice(0, -1),
@@ -592,17 +589,10 @@ posix.format = function(pathObject) {
592589
posix.parse = function(pathString) {
593590
if (!util.isString(pathString)) {
594591
throw new TypeError(
595-
"Parameter 'pathString' must be a string, not " + typeof pathString
592+
"Parameter 'pathString' must be a string, not " + typeof pathString
596593
);
597594
}
598595
var allParts = posixSplitPath(pathString);
599-
if (!allParts || allParts.length !== 4) {
600-
throw new TypeError("Invalid path '" + pathString + "'");
601-
}
602-
allParts[1] = allParts[1] || '';
603-
allParts[2] = allParts[2] || '';
604-
allParts[3] = allParts[3] || '';
605-
606596
return {
607597
root: allParts[0],
608598
dir: allParts[0] + allParts[1].slice(0, -1),

test/simple/test-path-parse-format.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var winPaths = [
2929
'\\foo\\C:',
3030
'file',
3131
'.\\file',
32+
'',
3233

3334
// unc
3435
'\\\\server\\share\\file_path',
@@ -52,7 +53,8 @@ var unixPaths = [
5253
'file',
5354
'.\\file',
5455
'./file',
55-
'C:\\foo'
56+
'C:\\foo',
57+
''
5658
];
5759

5860
var unixSpecialCaseFormatTests = [
@@ -67,7 +69,6 @@ var errors = [
6769
{method: 'parse', input: [true], message: /Parameter 'pathString' must be a string, not boolean/},
6870
{method: 'parse', input: [1], message: /Parameter 'pathString' must be a string, not number/},
6971
{method: 'parse', input: [], message: /Parameter 'pathString' must be a string, not undefined/},
70-
// {method: 'parse', input: [''], message: /Invalid path/}, // omitted because it's hard to trigger!
7172
{method: 'format', input: [null], message: /Parameter 'pathObject' must be an object, not/},
7273
{method: 'format', input: [''], message: /Parameter 'pathObject' must be an object, not string/},
7374
{method: 'format', input: [true], message: /Parameter 'pathObject' must be an object, not boolean/},
@@ -101,8 +102,13 @@ function checkErrors(path) {
101102
}
102103

103104
function checkParseFormat(path, paths) {
104-
paths.forEach(function(element, index, array) {
105+
paths.forEach(function(element) {
105106
var output = path.parse(element);
107+
assert.strictEqual(typeof output.root, 'string');
108+
assert.strictEqual(typeof output.dir, 'string');
109+
assert.strictEqual(typeof output.base, 'string');
110+
assert.strictEqual(typeof output.ext, 'string');
111+
assert.strictEqual(typeof output.name, 'string');
106112
assert.strictEqual(path.format(output), element);
107113
assert.strictEqual(output.dir, output.dir ? path.dirname(element) : '');
108114
assert.strictEqual(output.base, path.basename(element));

0 commit comments

Comments
 (0)