forked from remy/nodemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.test.js
395 lines (317 loc) · 16 KB
/
parse.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'use strict';
/*global describe:true, it: true */
var cli = require('../../lib/cli/'),
exec = require('../../lib/config/exec'),
pkg = require('../../package'),
assert = require('assert'),
command = require('../../lib/config/command'),
utils = require('../../lib/utils');
const mutateExecOptions = require('../../lib/config/load').mutateExecOptions;
function asCLI(cmd) {
return ('node nodemon ' + (cmd || '')).trim();
}
function parse(cmd) {
var parsed = cli.parse(cmd);
// mirrored based on /lib/config/load.js:36
parsed.execOptions = exec({
script: parsed.script,
exec: parsed.exec,
args: parsed.args,
scriptPosition: parsed.scriptPosition,
nodeArgs: parsed.nodeArgs,
ext: parsed.ext,
env: parsed.env
});
return parsed;
return mutateExecOptions(cli.parse(cmd));
}
function commandToString(command) {
return utils.stringify(command.executable, command.args);
}
describe('nodemon CLI parser', function () {
it('should support --debug with script detect via package', function () {
var cwd = process.cwd();
process.chdir('test/fixtures/packages/express4');
var settings = parse(asCLI('--debug'));
var cmd = commandToString(command(settings));
process.chdir(cwd);
assert.equal(cmd, 'NODE_ENV=development node ./bin/www --debug');
});
it('should replace {{filename}}', function () {
var settings = parse(asCLI('test/fixtures/app.js --exec "node {{filename}}.tmp" --somethingElse'));
var cmd = commandToString(command(settings));
assert(cmd === 'node test/fixtures/app.js.tmp --somethingElse', cmd);
});
it('should replace {{filename}} multiple times', function () {
var settings = parse(asCLI('test/fixtures/app.js --exec "node {{filename}}.tmp {{filename}}.tmp" --somethingElse'));
var cmd = commandToString(command(settings));
assert(cmd === 'node test/fixtures/app.js.tmp test/fixtures/app.js.tmp --somethingElse', cmd);
});
it('should parse the help examples #1', function () {
var settings = parse(asCLI('test/fixtures/app.js')),
cmd = commandToString(command(settings));
assert(cmd === 'node test/fixtures/app.js', 'node test/fixtures/app.js: ' + cmd);
});
it('should parse the help examples #2', function () {
var settings = parse(asCLI('-w ../lib test/fixtures/app.js apparg1 apparg2')),
cmd = commandToString(command(settings));
assert.deepEqual(settings.watch, ['../lib'], 'watching ../lib: ' + settings.watch);
assert.deepEqual(settings.execOptions.args, ['apparg1', 'apparg2'], 'args are corr ' + settings.execOptions.args);
assert(cmd === 'node test/fixtures/app.js apparg1 apparg2', 'command is ' + cmd);
});
it('should parse the help examples #3', function () {
var settings = parse(asCLI('--exec python app.py')),
cmd = commandToString(command(settings));
assert(cmd === 'python app.py', 'command is ' + cmd);
assert(settings.execOptions.exec === 'python', 'exec is python');
});
it('should parse the help examples #4', function () {
var settings = parse(asCLI('--exec "make build" -e "styl hbs"')),
cmd = commandToString(command(settings));
assert(cmd === 'make build', 'command is ' + cmd);
assert.deepEqual(settings.execOptions.ext.split(','), ['styl', 'hbs'], 'correct extensions being watched: ' + settings.execOptions.ext);
});
it('should parse the help examples #5', function () {
var settings = parse(asCLI('test/fixtures/app.js -- -L')),
cmd = commandToString(command(settings));
assert(cmd === 'node test/fixtures/app.js -L', 'command is ' + cmd);
});
it('should put the script at the end if found in package.main', function () {
var pwd = process.cwd();
process.chdir('test/fixtures'); // allows us to load text/fixtures/package.json
var settings = parse(asCLI('--harmony')),
cmd = commandToString(command(settings));
process.chdir(pwd);
assert(cmd === 'node --harmony app.js', 'command is ' + cmd);
});
it('should support default express4 format', function () {
var pwd = process.cwd();
process.chdir('test/fixtures/packages/express4'); // allows us to load text/fixtures/package.json
var settings = parse(asCLI()),
cmd = commandToString(command(settings));
process.chdir(pwd);
assert.equal(cmd, 'NODE_ENV=development node ./bin/www', 'command is "' + cmd + '"');
});
it('should support spaces', function () {
var pwd = process.cwd();
process.chdir('test/fixtures/');
var settings = parse(asCLI('--exec \'"app with spaces.js" foo\''));
var cmd = commandToString(command(settings));
process.chdir(pwd);
assert(cmd === '"app with spaces.js" foo', cmd);
});
it('should support quotes around arguments', function () {
var settings = parse(asCLI('--watch "foo bar"'));
assert(settings.watch[0] === 'foo bar');
});
it('should keep eating arguments that are for nodemon after the script.js', function () {
var settings = parse(asCLI('--watch "foo bar" test/fixtures/app.js -V --scriptOpt1 -- -V'));
assert.deepEqual(settings.execOptions.args, ['--scriptOpt1', '-V'], 'script args are: ' + settings.execOptions.args.join(' '));
assert(settings.verbose === true, 'verbose');
assert(settings.watch[0] === 'foo bar', 'watching "foo bar" dir');
});
it('should allow -- to appear anywhere, and still find user script', function () {
var settings = parse(asCLI('test/fixtures/app.js -- -V'));
assert(!settings.verbose, '-V arg was passed to script, not nodemon');
assert.deepEqual(settings.execOptions.args, ['-V'], 'script passed -V via --');
settings = parse(asCLI('-- test/fixtures/app.js -V'));
assert.deepEqual(settings.execOptions.args, ['-V'], 'leading -- finds script');
settings = parse(asCLI('test/fixtures/app.js -V --'));
assert.deepEqual(settings.execOptions.args, [], '-- is ignored');
assert(settings.verbose, '-V was passed to nodemon');
});
it('should support arguments from the cli', function () {
var settings = parse(['node', 'nodemon', '--watch', 'foo bar']);
assert(settings.watch[0] === 'foo bar');
});
it('should support stand alone `nodemon` command', function () {
var settings = parse(asCLI(''));
assert(settings.execOptions.script === pkg.main + '.js', `${settings.execOptions.script} === ${pkg.main}`);
});
it('should put --debug in the right place with coffescript', function () {
var settings = parse(asCLI('--debug test/fixtures/app.coffee'));
// using indexOf instead of === because on windows
// coffee is coffee.cmd - so we check for a partial match
assert(commandToString(command(settings)).indexOf('--nodejs --debug test/fixtures/app.coffee') !== -1);
assert(settings.execOptions.exec.indexOf('coffee') === 0, 'executable is CoffeeScript');
});
it('should support period path', function () {
var settings = parse(asCLI('.'));
assert(commandToString(command(settings)) === 'node .');
});
it('should parse `nodemon lib/index.js`', function () {
var settings = parse(asCLI('lib/index.js'));
assert(settings.script === 'lib/index.js');
});
it('should parse `nodemon --config my/.nodemon.json server.js`', function () {
var settings = parse(asCLI('--config my/.nodemon.json test/fixtures/app.js'));
assert(settings.configFile === 'my/.nodemon.json');
assert(settings.script === 'test/fixtures/app.js');
});
it('should parse `nodemon test/fixtures/app.coffee`', function () {
var settings = parse(asCLI('test/fixtures/app.coffee'));
assert(settings.script === 'test/fixtures/app.coffee');
assert(settings.execOptions.exec.indexOf('coffee') === 0, 'executable is CoffeeScript');
});
it('should parse `nodemon --watch src/ -e js,coffee test/fixtures/app.js`', function () {
var settings = parse(asCLI('--watch src/ -e js,coffee test/fixtures/app.js'));
assert(settings.script === 'test/fixtures/app.js');
assert(settings.execOptions.exec === 'node');
});
it('should pass --debug to node', function () {
var settings = parse(asCLI('--debug test/fixtures/app.js'));
assert(settings.script === 'test/fixtures/app.js');
assert(settings.execOptions.exec === 'node');
assert(commandToString(command(settings)).indexOf('--debug') !== -1);
});
it('should pass --harmony to node', function () {
var settings = parse(asCLI('--harmony test/fixtures/app.js'));
assert(settings.script === 'test/fixtures/app.js');
assert(settings.execOptions.exec === 'node');
assert(commandToString(command(settings)).indexOf('--harmony') !== -1);
});
});
describe('nodemon argument parser', function () {
it('support strings', function () {
var settings = cli.parse('node nodemon -v');
assert(settings.version === true, 'version flag');
});
it('should support short versions of flags', function () {
var settings = cli.parse('node nodemon -v -x java -I -V -q -w fixtures -i fixtures -d 5 -L -C -e pug -s SIGHUP');
assert(settings.version, 'version');
assert(settings.verbose, 'verbose');
assert(settings.exec === 'java', 'exec');
assert(settings.quiet, 'quiet');
assert(settings.stdin === false, 'read stdin');
assert(settings.watch[0] === 'fixtures', 'watch');
assert(settings.ignore[0] === 'fixtures', 'ignore');
assert(settings.delay === 5000, 'delay 5 seconds');
assert(settings.runOnChangeOnly, 'run on change only');
assert(settings.ext === 'pug', 'extension is pug');
assert(settings.signal === 'SIGHUP', 'signal is SIGHUP');
});
it('should support long versions of flags', function () {
var settings = cli.parse('node nodemon --version --exec java --verbose --quiet --watch fixtures --ignore fixtures --no-stdin --delay 5 --legacy-watch --exitcrash --on-change-only --ext pug --config my/.nodemon.json --signal SIGHUP');
assert(settings.version, 'version');
assert(settings.verbose, 'verbose');
assert(settings.exec === 'java', 'exec');
assert(settings.quiet, 'quiet');
assert(settings.stdin === false, 'read stdin');
assert(settings.exitcrash, 'exit if crash');
assert(settings.watch[0] === 'fixtures', 'watch');
assert(settings.ignore[0] === 'fixtures', 'ignore');
assert(settings.delay === 5000, 'delay 5 seconds');
assert(settings.runOnChangeOnly, 'run on change only');
assert(settings.ext === 'pug', 'extension is pug');
assert(settings.configFile === 'my/.nodemon.json', 'custom config file name is my/.nodemon.json');
assert(settings.signal === 'SIGHUP', 'signal is SIGHUP');
});
});
describe('nodemon respects custom "ext" and "execMap"', function () {
it('should support "ext" and "execMap" for same extension', function () {
var settings = parse(asCLI('-x "node --harmony" -e "js json coffee" test/fixtures/app.coffee'));
assert(settings.execOptions.ext.indexOf('js') === 0, 'js is monitored: ' + settings.execOptions.ext);
assert(settings.execOptions.ext.split(',').length === 3, 'all extensions monitored');
assert(settings.execOptions.exec.indexOf('node') === 0, 'node is exec: ' + settings.execOptions.exec);
});
});
describe('nodemon should support implicit extensions', () => {
it('should expand script to script.js', () => {
const cwd = process.cwd();
process.chdir('test/fixtures/');
const settings = parse(asCLI('env'));
process.chdir(cwd);
var cmd = commandToString(command(settings));
assert.equal(cmd, 'node env.js', 'implicit extension added');
});
it('should support non-js', () => {
const cwd = process.cwd();
process.chdir('test/fixtures/');
const settings = parse(asCLI('hello --ext py'));
process.chdir(cwd);
var cmd = commandToString(command(settings));
assert.equal(cmd, 'node hello.py', 'implicit extension added');
});
});
describe('nodemon should slurp properly', () => {
it('should read quotes as a single entity', () => {
const settings = parse(asCLI('notindex.js -- -b "hello - world"'));
assert(settings.execOptions.exec === 'node', 'node is exec');
assert(settings.args.length === 3, 'only has 3 arguments to node');
});
it('should pass non-slurped args to script', () => {
const settings = parse(asCLI('-- --log'));
var cmd = commandToString(command(settings));
assert.equal(cmd, 'node ./lib/nodemon.js --log', 'args passed to script');
});
it('should pass non-slurped args to explicit script', () => {
const settings = parse(asCLI('./lib/nodemon.js -- --log'));
var cmd = commandToString(command(settings));
assert.equal(cmd, 'node ./lib/nodemon.js --log', 'args passed to script');
});
it('should pass slurped args to explicit script', () => {
const settings = parse(asCLI('./lib/nodemon.js --log'));
var cmd = commandToString(command(settings));
assert.equal(cmd, 'node ./lib/nodemon.js --log', 'args passed to script');
});
it('should handle a mix of slurps', () => {
var cmd;
var settings;
cmd = commandToString(command(parse(asCLI('--inspect -- --log'))));
assert.equal(cmd, 'node --inspect ./lib/nodemon.js --log', 'args passed to script');
cmd = commandToString(command(parse(asCLI('--inspect ./lib/nodemon.js -- --log'))));
assert.equal(cmd, 'node --inspect ./lib/nodemon.js --log', 'args passed to script');
cmd = commandToString(command(parse(asCLI('--inspect --log ./lib/nodemon.js'))));
assert.equal(cmd, 'node --inspect --log ./lib/nodemon.js', 'args passed to script');
});
});
describe('nodemon with CoffeeScript', function () {
it('should not add --nodejs by default', function () {
var settings = parse(asCLI('test/fixtures/app.coffee'));
assert(settings.execOptions.exec.indexOf('coffee') === 0, 'executable is CoffeeScript');
assert(settings.execOptions.execArgs.indexOf('--nodejs') === -1, 'is not using --nodejs');
});
it('should not add --nodejs with app arguments', function () {
var settings = parse(asCLI('test/fixtures/app.coffee --my-app-arg'));
assert(settings.execOptions.exec.indexOf('coffee') === 0, 'executable is CoffeeScript');
assert(settings.execOptions.execArgs.indexOf('--nodejs') === -1, 'is not using --nodejs');
});
it('groups exec argument into a single --nodejs argument', function () {
var settings = parse(asCLI('--harmony --debug test/fixtures/app.coffee'));
assert(settings.execOptions.exec.indexOf('coffee') === 0, 'executable is CoffeeScript');
assert(settings.execOptions.execArgs[0] === '--nodejs', 'is using --nodejs');
assert(settings.execOptions.execArgs[1] === '--harmony --debug', 'is grouping exec arguments');
});
it('should add --nodejs when used with --debug', function () {
var settings = parse(asCLI('--debug test/fixtures/app.coffee'));
var cmd = commandToString(command(settings));
assert(settings.execOptions.exec.indexOf('coffee') === 0, 'executable is CoffeeScript');
assert(cmd.indexOf('--nodejs') !== -1, '--nodejs being used');
assert(cmd.indexOf('--debug') !== -1, '--debug being used');
});
it('should add --nodejs when used with --debug-brk', function () {
var settings = parse(asCLI('--debug-brk test/fixtures/app.coffee'));
var cmd = commandToString(command(settings));
assert(settings.execOptions.exec.indexOf('coffee') === 0, 'executable is CoffeeScript');
assert(cmd.indexOf('--nodejs') !== -1, '--nodejs being used');
assert(cmd.indexOf('--debug-brk') !== -1, '--debug-brk being used');
});
});
describe('nodemon --delay argument', function () {
it('should support an integer value', function () {
var settings = cli.parse('node nodemon --delay 5');
assert(settings.delay === 5000, 'delay 5 seconds');
});
it('should support a float value', function () {
var settings = cli.parse('node nodemon --delay 1.2');
assert(settings.delay === 1200, 'delay 1.2 seconds');
});
it('should support a value with a time specifier for seconds (s)', function () {
var settings = cli.parse('node nodemon --delay 5s');
assert(settings.delay === 5000, 'delay 5 seconds');
});
it('should support a value with a time specifier for milliseconds (ms)', function () {
var settings = cli.parse('node nodemon --delay 1200ms');
assert(settings.delay === 1200, 'delay 1.2 seconds');
});
});