Skip to content

Commit

Permalink
os: refine tmpdir() trailing slash stripping
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cjihrig committed May 13, 2015
1 parent 966acb9 commit 7693705
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ exports.platform = function() {
return process.platform;
};

const trailingSlashRe = isWindows ? /[^:]\\$/
: /.\/$/;

exports.tmpdir = function() {
var path;
if (isWindows) {
Expand All @@ -34,7 +37,7 @@ exports.tmpdir = function() {
process.env.TEMP ||
'/tmp';
}
if (/[\\\/]$/.test(path))
if (trailingSlashRe.test(path))
path = path.slice(0, -1);
return path;
};
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-os.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ if (process.platform === 'win32') {
assert.equal(os.tmpdir(), expected);
process.env.TEMP = '\\temp\\';
assert.equal(os.tmpdir(), '\\temp');
process.env.TEMP = '\\tmpdir/';
assert.equal(os.tmpdir(), '\\tmpdir/');
process.env.TEMP = '\\';
assert.equal(os.tmpdir(), '\\');
process.env.TEMP = 'C:\\';
assert.equal(os.tmpdir(), 'C:\\');
} else {
assert.equal(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '';
Expand All @@ -25,6 +31,10 @@ if (process.platform === 'win32') {
assert.equal(os.tmpdir(), '/tmp');
process.env.TMPDIR = '/tmpdir/';
assert.equal(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '/tmpdir\\';
assert.equal(os.tmpdir(), '/tmpdir\\');
process.env.TMPDIR = '/';
assert.equal(os.tmpdir(), '/');
}

var endianness = os.endianness();
Expand Down

0 comments on commit 7693705

Please sign in to comment.