Skip to content

Commit

Permalink
Fix home directory lookup behavior & fallback (fixes #41) (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siilwyn authored and phated committed Dec 17, 2018
1 parent 7cdff62 commit 85c4091
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 65 deletions.
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
sudo: false
language: node_js
node_js:
- "stable"
- "4"
- "0.12"
- "0.10"
matrix:
fast_finish: true
sudo: false
- '8'
- '6'
- '5'
- '4'
- '0.12'
- '0.10'
12 changes: 9 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
init:
- git config --global core.autocrlf input
# http://www.appveyor.com/docs/appveyor-yml
# http://www.appveyor.com/docs/lang/nodejs-iojs

environment:
matrix:
# node.js
- nodejs_version: "0.10"
- nodejs_version: "0.12"
- nodejs_version: "4"
- nodejs_version: "5"
- nodejs_version: "6"
- nodejs_version: "8"

install:
- ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version)
- ps: Install-Product node $env:nodejs_version
- npm install

test_script:
Expand All @@ -20,4 +25,5 @@ test_script:

build: off

# build version format
version: "{build}"
9 changes: 7 additions & 2 deletions config-path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const os = require('os');
const path = require('path');
const userHome = require('user-home');
const userHome = require('homedir-polyfill')();

const env = process.env;
const name = 'js-v8flags';
Expand All @@ -11,7 +12,7 @@ function macos () {

function windows () {
const appData = env.LOCALAPPDATA || path.join(userHome, 'AppData', 'Local');
return path.join(appData, name, 'Cache');
return path.join(appData, name);
}

// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
Expand All @@ -21,6 +22,10 @@ function linux () {
}

module.exports = function (platform) {
if (!userHome) {
return os.tmpdir();
}

if (platform === 'darwin') {
return macos();
}
Expand Down
5 changes: 0 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ function fail (err) {
}

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

fs.mkdir(configPath, function () {
tryOpenConfig(path.join(configPath, configfile), function (err, fd) {
if (err) return tryOpenConfig(path.join(os.tmpdir(), configfile), cb);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
"devDependencies": {
"async": "^2.5.0",
"chai": "^4.1.0",
"mocha": "^3.4.2"
"mocha": "^3.4.2",
"proxyquire": "^1.8.0"
},
"dependencies": {
"user-home": "^1.1.1"
"homedir-polyfill": "^1.0.1"
}
}
113 changes: 67 additions & 46 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const os = require('os');

const async = require('async');
const expect = require('chai').expect;
const proxyquire = require('proxyquire');

const env = process.env;

function eraseHome() {
function eraseHome () {
delete env.HOME;
delete env.USERPROFILE;
delete env.HOMEDRIVE;
Expand All @@ -20,14 +21,24 @@ function eraseHome() {
delete env.LOCALAPPDATA;
}

function setTemp(dir) {
const tmpdir = env.TMPDIR;
const temp = env.TEMP;
const tmp = env.TMP;

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

function resetTemp() {
env.TMPDIR = tmpdir;
env.TEMP = temp;
env.TMP = tmp;
}

function cleanup () {
var v8flags = require('./');
const v8flags = require('./');

var files = [
const files = [
path.resolve(v8flags.configPath, v8flags.configfile),
path.resolve(os.tmpdir(), v8flags.configfile),
];
Expand All @@ -37,7 +48,10 @@ function cleanup () {
} catch (e) {}
});

delete require.cache[require.resolve('user-home')];
delete require.cache[require.resolve('./')];
delete require.cache[require.resolve('./config-path')];
delete require.cache[require.resolve('homedir-polyfill')];

delete process.versions.electron;
}

Expand All @@ -46,8 +60,8 @@ describe('v8flags', function () {
afterEach(cleanup);

it('should cache and call back with the v8 flags for the running process', function (done) {
var v8flags = require('./');
var configfile = path.resolve(v8flags.configPath, v8flags.configfile);
const v8flags = require('./');
const configfile = path.resolve(v8flags.configPath, v8flags.configfile);
v8flags(function (err, flags) {
expect(flags).to.be.a('array');
expect(fs.existsSync(configfile)).to.be.true;
Expand All @@ -61,34 +75,20 @@ describe('v8flags', function () {
});

it('should not append the file when multiple calls happen concurrently and the config file does not yet exist', function (done) {
var v8flags = require('./');
var configfile = path.resolve(v8flags.configPath, v8flags.configfile);
const v8flags = require('./');
const configfile = path.resolve(v8flags.configPath, v8flags.configfile);
async.parallel([v8flags, v8flags, v8flags], function (err, result) {
v8flags(function (err2, res) {
done();
});
});
});

it('should fall back to writing to a temp dir if user home can\'t be found', function (done) {
eraseHome();
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');
// Clear require cached modules so the modified environment variable HOME is used
delete require.cache[require.resolve('./')];
delete require.cache[require.resolve('./config-path.js')];
var v8flags = require('./');
v8flags.configPath = env.HOME;
var configfile = path.resolve(os.tmpdir(), v8flags.configfile);
env.HOME = env.LOCALAPPDATA = path.join(__dirname, 'does-not-exist');
const v8flags = require('./');
const configfile = path.resolve(os.tmpdir(), v8flags.configfile);
v8flags(function (err, flags) {
expect(fs.existsSync(configfile)).to.be.true;
done();
Expand All @@ -98,9 +98,10 @@ describe('v8flags', function () {
it('should return flags even if an error is thrown', function (done) {
eraseHome();
setTemp('/nope');
var v8flags = require('./');
env.HOME = env.LOCALAPPDATA = null;
const v8flags = require('./');
v8flags(function (err, flags) {
setTemp('/tmp');
resetTemp();
expect(err).to.not.be.null;
expect(flags).to.not.be.undefined;
done();
Expand All @@ -109,7 +110,7 @@ describe('v8flags', function () {

it('should back with an empty array if the runtime is electron', function (done) {
process.versions.electron = 'set';
var v8flags = require('./');
const v8flags = require('./');
v8flags(function (err, flags) {
expect(flags).to.have.length(0);
expect(flags).to.be.an('array');
Expand All @@ -120,9 +121,7 @@ describe('v8flags', function () {
it('should handle usernames which are invalid file paths', function(done) {
eraseHome();
env.USER = 'invalid/user\\name';
delete require.cache[require.resolve('./')];
var v8flags = require('./');
console.log(v8flags.configfile);
const v8flags = require('./');
v8flags(function (err, flags) {
expect(err).to.be.null;
done();
Expand All @@ -131,9 +130,7 @@ describe('v8flags', function () {

it('should handle undefined usernames', function(done) {
eraseHome();
delete require.cache[require.resolve('./')];
var v8flags = require('./');
console.log(v8flags.configfile);
const v8flags = require('./');
v8flags(function (err, flags) {
expect(err).to.be.null;
done();
Expand All @@ -144,14 +141,13 @@ describe('v8flags', function () {
describe('config-path', function () {
const moduleName = 'js-v8flags';

beforeEach(function() {
env.HOME = 'somehome';
cleanup();
before(function () {
env.HOME = env.USERPROFILE = 'somehome';
});
afterEach(cleanup);

it('should return default linux path in other environments', function(done) {
delete require.cache[require.resolve('./config-path.js')];
after(cleanup);

it('should return default linux path in other environments', function (done) {
const configPath = require('./config-path.js')('other');

expect(configPath).to.equal(
Expand All @@ -160,8 +156,7 @@ describe('config-path', function () {
done();
});

it('should return default macos path in darwin environment', function(done) {
delete require.cache[require.resolve('./config-path.js')];
it('should return default macos path in darwin environment', function (done) {
const configPath = require('./config-path.js')('darwin');

expect(configPath).to.equal(
Expand All @@ -170,13 +165,39 @@ describe('config-path', function () {
done();
});

it('should return default windows path in win32 environment', function(done) {
delete require.cache[require.resolve('./config-path.js')];
it('should return default windows path in win32 environment', function (done) {
const configPath = require('./config-path.js')('win32');

expect(configPath).to.equal(
path.join(env.HOME, 'AppData', 'Local', moduleName, 'Cache')
path.join(env.HOME, 'AppData', 'Local', moduleName)
);
done();
});

it('should return fallback path when homedir is falsy', function (done) {
const configPath = proxyquire('./config-path.js', {
'homedir-polyfill': function () {
return null;
}
})('win32');

expect(configPath).to.equal(os.tmpdir());
done();
});
});

describe('platform specific tests', function () {
before(cleanup);

it('should return fallback path when no home is found under windows', function (done) {
if (os.platform() !== 'win32' || !process.version.match(/0\.10|0\.12/)) {
this.skip();
}

eraseHome();
const configPath = require('./config-path.js')('win32');

expect(configPath).to.equal(os.tmpdir());
done();
});
});

0 comments on commit 85c4091

Please sign in to comment.