From a11bf99ce0dae4d8f4de8a9c0c32159c1a9ecfbf Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 12 Jun 2012 19:05:51 -0700 Subject: [PATCH] Fix #3407 os.tmpDir() --- doc/api/os.markdown | 4 ++++ lib/os.js | 9 +++++++++ test/simple/test-os.js | 12 ++++++++++++ 3 files changed, 25 insertions(+) diff --git a/doc/api/os.markdown b/doc/api/os.markdown index 33eb9b6317d..b5fffcf1cb4 100644 --- a/doc/api/os.markdown +++ b/doc/api/os.markdown @@ -6,6 +6,10 @@ Provides a few basic operating-system related utility functions. Use `require('os')` to access this module. +## os.tmpDir() + +Returns the operating system's default directory for temp files. + ## os.hostname() Returns the hostname of the operating system. diff --git a/lib/os.js b/lib/os.js index c21971c84b5..47d1c565e6e 100644 --- a/lib/os.js +++ b/lib/os.js @@ -30,13 +30,22 @@ exports.cpus = binding.getCPUs; exports.type = binding.getOSType; exports.release = binding.getOSRelease; exports.networkInterfaces = binding.getInterfaceAddresses; + exports.arch = function() { return process.arch; }; + exports.platform = function() { return process.platform; }; +exports.tmpDir = function() { + return process.env.TMPDIR || + process.env.TMP || + process.env.TEMP || + (process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp'); +}; + exports.getNetworkInterfaces = function() { return exports.networkInterfaces(); }; diff --git a/test/simple/test-os.js b/test/simple/test-os.js index a70142f9f7b..b141f000893 100644 --- a/test/simple/test-os.js +++ b/test/simple/test-os.js @@ -27,6 +27,18 @@ var assert = require('assert'); var os = require('os'); +process.env.TMPDIR = '/tmpdir'; +process.env.TMP = '/tmp'; +process.env.TEMP = '/temp'; +var t = ( process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp' ); +assert.equal(os.tmpDir(), '/tmpdir'); +process.env.TMPDIR = ''; +assert.equal(os.tmpDir(), '/tmp'); +process.env.TMP = ''; +assert.equal(os.tmpDir(), '/temp'); +process.env.TEMP = ''; +assert.equal(os.tmpDir(), t); + var hostname = os.hostname(); console.log('hostname = %s', hostname); assert.ok(hostname.length > 0);