Skip to content

Commit a11bf99

Browse files
committed
Fix nodejs#3407 os.tmpDir()
1 parent 0dba4ad commit a11bf99

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

doc/api/os.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Provides a few basic operating-system related utility functions.
66

77
Use `require('os')` to access this module.
88

9+
## os.tmpDir()
10+
11+
Returns the operating system's default directory for temp files.
12+
913
## os.hostname()
1014

1115
Returns the hostname of the operating system.

lib/os.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,22 @@ exports.cpus = binding.getCPUs;
3030
exports.type = binding.getOSType;
3131
exports.release = binding.getOSRelease;
3232
exports.networkInterfaces = binding.getInterfaceAddresses;
33+
3334
exports.arch = function() {
3435
return process.arch;
3536
};
37+
3638
exports.platform = function() {
3739
return process.platform;
3840
};
3941

42+
exports.tmpDir = function() {
43+
return process.env.TMPDIR ||
44+
process.env.TMP ||
45+
process.env.TEMP ||
46+
(process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp');
47+
};
48+
4049
exports.getNetworkInterfaces = function() {
4150
return exports.networkInterfaces();
4251
};

test/simple/test-os.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ var assert = require('assert');
2727
var os = require('os');
2828

2929

30+
process.env.TMPDIR = '/tmpdir';
31+
process.env.TMP = '/tmp';
32+
process.env.TEMP = '/temp';
33+
var t = ( process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp' );
34+
assert.equal(os.tmpDir(), '/tmpdir');
35+
process.env.TMPDIR = '';
36+
assert.equal(os.tmpDir(), '/tmp');
37+
process.env.TMP = '';
38+
assert.equal(os.tmpDir(), '/temp');
39+
process.env.TEMP = '';
40+
assert.equal(os.tmpDir(), t);
41+
3042
var hostname = os.hostname();
3143
console.log('hostname = %s', hostname);
3244
assert.ok(hostname.length > 0);

0 commit comments

Comments
 (0)