Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace os.tmpDir with temp-dir to get real paths for temp directories #318

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/filesystem.js
@@ -1,6 +1,6 @@
'use strict';

const os = require('os');
const tmpdir = require('temp-dir');
const path = require('path');

const Directory = require('./directory');
Expand Down Expand Up @@ -56,7 +56,7 @@ function FileSystem(options) {
}

if (createTmp) {
defaults.push((os.tmpdir && os.tmpdir()) || os.tmpDir());
defaults.push(tmpdir);
}

defaults.forEach(function(dir) {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -41,6 +41,7 @@
"eslint-config-tschaub": "^13.1.0",
"mocha": "^6.1.4",
"rimraf": "^2.6.3",
"semver": "^6.0.0"
"semver": "^6.0.0",
"temp-dir": "^2.0.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -47,7 +47,7 @@ Some of these breaking changes may be restored in a future release.

Configure the `fs` module so it is backed by an in-memory file system.

Calling `mock` sets up a mock file system with two directories by default: `process.cwd()` and `os.tmpdir()` (or `os.tmpDir()` for older Node). When called with no arguments, just these two directories are created. When called with a `config` object, additional files, directories, and symlinks are created. To avoid creating a directory for `process.cwd()` and `os.tmpdir()`, see the [`options`](#options) below.
Calling `mock` sets up a mock file system with two directories by default: `process.cwd()` and `os.tmpdir()` (using [`temp-dir`](https://www.npmjs.com/package/temp-dir) to get the real path, not a symlink). When called with no arguments, just these two directories are created. When called with a `config` object, additional files, directories, and symlinks are created. To avoid creating a directory for `process.cwd()` and `os.tmpdir()`, see the [`options`](#options) below.

Property names of the `config` object are interpreted as relative paths to resources (relative from `process.cwd()`). Property values of the `config` object are interpreted as content or configuration for the generated resources.

Expand Down
12 changes: 5 additions & 7 deletions test/lib/filesystem.spec.js
@@ -1,6 +1,6 @@
'use strict';

const os = require('os');
const tmpdir = require('temp-dir');
const path = require('path');

const Directory = require('../../lib/directory');
Expand All @@ -25,12 +25,11 @@ describe('FileSystem', function() {
});

it('accepts a createTmp option', function() {
const tmp = os.tmpdir ? os.tmpdir() : os.tmpDir();
const withTmp = new FileSystem({createTmp: true});
const withoutTmp = new FileSystem({createTmp: false});

assert.instanceOf(withTmp.getItem(tmp), Directory);
assert.isNull(withoutTmp.getItem(tmp));
assert.instanceOf(withTmp.getItem(tmpdir), Directory);
assert.isNull(withoutTmp.getItem(tmpdir));
});
});

Expand Down Expand Up @@ -167,15 +166,14 @@ describe('FileSystem.create', function() {

it('passes options to the FileSystem constructor', function() {
const cwd = process.cwd();
const tmp = os.tmpdir ? os.tmpdir() : os.tmpDir();

const withoutCwd = FileSystem.create({}, {createCwd: false});
const withoutTmp = FileSystem.create({}, {createTmp: false});

assert.isNull(withoutCwd.getItem(cwd));
assert.instanceOf(withoutCwd.getItem(tmp), Directory);
assert.instanceOf(withoutCwd.getItem(tmpdir), Directory);

assert.isNull(withoutTmp.getItem(tmp));
assert.isNull(withoutTmp.getItem(tmpdir));
assert.instanceOf(withoutTmp.getItem(cwd), Directory);
});

Expand Down
22 changes: 3 additions & 19 deletions test/lib/index.spec.js
Expand Up @@ -3,7 +3,7 @@
const helper = require('../helper');
const fs = require('fs');
const mock = require('../../lib/index');
const os = require('os');
const tmpdir = require('temp-dir');
const path = require('path');
const File = require('../../lib/file');
const {fixWin32Permissions} = require('../../lib/item');
Expand Down Expand Up @@ -36,15 +36,7 @@ describe('The API', function() {
mock();

assert.isTrue(fs.statSync(process.cwd()).isDirectory());
let tmp;
if (os.tmpdir) {
tmp = os.tmpdir();
} else if (os.tmpDir) {
tmp = os.tmpDir();
}
if (tmp) {
assert.isTrue(fs.statSync(tmp).isDirectory());
}
assert.isTrue(fs.statSync(tmpdir).isDirectory());

mock.restore();
});
Expand All @@ -60,15 +52,7 @@ describe('The API', function() {
it('passes the createTmp option to the FileSystem constructor', function() {
mock({}, {createTmp: false});

let tmp;
if (os.tmpdir) {
tmp = os.tmpdir();
} else if (os.tmpDir) {
tmp = os.tmpDir();
}
if (tmp) {
assert.isFalse(fs.existsSync(tmp));
}
assert.isFalse(fs.existsSync(tmpdir));

mock.restore();
});
Expand Down