Skip to content

Commit c002d57

Browse files
authored
Improve specificity of ignored files in coverage reports (#961)
1 parent 7597eda commit c002d57

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/coverage/LcovResults.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import * as vscode from "vscode";
1616
import * as lcov from "lcov-parse";
1717
import * as asyncfs from "fs/promises";
18+
import * as path from "path";
1819
import { Writable } from "stream";
1920
import { promisify } from "util";
2021
import configuration from "../configuration";
@@ -23,6 +24,7 @@ import { execFileStreamOutput } from "../utilities/utilities";
2324
import { BuildFlags } from "../toolchain/BuildFlags";
2425
import { TestLibrary } from "../TestExplorer/TestRunner";
2526
import { DisposableFileCollection } from "../utilities/tempFolder";
27+
import { TargetType } from "../SwiftPackage";
2628

2729
interface 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

Comments
 (0)