From b57cc51d8d3f4ad279591ae8fa6584ee22773b97 Mon Sep 17 00:00:00 2001 From: Christian Tellnes Date: Fri, 6 Feb 2015 23:27:22 +0100 Subject: [PATCH] os: remove trailing slash from os.tmpdir() This commit makes `os.tmpdir()` behave consistently on all platforms. It changes `os.tmpdir()` to always return a path without trailing slash. Semver: major Fixes: https://github.com/iojs/io.js/issues/715 PR-URL: https://github.com/iojs/io.js/pull/747 Reviewed-By: Ben Noordhuis --- lib/os.js | 8 ++++++-- test/parallel/test-os.js | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) 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();