Skip to content

Commit 7693705

Browse files
committedMay 13, 2015
os: refine tmpdir() trailing slash stripping
os.tmpdir() began stripping trailing slashes in b57cc51. This causes problems if the temp directory is simply '/'. It also stripped trailing slashes without first determining which slash type is used by the current operating system. This commit only strips trailing slashes if another character precedes the slash. On Windows, it checks for ':', as not to strip slashes from something like 'C:\'. It also only strips slashes that are appropriate for the user's operating system. Fixes: #1669 PR-URL: #1673 Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: Christian Tellnes <[email protected]>
1 parent 966acb9 commit 7693705

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
 

‎lib/os.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ exports.platform = function() {
2222
return process.platform;
2323
};
2424

25+
const trailingSlashRe = isWindows ? /[^:]\\$/
26+
: /.\/$/;
27+
2528
exports.tmpdir = function() {
2629
var path;
2730
if (isWindows) {
@@ -34,7 +37,7 @@ exports.tmpdir = function() {
3437
process.env.TEMP ||
3538
'/tmp';
3639
}
37-
if (/[\\\/]$/.test(path))
40+
if (trailingSlashRe.test(path))
3841
path = path.slice(0, -1);
3942
return path;
4043
};

‎test/parallel/test-os.js

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ if (process.platform === 'win32') {
1515
assert.equal(os.tmpdir(), expected);
1616
process.env.TEMP = '\\temp\\';
1717
assert.equal(os.tmpdir(), '\\temp');
18+
process.env.TEMP = '\\tmpdir/';
19+
assert.equal(os.tmpdir(), '\\tmpdir/');
20+
process.env.TEMP = '\\';
21+
assert.equal(os.tmpdir(), '\\');
22+
process.env.TEMP = 'C:\\';
23+
assert.equal(os.tmpdir(), 'C:\\');
1824
} else {
1925
assert.equal(os.tmpdir(), '/tmpdir');
2026
process.env.TMPDIR = '';
@@ -25,6 +31,10 @@ if (process.platform === 'win32') {
2531
assert.equal(os.tmpdir(), '/tmp');
2632
process.env.TMPDIR = '/tmpdir/';
2733
assert.equal(os.tmpdir(), '/tmpdir');
34+
process.env.TMPDIR = '/tmpdir\\';
35+
assert.equal(os.tmpdir(), '/tmpdir\\');
36+
process.env.TMPDIR = '/';
37+
assert.equal(os.tmpdir(), '/');
2838
}
2939

3040
var endianness = os.endianness();

0 commit comments

Comments
 (0)
Please sign in to comment.