| 1 |
var fs = require('fs'); |
| 2 |
var path = require('path'); |
| 3 |
var os = require('os'); |
| 4 |
|
| 5 |
var async = require('async'); |
| 6 |
var expect = require('expect'); |
| 7 |
var proxyquire = require('proxyquire'); |
| 8 |
|
| 9 |
var env = process.env; |
| 10 |
|
| 11 |
function eraseHome() { |
| 12 |
delete env.HOME; |
| 13 |
delete env.USERPROFILE; |
| 14 |
delete env.HOMEDRIVE; |
| 15 |
delete env.HOMEPATH; |
| 16 |
delete env.LOGNAME; |
| 17 |
delete env.USER; |
| 18 |
delete env.LNAME; |
| 19 |
delete env.USERNAME; |
| 20 |
delete env.XDG_CACHE_HOME; |
| 21 |
delete env.LOCALAPPDATA; |
| 22 |
} |
| 23 |
|
| 24 |
var tmpdir = env.TMPDIR; |
| 25 |
var temp = env.TEMP; |
| 26 |
var tmp = env.TMP; |
| 27 |
|
| 28 |
function setTemp(dir) { |
| 29 |
env.TMPDIR = env.TEMP = env.TMP = dir; |
| 30 |
} |
| 31 |
|
| 32 |
function resetTemp() { |
| 33 |
env.TMPDIR = tmpdir; |
| 34 |
env.TEMP = temp; |
| 35 |
env.TMP = tmp; |
| 36 |
} |
| 37 |
|
| 38 |
function cleanup() { |
| 39 |
var v8flags = require('../'); |
| 40 |
|
| 41 |
var files = [ |
| 42 |
path.resolve(v8flags.configPath, v8flags.configfile), |
| 43 |
path.resolve(os.tmpdir(), v8flags.configfile), |
| 44 |
]; |
| 45 |
files.forEach(function(file) { |
| 46 |
try { |
| 47 |
fs.unlinkSync(file); |
| 48 |
} catch (e) { |
| 49 |
// Ignore error |
| 50 |
} |
| 51 |
}); |
| 52 |
|
| 53 |
delete require.cache[require.resolve('../')]; |
| 54 |
delete require.cache[require.resolve('../config-path')]; |
| 55 |
delete require.cache[require.resolve('homedir-polyfill')]; |
| 56 |
|
| 57 |
delete process.versions.electron; |
| 58 |
} |
| 59 |
|
| 60 |
describe('v8flags', function() { |
| 61 |
beforeEach(cleanup); |
| 62 |
afterEach(cleanup); |
| 63 |
|
| 64 |
it('should cache and call back with the v8 flags for the running process', function(done) { |
| 65 |
var v8flags = require('../'); |
| 66 |
var configfile = path.resolve(v8flags.configPath, v8flags.configfile); |
| 67 |
v8flags(function(err, flags) { |
| 68 |
expect(flags).toBeAn('array'); |
| 69 |
expect(fs.existsSync(configfile)).toEqual(true); |
| 70 |
fs.unlinkSync(configfile); |
| 71 |
fs.writeFileSync(configfile, JSON.stringify({ cached: true })); |
| 72 |
v8flags(function(cacheErr, cachedFlags) { |
| 73 |
expect(cachedFlags).toEqual({ cached: true }); |
| 74 |
done(); |
| 75 |
}); |
| 76 |
}); |
| 77 |
}); |
| 78 |
|
| 79 |
it('should not append the file when multiple calls happen concurrently and the config file does not yet exist', function(done) { |
| 80 |
var v8flags = require('../'); |
| 81 |
var configfile = path.resolve(v8flags.configPath, v8flags.configfile); |
| 82 |
async.parallel([v8flags, v8flags, v8flags], function(err, result) { |
| 83 |
v8flags(function(err2, res) { |
| 84 |
done(); |
| 85 |
}); |
| 86 |
}); |
| 87 |
}); |
| 88 |
|
| 89 |
it('should fall back to writing to a temp dir if user home is unwriteable', function(done) { |
| 90 |
eraseHome(); |
| 91 |
env.HOME = env.LOCALAPPDATA = path.join(__dirname, 'does-not-exist'); |
| 92 |
var v8flags = require('../'); |
| 93 |
var configfile = path.resolve(os.tmpdir(), v8flags.configfile); |
| 94 |
v8flags(function(err, flags) { |
| 95 |
expect(fs.existsSync(configfile)).toEqual(true); |
| 96 |
done(); |
| 97 |
}); |
| 98 |
}); |
| 99 |
|
| 100 |
it('should return flags even if an error is thrown', function(done) { |
| 101 |
eraseHome(); |
| 102 |
setTemp('/nope'); |
| 103 |
env.HOME = env.LOCALAPPDATA = null; |
| 104 |
var v8flags = require('../'); |
| 105 |
v8flags(function(err, flags) { |
| 106 |
resetTemp(); |
| 107 |
expect(err).toNotBe(null); |
| 108 |
expect(flags).toNotBe(undefined); |
| 109 |
done(); |
| 110 |
}); |
| 111 |
}); |
| 112 |
|
| 113 |
it('will not throw on non-matching return from node --v8-options', function(done) { |
| 114 |
if (os.platform() === 'win32') { |
| 115 |
this.skip(); |
| 116 |
} |
| 117 |
|
| 118 |
eraseHome(); |
| 119 |
var v8flags = require('../'); |
| 120 |
|
| 121 |
// Save original execPath |
| 122 |
var execPath = process.execPath; |
| 123 |
// Set execPath to our fake-bin |
| 124 |
process.execPath = __dirname + '/fake-bin'; |
| 125 |
|
| 126 |
v8flags(function(err, flags) { |
| 127 |
expect(err).toNotExist(); |
| 128 |
expect(flags).toEqual([]); |
| 129 |
// Restore original execPath |
| 130 |
process.execPath = execPath; |
| 131 |
done(); |
| 132 |
}); |
| 133 |
}); |
| 134 |
|
| 135 |
it('should back with an empty array if the runtime is electron', function(done) { |
| 136 |
process.versions.electron = 'set'; |
| 137 |
var v8flags = require('../'); |
| 138 |
v8flags(function(err, flags) { |
| 139 |
expect(flags.length).toEqual(0); |
| 140 |
expect(flags).toBeAn('array'); |
| 141 |
done(); |
| 142 |
}); |
| 143 |
}); |
| 144 |
|
| 145 |
it('should handle usernames which are invalid file paths', function(done) { |
| 146 |
eraseHome(); |
| 147 |
env.USER = 'invalid/user\\name'; |
| 148 |
var v8flags = require('../'); |
| 149 |
v8flags(function(err, flags) { |
| 150 |
expect(err).toBe(null); |
| 151 |
done(); |
| 152 |
}); |
| 153 |
}); |
| 154 |
|
| 155 |
it('should handle option names with multiple words', function(done) { |
| 156 |
if (parseInt(process.versions.node) < 4) { |
| 157 |
this.skip(); |
| 158 |
} |
| 159 |
|
| 160 |
eraseHome(); |
| 161 |
var v8flags = require('../'); |
| 162 |
v8flags(function(err, flags) { |
| 163 |
expect(flags).toInclude('--expose_gc_as'); |
| 164 |
done(); |
| 165 |
}); |
| 166 |
}); |
| 167 |
|
| 168 |
it('should handle undefined usernames', function(done) { |
| 169 |
eraseHome(); |
| 170 |
var v8flags = require('../'); |
| 171 |
v8flags(function(err, flags) { |
| 172 |
expect(err).toBe(null); |
| 173 |
done(); |
| 174 |
}); |
| 175 |
}); |
| 176 |
|
| 177 |
it('should detect non-v8 flags', function(done) { |
| 178 |
eraseHome(); |
| 179 |
var v8flags = require('../'); |
| 180 |
v8flags(function(err, flags) { |
| 181 |
expect(flags).toInclude('--no_deprecation'); |
| 182 |
done(); |
| 183 |
}); |
| 184 |
}); |
| 185 |
|
| 186 |
it('does not detect colliding flags from node', function(done) { |
| 187 |
eraseHome(); |
| 188 |
var v8flags = require('../'); |
| 189 |
v8flags(function(err, flags) { |
| 190 |
expect(flags).toNotInclude('--exec'); |
| 191 |
expect(flags).toNotInclude('--print'); |
| 192 |
expect(flags).toNotInclude('--interactive'); |
| 193 |
expect(flags).toNotInclude('--require'); |
| 194 |
expect(flags).toNotInclude('--version'); |
| 195 |
done(); |
| 196 |
}); |
| 197 |
}); |
| 198 |
}); |
| 199 |
|
| 200 |
describe('config-path', function() { |
| 201 |
var moduleName = 'js-v8flags'; |
| 202 |
|
| 203 |
before(function() { |
| 204 |
env.HOME = env.USERPROFILE = 'somehome'; |
| 205 |
}); |
| 206 |
|
| 207 |
after(cleanup); |
| 208 |
|
| 209 |
it('should return default linux path in other environments', function(done) { |
| 210 |
var configPath = require('../config-path.js')('other'); |
| 211 |
|
| 212 |
expect(configPath).toEqual( |
| 213 |
path.join(env.HOME, '.cache', moduleName) |
| 214 |
); |
| 215 |
done(); |
| 216 |
}); |
| 217 |
|
| 218 |
it('should return default macos path in darwin environment', function(done) { |
| 219 |
var configPath = require('../config-path.js')('darwin'); |
| 220 |
|
| 221 |
expect(configPath).toEqual( |
| 222 |
path.join(env.HOME, 'Library', 'Caches', moduleName) |
| 223 |
); |
| 224 |
done(); |
| 225 |
}); |
| 226 |
|
| 227 |
it('should return default windows path in win32 environment', function(done) { |
| 228 |
var configPath = require('../config-path.js')('win32'); |
| 229 |
|
| 230 |
expect(configPath).toEqual( |
| 231 |
path.join(env.HOME, 'AppData', 'Local', moduleName) |
| 232 |
); |
| 233 |
done(); |
| 234 |
}); |
| 235 |
|
| 236 |
it('should return fallback path when homedir is falsy', function(done) { |
| 237 |
var configPath = proxyquire('../config-path.js', { |
| 238 |
'homedir-polyfill': function() { |
| 239 |
return null; |
| 240 |
}, |
| 241 |
})('win32'); |
| 242 |
|
| 243 |
expect(configPath).toEqual(os.tmpdir()); |
| 244 |
done(); |
| 245 |
}); |
| 246 |
}); |
| 247 |
|
| 248 |
describe('platform specific tests', function() { |
| 249 |
before(cleanup); |
| 250 |
|
| 251 |
it('should return fallback path when no home is found under windows', function(done) { |
| 252 |
if (os.platform() !== 'win32' || !process.version.match(/0\.10|0\.12/)) { |
| 253 |
this.skip(); |
| 254 |
} |
| 255 |
|
| 256 |
eraseHome(); |
| 257 |
var configPath = require('../config-path.js')('win32'); |
| 258 |
|
| 259 |
expect(configPath).toEqual(os.tmpdir()); |
| 260 |
done(); |
| 261 |
}); |
| 262 |
}); |