1 | /*
|
---|
2 | EnvJasmine: Jasmine test runner for EnvJS.
|
---|
3 |
|
---|
4 | EnvJasmine allows you to run headless JavaScript tests.
|
---|
5 |
|
---|
6 | Based on info from:
|
---|
7 | http://agile.dzone.com/news/javascript-bdd-jasmine-without
|
---|
8 | http://www.mozilla.org/rhino/
|
---|
9 | http://www.envjs.com/
|
---|
10 | http://pivotal.github.com/jasmine/
|
---|
11 | https://github.com/velesin/jasmine-jquery
|
---|
12 | */
|
---|
13 |
|
---|
14 | importPackage(java.lang);
|
---|
15 | importPackage(java.io);
|
---|
16 | importPackage(org.mozilla.javascript);
|
---|
17 |
|
---|
18 | // Create the EnvJasmine namespace
|
---|
19 | if (!this.EnvJasmine) {
|
---|
20 | this.EnvJasmine = {};
|
---|
21 | }
|
---|
22 |
|
---|
23 | EnvJasmine.cx = Context.getCurrentContext();
|
---|
24 | EnvJasmine.cx.setOptimizationLevel(-1);
|
---|
25 | EnvJasmine.topLevelScope = this;
|
---|
26 |
|
---|
27 | EnvJasmine.about = function () {
|
---|
28 | print("usage: envjasmine.js [options] [spec_file...]");
|
---|
29 | print("");
|
---|
30 | print("options:");
|
---|
31 | print(" --configFile=<File> Set a config file to run before specs are executed");
|
---|
32 | print(" --disableColor Disable console colors (colors are not available in Windows)");
|
---|
33 | print(" --environment=<WIN|UNIX> Set the environment to UNIX or Windows");
|
---|
34 | print(" --help This list");
|
---|
35 | print(" --incrementalOutput Disable the ./F output and print results for each spec file");
|
---|
36 | print(" --rootDir=<Dir> Set the EnvJasmine root directory (REQUIRED)");
|
---|
37 | print(" --suppressConsoleMsgs Suppress window.console messages");
|
---|
38 | print(" --testDir=<Dir> Set the directory with the specs/ directory (REQUIRED)");
|
---|
39 | print(" --customSpecsDir=<Dir> Sets the name of the specs directory, default is 'specs'");
|
---|
40 | };
|
---|
41 |
|
---|
42 | EnvJasmine.printDebug = function(str) {
|
---|
43 | if (EnvJasmine.debug) {
|
---|
44 | print(str);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | EnvJasmine.normalizePath = function(path) {
|
---|
49 | var endsInSlash = (path.slice(-1) == "/");
|
---|
50 |
|
---|
51 | if (path.slice(0, 1) == ".") {
|
---|
52 | path = EnvJasmine.rootDir + "/" + path;
|
---|
53 | }
|
---|
54 |
|
---|
55 | return File(path).getCanonicalPath() + (endsInSlash ? "/" : "");
|
---|
56 | };
|
---|
57 |
|
---|
58 | EnvJasmine.loadFactory = function(scope) {
|
---|
59 | return function (path) {
|
---|
60 | var fileIn,
|
---|
61 | normalizedPath = EnvJasmine.normalizePath(path);
|
---|
62 |
|
---|
63 | try {
|
---|
64 | fileIn = new FileReader(normalizedPath);
|
---|
65 | EnvJasmine.cx.evaluateReader(scope, fileIn, normalizedPath, 0, null);
|
---|
66 | } catch (e) {
|
---|
67 | print (e);
|
---|
68 | print('Could not read file: ' + normalizedPath );
|
---|
69 | } finally {
|
---|
70 | fileIn.close();
|
---|
71 | }
|
---|
72 | };
|
---|
73 | };
|
---|
74 | EnvJasmine.loadGlobal = EnvJasmine.loadFactory(EnvJasmine.topLevelScope);
|
---|
75 |
|
---|
76 | EnvJasmine.setRootDir = function (rootDir) {
|
---|
77 | // These are standard directories in the EnvJasmine project.
|
---|
78 | EnvJasmine.rootDir = EnvJasmine.normalizePath(rootDir);
|
---|
79 | EnvJasmine.libDir = EnvJasmine.normalizePath(EnvJasmine.rootDir + "/lib/");
|
---|
80 | EnvJasmine.includeDir = EnvJasmine.normalizePath(EnvJasmine.rootDir + "/include/");
|
---|
81 |
|
---|
82 | // This is the standard spec suffix
|
---|
83 | EnvJasmine.specSuffix = new RegExp(/.spec.js$/);
|
---|
84 |
|
---|
85 | // Load the default dirs and files, these can be overridden with command line options
|
---|
86 | EnvJasmine.configFile = EnvJasmine.normalizePath(EnvJasmine.includeDir + "dependencies.js");
|
---|
87 | };
|
---|
88 |
|
---|
89 | EnvJasmine.setTestDir = function (testDir, override) {
|
---|
90 | if (typeof EnvJasmine.testDir === "undefined" || !EnvJasmine.testDir || override) {
|
---|
91 | EnvJasmine.testDir = EnvJasmine.normalizePath(testDir);
|
---|
92 | EnvJasmine.mocksDir = EnvJasmine.normalizePath(EnvJasmine.testDir + "/mocks/");
|
---|
93 | EnvJasmine.specsDir = EnvJasmine.normalizePath(EnvJasmine.testDir + "/specs/");
|
---|
94 | }
|
---|
95 | };
|
---|
96 |
|
---|
97 | EnvJasmine.setJsDir = function(jsDir) {
|
---|
98 | EnvJasmine.jsDir = jsDir;
|
---|
99 | };
|
---|
100 |
|
---|
101 | // Process command line options
|
---|
102 | (function(argumentList) {
|
---|
103 | var arg, nameValue, spec = "", specLoc;
|
---|
104 |
|
---|
105 | EnvJasmine.specs = [];
|
---|
106 | EnvJasmine.passedCount = 0;
|
---|
107 | EnvJasmine.failedCount = 0;
|
---|
108 | EnvJasmine.totalCount = 0;
|
---|
109 | for (var i = 0; i < argumentList.length; i++) {
|
---|
110 | arg = argumentList[i];
|
---|
111 | if (arg.slice(0, 2) == "--") {
|
---|
112 | nameValue = arg.slice(2).split('=');
|
---|
113 | switch(nameValue[0]) {
|
---|
114 | case "testDir":
|
---|
115 | EnvJasmine.setTestDir(nameValue[1], true);
|
---|
116 | break;
|
---|
117 | case "rootDir":
|
---|
118 | EnvJasmine.setRootDir(nameValue[1]);
|
---|
119 | EnvJasmine.setTestDir(nameValue[1]); // Set the root as the default testDir.
|
---|
120 | break;
|
---|
121 | case "jsDir":
|
---|
122 | EnvJasmine.setJsDir(nameValue[1]);
|
---|
123 | break;
|
---|
124 | case "environment":
|
---|
125 | EnvJasmine.environment = nameValue[1];
|
---|
126 | break;
|
---|
127 | case "configFile":
|
---|
128 | EnvJasmine.configFile = EnvJasmine.normalizePath(nameValue[1]);
|
---|
129 | break;
|
---|
130 | case "disableColor":
|
---|
131 | EnvJasmine.disableColorOverride = true;
|
---|
132 | break;
|
---|
133 | case "incrementalOutput":
|
---|
134 | EnvJasmine.incrementalOutput = true;
|
---|
135 | break;
|
---|
136 | case "suppressConsoleMsgs":
|
---|
137 | EnvJasmine.suppressConsoleMsgs = true;
|
---|
138 | break;
|
---|
139 | case "customSpecsDir":
|
---|
140 | EnvJasmine.customSpecsDir = nameValue[1];
|
---|
141 | break;
|
---|
142 | case "plugin":
|
---|
143 | EnvJasmine.addPlugin(nameValue[1]);
|
---|
144 | break;
|
---|
145 | case "debug":
|
---|
146 | EnvJasmine.debug = true;
|
---|
147 | break;
|
---|
148 | case "help":
|
---|
149 | EnvJasmine.about();
|
---|
150 | System.exit(0);
|
---|
151 | default:
|
---|
152 | print("Unknown option: " + arg);
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | } else {
|
---|
156 | if (arg.slice(-3) !== ".js") {
|
---|
157 | spec += arg + " ";
|
---|
158 | } else {
|
---|
159 | spec += arg;
|
---|
160 | if (arg[0] === "/" || (arg[1] === ":" && arg[2] === "\\")) {
|
---|
161 | specLoc = spec;
|
---|
162 | } else {
|
---|
163 | specLoc = EnvJasmine.testDir + "/" + spec
|
---|
164 | }
|
---|
165 | print(specLoc);
|
---|
166 | EnvJasmine.specs.push(EnvJasmine.normalizePath(specLoc));
|
---|
167 | spec = "";
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }(arguments));
|
---|
172 |
|
---|
173 | if (typeof EnvJasmine.customSpecsDir !== "undefined") {
|
---|
174 | EnvJasmine.specsDir = EnvJasmine.normalizePath(EnvJasmine.testDir + EnvJasmine.customSpecsDir);
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (typeof EnvJasmine.rootDir == "undefined" || typeof EnvJasmine.environment == "undefined") {
|
---|
178 | EnvJasmine.about();
|
---|
179 | System.exit(1);
|
---|
180 | }
|
---|
181 |
|
---|
182 | EnvJasmine.SEPARATOR = (function (env) {
|
---|
183 | if (env == "UNIX") {
|
---|
184 | return "/";
|
---|
185 | } else if (env == "WIN") {
|
---|
186 | return "\\";
|
---|
187 | } else {
|
---|
188 | EnvJasmine.about();
|
---|
189 | System.exit(1);
|
---|
190 | }
|
---|
191 | }(EnvJasmine.environment));
|
---|
192 |
|
---|
193 | EnvJasmine.disableColor = (function (env) {
|
---|
194 | return EnvJasmine.disableColorOverride || (env == "WIN");
|
---|
195 | }(EnvJasmine.environment));
|
---|
196 |
|
---|
197 | (function() {
|
---|
198 | if (EnvJasmine.disableColor) {
|
---|
199 | EnvJasmine.green = function(msg) { return msg; };
|
---|
200 | EnvJasmine.red = function(msg) { return msg; };
|
---|
201 | EnvJasmine.plain = function(msg) { return msg; };
|
---|
202 | } else {
|
---|
203 | var green = "\033[32m",
|
---|
204 | red = "\033[31m",
|
---|
205 | end = "\033[0m";
|
---|
206 |
|
---|
207 | EnvJasmine.green = function(msg) { return green + msg + end; };
|
---|
208 | EnvJasmine.red = function(msg) { return red + msg + end; };
|
---|
209 | EnvJasmine.plain = function(msg) { return msg; };
|
---|
210 | }
|
---|
211 | }());
|
---|
212 | EnvJasmine.results = [];
|
---|
213 |
|
---|
214 | EnvJasmine.addFinallyFunction = function(f) {
|
---|
215 | EnvJasmine.finallyFunctions.push(f);
|
---|
216 | };
|
---|
217 | EnvJasmine.finallyFunctions = [];
|
---|
218 |
|
---|
219 |
|
---|
220 | EnvJasmine.loadConfig = function () {
|
---|
221 | EnvJasmine.loadGlobal(EnvJasmine.configFile);
|
---|
222 | };
|
---|
223 |
|
---|
224 | EnvJasmine.plugins = [];
|
---|
225 | EnvJasmine.addPlugin = function(path) {
|
---|
226 | EnvJasmine.plugins.push(path);
|
---|
227 | };
|
---|
228 |
|
---|
229 | (function() {
|
---|
230 | var i, fileIn, len;
|
---|
231 |
|
---|
232 | EnvJasmine.loadConfig();
|
---|
233 |
|
---|
234 | for (i = 0; i < EnvJasmine.plugins.length; i++) {
|
---|
235 | EnvJasmine.loadGlobal(EnvJasmine.plugins[i]);
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (typeof EnvJasmine.reporterClass === "undefined") {
|
---|
239 | // Use the standard reporter
|
---|
240 | EnvJasmine.reporterClass = RhinoReporter;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | jasmine.getEnv().addReporter(new EnvJasmine.reporterClass());
|
---|
245 |
|
---|
246 | if (EnvJasmine.suppressConsoleMsgs === true) {
|
---|
247 | // suppress console messages
|
---|
248 | window.console = $.extend({}, window.console, {
|
---|
249 | info: jasmine.createSpy(),
|
---|
250 | log: jasmine.createSpy(),
|
---|
251 | debug: jasmine.createSpy(),
|
---|
252 | warning: jasmine.createSpy(),
|
---|
253 | error: jasmine.createSpy()
|
---|
254 | });
|
---|
255 | }
|
---|
256 |
|
---|
257 | EnvJasmine.loadGlobal(EnvJasmine.libDir + "spanDir/spanDir.js");
|
---|
258 | if (EnvJasmine.specs.length == 0) {
|
---|
259 | spanDir(EnvJasmine.specsDir, function(spec) {
|
---|
260 | if (EnvJasmine.specSuffix.test(spec)) {
|
---|
261 | EnvJasmine.specs.push(spec);
|
---|
262 | }
|
---|
263 | });
|
---|
264 | }
|
---|
265 |
|
---|
266 | for (i = 0, len = EnvJasmine.specs.length >>> 0; i < len; i += 1) {
|
---|
267 | try {
|
---|
268 | EnvJasmine.currentScope = {};
|
---|
269 | EnvJasmine.load = EnvJasmine.loadFactory(EnvJasmine.currentScope);
|
---|
270 | EnvJasmine.specFile = EnvJasmine.specs[i];
|
---|
271 | fileIn = new FileReader(EnvJasmine.specFile);
|
---|
272 | EnvJasmine.cx.evaluateReader(EnvJasmine.currentScope, fileIn, EnvJasmine.specs[i], 0, null);
|
---|
273 | EnvJasmine.cx.evaluateString(EnvJasmine.currentScope, 'window.location.assign(["file://", EnvJasmine.libDir, "envjasmine.html"].join(EnvJasmine.SEPARATOR));', 'Executing '+EnvJasmine.specs[i], 0, null);
|
---|
274 | }
|
---|
275 | catch (e) {
|
---|
276 | print("Problem opening " + EnvJasmine.specFile + ": " + e);
|
---|
277 | }
|
---|
278 | finally {
|
---|
279 | if(fileIn) {
|
---|
280 | fileIn.close();
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | for (i = 0; i < EnvJasmine.finallyFunctions.length; i++) {
|
---|
286 | EnvJasmine.finallyFunctions[i]();
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (EnvJasmine.results.length > 0) {
|
---|
290 | print("\n");
|
---|
291 | print(EnvJasmine.red(EnvJasmine.results.join("\n\n")));
|
---|
292 | }
|
---|
293 |
|
---|
294 | print();
|
---|
295 | print(EnvJasmine[EnvJasmine.passedCount ? 'green' : 'plain']("Passed: " + EnvJasmine.passedCount));
|
---|
296 | print(EnvJasmine[EnvJasmine.failedCount ? 'red' : 'plain']("Failed: " + EnvJasmine.failedCount));
|
---|
297 | print(EnvJasmine.plain("Total : " + EnvJasmine.totalCount));
|
---|
298 |
|
---|
299 | if (EnvJasmine.failedCount > 0) {
|
---|
300 | System.exit(1);
|
---|
301 | }
|
---|
302 | }());
|
---|