-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreport.js
127 lines (106 loc) · 3.55 KB
/
report.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
/*
* Iterate through all screenshots made for the first environment, extract specific data
* from file names and save them as report.json
*/
var fs = require('fs'),
utils = require('utils'),
config = require('config'),
i, j, k,
report = {
cacheBuster: ('' + Math.random()).replace('0.', ''),
environments: config.environments,
tasks: {}
};
//load tasks configurations and map them into the report
var tasks = fs.list('tasks');
for (i = 0; i < tasks.length; i++) {
var filename = tasks[i];
//filter out "up one directory" links and dist file from the files list
if (filename.indexOf('.js') === -1 || filename.indexOf('.js') !== filename.length - 3) {
continue;
}
//load task configuration and use it to extend the global config
var taskConfig = require('tasks/' + filename.replace('.js', '')).config;
var taskData = utils.mergeObjects({},config);
utils.mergeObjects(taskData,taskConfig);
taskData.actions = taskData.actions || [];
var task = {
file: filename,
actions: {},
variants: []
};
//create task variants for all viewport sizes for each defined action
for (j = 0; j < taskData.options.viewportSize.length; j++) {
task.variants.push({
viewportSize: taskData.options.viewportSize[j].width+'x'+taskData.options.viewportSize[j].height,
media: null,
diff: -1,
path: taskData.path || ''
})
}
for(k = 0; k < taskData.actions.length; k++) {
var action = {
file: filename,
variants: []
}
for (j = 0; j < taskData.options.viewportSize.length; j++) {
action.variants.push({
viewportSize: taskData.options.viewportSize[j].width+'x'+taskData.options.viewportSize[j].height,
media: null,
diff: -1,
path: taskData.path || ''
})
}
task.actions[taskData.actions[k]] = action;
}
//create task variants for all defined media
if(taskData.media) {
for (j = 0; j < taskData.media.length; j++) {
task.variants.push({
viewportSize: null,
media: taskData.media[j],
action: null,
diff: -1,
path: taskData.path || ''
})
}
}
report.tasks[taskData.package] = task;
}
//browse through analysed images and update tasks in report with difference value
var files = fs.list('candidate/hq/');
for (i = 0; i < files.length; i++) {
var filename = String(files[i]);
var matches = /(\d+)_(\d+x\d+)(_(.[a-z]+))?_(.*)\./.exec(filename);
if (!utils.isArray(matches)) {
continue;
}
var name = matches[5].replace(/@.[^_]*/,''),
action = /@(.[^_]*)/.exec(matches[5]),
viewport = matches[2],
media = matches[4],
diff = matches[1];
if(action) {
action = action[1];
}
if (typeof(media) == 'undefined' || media == 'null') {
media = null;
}
if(action) {
var variants = report.tasks[name].actions[action].variants;
} else {
variants = report.tasks[name].variants;
}
for(j=0; j<variants.length;j++) {
if(variants[j].viewportSize === viewport && variants[j].media === media) {
variants[j].diff = diff;
break;
}
}
}
if(config.output.junit) {
var junit = require('junit');
fs.write('junit.xml', junit.generate(report), 'w');
}
fs.write('report.json', JSON.stringify(report), 'w');
phantom.exit();