forked from w3c/wot-thing-description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeresults.js
146 lines (138 loc) · 4.91 KB
/
mergeresults.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
/* Merge test results.
* Read from all assertion test results CSV files given on the
* command line, send a merged file to stdout.
*
* For file format and usage, see testing/README.md and testing/results.
*
* Results mentioned in only one input will be output as-is.
* Results for the same assertion mentioned in more
* than one file with different statuses will be combined
* as follows (order of inputs does not matter):
* {pass, fail} -> fail
* {not-impl, fail} -> fail
* {not-impl, pass} -> pass
* In other words, one failure in any test means the entire
* result is considered a failure. Passes also dominate
* not-implemented statuses.
*
* The purpose of this script is to combine results
* for a single implementation (eg from different test runs
* or for different test cases) so each implementation is
* counted only once in the implementation report.
* Each file in testing/results is considered to be
* for a separate implementation so results for such
* multiple internal tests should be combined into a single
* file before generating the implementation report.
*
* Comments in the input will be concatenated in the output.
*/
// Dependencies
const csvtojson=require('csvtojson'); // V2
// Parameters
const debug_v = false;
// Get all results from files, store in an array of JSON objects
// (Asynchronous)
function get_results(files,results,done_callback) {
// handle boundary cases
if (undefined === files || 0 == files.length) {
done_callback(results);
}
// process one file, tail-recurse if more
csvtojson().fromFile(files[0]).then((data) => {
results.push(data);
if (1 == files.length) {
done_callback(results);
} else {
get_results(files.slice(1),results,done_callback);
}
});
}
function merge_results(results,done_callback) {
let merged_results = new Map();
for (let i=0; i<results.length; i++) {
let data = results[i];
for (let j=0; j<data.length; j++) {
let id = data[j]["ID"];
let st = data[j]["Status"];
let cm = data[j]["Comment"];
if (undefined === id) {
console.error(new Error("Missing ID CSV header"));
// Failure
process.exit(1);
}
if (undefined === st) {
console.error(new Error("Missing Status CSV header"));
// Failure
process.exit(1);
}
if (undefined === cm) {
cm = "";
}
let current = merged_results.get(id);
if (undefined === current) {
merged_results.set(id,[st, cm]);
} else {
let current_st = current[0];
let current_cm = current[1];
if ("fail" === st || "fail" === current_st) {
// failure dominates anything else
merged_results.set(id,["fail", get_comment(st,current_st,"fail",cm,current_cm)]);
} else if ("pass" === st || "pass" === current_st) {
// pass dominates not-impl
merged_results.set(id,["pass", get_comment(st,current_st,"pass",cm,current_cm)]);
} else {
// both must be not-impl, but may need to update comments
merged_results.set(id,["not-impl", get_comment(st,current_st,"not-impl",cm,current_cm)]);
}
}
}
}
done_callback(merged_results);
}
function get_comment(
st, // status of just read input
current_st, // current status
value_st, // new status
cm, // comment of just read input
current_cm // current comment
) {
let comment = "";
if ((current_cm) && (value_st === current_st)) {
comment = current_cm;
}
if ((cm) && (value_st === st)) {
if ((comment) && (comment.indexOf(cm) < 0)) {
comment = comment + " + " + cm;
} else {
comment = cm;
}
}
return comment;
}
function output_results(merged_results) {
process.stdout.write('"ID","Status","Comment"\n');
merged_results.forEach((data,id) => {
if ("" === data[1]) {
process.stdout.write('"'+id+'","'+data[0]+'",\n');
} else {
process.stdout.write('"'+id+'","'+data[0]+'","'+data[1]+'"\n');
}
});
}
if (process.argv.length > 2) {
let files = process.argv.slice(2);
get_results(files,[],function(input_results) {
if (debug_v) console.warn("input results:\n",input_results);
merge_results(input_results,function(merged_results) {
if (debug_v) console.warn("merged results:\n",merged_results);
output_results(merged_results);
// Success
process.exit(0);
});
});
} else {
// Usage
console.warn("Usage:",process.argv[0],process.argv[1],"file1.csv file2.csv ...");
console.warn("See testing/README.md and testing/results");
process.exit(1);
}