This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoverageTest.js
270 lines (214 loc) · 6.98 KB
/
CoverageTest.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
const vscode = require(`vscode`);
const tmp = require(`tmp`);
const path = require(`path`);
const fs = require(`fs`);
const unzipper = require(`unzipper`);
const util = require(`util`);
let parseString = util.promisify(require(`xml2js`).parseString);
const tmpFile = util.promisify(tmp.file);
const tmpDir = util.promisify(tmp.dir);
const readFileAsync = util.promisify(fs.readFile);
const {instance} = vscode.extensions.getExtension(`halcyontechltd.code-for-ibmi`).exports;
module.exports = class CoverageTest {
/**
* Create a coverage test
* @param {{runCommand: string, program: string, bindingDirectory?: string}} data
*/
constructor(data) {
this.command = data.runCommand;
this.program = data.program;
this.bindingDirectory = data.bindingDirectory;
}
/**
* Returns a list of service programs from a binding directory
* @param {string} path Path to binding directory
* @returns {string[]}
*/
static async getServicePrograms(path) {
const content = instance.getContent();
const config = instance.getConfig();
const tempLib = config.tempLibrary;
let TempName = `C` + new Date().getTime().toString();
if (TempName.length > 10) TempName = TempName.substr(0, 10);
const bnddir = await vscode.commands.executeCommand(`code-for-ibmi.runCommand`, {
command: `DSPBNDDIR BNDDIR(${path}) OUTPUT(*OUTFILE) OUTFILE(${tempLib}/${TempName})`,
environment: `ile`
});
if (bnddir.code === 0 || bnddir.code === null) {
const results = await content.getTable(tempLib, TempName, TempName);
if (results.length === 1) {
if (results[0].BNOLNM.trim() === ``) {
return []
}
}
return results
.filter(result => result.BNOBTP === `*SRVPGM`)
.map(result => `(${result.BNOLNM}/${result.BNOBNM} ${result.BNOBTP} *ALL)`);
} else {
return [];
}
}
async runConverage() {
const outputZip = `/tmp/${new Date().getTime()}.cczip`;
/** @type {ListItem[]} */
let items = [];
let moduleList = [`(${this.program} *PGM *ALL)`], promises = [];
if (this.bindingDirectory) {
if (this.bindingDirectory.trim().length > 0) {
for (const path of this.bindingDirectory.split(`,`)) {
promises.push(CoverageTest.getServicePrograms(path));
}
const lists = await Promise.all(promises);
lists.forEach(list => {
moduleList.push(...list);
});
}
}
const execution = await vscode.commands.executeCommand(`code-for-ibmi.runCommand`, {
command: `CODECOV CMD(${this.command}) MODULE(${moduleList.join(` `)}) OUTSTMF('${outputZip}')`,
environment: `ile`
});
if (execution.code === 0 || execution.code === null) {
items = await this.getCoverage(outputZip);
return items;
} else {
vscode.window.showErrorMessage(execution.stderr, `Show Output`)
.then(async (item) => {
if (item === `Show Output`) {
vscode.commands.executeCommand(`code-for-ibmi.showOutputPanel`);
}
});
}
return items;
}
async getCoverage(outputZip) {
const content = instance.getConnection();
const client = content.client;
const tmpobj = await tmpFile({});
const tmpdir = await tmpDir({});
let items = [];
await client.getFile(tmpobj, outputZip);
console.log(`Zip downloaded`);
await fs
.createReadStream(tmpobj)
.pipe(unzipper.Extract({ path: tmpdir }))
.promise();
console.log(`Zip extracted`);
const xml = await readFileAsync(path.join(tmpdir, `ccdata`));
console.log(`CCdata read`);
const coverageResult = await parseString(xml);
console.log(`XML parser`);
let data, testCase, sourceCode, activeLines, indexesExecuted;
let lineKeys, percentRan, countRan;
for (const source of coverageResult.LLC.lineLevelCoverageClass) {
console.log(source.testcase);
data = source[`$`];
if (source.testcase === undefined) {
//Indicates that no lines were ran
testCase = { hits: `` };
} else {
testCase = source.testcase[0][`$`];
}
sourceCode = (
await readFileAsync(
path.join(tmpdir, `src`, data.baseFileName),
`utf8`,
)
).split(`\n`);
const realHits = testCase.v2fileHits || testCase.hits;
const realLines = data.v2fileLines || data.lines;
const realSigs = data.v2qualifiedSignatures || data.signatures;
indexesExecuted = this.getRunLines(sourceCode.length, realHits);
activeLines = this.getLines(realLines, indexesExecuted);
lineKeys = Object.keys(activeLines);
countRan = 0;
lineKeys.forEach(key => {
if (activeLines[key] === true) countRan++;
})
percentRan = ((countRan / lineKeys.length) * 100).toFixed(0);
items.push({
basename: path.basename(data.sourceFile),
path: data.sourceFile,
localPath: path.join(tmpdir, `src`, data.baseFileName),
coverage: {
signitures: realSigs.split(`+`),
lineString: realLines,
activeLines,
percentRan
},
});
}
return items;
}
getLines(string, indexesExecuted) {
let lineNumbers = [];
let line = 0;
let currentValue = ``;
let concat = false;
for (const char of string) {
switch (char) {
case `#`:
if (currentValue !== ``) {
line = Number(currentValue);
lineNumbers.push(line);
}
concat = true;
line = 0;
currentValue = ``;
break;
case `,`:
if (currentValue !== ``) {
line = Number(currentValue);
lineNumbers.push(line);
}
currentValue = ``;
break;
case `+`:
line = Number(currentValue);
lineNumbers.push(line);
concat = false;
break;
default:
if (concat) currentValue += char;
else {
currentValue = ``
line += Number(char);
lineNumbers.push(line);
}
break;
}
}
let lines = {};
for (const i in lineNumbers) {
lines[lineNumbers[i]] = indexesExecuted.includes(Number(i));
}
return lines;
}
getRunLines(numLines, hits) {
let hitLines = [];
let hitChar;
for (let i = 0, lineIndex = 0; lineIndex < numLines && i < hits.length; i++) {
hitChar = hits.charCodeAt(i);
if (hitChar <= 80) {
hitChar -= 65;
if (hitChar === 0) {
lineIndex += 4;
} else {
if ((hitChar & 8) !== 0)
hitLines.push(lineIndex);
lineIndex++;
if ((hitChar & 4) !== 0 && lineIndex < numLines)
hitLines.push(lineIndex);
lineIndex++;
if ((hitChar & 2) !== 0 && lineIndex < numLines)
hitLines.push(lineIndex);
lineIndex++;
if ((hitChar & 1) !== 0 && lineIndex < numLines)
hitLines.push(lineIndex);
lineIndex++;
}
}
}
return hitLines;
}
}