15
15
import * as vscode from "vscode" ;
16
16
import * as lcov from "lcov-parse" ;
17
17
import * as asyncfs from "fs/promises" ;
18
+ import * as path from "path" ;
18
19
import { Writable } from "stream" ;
19
20
import { promisify } from "util" ;
20
21
import configuration from "../configuration" ;
@@ -23,6 +24,7 @@ import { execFileStreamOutput } from "../utilities/utilities";
23
24
import { BuildFlags } from "../toolchain/BuildFlags" ;
24
25
import { TestLibrary } from "../TestExplorer/TestRunner" ;
25
26
import { DisposableFileCollection } from "../utilities/tempFolder" ;
27
+ import { TargetType } from "../SwiftPackage" ;
26
28
27
29
interface CodeCovFile {
28
30
testLibrary : TestLibrary ;
@@ -173,7 +175,7 @@ export class TestCoverage {
173
175
"--format" ,
174
176
"lcov" ,
175
177
...coveredBinaries ,
176
- " --ignore-filename-regex=Tests|swift-testing|Testing|.build|Snippets|Plugins" ,
178
+ ` --ignore-filename-regex=${ this . ignoredFilenamesRegex ( ) } ` ,
177
179
`--instr-profile=${ mergedProfileFile } ` ,
178
180
] ,
179
181
writableStream ,
@@ -189,6 +191,20 @@ export class TestCoverage {
189
191
return buffer ;
190
192
}
191
193
194
+ /**
195
+ * Constructs a string containing all the paths to exclude from the code coverage report.
196
+ * This should exclude everything in the `.build` folder as well as all the test targets.
197
+ */
198
+ private ignoredFilenamesRegex ( ) : string {
199
+ const basePath = this . folderContext . folder . path ;
200
+ const buildFolder = path . join ( basePath , ".build" ) ;
201
+ const testTargets = this . folderContext . swiftPackage
202
+ . getTargets ( TargetType . test )
203
+ . map ( target => path . join ( basePath , target . path ) ) ;
204
+
205
+ return [ buildFolder , ...testTargets ] . join ( "|" ) ;
206
+ }
207
+
192
208
private async loadLcov ( lcovContents : string ) : Promise < lcov . LcovFile [ ] > {
193
209
return promisify ( lcov . source ) ( lcovContents ) . then ( value => value ?? [ ] ) ;
194
210
}
0 commit comments