diff --git a/lib/os.js b/lib/os.js index 75a863e2f72d1e..cd4eb1c12f49bc 100644 --- a/lib/os.js +++ b/lib/os.js @@ -23,16 +23,20 @@ exports.platform = function() { }; exports.tmpdir = function() { + var path; if (isWindows) { - return process.env.TEMP || + path = process.env.TEMP || process.env.TMP || (process.env.SystemRoot || process.env.windir) + '\\temp'; } else { - return process.env.TMPDIR || + path = process.env.TMPDIR || process.env.TMP || process.env.TEMP || '/tmp'; } + if (/[\\\/]$/.test(path)) + path = path.slice(0, -1); + return path; }; exports.tmpDir = exports.tmpdir; diff --git a/test/parallel/test-os.js b/test/parallel/test-os.js index ae57353435333a..b5f39973a6f71f 100644 --- a/test/parallel/test-os.js +++ b/test/parallel/test-os.js @@ -13,6 +13,8 @@ if (process.platform === 'win32') { process.env.TMP = ''; var expected = (process.env.SystemRoot || process.env.windir) + '\\temp'; assert.equal(os.tmpdir(), expected); + process.env.TEMP = '\\temp\\'; + assert.equal(os.tmpdir(), '\\temp'); } else { assert.equal(os.tmpdir(), '/tmpdir'); process.env.TMPDIR = ''; @@ -21,6 +23,8 @@ if (process.platform === 'win32') { assert.equal(os.tmpdir(), '/temp'); process.env.TEMP = ''; assert.equal(os.tmpdir(), '/tmp'); + process.env.TMPDIR = '/tmpdir/'; + assert.equal(os.tmpdir(), '/tmpdir'); } var endianness = os.endianness();