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

Attempt to cache using a temp file if home is unwriteable #30

Merged
merged 1 commit into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ function fail (err) {

function openConfig (cb) {
var userHome = require('user-home');
var configpath = path.join(userHome || os.tmpdir(), configfile);
var content;
if (!userHome) {
return tryOpenConfig(path.join(os.tmpdir(), configfile), cb);
}

tryOpenConfig(path.join(userHome, configfile), function (err, fd) {
if (err) return tryOpenConfig(path.join(os.tmpdir(), configfile), cb);
return cb(null, fd);
});
}

function tryOpenConfig (configpath, cb) {
try {
// if the config file is valid, it should be json and therefore
// node should be able to require it directly. if this doesn't
Expand Down
44 changes: 28 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const os = require('os');

const async = require('async');
const expect = require('chai').expect;
Expand All @@ -17,30 +18,31 @@ function eraseHome() {
delete env.USERNAME;
}

function setTemp() {
env.TMPDIR = env.TEMP = env.TMP = '/nope';
function setTemp(dir) {
env.TMPDIR = env.TEMP = env.TMP = dir;
}

function cleanup () {
delete require.cache[require.resolve('os')];
delete require.cache[require.resolve('user-home')];
var os = require('os')
var v8flags = require('./');
try {
[
path.resolve(require('user-home'), v8flags.configfile),
path.resolve(os.tmpdir(), v8flags.configfile),
path.resolve('/tmp', v8flags.configfile)
].map(fs.unlinkSync);
} catch (e) {}
delete require.cache[require.resolve('os')];
var userHome = require('user-home');
if (userHome === null) userHome = __dirname;

var files = [
path.resolve(userHome, v8flags.configfile),
path.resolve(os.tmpdir(), v8flags.configfile),
];
files.forEach(function (file) {
try {
fs.unlinkSync(file);
} catch (e) {}
});

delete require.cache[require.resolve('user-home')];
delete process.versions.electron;
}

describe('v8flags', function () {

beforeEach(cleanup)
beforeEach(cleanup);
afterEach(cleanup);

it('should cache and call back with the v8 flags for the running process', function (done) {
Expand Down Expand Up @@ -70,7 +72,17 @@ describe('v8flags', function () {

it('should fall back to writing to a temp dir if user home can\'t be found', function (done) {
eraseHome();
var os = require('os');
var v8flags = require('./');
var configfile = path.resolve(os.tmpdir(), v8flags.configfile);
v8flags(function (err, flags) {
expect(fs.existsSync(configfile)).to.be.true;
done();
});
});

it('should fall back to writing to a temp dir if user home is unwriteable', function (done) {
eraseHome();
env.HOME = path.join(__dirname, 'does-not-exist');
var v8flags = require('./');
var configfile = path.resolve(os.tmpdir(), v8flags.configfile);
v8flags(function (err, flags) {
Expand Down