1515import * as vscode from "vscode" ;
1616import * as lcov from "lcov-parse" ;
1717import * as asyncfs from "fs/promises" ;
18+ import * as path from "path" ;
1819import { Writable } from "stream" ;
1920import { promisify } from "util" ;
2021import configuration from "../configuration" ;
@@ -23,6 +24,7 @@ import { execFileStreamOutput } from "../utilities/utilities";
2324import { BuildFlags } from "../toolchain/BuildFlags" ;
2425import { TestLibrary } from "../TestExplorer/TestRunner" ;
2526import { DisposableFileCollection } from "../utilities/tempFolder" ;
27+ import { TargetType } from "../SwiftPackage" ;
2628
2729interface CodeCovFile {
2830 testLibrary : TestLibrary ;
@@ -173,7 +175,7 @@ export class TestCoverage {
173175 "--format" ,
174176 "lcov" ,
175177 ...coveredBinaries ,
176- " --ignore-filename-regex=Tests|swift-testing|Testing|.build|Snippets|Plugins" ,
178+ ` --ignore-filename-regex=${ this . ignoredFilenamesRegex ( ) } ` ,
177179 `--instr-profile=${ mergedProfileFile } ` ,
178180 ] ,
179181 writableStream ,
@@ -189,6 +191,20 @@ export class TestCoverage {
189191 return buffer ;
190192 }
191193
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+
192208 private async loadLcov ( lcovContents : string ) : Promise < lcov . LcovFile [ ] > {
193209 return promisify ( lcov . source ) ( lcovContents ) . then ( value => value ?? [ ] ) ;
194210 }
0 commit comments