-
Notifications
You must be signed in to change notification settings - Fork 44
/
Venus.js
319 lines (274 loc) · 10.1 KB
/
Venus.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
/*
* Venus
* Copyright 2013 LinkedIn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
**/
/**
* The Venus application code that is called by the Venus shell script (bin/venus).
* @file
*/
var Module = require('module'),
actualRequire = Module.prototype.require;
/*
* The following call to graceful-fs is needed so that require('fs') inside it
* will refer to the original fs module.
*/
require('graceful-fs');
/*
* Overriding module.require to make sure require('fs') returns graceful-fs.
* Require of a module other than fs will return the same module.
*
* TODO: remove this either by removing fs-tools or by making sure fs-tools make use of graceful-fs
*/
Module.prototype.require = function(id) {
var module;
if (id === 'fs') {
module = actualRequire.call(this, 'graceful-fs');
} else {
module = actualRequire.apply(this, arguments);
}
return module;
};
var _ = require('underscore'),
fs = require('fs'),
executor = require('./lib/executor'),
i18n = require('./lib/util/i18n'),
locale = require('./lib/util/locale'),
logger = require('./lib/util/logger'),
program = require('commander'),
prompt = require('cli-prompt'),
wrench = require('wrench'),
path = require('path'),
deferred = require('deferred'),
ps = require('./lib/util/ps');
/**
* The Venus application object
* @constructor
*/
function Venus() {}
/**
* Starts the application
* @param {Object[]} args Command line arguments. See {@link http://nodejs.org/api/process.html#process_process_argv}
* for the order of arguments.
* @method Venus#start
*/
Venus.prototype.start = function (args) {
this.noCommand = true;
this.commandLineArguments = args;
this.init(args);
};
/**
* Stops the application
* @method Venus#shutdown
*/
Venus.prototype.shutdown = function () {
throw new Error('Not implemented');
};
// Initialize the application
/**
* Initializes the application
* @param {Object[]} args Command line arguments. See {@link http://nodejs.org/api/process.html#process_process_argv}
* for the order of arguments.
* @method Venus#init
*/
Venus.prototype.init = function (args) {
// init command
program
.command('init')
.description(i18n('initialize new venus project directory'))
.option('-l, --locale [locale]', i18n('Specify locale to use'))
.option('-v, --verbose', i18n('Run in verbose mode'))
.option('-d, --debug', i18n('Run in debug mode'))
.action(_.bind(this.command(this.initProjectDirectory), this));
// demo mode command
program
.command('demo')
.description(i18n('run an example venus test'))
.option('-l, --locale [locale]', i18n('Specify locale to use'))
.option('-v, --verbose', i18n('Run in verbose mode'))
.option('-d, --debug', i18n('Run in debug mode'))
.action(_.bind(this.command(this.runDemo), this));
// run command
program
.command('run')
.description(i18n('Run tests'))
.option('-t, --test [tests]', i18n('Comma separated string of tests to run'))
.option('-p, --port [port]', i18n('port to run on'), function (value) { return parseInt(value, 10); })
.option('-l, --locale [locale]', i18n('Specify locale to use'))
.option('-v, --verbose', i18n('Run in verbose mode'))
.option('-d, --debug', i18n('Run in debug mode'))
.option('-c, --coverage', i18n('Generate Code Coverage Report'))
.option('--hostname [host]', i18n('Set hostname for test URLs, defaults to your ip address'))
.option('--no-annotations', i18n('Include test files with no Venus annotations (@venus-*)'))
.option('-e, --environment [env]', i18n('Specify environment to run tests in'))
.option('-r, --reporter [reporter]', i18n('Test reporter to use. Default is "DefaultReporter"'))
.option('-o, --output-file [path]', i18n('File to record test results'))
.option('-n, --phantom', i18n('Run with PhantomJS. This is a shortcut to --environment ghost'))
.option('--singleton', i18n('Ensures all other Venus processes are killed before starting'))
.action(_.bind(this.command(this.run), this));
program.parse(args);
// No command (e.g., "init", "demo", "run") was provided in command line arguments, so run venus with defaults
if (this.noCommand) {
// Define command line options
program
.version(require('./package').version)
.option('-p, --port [port]', i18n('port to run on'), function (value) { return parseInt(value, 10); })
.option('-l, --locale [locale]', i18n('Specify locale to use'))
.option('-v, --verbose', i18n('Run in verbose mode'))
.option('-d, --debug', i18n('Run in debug mode'))
.option('-c, --coverage', i18n('Generate Code Coverage Report'))
.option('--hostname [host]', i18n('Set hostname for test URLs, defaults to your ip address'))
.option('--no-annotations', i18n('Include test files with no Venus annotations (@venus-*)'))
.option('-e, --environment [env]', i18n('Specify environment to run tests in'))
.option('-r, --reporter [reporter]', i18n('Test reporter to use. Default is "DefaultReporter"'))
.option('-o, --output-file [path]', i18n('File to record test results'))
.option('-n, --phantom', i18n('Run with PhantomJS. This is a shortcut to --environment ghost'))
.option('--singleton', i18n('Ensures all other Venus processes are killed before starting'));
program.parse(args);
this.runWithDefaults();
}
};
/**
* Try to auto run venus with default settings
*/
Venus.prototype.runWithDefaults = function () {
var args = this.commandLineArguments,
encounteredFlag = false,
possibleTestPaths;
if (args < 2) {
return false;
}
possibleTestPaths = args.slice(2).filter(function (testPath) {
if (testPath[0] === '-') {
encounteredFlag = true;
}
return !encounteredFlag;
});
logger.verbose(i18n('Running demo'));
if (possibleTestPaths.length === 0) {
possibleTestPaths = ['.'];
}
program.test = possibleTestPaths.join(',');
// program.environment = 'ghost';
// program.coverage = true;
this.run(program);
};
/**
* Marks function as a Venus command, by setting its execution context to the Venus instance.
*
* @param {Function} fn The function to wrap
* @method Venus#command
*/
Venus.prototype.command = function (fn) {
return function () {
this.noCommand = false;
fn.apply(this, arguments);
}.bind(this);
};
/**
* Applies logging and L10N options based on options passed in.
*
* @param {Object} program Options to set
* @param {Boolean} [program.debug] Specify true to enable debug-level messages.
* @param {String} [program.locale] ISO2 code of the desired language to support.
* @method Venus#applyCommandLineFlags
*/
Venus.prototype.applyCommandLineFlags = function (program) {
// Check if debug logging should be enabled
if (program.debug) {
logger.transports.console.level = 'debug';
}
// Set locale
if (program.locale) {
locale(program.locale);
}
};
/**
* Starts the Venus server (runs/serves tests), with specified options.
*
* @param {Object} program Options to set.
* @param {Boolean} [program.debug] Specify true to enable debug-level messages.
* @param {String} [program.locale] ISO2 code of the desired language to support.
* @method Venus#run
*/
Venus.prototype.run = function (program) {
logger.verbose(i18n('Starting in executor mode'));
if (program.hasOwnProperty('phantom')) {
program.environment = 'ghost';
}
this.applyCommandLineFlags(program);
if (program.hasOwnProperty('singleton')) {
this.killOtherVenusProcesses().then(proceed);
} else {
proceed.call(this);
}
function proceed() {
this.server = new executor.Executor();
program.homeFolder = __dirname;
this.server.init(program);
}
};
/**
* Kill all other Venus processes besides the current process.
*/
Venus.prototype.killOtherVenusProcesses = function () {
return ps.grep('venus').then(ps.kill);
};
/**
* Runs Venus in demo mode, with hard-coded tests.
* @param {Object} program Options to set.
* @param {Boolean} [program.debug] Specify true to enable debug-level messages.
* @param {String} [program.locale] ISO2 code of the desired language to support.
* @method Venus#runDemo
*/
Venus.prototype.runDemo = function (program) {
var testFile = path.resolve(__dirname, 'examples', 'mocha', 'Greeter');
logger.verbose(i18n('Running demo'));
program.test = testFile;
program.environment = 'ghost';
program.coverage = true;
this.run(program);
};
/**
* Initializes the directory from which to serve test cases and resources.
* @param {Object} program Options to set.
* @param {Boolean} [program.debug] Specify true to enable debug-level messages.
* @param {String} [program.locale] ISO2 code of the desired language to support.
* @method Venus#initProjectDirectory
*/
Venus.prototype.initProjectDirectory = function (program) {
var venusConfigFolderName = '.venus';
logger.verbose(i18n('Initializing new Venus project'));
this.applyCommandLineFlags(program);
program.homeFolder = __dirname;
function createDir() {
// copy global directory
wrench.copyDirSyncRecursive(path.resolve(__dirname, venusConfigFolderName, '.init-data'), venusConfigFolderName);
console.log(i18n('New Venus project created in ' + process.cwd()));
return;
}
if (fs.existsSync(venusConfigFolderName)) {
prompt(i18n('Warning'.red + ': ' + venusConfigFolderName + ' exists. Overwrite? (y/n) '), function (input) {
if (input.toUpperCase() === 'Y') {
createDir();
} else {
return;
}
});
} else {
createDir();
}
};
module.exports = Venus;
Object.seal(module.exports);