-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_helpers.js
152 lines (134 loc) · 4.05 KB
/
test_helpers.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
////
//// Take incoming results and drive resultsTree
////
running = true;
totalCount = 0;
passedCount = 0;
failedCount = 0;
failedTests = [];
resultTree = [];
Package = {};
// report a series of events in a single test, or just the existence of
// that test if no events. this is the entry point for test results to
// this module.
reportResults = function(results) {
var test = _findTestForResults(results);
// Tolerate repeated reports: first undo the effect of any previous report
var status = _testStatus(test);
if (status === "failed") {
failedCount--;
} else if (status === "succeeded") {
passedCount--;
}
// Now process the current report
if (_.isArray(results.events)) {
// append events, if present
Array.prototype.push.apply((test.events || (test.events = [])),
results.events);
// sort and de-duplicate, based on sequence number
test.events.sort(function (a, b) {
return a.sequence - b.sequence;
});
var out = [];
_.each(test.events, function (e) {
if (out.length === 0 || out[out.length - 1].sequence !== e.sequence)
out.push(e);
});
test.events = out;
}
status = _testStatus(test);
if (status === "failed") {
failedCount++;
// Expand a failed test (but only set this if the user hasn't clicked on the
// test name yet).
if (test.expanded === undefined)
test.expanded = true;
if (!_.contains(failedTests, test.fullName))
failedTests.push(test.fullName);
} else if (status === "succeeded") {
passedCount++;
} else if (test.expanded) {
// re-render the test if new results come in and the test is
// currently expanded.
}
};
// given a 'results' as delivered via reportResults, find the
// corresponding leaf object in resultTree, creating one if it doesn't
// exist. it will be an object with attributes 'name', 'parent', and
// possibly 'events'.
var _findTestForResults = function (results) {
var groupPath = results.groupPath; // array
if ((! _.isArray(groupPath)) || (groupPath.length < 1)) {
throw new Error("Test must be part of a group");
}
var group;
var i = 0;
_.each(groupPath, function(gname) {
var array = (group ? (group.groups || (group.groups = []))
: resultTree);
var newGroup = _.find(array, function(g) { return g.name === gname; });
if (! newGroup) {
newGroup = {
name: gname,
parent: (group || null),
path: groupPath.slice(0, i+1),
dep: new Tracker.Dependency
}; // create group
array.push(newGroup);
}
group = newGroup;
i++;
});
var testName = results.test;
var server = !!results.server;
var test = _.find(group.tests || (group.tests = []),
function(t) { return t.name === testName &&
t.server === server; });
if (! test) {
// create test
var nameParts = _.clone(groupPath);
nameParts.push(testName);
var fullName = nameParts.join(' - ');
test = {
name: testName,
parent: group,
server: server,
fullName: fullName,
dep: new Tracker.Dependency
};
group.tests.push(test);
totalCount++;
}
return test;
};
////
//// Helpers on test objects
////
var _testTime = function(t) {
if (t.events && t.events.length > 0) {
var lastEvent = _.last(t.events);
if (lastEvent.type === "finish") {
if ((typeof lastEvent.timeMs) === "number") {
return lastEvent.timeMs;
}
}
}
return null;
};
var _testStatus = function(t) {
var events = t.events || [];
if (_.find(events, function(x) { return x.type === "exception"; })) {
// "exception" should be last event, except race conditions on the
// server can make this not the case. Technically we can't tell
// if the test is still running at this point, but it can only
// result in FAIL.
return "failed";
} else if (events.length == 0 || (_.last(events).type != "finish")) {
return "running";
} else if (_.any(events, function(e) {
return e.type == "fail" || e.type == "exception"; })) {
return "failed";
} else {
return "succeeded";
}
};