forked from Schibsted-Tech-Polska/stp.pediff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverage.js
79 lines (58 loc) · 1.97 KB
/
coverage.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
/*
* Iterate through all routes defined in configuration, and check whether they are tested,
* based on the paths in tasks options.
*/
require('./coverage/route');
require('./coverage/routeVariant');
require('./coverage/test');
// Good to know that PhantomJS, so Casper too,
// uses own fs module, not Node fs
var fs = require('fs'),
utils = require('utils'),
pediffConfig = require('./config'),
coverageConfig = pediffConfig.coverage;
if(coverageConfig !== false) {
//collect routes
var routesJSON = JSON.parse(fs.read(coverageConfig.routes).replace(/\n|\r/g, ""));
var routes = routesJSON.map(function(route) {
return new Route(route.path.replace(/\(\/:/g,'/(:'), route.name);
});
//collect tasks
var taskFiles = fs.list('tasks'),
tasks = [],
tasksOptions = pediffConfig.options;
tasks = taskFiles.map(function(filename) {
var task, config = {}, mediaKeys;
try {
task = require('./tasks/'+filename)
} catch (e) {
return void 0
};
utils.mergeObjects(config, tasksOptions);
utils.mergeObjects(config, task.config);
mediaKeys = Object.keys(config.media)
.filter(function(key) {
return config.media[key];
});
media = config.viewportSize
.map(function(size) {
return size.width + 'x' + size.height;
})
.concat(mediaKeys);
var path = (typeof(config.path) !== 'undefined') ? config.path : '';
// filename
return new Test(path.replace('#!/',''), media, filename);
})
// null/undefined filter
.filter(function(x) { return x });
// test routes
routes.forEach(function(route) {
route.test(tasks);
});
} else {
routes = false;
}
var report = JSON.parse(fs.read('report.json').replace(/\n|\r/g, ""));
report.coverage = routes;
fs.write('report.json', JSON.stringify(report), 'w');
phantom.exit();