forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBootstrapReporterView.js
368 lines (298 loc) · 14.9 KB
/
BootstrapReporterView.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
/*
* Copyright (c) 2012 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.
*
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50, regexp: true, forin: true */
/*global jasmine, $, define, document, require */
define(function (require, exports, module) {
'use strict';
var UrlParams = require("utils/UrlParams").UrlParams,
StringUtils = require("utils/StringUtils"),
SpecRunnerUtils = require("spec/SpecRunnerUtils");
var BootstrapReporterView = function (doc, reporter) {
doc = doc || document;
$(reporter)
.on("runnerStart", this._handleRunnerStart.bind(this))
.on("runnerEnd", this._handleRunnerEnd.bind(this))
.on("suiteEnd", this._handleSuiteEnd.bind(this))
.on("specStart", this._handleSpecStart.bind(this))
.on("specEnd", this._handleSpecEnd.bind(this));
// build DOM immediately
var container = $(
'<div class="container-fluid">' +
'<div class="row-fluid">' +
'<div class="span4">' +
'<ul id="suite-list" class="nav nav-pills nav-stacked">' +
'</ul>' +
'</div>' +
'<div id="results-container" class="span8">' +
'</div>' +
'</div>' +
'</div>'
);
$(doc.body).append(container);
this._topLevelSuiteMap = {};
this.$suiteList = $("#suite-list");
this.$resultsContainer = $("#results-container");
};
BootstrapReporterView.prototype._createSuiteListItem = function (suiteName, specCount) {
var $badgeAll = $('<span class="badge">' + specCount + "</span>"),
$badgePassed = $('<span class="badge badge-success" style="display:none"/>'),
$badgeFailed = $('<span class="badge badge-important" style="display:none"/>'),
$anchor = $('<a href="?spec=' + encodeURIComponent(suiteName) + '">' + suiteName + '</a>').append($badgeAll).append($badgePassed).append($badgeFailed),
$listItem = $('<li/>').append($anchor),
self = this,
active;
this._topLevelSuiteMap[suiteName] = {
$badgeAll: $badgeAll,
$badgePassed: $badgePassed,
$badgeFailed: $badgeFailed,
$anchor: $anchor,
$listItem: $listItem
};
return $listItem;
};
BootstrapReporterView.prototype._createSuiteList = function (suites, sortedNames, totalSpecCount) {
var self = this;
sortedNames.forEach(function (name, index) {
var count = suites[name].specCount;
if (count > 0) {
self.$suiteList.append(self._createSuiteListItem(name, count));
}
});
// add an "all" top-level suite
this.$suiteList.prepend(this._createSuiteListItem("All", totalSpecCount));
};
BootstrapReporterView.prototype._showProgressBar = function (spec) {
if (!this.$progressBar) {
this.$progress = $('<div class="bar"/>');
this.$progressBar = $('<div class="progress progress-striped"/>').append(this.$progress);
}
this.$resultsContainer.append(this.$progressBar);
};
BootstrapReporterView.prototype._handleRunnerStart = function (event, reporter) {
var topLevelData,
self = this;
// create top level suite list navigation
this._createSuiteList(reporter.suites, reporter.sortedNames, reporter.totalSpecCount);
// highlight the current suite
topLevelData = reporter.activeSuite ? this._topLevelSuiteMap[reporter.activeSuite] : null;
if (topLevelData) {
topLevelData.$listItem.toggleClass("active", true);
}
if (reporter.activeSpecCount) {
this._showProgressBar();
// display current running test
this.$info = $('<div class="alert alert-info"/>');
this.$resultsContainer.append(this.$info);
this.$resultsContainer.append($('<hr/>'));
}
};
BootstrapReporterView.prototype._handleRunnerEnd = function (event, reporter) {
if (this.$info) {
this.$info.toggleClass("alert-info", false);
if (reporter.passed) {
this.$info.toggleClass("alert-success", true).text("Complete. No failures.");
} else {
this.$info.toggleClass("alert-error", true).text("Complete. See failures.");
}
}
};
BootstrapReporterView.prototype._handleSuiteEnd = function (event, reporter, suiteData) {
var data = this._topLevelSuiteMap[suiteData.name];
if ((suiteData.name === reporter.activeSuite) && data) {
data.$badgeAll.hide();
}
};
BootstrapReporterView.prototype._handleSpecStart = function (event, reporter, specName) {
this.$info.text("Running " + specName);
};
BootstrapReporterView.prototype._updateSuiteStatus = function (name, specCount, passedCount, failedCount) {
var data = this._topLevelSuiteMap[name];
if (!data) {
return;
}
// update status badges
if (passedCount) {
data.$badgePassed.show().text(passedCount);
} else {
data.$badgePassed.hide();
}
if (failedCount) {
data.$badgeFailed.show().text(failedCount);
} else {
data.$badgeFailed.hide();
}
var specsRemaining = specCount - passedCount - failedCount;
if (specsRemaining === 0) {
data.$badgeAll.hide();
} else {
data.$badgeAll.text(specsRemaining);
}
};
BootstrapReporterView.prototype._createRows = function (record, level) {
var rows = [],
$row,
indent = "",
i,
self = this;
level = (level || 0);
for (i = 0; i < level; i++) {
indent = indent.concat(" ");
}
if (level > 0) {
indent = indent.concat("• ");
} else if (record.children) {
indent = "» ".concat(indent);
}
$row = $("<tr/>");
$row.append($("<td>" + indent + record.name + "</td><td>" + record.value + "</td>"));
rows.push($row);
if (record.children) {
level++;
record.children.forEach(function (child) {
Array.prototype.push.apply(rows, self._createRows(child, level));
});
}
return rows;
};
BootstrapReporterView.prototype._handleSpecEnd = function (event, reporter, specData, suiteData) {
var $specLink,
$resultDisplay,
self = this;
this._updateSuiteStatus(suiteData.name, suiteData.specCount, suiteData.passedCount, suiteData.failedCount);
this._updateSuiteStatus("All", reporter.totalSpecCount, reporter.totalPassedCount, reporter.totalFailedCount);
this.$progress.css("width", Math.round((reporter.activeSpecCompleteCount / reporter.activeSpecCount) * 100) + "%");
if (!specData.passed) {
// print suite name if not present
var $suiteHeader = $("#suite-results-" + suiteData.id);
if ($suiteHeader.length === 0) {
this.$resultsContainer.append($('<div id="suite-results-' + suiteData.id + '" class="alert alert-info"/>').text(suiteData.name));
}
// print spec name
$specLink = $('<a href="?spec=' + encodeURIComponent(specData.name) + '"/>').text(specData.description);
$resultDisplay = $('<div class="alert alert-error"/>').append($specLink);
// print failure details
if (specData.messages) {
specData.messages.forEach(function (message) {
// Render with clickable links if parent Brackets window available; plain text otherwise
if (window.opener) {
var htmlMessage = self._linkerizeStack(message);
$resultDisplay.append($('<pre/>').html(htmlMessage));
} else {
$resultDisplay.append($('<pre/>').text(message));
}
});
}
$resultDisplay.on("click", ".link-to-source", this._handleSourceLinkClick.bind(this));
this.$resultsContainer.append($resultDisplay);
}
if (specData.passed && specData.perf) {
// add spec name
$specLink = $('<a href="?spec=' + encodeURIComponent(specData.name) + '"/>').text(specData.name);
this.$resultsContainer.append($('<div class="alert alert-info"/>').append($specLink));
// add table
var $table = $('<table class="table table-striped table-bordered table-condensed"><thead><tr><th>Measurement</th><th>Value</th></tr></thead></table>'),
$tbody = $table.append($('<tbody/>')),
rows,
specRecords = specData.perf;
this.$resultsContainer.append($table);
specRecords.forEach(function (record) {
rows = self._createRows(record);
rows.forEach(function (row) {
$tbody.append(row);
});
});
}
};
var _codeRefRegExp = /file:\/\/.*?:(\d+):(\d+)/g; // matches file:// followed by two :-prefixed numbers, all on the same line
/**
* Given a plaintext stack trace, returns an HTML version where all source file references are .link-to-source links
* @param {!string} text
* @return {!string} HTML
*/
BootstrapReporterView.prototype._linkerizeStack = function (text) {
var html = "",
indexAfterLastMatch = 0, // index into 'text'
plainText,
match;
// We'll style links to Jasmine code less prominently (vs. test spec code / core Brackets code)
function isTestFrameworkCode() {
return match[0].indexOf("/jasmine-core/") !== -1;
}
while ((match = _codeRefRegExp.exec(text)) !== null) {
// Add any plain text before the link
plainText = text.substring(indexAfterLastMatch, match.index);
html += StringUtils.htmlEscape(plainText);
// Create a clickable link for the file
var line = match[1], ch = match[2];
var cssClasses = "link-to-source";
if (isTestFrameworkCode()) {
cssClasses += " testframework-link";
}
var linkPrefix = "<a href='#' class='" + cssClasses + "' data-line='" + line + "' data-ch='" + ch + "'>";
html += linkPrefix + StringUtils.htmlEscape(match[0]) + "</a>";
indexAfterLastMatch = match.index + match[0].length;
}
// Add any trailing plain text after last link
plainText = text.substring(indexAfterLastMatch);
html += StringUtils.htmlEscape(plainText);
return html;
};
/** Handles links generated by _linkerizeStack(), opening the source file in our parent Brackets window */
BootstrapReporterView.prototype._handleSourceLinkClick = function (event) {
var CommandManager = window.opener.brackets.getModule("command/CommandManager"),
Commands = window.opener.brackets.getModule("command/Commands"),
EditorManager = window.opener.brackets.getModule("editor/EditorManager"),
ProjectManager = window.opener.brackets.getModule("project/ProjectManager"),
FileUtils = window.opener.brackets.getModule("file/FileUtils");
var uri = $(event.target).text(),
lineData = $(event.target).attr("data-line"),
chData = $(event.target).attr("data-ch"),
lineNum = parseInt(lineData, 10) - 1,
chNum = parseInt(chData, 10) - 1;
// Remove file:// prefix and :line:ch suffix, then convert that "clean" URI to a native path
var path = uri.substring("file://".length, uri.length - lineData.length - chData.length - 2);
if (path.indexOf("localhost") === 0) { // Macs also bizarrely prepend the URI with "localhost"
path = path.substring("localhost".length);
}
path = FileUtils.convertToNativePath(path);
// Convert from symlinked path to real path - otherwise Brackets will think they are two separate files.
// Note: we assume the current project open in our parent Brackets window is the Brackets source
var bracketsRoot = FileUtils.getNativeBracketsDirectoryPath();
if (bracketsRoot.substr(bracketsRoot.length - 4) === "/src") {
var symlinkPrefix = bracketsRoot.substring(0, bracketsRoot.length - 3); // include trailing "/"
if (path.indexOf(symlinkPrefix) === 0) {
var realPrefix = ProjectManager.getProjectRoot().fullPath;
path = realPrefix + path.substring(symlinkPrefix.length);
}
}
// Open file in parent Brackets window & jump cursor to indicated pos
// TODO: can we bring the Brackets window to the front?
CommandManager.execute(Commands.FILE_OPEN, {fullPath: path})
.done(function (doc) {
EditorManager.getCurrentFullEditor().setCursorPos(lineNum, chNum, true);
});
};
BootstrapReporterView.prototype.log = function (str) {
};
exports.BootstrapReporterView = BootstrapReporterView;
});