Skip to content

Commit 65963ec

Browse files
thefourtheyeTrott
authored andcommitted
doc,test: empty strings in path module
The path module's `join, normalize, isAbsolute, relative and resolve` functions return/use the current directory if they are passed zero length strings. > process.version 'v2.3.4-pre' > path.win32.join('') '.' > path.posix.join('') '.' > path.win32.normalize('') '.' > path.posix.normalize('') '.' > path.win32.isAbsolute('') false > path.posix.isAbsolute('') false > path.win32.relative('', '') '' > path.posix.relative('', '') '' > path.win32relative('.', '') '' > path.posix.relative('.', '') '' > path.posix.resolve('') '/home/thefourtheye/Desktop' > path.win32.resolve('') '\\home\\thefourtheye\\Desktop' Since empty paths are not valid in any of the operating systems people normally use, this behaviour might be a surprise to the users. This commit introduces "Notes" about this, wherever applicable in `path`'s documentation. The tests makes sure that the behaviour is intact between commits. PR-URL: #2106 Reviewed-By: Rich Trott <[email protected]>
1 parent 5acad6b commit 65963ec

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

doc/api/path.markdown

+20-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Example:
2222
// returns
2323
'/foo/bar/baz/asdf'
2424

25+
*Note:* If the path string passed as argument is a zero-length string then `'.'`
26+
will be returned, which represents the current working directory.
27+
2528
## path.join([path1][, path2][, ...])
2629

2730
Join all arguments together and normalize the resulting path.
@@ -39,6 +42,11 @@ Example:
3942
// throws exception
4043
TypeError: Arguments to path.join must be strings
4144

45+
*Note:* If the arguments to `join` have zero-length strings, unlike other path
46+
module functions, they will be ignored. If the joined path string is a
47+
zero-length string then `'.'` will be returned, which represents the
48+
current working directory.
49+
4250
## path.resolve([from ...], to)
4351

4452
Resolves `to` to an absolute path.
@@ -78,6 +86,9 @@ Examples:
7886
// if currently in /home/myself/iojs, it returns
7987
'/home/myself/iojs/wwwroot/static_files/gif/image.gif'
8088

89+
*Note:* If the arguments to `resolve` have zero-length strings then the current
90+
working directory will be used instead of them.
91+
8192
## path.isAbsolute(path)
8293

8394
Determines whether `path` is an absolute path. An absolute path will always
@@ -94,9 +105,13 @@ Windows examples:
94105

95106
path.isAbsolute('//server') // true
96107
path.isAbsolute('C:/foo/..') // true
97-
path.isAbsolute('bar\\baz') // false
108+
path.isAbsolute('bar\\baz') // false
98109
path.isAbsolute('.') // false
99110

111+
*Note:* If the path string passed as parameter is a zero-length string, unlike
112+
other path module functions, it will be used as-is and `false` will be
113+
returned.
114+
100115
## path.relative(from, to)
101116

102117
Solve the relative path from `from` to `to`.
@@ -117,6 +132,10 @@ Examples:
117132
// returns
118133
'../../impl/bbb'
119134

135+
*Note:* If the arguments to `relative` have zero-length strings then the current
136+
working directory will be used instead of the zero-length strings. If
137+
both the paths are the same then a zero-length string will be returned.
138+
120139
## path.dirname(p)
121140

122141
Return the directory name of a path. Similar to the Unix `dirname` command.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
// These testcases are specific to one uncommon behaviour in path module. Few
4+
// of the functions in path module, treat '' strings as current working
5+
// directory. This test makes sure that the behaviour is intact between commits.
6+
// See: https://github.com/nodejs/io.js/pull/2106
7+
8+
const common = require('../common');
9+
const assert = require('assert');
10+
const path = require('path');
11+
const pwd = process.cwd();
12+
13+
// join will internally ignore all the zero-length strings and it will return
14+
// '.' if the joined string is a zero-length string.
15+
assert.equal(path.join(''), '.');
16+
assert.equal(path.join('', ''), '.');
17+
assert.equal(path.join(pwd), pwd);
18+
assert.equal(path.join(pwd, ''), pwd);
19+
20+
// normalize will return '.' if the input is a zero-length string
21+
assert.equal(path.normalize(''), '.');
22+
assert.equal(path.normalize(pwd), pwd);
23+
24+
// Since '' is not a valid path in any of the common environments, return false
25+
assert.equal(path.isAbsolute(''), false);
26+
27+
// resolve, internally ignores all the zero-length strings and returns the
28+
// current working directory
29+
assert.equal(path.resolve(''), pwd);
30+
assert.equal(path.resolve('', ''), pwd);
31+
32+
// relative, internally calls resolve. So, '' is actually the current directory
33+
assert.equal(path.relative('', pwd), '');
34+
assert.equal(path.relative(pwd, ''), '');
35+
assert.equal(path.relative(pwd, pwd), '');

0 commit comments

Comments
 (0)