diff --git a/lib/os.js b/lib/os.js index cd4eb1c12f..4426612285 100644 --- a/lib/os.js +++ b/lib/os.js @@ -22,6 +22,9 @@ exports.platform = function() { return process.platform; }; +const trailingSlashRe = isWindows ? /[^:]\\$/ + : /.\/$/; + exports.tmpdir = function() { var path; if (isWindows) { @@ -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; }; diff --git a/test/parallel/test-os.js b/test/parallel/test-os.js index b5f39973a6..dd1a1ae339 100644 --- a/test/parallel/test-os.js +++ b/test/parallel/test-os.js @@ -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 = ''; @@ -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();