This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathSpecRunner.js
443 lines (364 loc) · 17 KB
/
SpecRunner.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Copyright (c) 2012 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*global beforeEach, afterEach, beforeFirst, afterLast, jasmine */
// Set the baseUrl to brackets/src
require.config({
baseUrl: "../src",
paths: {
"test" : "../test",
"perf" : "../test/perf",
"spec" : "../test/spec",
"text" : "thirdparty/text/text",
"i18n" : "thirdparty/i18n/i18n",
"fileSystemImpl" : "filesystem/impls/appshell/AppshellFileSystem",
"preferences/PreferencesImpl" : "../test/TestPreferencesImpl",
"preact-compat" : "thirdparty/preact-compat/preact-compat.min",
"preact" : "thirdparty/preact/preact",
"preact-test-utils" : "thirdparty/preact-test-utils/preact-test-utils",
"simulate-event" : "thirdparty/simulate-event/simulate-event",
"xtend" : "thirdparty/xtend"
},
map: {
"*": {
"thirdparty/preact" : "preact-compat",
"thirdparty/preact-test-utils" : "preact-test-utils"
}
}
});
define(function (require, exports, module) {
'use strict';
// Utility dependencies
var AppInit = require("utils/AppInit"),
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
ExtensionLoader = require("utils/ExtensionLoader"),
Async = require("utils/Async"),
FileSystem = require("filesystem/FileSystem"),
FileUtils = require("file/FileUtils"),
UrlParams = require("utils/UrlParams").UrlParams,
UnitTestReporter = require("test/UnitTestReporter").UnitTestReporter,
NodeConnection = require("utils/NodeConnection"),
BootstrapReporterView = require("test/BootstrapReporterView").BootstrapReporterView,
NativeApp = require("utils/NativeApp");
// Load modules for later use
require("language/CodeInspection");
require("thirdparty/lodash");
require("editor/CodeHintManager");
require("utils/Global");
require("command/Menus");
require("utils/NodeDomain");
require("utils/ColorUtils");
require("preferences/PreferencesBase");
require("JSUtils/Session");
require("JSUtils/ScopeManager");
require("widgets/InlineMenu");
// Load modules that self-register and just need to get included in the test-runner window
require("document/ChangedDocumentTracker");
// TODO (#2155): These are used by extensions via brackets.getModule(), so tests that run those
// extensions need these to be required up front. We need a better solution for this eventually.
require("utils/ExtensionUtils");
// Load both top-level suites. Filtering is applied at the top-level as a filter to BootstrapReporter.
require("test/UnitTestSuite");
require("test/PerformanceTestSuite");
// Load JUnitXMLReporter
require("test/thirdparty/jasmine-reporters/jasmine.junit_reporter");
// Load CodeMirror add-ons--these attach themselves to the CodeMirror module
require("thirdparty/CodeMirror/addon/fold/xml-fold");
require("thirdparty/CodeMirror/addon/edit/matchtags");
require("thirdparty/CodeMirror/addon/edit/matchbrackets");
require("thirdparty/CodeMirror/addon/edit/closebrackets");
require("thirdparty/CodeMirror/addon/edit/closetag");
require("thirdparty/CodeMirror/addon/selection/active-line");
require("thirdparty/CodeMirror/addon/mode/multiplex");
require("thirdparty/CodeMirror/addon/mode/overlay");
require("thirdparty/CodeMirror/addon/search/searchcursor");
require("thirdparty/CodeMirror/keymap/sublime");
//load Language Tools Module
require("languageTools/PathConverters");
require("languageTools/LanguageTools");
require("languageTools/ClientLoader");
require("languageTools/BracketsToNodeInterface");
require("languageTools/DefaultProviders");
require("languageTools/DefaultEventHandlers");
//load language features
require("features/ParameterHintsManager");
require("features/JumpToDefManager");
var selectedSuites,
params = new UrlParams(),
reporter,
reporterView,
_writeResults = new $.Deferred(),
resultsPath;
/**
* @const
* Amount of time to wait before automatically rejecting the connection
* deferred. If we hit this timeout, we'll never have a node connection
* for the installer in this run of Brackets.
*/
var NODE_CONNECTION_TIMEOUT = 30000; // 30 seconds - TODO: share with StaticServer?
// parse URL parameters
params.parse();
resultsPath = params.get("resultsPath");
function _loadExtensionTests() {
// augment jasmine to identify extension unit tests
var addSuite = jasmine.Runner.prototype.addSuite;
jasmine.Runner.prototype.addSuite = function (suite) {
suite.category = "extension";
addSuite.call(this, suite);
};
var bracketsPath = FileUtils.getNativeBracketsDirectoryPath(),
paths = ["default"];
// load dev and user extensions only when running the extension test suite
if (selectedSuites.indexOf("extension") >= 0) {
paths.push("dev");
paths.push(ExtensionLoader.getUserExtensionPath());
}
// This returns path to test folder, so convert to src
bracketsPath = bracketsPath.replace(/\/test$/, "/src");
return Async.doInParallel(paths, function (dir) {
var extensionPath = dir;
// If the item has "/" in it, assume it is a full path. Otherwise, load
// from our source path + "/extensions/".
if (dir.indexOf("/") === -1) {
extensionPath = bracketsPath + "/extensions/" + dir;
}
return ExtensionLoader.testAllExtensionsInNativeDirectory(extensionPath);
});
}
function _documentReadyHandler() {
if (brackets.app.showDeveloperTools) {
$("#show-dev-tools").click(function () {
brackets.app.showDeveloperTools();
});
} else {
$("#show-dev-tools").remove();
}
$("#reload").click(function () {
window.location.reload(true);
});
if (selectedSuites.length === 1) {
$("#" + (selectedSuites[0])).closest("li").toggleClass("active", true);
}
AppInit._dispatchReady(AppInit.APP_READY);
jasmine.getEnv().execute();
}
function writeResults(path, text) {
// check if the file already exists
var file = FileSystem.getFileForPath(path);
file.exists(function (err, exists) {
if (err) {
_writeResults.reject(err);
return;
}
if (exists) {
// file exists, do not overwrite
_writeResults.reject();
} else {
// file not found, write the new file with xml content
FileUtils.writeText(file, text)
.done(function () {
_writeResults.resolve();
})
.fail(function (err) {
_writeResults.reject(err);
});
}
});
}
/**
* Listener for UnitTestReporter "runnerEnd" event. Attached only if
* "resultsPath" URL parameter exists. Does not overwrite existing file.
*
* @param {!$.Event} event
* @param {!UnitTestReporter} reporter
*/
function _runnerEndHandler(event, reporter) {
if (resultsPath && resultsPath.substr(-5) === ".json") {
writeResults(resultsPath, reporter.toJSON());
}
_writeResults.always(function () { brackets.app.quit(); });
}
/**
* Patch JUnitXMLReporter to use FileSystem and to consolidate all results
* into a single file.
*/
function _patchJUnitReporter() {
jasmine.JUnitXmlReporter.prototype.reportSpecResultsOriginal = jasmine.JUnitXmlReporter.prototype.reportSpecResults;
jasmine.JUnitXmlReporter.prototype.getNestedOutputOriginal = jasmine.JUnitXmlReporter.prototype.getNestedOutput;
jasmine.JUnitXmlReporter.prototype.reportSpecResults = function (spec) {
if (spec.results().skipped) {
return;
}
this.reportSpecResultsOriginal(spec);
};
jasmine.JUnitXmlReporter.prototype.getNestedOutput = function (suite) {
if (suite.results().totalCount === 0) {
return "";
}
return this.getNestedOutputOriginal(suite);
};
jasmine.JUnitXmlReporter.prototype.reportRunnerResults = function (runner) {
var suites = runner.suites(),
output = '<?xml version="1.0" encoding="UTF-8" ?>',
i;
output += "\n<testsuites>";
for (i = 0; i < suites.length; i++) {
var suite = suites[i];
if (!suite.parentSuite) {
output += this.getNestedOutput(suite);
}
}
output += "\n</testsuites>";
writeResults(resultsPath, output);
// When all done, make it known on JUnitXmlReporter
jasmine.JUnitXmlReporter.finished_at = (new Date()).getTime();
};
jasmine.JUnitXmlReporter.prototype.writeFile = function (path, filename, text) {
// do nothing
};
}
function _registerBeforeAfterHandlers() {
// Initiailize unit test preferences for each spec
beforeEach(function () {
// Unique key for unit testing
window.localStorage.setItem("preferencesKey", SpecRunnerUtils.TEST_PREFERENCES_KEY);
// Reset preferences from previous test runs
window.localStorage.removeItem("doLoadPreferences");
window.localStorage.removeItem(SpecRunnerUtils.TEST_PREFERENCES_KEY);
SpecRunnerUtils.runBeforeFirst();
});
// Revert unit test preferences after each spec
afterEach(function () {
// Clean up preferencesKey
window.localStorage.removeItem("preferencesKey");
SpecRunnerUtils.runAfterLast();
});
// Delete temp folder before running the first test
beforeFirst(function () {
SpecRunnerUtils.removeTempDirectory();
});
// Delete temp folder after running the last test
afterLast(function () {
SpecRunnerUtils.removeTempDirectory();
});
}
function init() {
selectedSuites = (params.get("suite") || window.localStorage.getItem("SpecRunner.suite") || "unit").split(",");
// Create a top-level filter to show/hide performance and extensions tests
var runAll = (selectedSuites.indexOf("all") >= 0);
var topLevelFilter = function (spec) {
// special case "all" suite to run unit, perf, extension, and integration tests
if (runAll) {
return true;
}
var currentSuite = spec.suite,
category = spec.category;
if (!category) {
// find the category from the closest suite
while (currentSuite) {
if (currentSuite.category) {
category = currentSuite.category;
break;
}
currentSuite = currentSuite.parentSuite;
}
}
// if unit tests are selected, make sure there is no category in the heirarchy
// if not a unit test, make sure the category is selected
return (selectedSuites.indexOf("unit") >= 0 && category === undefined) ||
(selectedSuites.indexOf(category) >= 0);
};
/*
* TODO (jason-sanjose): extension unit tests should only load the
* extension and the extensions dependencies. We should not load
* unrelated extensions. Currently, this solution is all or nothing.
*/
// configure spawned test windows to load extensions
SpecRunnerUtils.setLoadExtensionsInTestWindow(selectedSuites.indexOf("extension") >= 0);
_loadExtensionTests(selectedSuites).always(function () {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
_registerBeforeAfterHandlers();
// Create the reporter, which is really a model class that just gathers
// spec and performance data.
reporter = new UnitTestReporter(jasmineEnv, topLevelFilter, params.get("spec"));
SpecRunnerUtils.setUnitTestReporter(reporter);
// Optionally emit JUnit XML file for automated runs
if (resultsPath) {
if (resultsPath.substr(-4) === ".xml") {
_patchJUnitReporter();
jasmineEnv.addReporter(new jasmine.JUnitXmlReporter(null, true, false));
}
// Close the window
$(reporter).on("runnerEnd", _runnerEndHandler);
} else {
_writeResults.resolve();
}
jasmineEnv.addReporter(reporter);
// Create the view that displays the data from the reporter. (Usually in
// Jasmine this is part of the reporter, but we separate them out so that
// we can more easily grab just the model data for output during automatic
// testing.)
reporterView = new BootstrapReporterView(window.document, reporter);
// remember the suite for the next unit test window launch
window.localStorage.setItem("SpecRunner.suite", selectedSuites);
$(window.document).ready(_documentReadyHandler);
});
// Prevent clicks on any link from navigating to a different page (which could lose unsaved
// changes). We can't use a simple .on("click", "a") because of http://bugs.jquery.com/ticket/3861:
// jQuery hides non-left clicks from such event handlers, yet middle-clicks still cause CEF to
// navigate. Also, a capture handler is more reliable than bubble.
window.document.body.addEventListener("click", function (e) {
// Check parents too, in case link has inline formatting tags
var node = e.target, url;
while (node) {
if (node.tagName === "A") {
url = node.getAttribute("href");
if (url && url.match(/^http/)) {
NativeApp.openURLInDefaultBrowser(url);
e.preventDefault();
}
break;
}
node = node.parentElement;
}
}, true);
}
function connectToTestDomain() {
var _nodeConnectionDeferred = new $.Deferred(),
_nodeConnection = new NodeConnection();
_nodeConnection.connect(true).then(function () {
var domainPath = FileUtils.getNativeBracketsDirectoryPath() + "/" + FileUtils.getNativeModuleDirectoryPath(module) + "/../test/node/TestingDomain";
_nodeConnection.loadDomains(domainPath, true)
.then(init, function () {
// Failed to connect
console.error("[SpecRunner] Failed to connect to node", arguments);
var container = $('<div class="container-fluid">');
container.append('<div class="alert alert-error">Failed to connect to Node</div>');
$(window.document.body).append(container);
});
});
Async.withTimeout(_nodeConnectionDeferred.promise(), NODE_CONNECTION_TIMEOUT);
brackets.testing = { nodeConnection: _nodeConnection };
}
connectToTestDomain();
});