-
Notifications
You must be signed in to change notification settings - Fork 29.3k
/
testCoverage.ts
192 lines (163 loc) · 6.12 KB
/
testCoverage.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancellationToken } from 'vs/base/common/cancellation';
import { ResourceMap } from 'vs/base/common/map';
import { deepClone } from 'vs/base/common/objects';
import { ITransaction, observableSignal } from 'vs/base/common/observable';
import { IPrefixTreeNode, WellDefinedPrefixTree } from 'vs/base/common/prefixTree';
import { URI } from 'vs/base/common/uri';
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity';
import { CoverageDetails, ICoveredCount, IFileCoverage } from 'vs/workbench/contrib/testing/common/testTypes';
export interface ICoverageAccessor {
getCoverageDetails: (id: string, token: CancellationToken) => Promise<CoverageDetails[]>;
}
let incId = 0;
/**
* Class that exposese coverage information for a run.
*/
export class TestCoverage {
private readonly fileCoverage = new ResourceMap<FileCoverage>();
public readonly didAddCoverage = observableSignal<IPrefixTreeNode<AbstractFileCoverage>[]>(this);
public readonly tree = new WellDefinedPrefixTree<AbstractFileCoverage>();
public readonly associatedData = new Map<unknown, unknown>();
constructor(
public readonly fromTaskId: string,
private readonly uriIdentityService: IUriIdentityService,
private readonly accessor: ICoverageAccessor,
) { }
public append(rawCoverage: IFileCoverage, tx: ITransaction | undefined) {
const coverage = new FileCoverage(rawCoverage, this.accessor);
const previous = this.getComputedForUri(coverage.uri);
const applyDelta = (kind: 'statement' | 'branch' | 'declaration', node: ComputedFileCoverage) => {
if (!node[kind]) {
if (coverage[kind]) {
node[kind] = { ...coverage[kind]! };
}
} else {
node[kind]!.covered += (coverage[kind]?.covered || 0) - (previous?.[kind]?.covered || 0);
node[kind]!.total += (coverage[kind]?.total || 0) - (previous?.[kind]?.total || 0);
}
};
// We insert using the non-canonical path to normalize for casing differences
// between URIs, but when inserting an intermediate node always use 'a' canonical
// version.
const canonical = [...this.treePathForUri(coverage.uri, /* canonical = */ true)];
const chain: IPrefixTreeNode<AbstractFileCoverage>[] = [];
this.tree.insert(this.treePathForUri(coverage.uri, /* canonical = */ false), coverage, node => {
chain.push(node);
if (chain.length === canonical.length - 1) {
node.value = coverage;
} else if (!node.value) {
// clone because later intersertions can modify the counts:
const intermediate = deepClone(rawCoverage);
intermediate.id = String(incId++);
intermediate.uri = this.treePathToUri(canonical.slice(0, chain.length));
node.value = new ComputedFileCoverage(intermediate);
} else {
applyDelta('statement', node.value);
applyDelta('branch', node.value);
applyDelta('declaration', node.value);
node.value.didChange.trigger(tx);
}
});
this.fileCoverage.set(coverage.uri, coverage);
if (chain) {
this.didAddCoverage.trigger(tx, chain);
}
}
/**
* Gets coverage information for all files.
*/
public getAllFiles() {
return this.fileCoverage;
}
/**
* Gets coverage information for a specific file.
*/
public getUri(uri: URI) {
return this.fileCoverage.get(uri);
}
/**
* Gets computed information for a file, including DFS-computed information
* from child tests.
*/
public getComputedForUri(uri: URI) {
return this.tree.find(this.treePathForUri(uri, /* canonical = */ false));
}
private *treePathForUri(uri: URI, canconicalPath: boolean) {
yield uri.scheme;
yield uri.authority;
const path = !canconicalPath && this.uriIdentityService.extUri.ignorePathCasing(uri) ? uri.path.toLowerCase() : uri.path;
yield* path.split('/');
}
private treePathToUri(path: string[]) {
return URI.from({ scheme: path[0], authority: path[1], path: path.slice(2).join('/') });
}
}
export const getTotalCoveragePercent = (statement: ICoveredCount, branch: ICoveredCount | undefined, function_: ICoveredCount | undefined) => {
let numerator = statement.covered;
let denominator = statement.total;
if (branch) {
numerator += branch.covered;
denominator += branch.total;
}
if (function_) {
numerator += function_.covered;
denominator += function_.total;
}
return denominator === 0 ? 1 : numerator / denominator;
};
export abstract class AbstractFileCoverage {
public readonly id: string;
public readonly uri: URI;
public statement: ICoveredCount;
public branch?: ICoveredCount;
public declaration?: ICoveredCount;
public readonly didChange = observableSignal(this);
/**
* Gets the total coverage percent based on information provided.
* This is based on the Clover total coverage formula
*/
public get tpc() {
return getTotalCoveragePercent(this.statement, this.branch, this.declaration);
}
constructor(coverage: IFileCoverage) {
this.id = coverage.id;
this.uri = coverage.uri;
this.statement = coverage.statement;
this.branch = coverage.branch;
this.declaration = coverage.declaration;
}
}
/**
* File coverage info computed from children in the tree, not provided by the
* extension.
*/
export class ComputedFileCoverage extends AbstractFileCoverage { }
export class FileCoverage extends AbstractFileCoverage {
private _details?: Promise<CoverageDetails[]>;
private resolved?: boolean;
/** Gets whether details are synchronously available */
public get hasSynchronousDetails() {
return this._details instanceof Array || this.resolved;
}
constructor(coverage: IFileCoverage, private readonly accessor: ICoverageAccessor) {
super(coverage);
}
/**
* Gets per-line coverage details.
*/
public async details(token = CancellationToken.None) {
this._details ??= this.accessor.getCoverageDetails(this.id, token);
try {
const d = await this._details;
this.resolved = true;
return d;
} catch (e) {
this._details = undefined;
throw e;
}
}
}