-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
293 lines (273 loc) · 10.2 KB
/
index.js
File metadata and controls
293 lines (273 loc) · 10.2 KB
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
/* global __dirname: false */
/* global process: false */
(function (module) {
"use strict";
var express = require("express");
var gulp = require("gulp");
var rename = require("gulp-rename");
var htmlreplace = require("gulp-html-replace");
var spawn = require("spawn-cmd").spawn;
var log = require("fancy-log");
var webdriver_update = require("gulp-protractor").webdriver_update;
var webdriver_update_specific = require("gulp-protractor").webdriver_update_specific;
var protractor = require("gulp-protractor").protractor;
var path = require("path");
var uuid = require("uuid");
var fs = require("fs");
var xml2js = require("xml2js");
var async = require("async");
var karma = require('karma');
var coveralls = require('@kollavarsham/gulp-coveralls');
var _ = require("lodash");
var https = require("https");
var http = require("http");
var key = fs.readFileSync(path.join(__dirname, "keys", "https-key.pem"));
var cert = fs.readFileSync(path.join(__dirname, "keys", "https-cert.pem"));
var e2ePort = process.env.E2E_PORT || 8099;
var httpServer;
var factory = {
getCoverageReportDir: function() {
return process.env.CIRCLE_ARTIFACTS ?
path.join(process.env.CIRCLE_ARTIFACTS, "./coverage") :
path.join(__dirname, "../../reports/coverage");
},
gulpTaskFactory: {
webdriveUpdate: function () {
return webdriver_update;
},
webdriverUpdateSpecific: webdriver_update_specific,
testServer: function (options) {
options = options || {};
return function (done) {
var server = express();
var rootPath = options.rootPath || "./";
server.use(express.static(rootPath));
if(options.html5mode){
server.get('*', function(request, response){
response.sendFile('index.html', {"root": rootPath});
});
}
var credentials = {key: key, cert: cert};
var hServer;
if(options.https) {
hServer = https.createServer(credentials, server);
}
else {
hServer = http.createServer(server);
}
httpServer = hServer.listen(options.port || e2ePort);
done();
};
},
testServerClose: function () {
return function (done) {
httpServer.close();
done();
};
},
htmlE2E: function (options) {
options = options || {};
if (!options.hasOwnProperty("e2egadgets")) {
options.e2egadgets = "../node_modules/widget-tester/mocks/gadget-mocks.js";
}
if (!options.hasOwnProperty("files")) {
options.files = "./src/settings.html";
}
return function () {
return gulp.src(options.files)
.pipe(htmlreplace(options))
.pipe(rename(function (path) {
path.basename += "-e2e";
}))
.pipe(gulp.dest("./src/"));
};
},
testE2E: function (options) {
return function (cb) {
cb('Deprecated - it was using CasperJS');
};
},
ensureReportDirectory: function (options) {
options = options || {};
return function (cb) {
if(!fs.existsSync("./reports")) {
fs.mkdir("./reports/", function (err) {
cb(err);
});
}
else {
cb();
}
};
},
testUnitAngular: function (options) {
options = options || {};
return function(cb) {
// Be sure to return the stream
if(!options.testFiles) {
cb("Test files is missing.");
}
else {
var karmaOptions = {
files: options.testFiles,
configFile: options.configFile || path.join(__dirname, "karma.conf.js"),
singleRun: options.singleRun || true,
basePath : options.basePath || "./",
};
if (options.coverageFiles) {
karmaOptions.preprocessors = {}
karmaOptions.preprocessors[options.coverageFiles] = "coverage";
karmaOptions.coverageReporter = {
dir : factory.getCoverageReportDir(),
reporters: [
{ type: 'lcov'},
{ type: 'text' },
{ type: 'text-summary' },
]
};
}
var server = new karma.Server(karmaOptions, function(exitCode) {
if (exitCode === 0) {
cb();
} else {
var msg = "Tests failed with exit code:" + exitCode;
console.error(msg);
cb(msg);
}
});
server.start();
}
};
},
testUnitReact: function (options) {
return function (cb) {
karma.server.start({
configFile: options.configFile || path.join(__dirname, "karma.conf.js"),
singleRun: options.singleRun || true
}, function(exitCode) {
if (exitCode === 0) {
cb();
} else {
var msg = "Tests failed with exit code:" + exitCode;
console.error(msg);
cb(msg);
}
});
}
},
testE2EAngular: function (options) {
options = options || {};
var runAngularTest = function (cb) {
var args = ["--baseUrl", options.baseUrl || "http://" + require("./getIpAddress")() + ":" + e2ePort + "/src/settings-e2e.html",
"--params.login.user", options.loginUser, "--params.login.pass", options.loginPass,
"--params.login.user1", options.loginUser1, "--params.login.pass1", options.loginPass1,
"--params.login.user2", options.loginUser2, "--params.login.pass2", options.loginPass2,
"--params.twitter.user", options.twitterUser, "--params.twitter.pass", options.twitterPass,
"--params.login.stageEnv", options.stageEnv];
var argv = require("yargs").argv;
if(!options.specs && argv.specs) {
options.specs = argv.specs;
}
return gulp.src(options.src || options.testFiles || options.specs || ["./test/e2e/**/*scenarios.js"])
.pipe(protractor({
configFile: options.configFile || path.join(__dirname, "protractor.conf.js"),
args: args
}))
.on("error", function (e) {
log(e);
/*if(fs.statSync("./reports/angular-xunit.xml")) {
//output test result to console
log("Test report", fs.readFileSync("./reports/angular-xunit.xml", {encoding: "utf8"}));
}*/
if(options.throw || options.throw ===undefined) {
throw e;
}
else {
cb(e);
}
});
};
var id = uuid.v1();
gulp.task(id + ":ensureReportDirectory", factory.gulpTaskFactory.ensureReportDirectory());
gulp.task(id + ":runAngularTest", runAngularTest);
return gulp.series(
id + ":ensureReportDirectory",
id + ":runAngularTest"
);
},
coveralls: function() {
return function () {
return gulp.src(factory.getCoverageReportDir()+'/**/lcov.info')
.pipe(coveralls())
.on('error', function(error) {
// we have an error
console.error(error);
this.emit('end');
});
};
},
metrics: function (options) {
options = options || {};
var generateMetrics = function(cb) {
var glob = require("glob");
// options is optional
glob("reports/*xunit.xml", {}, function (er, files) {
async.map(files, function (file, mapCallback) {
var parser = new xml2js.Parser();
log("Processing file", file, "...");
parser.parseString(fs.readFileSync(file), function (err, result) {
var results = [];
var processTestSuite = function(testSuite) {
results.push({
tests: testSuite.$.tests,
failures: testSuite.$.failures,
errors: testSuite.$.errors,
skipped: testSuite.$.skipped
});
};
if(result.testsuites && result.testsuites.testsuite) {
result.testsuites.testsuite.forEach(processTestSuite);
}
else if (result.testsuite){
processTestSuite(result.testsuite);
}
mapCallback(err, results);
});
},
function (err, results) {
results = _.flatten(results);
if(err) {cb(err); }
else {
async.reduce(results, {tests: 0, failures: 0, errors: 0, skipped: 0}, function (memo, item, callback){
callback(null, {
tests: memo.tests + (parseInt(item.tests) || 0),
failures: memo.failures + (parseInt(item.failures) || 0),
errors: memo.errors + (parseInt(item.errors) || 0),
skipped: memo.skipped + (parseInt(item.skipped) || 0)
});
},
function (err, result){
if(result) {
log("Aggregated metrics result:", result);
fs.writeFileSync("reports/metrics.json", JSON.stringify(result)); }
//save as Java .properties file to be picked up by Jenkins EnvInject Plugin
fs.writeFileSync("reports/metrics.json.properties",
require("properties").stringify({"CI_METRICS": JSON.stringify(result)}));
cb(err, result);
});
}
});
});
};
var id = uuid.v1();
gulp.task(id + ":ensureReportDirectory", factory.gulpTaskFactory.ensureReportDirectory());
gulp.task(id + ":generateMetrics", generateMetrics);
return gulp.series(
id + ":ensureReportDirectory",
id + ":generateMetrics"
);
}
}
};
module.exports = factory;
})(module);