shelljs/test/tempdir.js
Nate Fischer 6b3c7b1e44
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.
2018-11-08 23:00:22 -08:00

32 lines
588 B
JavaScript

import fs from 'fs';
import test from 'ava';
import shell from '..';
import { isCached, clearCache } from '../src/tempdir';
shell.config.silent = true;
//
// Valids
//
test('basic usage', t => {
const tmp = shell.tempdir();
t.falsy(shell.error());
t.truthy(fs.existsSync(tmp));
// It's a directory
t.truthy(shell.test('-d', tmp));
});
test('cache', t => {
clearCache(); // In case this runs after any test which relies on tempdir().
t.falsy(isCached());
const tmp1 = shell.tempdir();
t.truthy(isCached());
const tmp2 = shell.tempdir();
t.is(tmp1, tmp2);
});