File: index.js

package info (click to toggle)
node-v8flags 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 208 kB
  • sloc: javascript: 353; makefile: 2
file content (262 lines) | stat: -rw-r--r-- 6,851 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
var fs = require('fs');
var path = require('path');
var os = require('os');

var async = require('async');
var expect = require('chai').expect;
//var proxyquire = require('proxyquire');

var env = process.env;

function eraseHome() {
  delete env.HOME;
  delete env.USERPROFILE;
  delete env.HOMEDRIVE;
  delete env.HOMEPATH;
  delete env.LOGNAME;
  delete env.USER;
  delete env.LNAME;
  delete env.USERNAME;
  delete env.XDG_CACHE_HOME;
  delete env.LOCALAPPDATA;
}

var tmpdir = env.TMPDIR;
var temp = env.TEMP;
var 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('../');

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

  delete require.cache[require.resolve('../')];
  delete require.cache[require.resolve('../config-path')];

  delete process.versions.electron;
}

describe('v8flags', function() {
  beforeEach(cleanup);
  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);
    v8flags(function(err, flags) {
      expect(flags).to.be.a('array');
      expect(fs.existsSync(configfile)).to.equal(true);
      fs.unlinkSync(configfile);
      fs.writeFileSync(configfile, JSON.stringify({ cached: true }));
      v8flags(function(cacheErr, cachedFlags) {
        expect(cachedFlags).to.deep.equal({ cached: true });
        done();
      });
    });
  });

  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);
    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 is unwriteable', function(done) {
    eraseHome();
    env.HOME = env.LOCALAPPDATA = path.join(__dirname, 'does-not-exist');
    var v8flags = require('../');
    var configfile = path.resolve(os.tmpdir(), v8flags.configfile);
    v8flags(function(err, flags) {
      expect(fs.existsSync(configfile)).to.equal(true);
      done();
    });
  });

  it('should return flags even if an error is thrown', function(done) {
    eraseHome();
    setTemp('/nope');
    env.HOME = env.LOCALAPPDATA = null;
    var v8flags = require('../');
    v8flags(function(err, flags) {
      resetTemp();
      expect(err).to.not.be.a('null');
      expect(flags).to.not.be.a('undefined');
      done();
    });
  });

  /*
  it('will not throw on non-matching return from node --v8-options', function(done) {
    if (os.platform() === 'win32') {
      this.skip();
    }

    eraseHome();
    var v8flags = require('../');

    // Save original execPath
    var execPath = process.execPath;
    // Set execPath to our fake-bin
    process.execPath = __dirname + '/fake-bin';

    v8flags(function(err, flags) {
      expect(err).to.be.null;
      // Restore original execPath
      process.execPath = execPath;
      done();
    });
  });
  */

  it('should back with an empty array if the runtime is electron', function(done) {
    process.versions.electron = 'set';
    var v8flags = require('../');
    v8flags(function(err, flags) {
      expect(flags.length).to.equal(0);
      expect(flags).to.be.a('array');
      done();
    });
  });

  it.skip('should handle usernames which are invalid file paths', function(done) {
    eraseHome();
    env.USER = 'invalid/user\\name';
    var v8flags = require('../');
    v8flags(function(err, flags) {
      expect(err).toBe(null);
      done();
    });
  });

  it('should handle option names with multiple words', function(done) {
    if (parseInt(process.versions.node) < 4) {
      this.skip();
    }

    eraseHome();
    var v8flags = require('../');
    v8flags(function(err, flags) {
      expect(flags).to.include('--expose_gc_as');
      done();
    });
  });

  it.skip('should handle undefined usernames', function(done) {
    eraseHome();
    var v8flags = require('../');
    v8flags(function(err, flags) {
      expect(err).toBe(null);
      done();
    });
  });

  it('should detect non-v8 flags', function(done) {
    eraseHome();
    var v8flags = require('../');
    v8flags(function(err, flags) {
      expect(flags).to.include('--no_deprecation');
      done();
    });
  });

  it('does not detect colliding flags from node', function(done) {
    eraseHome();
    var v8flags = require('../');
    v8flags(function(err, flags) {
      expect(flags).to.not.include('--exec');
      expect(flags).to.not.include('--print');
      expect(flags).to.not.include('--interactive');
      expect(flags).to.not.include('--require');
      expect(flags).to.not.include('--version');
      done();
    });
  });
});

describe('config-path', function() {
  var moduleName = 'js-v8flags';

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

  after(cleanup);

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

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

  it('should return default macos path in darwin environment', function(done) {
    var configPath = require('../config-path.js')('darwin');

    expect(configPath).to.equal(
      path.join(env.HOME, 'Library', 'Caches', moduleName)
    );
    done();
  });

  it('should return default windows path in win32 environment', function(done) {
    var configPath = require('../config-path.js')('win32');

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

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

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

  it.skip('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();
    var configPath = require('../config-path.js')('win32');

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