Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b3c7b1

Browse files
authored
refactor: don't expose tempdir in common.state (#903)
Previously, the cached `tempdir` value was stored in `common.state`. Unlike the other `common.state` values, this isn't immediately useful to other commands (they can just call the tempdir API). So, this moves the cached value into `tempdir.js`. This also adds a unit test for the caching behavior, and exposes test-only helpers to verify this behavior. Finally, this adds a note to `common.state` that values should generally be considered read-only, since this can be important for customized behavior. Although, I recognize our code base has one exception to this rule (`echo()`), we should strive to maintain this. Fixes #902 Test: Added a unit test.
1 parent 4bd22e7 commit 6b3c7b1

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ var config = {
4242
config.reset();
4343
exports.config = config;
4444

45+
// Note: commands should generally consider these as read-only values.
4546
var state = {
4647
error: null,
4748
errorCode: 0,
4849
currentCmd: 'shell.js',
49-
tempDir: null,
5050
};
5151
exports.state = state;
5252

src/exec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var common = require('./common');
2-
var _tempDir = require('./tempdir');
2+
var _tempDir = require('./tempdir').tempDir;
33
var _pwd = require('./pwd');
44
var path = require('path');
55
var fs = require('fs');

src/tempdir.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function writeableDir(dir) {
2424
}
2525
}
2626

27+
// Variable to cache the tempdir value for successive lookups.
28+
var cachedTempDir;
2729

2830
//@
2931
//@ ### tempdir()
@@ -37,10 +39,9 @@ function writeableDir(dir) {
3739
//@ Searches and returns string containing a writeable, platform-dependent temporary directory.
3840
//@ Follows Python's [tempfile algorithm](http://docs.python.org/library/tempfile.html#tempfile.tempdir).
3941
function _tempDir() {
40-
var state = common.state;
41-
if (state.tempDir) return state.tempDir; // from cache
42+
if (cachedTempDir) return cachedTempDir;
4243

43-
state.tempDir = writeableDir(os.tmpdir()) ||
44+
cachedTempDir = writeableDir(os.tmpdir()) ||
4445
writeableDir(process.env.TMPDIR) ||
4546
writeableDir(process.env.TEMP) ||
4647
writeableDir(process.env.TMP) ||
@@ -54,6 +55,21 @@ function _tempDir() {
5455
writeableDir('/usr/tmp') ||
5556
writeableDir('.'); // last resort
5657

57-
return state.tempDir;
58+
return cachedTempDir;
5859
}
59-
module.exports = _tempDir;
60+
61+
// Indicates if the tempdir value is currently cached. This is exposed for tests
62+
// only. The return value should only be tested for truthiness.
63+
function isCached() {
64+
return cachedTempDir;
65+
}
66+
67+
// Clears the cached tempDir value, if one is cached. This is exposed for tests
68+
// only.
69+
function clearCache() {
70+
cachedTempDir = undefined;
71+
}
72+
73+
module.exports.tempDir = _tempDir;
74+
module.exports.isCached = isCached;
75+
module.exports.clearCache = clearCache;

test/tempdir.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs';
33
import test from 'ava';
44

55
import shell from '..';
6+
import { isCached, clearCache } from '../src/tempdir';
67

78
shell.config.silent = true;
89

@@ -19,3 +20,12 @@ test('basic usage', t => {
1920
// It's a directory
2021
t.truthy(shell.test('-d', tmp));
2122
});
23+
24+
test('cache', t => {
25+
clearCache(); // In case this runs after any test which relies on tempdir().
26+
t.falsy(isCached());
27+
const tmp1 = shell.tempdir();
28+
t.truthy(isCached());
29+
const tmp2 = shell.tempdir();
30+
t.is(tmp1, tmp2);
31+
});

0 commit comments

Comments
 (0)