-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmavenRunner.js
320 lines (285 loc) · 7.99 KB
/
mavenRunner.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
/*
* Node JS Project to run Maven Cobertura iteratively on a series of tests
* In the ODF Toolkit.
* Aim is to accumulate progressive coverage results
*
* We will then include the associated documents and run Production Coverage
* via ODFE and get a comparison of Production versus Code Coverage
*/
var spawn = require('child_process').spawn;
var shelljs = require('shelljs');
var async = require('async');
var htmlparser = require('htmlparser2');
var fs = require('fs-extra');
var jsonfile = require('jsonfile');
var json2csv = require('json2csv');
var coberParser = require('./CoberHtmlParser');
var ODFDOM = "/Users/Ian/odf/odfdom";
var ODF = "/Users/Ian/odf/";
var TESTBASE = "/Users/Ian/odf/testBase/odfdom/";
var TESTSRC = "/Users/Ian/odf/odfdom/src/";
var ORIG_ODF_TEST_DIR = TESTSRC + "test/java/org/odftoolkit/odfdom/";
var TEMP_ODF_TEST_DIR = TESTBASE + "test/java/org/odftoolkit/odfdom/";
exports.runCobertura = runCobertura;
/*var csvInfo = {};
csvInfo.lineName = "LineCoverage";
csvInfo.branchName = "BranchCoverage";
csvInfo.lineCSV = ""; //the results CSV string.
csvInfo.branchCSV = ""; //the results CSV string.
csvInfo.titleDone = false;
console.log("maven runner");*/
//sequenceTests();
//resultsToCSV();
function doMaven(test, key, callback) {
console.log("Do the stuff for " + test);
shelljs.cp('-Rf', TESTBASE + test, TESTSRC + test);
runCobertura(ODFDOM, test, callback);
console.log("Done");
}
function allDone(err) {
console.log("All Done and I'm knackered");
if (err) {
console.log(err);
} else {
console.log("Results:" + results);
}
}
/*for(var i=0; i<2; i++) {//i<numTests; i++) {
console.log(tests[i]);
//Add each test to the src test directory
shelljs.cp('-Rf', TESTBASE + tests[i], TESTSRC + tests[i]);
//Then run Cobertura
// log the test and corresponding coverage
// at the conclusion of the cobertura run
//need to make this synchronous...
//Use promises?
runCobertura(ODFDOM, tests[i], firstRun);
firstRun = false;
}*/
function runCobertura(testDirectory, test, key, callback) {
console.log('maven coverage');
var mvn = require('maven').create({
cwd : testDirectory
});
//could run it and generate XML instead of the html - or both?
//XML does not seem to be forthcoming... just live with html
var mvnCommands = [];
if (key === 0) {
//mvnCommands.push('clean');
mvnCommands.push('cobertura:cobertura');
} else {
mvnCommands.push('cobertura:cobertura');
}
mvn.execute(mvnCommands, {
format : 'html'
}).then(function (result) {
console.log("Tests run\n");
console.log("Now get the coverage statistics");
coberParser.parseResultsToJSON(testDirectory, test, key);
console.log("Parsing done");
callback();
}, function (err) {
callback(err);
});
}
/* Want to start with a given directory of tests
** Copy it to a temporary location
** Iterate the tests by some increment
** copy the tests back
** run the tests and generate the coverage results
**
** Somehow need to learn or also have a list of associated documents
** to be used in Production coverage
*/
// start from odfdom/src/test/java/org/odftoolkit/odfdom
function sequenceTests() {
//start with - move the tests. use fs-extra
console.log("Move tests from " + ORIG_ODF_TEST_DIR + " to " + TEMP_ODF_TEST_DIR);
//Only want to move the tests....
//If we move all of the dir then we lose the utility classes
//Maybe we should have a filter on none tests and move them back
//Can we do that?
var otests = shelljs.ls('-R', ORIG_ODF_TEST_DIR).filter(function (file) {
return file.match(/\Test.java$/);
});
var onumTests = otests.length;
console.log("Original number of tests: " + onumTests);
async.forEachOfSeries(otests, moveOrig, allMoved);
}
function moveOrig(test, key, callback) {
fs.move(ORIG_ODF_TEST_DIR + test, TEMP_ODF_TEST_DIR + test, function (err) {
if (err) {
callback(err);
} else {
console.log("Moved " + ORIG_ODF_TEST_DIR + test + " to " + TEMP_ODF_TEST_DIR + test);
callback()
}
});
}
function allMoved(err) {
if (err) {
console.log("Orig Move :" + err);
} else {
console.log("All original tests moved");
var tests = shelljs.ls('-R', TEMP_ODF_TEST_DIR).filter(function (file) {
return file.match(/\Test.java$/);
});
var numTests = tests.length;
console.log("Num Tests: " + tests.length);
//so now move them back and test each set
async.forEachOfSeries(tests, moveBackAndTest, allTested);
}
}
function moveBackAndTest(test, key, callback) {
fs.move(TEMP_ODF_TEST_DIR + test, ORIG_ODF_TEST_DIR + test, function (err) {
if (err) {
callback(err);
} else {
console.log("Returned " + TEMP_ODF_TEST_DIR + test + " to " + ORIG_ODF_TEST_DIR + test);
var otests = shelljs.ls('-R', ORIG_ODF_TEST_DIR).filter(function (file) {
return file.match(/\Test.java$/);
});
var onumTests = otests.length;
runCobertura(ODFDOM, test, key, callback);
}
});
}
function allTested(err) {
if (err) {
console.log("All tested :" + err);
} else {
console.log("All tests performed");
}
}
function resultsToCSV() {
console.log("Generate CSV of results");
async.waterfall([
getResultsFiles,
openLineCSV,
openBranchCSV,
getCSVResults,
writeLineResults,
writeBranchResults,
closeLineCSV,
closeBranchCSV
], resultsDone);
}
function resultsDone(err, result) {
if (err) {
console.log(err);
} else {
console.log("CSV " + result);
}
}
function getResultsFiles(callback) {
csvInfo.resultFiles = shelljs.ls('-R', "results").filter(function (file) {
return file.match(/.json$/);
});
callback(null);
}
function openLineCSV(callback) {
var numResults = csvInfo.resultFiles.length;
console.log("Num results files: " + numResults);
console.log("open " + csvInfo.lineName);
fs.open(csvInfo.lineName + ".csv", "w", function (err, fd) {
if (err) {
callback(err);
} else {
csvInfo.lineFD = fd;
callback(null);
}
});
}
function openBranchCSV(callback) {
console.log("open " + csvInfo.branchName);
fs.open(csvInfo.branchName + ".csv", "w", function (err, fd) {
if (err) {
callback(err);
} else {
csvInfo.branchFD = fd;
callback(null);
}
});
}
function getCSVResults(callback) {
async.forEachOfSeries(csvInfo.resultFiles, addToCSV, allAdded);
callback(null);
}
function writeLineResults(callback) {
fs.write(csvInfo.lineFD, csvInfo.lineCSV, function (err) {
if (err) {
callback(err);
} else {
callback(null);
}
});
}
function writeBranchResults(callback) {
fs.write(csvInfo.branchFD, csvInfo.branchCSV, function (err) {
if (err) {
callback(err);
} else {
callback(null);
}
});
}
/**
* Offer some sort of filtering mechanism here?
* or leave to presentation stage?
* Generate separate line and Branch coverage reports?
* -same funtion but parameter to determine which to generate
*
*/
function addToCSV(resultFile, key, callback) {
console.log("Generate CSV from " + resultFile);
var results = JSON.parse(fs.readFileSync("results/" + resultFile, 'utf8'));
var numDataElements = results.data.length;
if (csvInfo.titleDone === false) {
csvInfo.lineCSV += "Test";
csvInfo.branchCSV += "Test";
for (var e = 0; e < numDataElements; e++) {
csvInfo.lineCSV += "," + results.data[e].name;
csvInfo.branchCSV += "," + results.data[e].name;
}
csvInfo.lineCSV += "\n";
csvInfo.branchCSV += "\n";
csvInfo.titleDone = true;
}
csvInfo.lineCSV += results.test;
csvInfo.branchCSV += results.test;
for (var e = 0; e < numDataElements; e++) {
var values = results.data[e].values;
csvInfo.lineCSV += "," + values[0];
var bc = values[2];
if (bc === "N/A") {
bc = 0;
}
csvInfo.branchCSV += "," + bc;
}
csvInfo.lineCSV += "\n";
csvInfo.branchCSV += "\n";
callback();
}
function allAdded(err) {
console.log("All Added to CSVs");
}
function closeLineCSV(callback) {
fs.close(csvInfo.lineFD, function (err) {
if (err) {
callback(err);
} else {
console.log("Close Line CSV");
callback(null);
}
});
}
function closeBranchCSV(callback) {
fs.close(csvInfo.branchFD, function (err) {
if (err) {
callback(err);
} else {
console.log("Close Branch CSV");
callback(null);
}
});
}