-
Notifications
You must be signed in to change notification settings - Fork 34
/
parse_reports.ts
405 lines (359 loc) · 12 KB
/
parse_reports.ts
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import {
FunctionDescriptor,
ITestFailureInfo,
ITestReport,
ReportForTest,
} from "..";
import fs from "fs";
import path from "path";
import { SimilarityReport } from "./editDistance";
// https://nodejs.org/api/errors.html#nodejs-error-codes
const FILE_SYS_ERRORS = ["EEXIST", "EISDIR", "ENOENT", "ENOTEMPTY", "EACCES"];
export type SimilarityStats = {
[packageName: string]: {
proj: string;
similarityReport: SimilarityReport;
};
};
export type CoverageStats = {
[packageName: string]: {
proj: string;
nrUniqueSnippets: number;
numTests: number;
numPassing: number;
stmtCoverage: number;
branchCoverage: number;
nonTrivialTests: number;
nonTrivialPassing: number;
nonTrivialCoverage: number | "unknown";
numUniquelyCoveringTests: number | null;
};
};
export type FailureStats = {
[packageName: string]: {
proj: string;
numFailing: number;
numAssertionErrors: number;
numFileSysErrors: number;
numCorrectnessErrors: number;
numTimeoutErrors: number;
numOther: number;
};
};
export type PackageStats = {
[packageName: string]: {
proj: string;
repo: string;
sha: string;
loc: number;
numExistingTests: number;
weeklyDownloads: number;
stmtCoverageFromLoading: number;
branchCoverageFromLoading: number;
nrUniqueSnippets: number;
numFunctions: number;
numFunctionsWithExamples: number;
numFunctionsWithDocComments: number;
};
};
export type RefinersData = {
[refinerName: string]: {
passingTests: number;
coverage: number;
nonTrivialCoverage: number;
};
};
export type RefinerStats = {
refinerNames: Set<string>;
stats: {
[packageName: string]: {
proj: string;
refinersData: RefinersData;
};
};
};
/**
* Categorize types of failures in given tests
* @param data report data, as found in report.json
* @returns an object with the number of occurrences of each type of failure
*/
function getFailedStats(data: ITestReport) {
const failures = data.tests
.filter((test) => test.status === "FAILED")
.map((test) => test.err as ITestFailureInfo);
const numFailing = failures.length;
let numAssertionErrors = 0;
let numFileSysErrors = 0;
//correctness errors include Type errors, Reference errors, done errors, and infinite recursion/call stack errors
let numCorrectnessErrors = 0;
let numTimeoutErrors = 0;
let numOther = 0;
for (const failure of failures) {
if (isAssertionError(failure)) {
numAssertionErrors++;
} else if (isFileSysError(failure)) {
numFileSysErrors++;
} else if (isCorrectnessError(failure) || isSyntaxError(failure)) {
numCorrectnessErrors++;
} else if (isTimedOutTest(failure)) {
numTimeoutErrors++;
} else {
numOther++;
}
}
return {
numFailing,
numAssertionErrors,
numFileSysErrors,
numCorrectnessErrors,
numTimeoutErrors,
numOther,
};
}
function isSyntaxError(err: ITestFailureInfo) {
if (!err.message) return false;
return err.message.includes("Invalid syntax");
}
/**
* Checks if tests fails because of a correctness error (right now: type error, reference error, done error, infinite recursion/call stack error)
* @param err test failure info to check
* @returns true/false
*/
function isCorrectnessError(err: ITestFailureInfo) {
if (!err.stack) return false;
return (
err.stack.includes("ReferenceError") ||
err.stack.includes("TypeError") ||
err.stack.includes("done() invoked with non-Error") ||
err.stack.includes("Maximum call stack size exceeded")
);
}
/**
* Checks if tests fails because of an assertion error
* @param err test failure info to check
* @returns true/false
*/
function isAssertionError(err: ITestFailureInfo) {
if (!err.stack) return false;
return err.stack.includes("AssertionError");
}
/**
* Checks if tests fails because of file system errors, as defined in FILE_SYS_ERRORS
* @param err test failure info to check
* @returns true/false
*/
function isFileSysError(err: ITestFailureInfo) {
if (!err.code) return false;
return FILE_SYS_ERRORS.includes(err.code);
}
/**
* Checks if tests fails because of time outs
* @param err test failure info to check
* @returns true/false
*/
function isTimedOutTest(err: ITestFailureInfo) {
if (!err.code) return false;
return err.code === "ERR_MOCHA_TIMEOUT";
}
/**
* Parse the `report.json`, `stats.json`, and `api.json` files for all projects under the
* given root directory and return five objects summarizing the results:
*
* - `coverageStats`: a mapping from project configuration (i.e., project name
* plus number of snippets) to an object with statistics about the project and
* the statement coverage our tests achieve
* - `failureStats`: a mapping from project configuration to an object with
* statistics on the kinds of test failures we observe
* - `packageStats`: a mapping from project configuration to an object with
* descriptive statistics of the packages
* - `refinerStats`: a mapping from project configuration to an object with
* the coverage data of each refiner
* - `performanceStats`: a mapping from project configuration to an object with
* performance data
* - `similarityStats`: a mapping from project configuration to an object with
* similarity data (Based on edit distance)
*/
export function parseReports(
root: string,
calculateUniquelyCoveringTests = false
) {
const coverageStats: CoverageStats = {};
const failureStats: FailureStats = {};
const packageStats: PackageStats = {};
const refinersStats: RefinerStats = { refinerNames: new Set(), stats: {} };
const performanceStats: any = {};
const similarityStats: any = {};
for (const proj of fs.readdirSync(root)) {
if (proj === ".DS_Store") {
continue;
}
const projDir = path.join(root, proj);
const reportFile = path.join(projDir, "report.json");
const data = JSON.parse(fs.readFileSync(reportFile, "utf8")) as ITestReport;
var packageName = data.metaData.packageName;
//special handling of gitlab-js
if (packageName !== undefined && packageName.includes("/")) {
const parts = packageName.split("/");
packageName = parts[1];
}
const numTests = data.stats?.nrTests ?? 0;
const numPassing = data.stats?.nrPasses ?? 0;
const nrUniqueSnippets = data.nrUniqueSnippets ?? 0;
const stmtCoverage = data.coverage?.total.statements?.pct ?? 0;
const branchCoverage = data.coverage?.total.branches?.pct ?? 0;
const nonTrivialTests = data.stats?.nrNonTrivialTests ?? 0;
const nonTrivialPassing = data.stats?.nrNonTrivialPasses ?? 0;
const nonTrivialCoverage =
data.coverage?.total.statements?.nonTrivialPct ?? 0;
const apiExplorationTime = data.stats?.apiExplorationTime ?? -1;
const docCommentExtractionTime = data.stats?.docCommentExtractionTime ?? -1;
const snippetExtractionTime = data.stats?.snippetExtractionTime ?? -1;
const codexQueryTime = data.stats?.codexQueryTime ?? -1;
const totalTime = data.stats?.totalTime ?? -1;
var numExistingTests = -1;
let numUniquelyCoveringTests = null;
if (calculateUniquelyCoveringTests) {
numUniquelyCoveringTests = getNumUniquelyCoveringTests(data.tests);
}
coverageStats[packageName] = {
proj,
nrUniqueSnippets,
numTests,
numPassing,
stmtCoverage: stmtCoverage,
branchCoverage: branchCoverage,
nonTrivialTests,
nonTrivialPassing,
nonTrivialCoverage,
numUniquelyCoveringTests,
};
failureStats[packageName] = { proj, ...getFailedStats(data) };
const refinersReport = path.join(projDir, "refiners.json");
if (fs.existsSync(refinersReport)) {
const refinersData = JSON.parse(fs.readFileSync(refinersReport, "utf8"));
refinersStats.stats[packageName] = { proj, refinersData };
for (const refinerName of Object.keys(refinersData)) {
refinersStats.refinerNames.add(refinerName);
}
}
const packageStatsReport = path.join(projDir, "stats.json");
const snippetsReport = path.join(projDir, "snippetMap.json");
const apiReport = path.join(projDir, "api.json");
const apiStats = getAPIStats(snippetsReport, apiReport);
if (fs.existsSync(packageStatsReport)) {
const packageStatsData = JSON.parse(
fs.readFileSync(packageStatsReport, "utf8")
);
const weeklyDownloads = packageStatsData.weeklyDownloads;
const stmtCoverageFromLoading =
packageStatsData.coverageFromLoading.statements?.pct ?? 0;
const branchCoverageFromLoading =
packageStatsData.coverageFromLoading.branches?.pct ?? 0;
const repo = packageStatsData.repository;
const sha = packageStatsData.sha;
const loc = packageStatsData.loc;
packageStats[packageName] = {
proj,
repo,
sha,
loc,
numExistingTests,
weeklyDownloads,
stmtCoverageFromLoading,
branchCoverageFromLoading,
nrUniqueSnippets,
...apiStats,
};
}
performanceStats[packageName] = {
proj,
apiExplorationTime,
docCommentExtractionTime,
snippetExtractionTime,
codexQueryTime,
totalTime,
...apiStats,
};
const similarityStatsReport = path.join(projDir, "similarityReport.json");
if (fs.existsSync(similarityStatsReport)) {
const similarityReport = JSON.parse(
fs.readFileSync(similarityStatsReport, "utf8")
);
similarityStats[packageName] = {
proj,
similarityReport,
};
packageStats[packageName].numExistingTests =
similarityReport.numExistingTests;
}
}
return {
coverageStats,
failureStats,
refinersStats,
packageStats,
performanceStats,
similarityStats,
};
}
/***
* Parse `api.json` and `snippetMap.json` files of a project and return an object containing the following statistics:
* - `numFunctions`: number of functions in the project
* - `numFunctionsWithExamples`: number of functions with at least one example
* - `numFunctionsWithDocComments`: number of functions with doc comments
*/
function getAPIStats(snippetsReport: string, apiReport: string) {
let numFunctions = -1;
let numFunctionsWithExamples = -1;
let numFunctionsWithDocComments = -1;
if (fs.existsSync(apiReport)) {
const apiData = JSON.parse(fs.readFileSync(apiReport, "utf8")) as [
{ descriptor: FunctionDescriptor }
];
//note that it is inaccurate to base the number of functions on snippetsMap as functions with the same name get mapped to the same key,
//leading to an underestimate of the number of functions
numFunctions = apiData.length;
const functionsWithDocComments = apiData.filter(
(f) => f.descriptor.docComment !== undefined
);
numFunctionsWithDocComments = functionsWithDocComments.length;
}
if (fs.existsSync(snippetsReport)) {
const snippetsData = JSON.parse(
fs.readFileSync(snippetsReport, "utf8")
) as [string, string[]][];
numFunctionsWithExamples = snippetsData
.map((entry) => entry[1])
.filter((entry) => entry.length > 0).length;
}
return {
numFunctions,
numFunctionsWithExamples,
numFunctionsWithDocComments,
};
}
/**
* Finds number of tests that cover at least one statement no other test covers
* @param tests object containing all tests
* @returns number of tests that cover at least one statement no other test covers
*/
function getNumUniquelyCoveringTests(tests: ReportForTest[]) {
const stmtCovMap = new Map(); // map from statement to list of tests covering that statement
for (const test of tests) {
for (const coveredStmt of test.coveredStatements ?? []) {
if (!stmtCovMap.has(coveredStmt)) {
stmtCovMap.set(coveredStmt, []);
}
stmtCovMap.get(coveredStmt).push(test.testName);
}
}
let numUniquelyCoveredStmts = 0;
const uniquelyCoveringTests = new Set();
for (const coveringTests of stmtCovMap.values()) {
if (coveringTests.length == 1) {
numUniquelyCoveredStmts++;
uniquelyCoveringTests.add(coveringTests[0]);
}
}
return uniquelyCoveringTests.size;
}