forked from remy/nodemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.test.js
223 lines (177 loc) · 7.48 KB
/
exec.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
'use strict';
/*global describe:true, it: true */
const path = require('path');
const exec = require('../../lib/config/exec');
const expandScript = exec.expandScript;
const command = require('../../lib/config/command');
const assert = require('assert');
const utils = require('../../lib/utils');
function toCmd(options) {
var cmd = command({
script: options.script || 'app.js',
execOptions: options
});
return {
cmd: cmd,
string: utils.stringify(cmd.executable, cmd.args)
};
}
describe('expandScript', () => {
var pwd = process.cwd();
afterEach(function () {
process.chdir(pwd);
});
beforeEach(function () {
// move to the fixtures directory to allow for config loading
process.chdir(path.resolve(pwd, 'test/fixtures'));
});
it('should expand app.js', () => {
const script = expandScript('app');
assert.equal(script, 'app.js', script);
})
it('should expand hello.py', () => {
const script = expandScript('hello', '.py');
assert.equal(script, 'hello.py', script);
})
it('should ignore foo.js', () => {
const script = expandScript('foo', '.js');
assert.equal(script, 'foo', script);
})
});
describe('nodemon exec', function () {
var pwd = process.cwd();
afterEach(function () {
process.chdir(pwd);
});
beforeEach(function () {
// move to the fixtures directory to allow for config loading
process.chdir(path.resolve(pwd, 'test/fixtures'));
});
it('should default to node', function () {
var options = exec({ script: 'index.js' });
var cmd = toCmd(options);
assert.equal(options.exec, 'node', 'exec is node');
assert.equal(options.ext, 'js,mjs,json');
assert.equal(cmd.string, 'node index.js', cmd.string);
});
it('should support --debug', function () {
var options = exec({ script: 'app.js', nodeArgs: ['--debug'] });
var cmd = toCmd(options);
assert(cmd.string === 'node --debug app.js', cmd.string);
assert(options.ext.indexOf('js') !== -1, 'extension watched is .js');
});
it('should support --debug=XXXX', function () {
var options = exec({ script: 'app.js', nodeArgs: ['--debug=9999'] });
var cmd = toCmd(options);
assert(cmd.string === 'node --debug=9999 app.js', cmd.string);
assert(options.exec === 'node');
assert(options.ext.indexOf('js') !== -1);
});
it('should support multiple extensions', function () {
var options = exec({ script: 'app.js', ext: 'js, pug, hbs' });
var cmd = toCmd(options);
assert(cmd.string === 'node app.js', cmd.string);
assert(options.ext.indexOf('pug') !== -1, 'comma separated string');
options = exec({ script: 'app.js', ext: 'js|pug|hbs' });
assert(options.exec === 'node');
assert(options.ext.indexOf('pug') !== -1, 'pipe separated string');
});
it('should support watching all extensions', function () {
var options = exec({ script: 'app.js', ext: '' });
assert.equal(options.ext, '', 'does not set default extensions when empty extension requested');
options = exec({ script: 'app.js', ext: '.' });
assert.equal(options.ext, '', 'treats `.` as wildcard extension');
options = exec({ script: 'app.js', ext: '*' });
assert.equal(options.ext, '', 'treats `*` as wildcard extension');
options = exec({ script: 'app.coffee', exec: 'coffee', ext: '' });
assert.equal(options.ext, '', 'does not set default extensions when empty extension requested');
});
it('should replace {{filename}}', function () {
var options = exec({ script: 'app.js', exec: 'node {{filename}}.tmp --somethingElse' });
var cmd = toCmd(options);
assert(cmd.string === 'node app.js.tmp --somethingElse', cmd.string);
});
it('should not split on spaces in {{filename}}', function () {
var options = exec({ script: 'my app.js', exec: 'node {{filename}}.tmp --somethingElse' });
var cmd = toCmd(options);
// var cmd = command({ execOptions: options });
assert(cmd.string === 'node my app.js.tmp --somethingElse', cmd.string);
});
it('should support extension maps', function () {
var options = exec({ script: 'template.pug' }, { 'pug': 'pug {{filename}} --out /tmp' });
var cmd = toCmd(options);
assert(cmd.string === 'pug template.pug --out /tmp', cmd.string);
});
it('should support input from argv#parse', function () {
var parse = require('../../lib/cli/parse');
parse('node /usr/local/bin/nodemon.js --debug -e js,pug,hbs app.js'.split(' '));
});
it('should use coffeescript on .coffee', function () {
var options = exec({ script: 'index.coffee' });
assert(options.exec.indexOf('coffee') === 0, 'using coffeescript to execute');
assert(options.ext.indexOf('coffee') !== -1);
});
it('should support coffeescript in debug mode', function () {
var options = exec({ script: 'app.coffee', nodeArgs: ['--debug'] });
assert(options.exec.indexOf('coffee') === 0, 'using coffeescript to execute');
assert(options.execArgs[1].indexOf('--debug') !== -1);
assert(options.ext.indexOf('coffee') !== -1);
});
it('should support custom execs', function () {
var options = exec({ script: 'app.py', exec: 'python' });
assert(options.exec === 'python');
assert(options.ext.indexOf('py') !== -1);
});
it('should support custom executables with arguments', function () {
var options = exec({ script: 'app.py', exec: 'python --debug' });
var cmd = toCmd(options);
assert(cmd.string === 'python --debug app.py', cmd.string);
assert(options.ext.indexOf('py') !== -1);
});
it('should support an array of exec arguments', function () {
var options = exec({ script: 'app.js', exec: ['/path to node', '-v'] });
assert(options.exec === '/path to node', options.exec);
assert(options.execArgs.length === 1, options.execArgs.length);
assert(options.execArgs[0] === '-v', options.execArgs[0]);
});
it('should support non-english filenames', function () {
var parse = require('../../lib/cli/parse');
var options = parse('node nodemon.js -e ζ ./server.js "$@"'.split(' '));
var res = exec(options);
assert(res.ext === 'ζ', 'exec did not bail');
});
it('should support multi-level file extensions', function () {
var options = exec({ ext: '.ts.d,js md' });
assert(options.ext.indexOf('ts.d') !== -1);
assert(options.ext.indexOf('js') !== -1);
assert(options.ext.indexOf('md') !== -1);
});
it('should support single-level file extensions', function () {
var options = exec({ ext: '.js, pug' });
assert(options.ext.indexOf('js') !== -1);
assert(options.ext.indexOf('pug') !== -1);
});
it('should expand app to app.js', function () {
var options = exec({ script: 'app' });
var cmd = toCmd(options);
assert(cmd.string === 'node app.js', cmd.string);
options = exec({ script: 'app', ext: '' });
cmd = toCmd(options);
assert(cmd.string === 'node app.js', cmd.string);
});
it('should expand based on custom extensions to hello.py', function () {
var options = exec({ script: 'hello', ext: '.py', exec: 'python' });
var cmd = toCmd(options);
assert(cmd.string === 'python hello.py', cmd.string);
});
it('should expand based on custom extensions to app.js (js,jsx,mjs)', function () {
var options = exec({ script: 'app', ext: 'js,jsx,mjs' });
var cmd = toCmd(options);
assert(cmd.string === 'node app.js', cmd.string);
});
it('should not expand index to non-existant index.js', function () {
var options = exec({ script: 'index' });
var cmd = toCmd(options);
assert(cmd.string === 'node index', cmd.string);
});
});