/
load.test.js
324 lines (273 loc) · 10 KB
/
load.test.js
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
'use strict';
/*global describe, it, afterEach, beforeEach, after */
var load = require('../../lib/config/load');
var defaults = require('../../lib/config/defaults');
var cli = require('../../lib/cli/');
var path = require('path');
var testUtils = require('../utils');
var utils = require('../../lib/utils');
var rules = require('../../lib/rules');
var exec = require('../../lib/config/exec');
var nodemon = require('../../lib/nodemon');
var command = require('../../lib/config/command');
var assert = require('assert');
function asCLI(cmd) {
return ('node nodemon ' + (cmd || '')).trim();
}
function commandToString(command) {
return utils.stringify(command.executable, command.args);
}
describe('config load', function () {
var pwd = process.cwd(),
oldhome = utils.home;
afterEach(function () {
process.chdir(pwd);
utils.home = oldhome;
});
after(function (done) {
// clean up just in case.
nodemon.once('exit', function () {
nodemon.reset(done);
}).emit('quit');
});
function removeRegExp(options) {
delete options.watch.re;
delete options.ignore.re;
}
beforeEach(function () {
// move to the fixtures directory to allow for config loading
process.chdir(path.resolve(pwd, 'test/fixtures'));
utils.home = path.resolve(pwd, ['test', 'fixtures', 'global'].join(path.sep));
rules.reset();
nodemon.config.reset();
});
it('should remove ignore defaults if user provides their own', function (done) {
nodemon({
script: testUtils.appjs,
verbose: true
}).on('log', function (event) {
// console.log(event.colour);
}).on('start', function () {
assert.ok(nodemon.config.options.ignore.indexOf('one') !== -1, 'Contains "one" path');
assert.ok(nodemon.config.options.ignore.indexOf('three') !== -1, 'Contains "three" path');
// note: we use the escaped format: \\.git
assert.ok(nodemon.config.options.ignore.indexOf('\\.git') === -1, 'nodemon is not ignoring (default) .git');
nodemon.on('exit', function () {
nodemon.reset(done);
});
setTimeout(function () {
nodemon.emit('quit');
}, 1000);
});
});
it('should support old .nodemonignore', function (done) {
// prevents our test from finding the nodemon.json files
process.chdir(path.resolve(pwd, 'test/fixtures/legacy'));
utils.home = path.resolve(pwd, 'test/fixtures/legacy');
var config = {},
settings = {},
options = {};
load(settings, options, config, function (config) {
assert(config.ignore.length === 5, '5 rules found: ' + config.ignore);
done();
});
});
it('should read global config', function (done) {
var config = {},
settings = { quiet: true },
options = {};
load(settings, options, config, function (config) {
assert(config.verbose, 'we are verbose');
// ensure global mapping works too
var options = exec({ script: 'template.pug' }, config.execMap);
assert(options.exec === 'bin/pug template.pug --out /tmp', 'exec used, should be "bin/pug": ' + options.exec);
done();
});
});
it('should read package.json config', function (done) {
var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings');
process.chdir(dir);
var config = {},
settings = { quiet: true },
options = {};
load(settings, options, config, function (config) {
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
done();
});
});
it('should give local files preference', function (done) {
var config = {},
settings = { quiet: true },
options = {};
load(settings, options, config, function (config) {
removeRegExp(config);
assert.ok(config.ignore.indexOf('one') !== -1, 'ignore contains "one": ' + config.ignore);
assert.ok(config.ignore.indexOf('three') !== -1, 'ignore contains "three": ' + config.ignore);
assert.deepEqual(config.watch, ['four'], 'watch is "four": ' + config.watch);
done();
});
});
it('should give local files preference over package.json config', function (done) {
var dir = path.resolve(pwd, 'test/fixtures/packages/nodemon-settings-and-package-json-settings');
process.chdir(dir);
var config = {},
settings = { quiet: true },
options = {};
load(settings, options, config, function (config) {
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
done();
});
});
it('should give package.json config preference', function (done) {
var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings');
process.chdir(dir);
var config = {},
settings = { quiet: true },
options = {};
load(settings, options, config, function (config) {
removeRegExp(config);
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
assert.ok(config.ignore.indexOf('one') !== -1, 'ignore contains "one": ' + config.ignore);
assert.ok(config.ignore.indexOf('three') !== -1, 'ignore contains "three": ' + config.ignore);
assert.deepEqual(config.watch, ['four'], 'watch is "four": ' + config.watch);
done();
});
});
it('should give user specified settings preference', function (done) {
var config = {},
settings = { ignore: ['one'], watch: ['one'], quiet: true },
options = {};
load(settings, options, config, function (config) {
removeRegExp(config);
assert(config.ignore.indexOf('one') !== -1, '"one" is ignored: ' + config.ignore);
assert.deepEqual(config.watch, ['one'], 'watch is "one": ' + config.watch);
done();
});
});
it('should give user specified settings preference over package.json config', function (done) {
var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings');
process.chdir(dir);
var config = {},
settings = { exec: 'foo-user', quiet: true },
options = {};
load(settings, options, config, function (config) {
assert.deepEqual(config.exec, 'foo-user', 'exec is "foo-user": ' + config.exec);
done();
});
});
it('should give user specified exec preference over package.scripts.start', function (done) {
var dir = path.resolve(pwd, 'test/fixtures/packages/start-and-settings');
process.chdir(dir);
var config = {},
settings = { 'script': './index.js', },
options = {};
load(settings, options, config, function (config) {
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
done();
});
});
it('should give package.json specified exec config over package.scripts.start', function (done) {
var dir = path.resolve(pwd, 'test/fixtures/packages/start-and-package-json-settings');
process.chdir(dir);
var config = {},
settings = {},
options = {};
load(settings, options, config, function (config) {
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
done();
});
});
// it('should put the script at the end if found in package.scripts.start', function (done) {
// process.chdir(path.resolve(pwd, 'test/fixtures/packages/start')); // allows us to load text/fixtures/package.json
// var settings = cli.parse(asCLI('--harmony'));
// var config = {};
// var options = {};
// load(settings, options, config, function (config) {
// var cmd = commandToString(command(config));
// assert.equal(cmd, 'node --harmony app.js', 'command is ' + cmd);
// done();
// });
// });
it('should support "ext" with "execMap"', function (done) {
// prevents our test from finding the nodemon.json files
process.chdir(path.resolve(pwd, 'test/fixtures/legacy'));
utils.home = path.resolve(pwd, 'test/fixtures/legacy');
var settings = {
script: './index.js',
verbose: true,
ignore: ['*/artic/templates/*'],
ext: 'js coffee json',
watch: ['*.coffee'],
execMap: { js: 'node --harmony', coffee: 'node --harmony', },
};
var config = {};
var options = {};
load(settings, options, config, function (config) {
var cmd = commandToString(command(config));
assert(cmd === 'node --harmony ./index.js', 'cmd is: ' + cmd);
done();
});
});
it('should merge ignore rules', function (done) {
load({
ignore: ['*/artic/templates/*', 'views/*'],
}, {}, {}, function (config) {
assert.equal(config.ignore.length, defaults.ignoreRoot.length + 2);
done();
});
});
it('should allow user to override ignoreRoot', function (done) {
load({
ignore: ['*/artic/templates/*', 'views/*'],
ignoreRoot: ['.git'],
}, {}, {}, function (config) {
assert.equal(config.ignore.length, 3);
done();
});
});
it('should merge ignore rules even when strings', function (done) {
load({
ignore: 'public',
}, {}, {}, function (config) {
assert.equal(config.ignore.length, defaults.ignoreRoot.length + 1);
done();
});
});
it('should allow user to override root ignore rules', function (done) {
load({
ignore: 'public',
ignoreRoot: [],
}, {}, {}, function (config) {
assert.equal(config.ignore.length, 1);
done();
});
});
it('should allow user to set execArgs', done => {
const execArgs = ['--inspect']
load({
execArgs,
}, {}, {}, config => {
assert.deepEqual(config.execArgs, execArgs);
done();
})
});
it('should support pkg.main and keep user args on args', done => {
process.chdir(path.resolve(pwd, 'test/fixtures/packages/main-and-start'));
const settings = { scriptPosition: 0, script: null, args: [ 'first', 'second' ] };
const options = { ignore: [], watch: [], monitor: [] };
const config = {
run: false,
system: { cwd: '/Users/remy/dev/nodemon/issues/1758' },
required: false,
dirs: [],
timeout: 1000,
options: { ignore: [], watch: [], monitor: [] },
lastStarted: 0,
loaded: []
}
load(settings, options, config, res => {
assert.deepEqual(res.execOptions.args, ['first', 'second']);
done();
})
});
});