Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: improve path tests
This commit adds new tests, executes tests for other platforms
instead of limiting platform-specific tests to those platforms,
and fixes a few style/formatting inconsistencies.
  • Loading branch information
mscdex committed Feb 6, 2016
commit d8556dd4d4c05a732c2bed7a712e9a08d2278b2e
72 changes: 71 additions & 1 deletion test/parallel/test-path-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ const unixPaths = [
'./file',
'C:\\foo',
'/',
''
'',
'.',
'..',
'/foo',
'/foo.',
'/foo.bar',
'/.',
'/.foo',
'/.foo.bar',
'/foo/bar.baz',
];

const unixSpecialCaseFormatTests = [
Expand Down Expand Up @@ -82,6 +91,67 @@ checkErrors(path.posix);
checkFormat(path.win32, winSpecialCaseFormatTests);
checkFormat(path.posix, unixSpecialCaseFormatTests);

// Test removal of trailing path separators
const trailingTests = [
[ path.win32.parse,
[['.\\', { root: '', dir: '', base: '.', ext: '', name: '.' }],
['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
['c:\\foo\\\\\\',
{ root: 'c:\\', dir: 'c:\\', base: 'foo', ext: '', name: 'foo' }],
['D:\\foo\\\\\\bar.baz',
{ root: 'D:\\',
dir: 'D:\\foo\\\\',
base: 'bar.baz',
ext: '.baz',
name: 'bar'
}
]
]
],
[ path.posix.parse,
[['./', { root: '', dir: '', base: '.', ext: '', name: '.' }],
['//', { root: '/', dir: '/', base: '', ext: '', name: '' }],
['///', { root: '/', dir: '/', base: '', ext: '', name: '' }],
['/foo///', { root: '/', dir: '/', base: 'foo', ext: '', name: 'foo' }],
['/foo///bar.baz',
{ root: '/', dir: '/foo//', base: 'bar.baz', ext: '.baz', name: 'bar' }
]
]
]
];
const failures = [];
trailingTests.forEach(function(test) {
const parse = test[0];
test[1].forEach(function(test) {
const actual = parse(test[0]);
const expected = test[1];
const fn = 'path.' +
(parse === path.win32.parse ? 'win32' : 'posix') +
'.parse(';
const message = fn +
JSON.stringify(test[0]) +
')' +
'\n expect=' + JSON.stringify(expected) +
'\n actual=' + JSON.stringify(actual);
const actualKeys = Object.keys(actual);
const expectedKeys = Object.keys(expected);
let failed = (actualKeys.length !== expectedKeys.length);
if (!failed) {
for (let i = 0; i < actualKeys.length; ++i) {
const key = actualKeys[i];
if (expectedKeys.indexOf(key) === -1 || actual[key] !== expected[key]) {
failed = true;
break;
}
}
}
if (failed)
failures.push('\n' + message);
});
});
assert.equal(failures.length, 0, failures.join(''));

function checkErrors(path) {
errors.forEach(function(errorCase) {
try {
Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-path-zero-length-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ const pwd = process.cwd();

// join will internally ignore all the zero-length strings and it will return
// '.' if the joined string is a zero-length string.
assert.equal(path.join(''), '.');
assert.equal(path.join('', ''), '.');
assert.equal(path.posix.join(''), '.');
assert.equal(path.posix.join('', ''), '.');
assert.equal(path.win32.join(''), '.');
assert.equal(path.win32.join('', ''), '.');
assert.equal(path.join(pwd), pwd);
assert.equal(path.join(pwd, ''), pwd);

// normalize will return '.' if the input is a zero-length string
assert.equal(path.normalize(''), '.');
assert.equal(path.posix.normalize(''), '.');
assert.equal(path.win32.normalize(''), '.');
assert.equal(path.normalize(pwd), pwd);

// Since '' is not a valid path in any of the common environments, return false
assert.equal(path.isAbsolute(''), false);
assert.equal(path.posix.isAbsolute(''), false);
assert.equal(path.win32.isAbsolute(''), false);

// resolve, internally ignores all the zero-length strings and returns the
// current working directory
Expand Down
Loading