-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.js
215 lines (193 loc) · 6.26 KB
/
parser.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
'use strict';
var localizationTable = require('./localization.json');
var spawn = require('child_process').spawn,
EventEmitter = require('events').EventEmitter;
/**
* Determines if this line marks the beginning of results
* @param {string} line
* @returns {boolean}
*/
function isBeginMarker(line) {
return line.indexOf('----') === 0;
}
/**
* Determines if this line marks the end of results
* @param {string} line
* @returns {boolean}
*/
function isEndMarker(line) {
return (/\d/).test(line[0]) || line.indexOf(this.translatedMessages.FinalTestResults) === 0;
}
/**
* Determines if this line marks the beginning of a new test result
* @param {string} line
* @returns {boolean}
*/
function isNewTest(line) {
var possibleResults = [this.translatedMessages.Passed, this.translatedMessages.Failed, this.translatedMessages.Inconclusive];
for (var i = 0, len = possibleResults.length; i < len; i++) {
if (line.indexOf(possibleResults[i]) === 0) {
return true;
}
}
return false;
}
/**
* Determines if the line marks the beginning of an attribute
* @param {string} line
* @returns {boolean}
*/
function isAttribute(line) {
//return line.indexOf("\t") != 0;
return line.indexOf('[') === 0;
}
/**
* Runs and parses MSTest results on the command-line
* @param {string} exePath Path to mstest.exe
* @param {string[]} args Additional arguments
* @param {string} [workingDir] Working directory for tests
* @param {string[]} [detailsMap] Map details to user-defined property names
* @constructor
*/
var Parser = function (exePath, args, workingDir, detailsMap, language) {
this.results = [];
this.passedTests = [];
this.failedTests = [];
this.detailsMap = detailsMap;
this.language = language || 'en';
this.translatedMessages = localizationTable[this.language];
this.isAttribute = isAttribute;
this.isNewTest = isNewTest;
this.isEndMarker = isEndMarker;
this.isBeginMarker = isBeginMarker;
if (this.translatedMessages === undefined) {
this.translatedMessages = localizationTable['en'];
}
var self = this,
spawnOptions = {},
latestResult = null,
latestAttribute = {
key: '',
value: ''
},
startParsing = false,
stopParsing = false,
child;
if (workingDir) {
spawnOptions.cwd = workingDir;
}
child = spawn(exePath, args, spawnOptions);
child.stdout.on('data', function (data) {
data = data.toString();
// Skip the rest if we've already stopped parsing
if (startParsing && stopParsing) {
return;
}
// Parse it all line by line
var lines = data.split('\r\n');
for (var i = 0, len = lines.length; i < len; i++) {
var line = lines[i];
// Deal with extra breaks from the split
if (line.length === 0) {
continue;
}
// Don't start parsing until we see results
if (!startParsing) {
startParsing = self.isBeginMarker(line);
continue;
} else if (self.isEndMarker(line)) {
stopParsing = true;
// Push in the last result
self._pushResult(latestResult, latestAttribute);
return;
}
// Start parsing a new test result
if (self.isNewTest(line)) {
self._pushResult(latestResult, latestAttribute);
var statusAndName = line.split(/ +/);
latestResult = {
status: statusAndName[0],
name: statusAndName[1]
};
} else if (self.isAttribute(line)) {
// Just in case we've been building up another attribute
if (latestAttribute.value.length > 0) {
self._setAttribute(latestResult, latestAttribute.key, latestAttribute.value);
}
var keyAndValue = line.split(' = '),
key = keyAndValue[0].replace(/\[|\]/g, ''),
value = keyAndValue[1];
if (latestResult === null) {
self.emit('error', 'Unexpected attribute: ' + key + '\nLine: ' + line);
} else {
latestAttribute = {
key: key,
value: value
};
}
} else {
// Must be a continuing attribute
if (latestAttribute.value.length === 0) {
self.emit('Expected continuing attribute but got: ' + line);
return;
}
latestAttribute.value += '\r\n' + line;
}
}
});
child.stderr.on('data', function (err) {
self.emit('error', err.toString());
});
child.on('close', function () {
self.emit('done', self.results, self.passedTests, self.failedTests);
});
};
Parser.prototype = Object.create(EventEmitter.prototype);
/**
* Adds a new test result
* @param {TestResult} result
* @param {object} attribute
* @private
*/
Parser.prototype._pushResult = function (result, attribute) {
if (result === null) {
return;
}
if (attribute.value.length > 0) {
this._setAttribute(result, attribute.key, attribute.value);
}
attribute.key = '';
attribute.value = '';
this.results.push(result);
if (result.status === this.translatedMessages.Passed) {
result.passed = true;
this.passedTests.push(result);
} else {
result.passed = false;
this.failedTests.push(result);
}
this.emit('test', result);
};
/**
* Sets an attribute
* @param {TestResult} result
* @param {string} key
* @param {*} value
* @private
*/
Parser.prototype._setAttribute = function (result, key, value) {
for (var i = 0, len = this.detailsMap.length; i < len; i++) {
if (this.detailsMap[i].toLowerCase() === key) {
key = this.detailsMap[i];
break;
}
}
result[key] = value;
};
module.exports = Parser;
/**
* @name TestResult
* @property {string} status
* @property {boolean} passed
* @property {string} @detailName
*/