Skip to content

Improve specificity of ignored files in coverage reports #961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/coverage/LcovResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import * as vscode from "vscode";
import * as lcov from "lcov-parse";
import * as asyncfs from "fs/promises";
import * as path from "path";
import { Writable } from "stream";
import { promisify } from "util";
import configuration from "../configuration";
Expand All @@ -23,6 +24,7 @@ import { execFileStreamOutput } from "../utilities/utilities";
import { BuildFlags } from "../toolchain/BuildFlags";
import { TestLibrary } from "../TestExplorer/TestRunner";
import { DisposableFileCollection } from "../utilities/tempFolder";
import { TargetType } from "../SwiftPackage";

interface CodeCovFile {
testLibrary: TestLibrary;
Expand Down Expand Up @@ -173,7 +175,7 @@ export class TestCoverage {
"--format",
"lcov",
...coveredBinaries,
"--ignore-filename-regex=Tests|swift-testing|Testing|.build|Snippets|Plugins",
`--ignore-filename-regex=${this.ignoredFilenamesRegex()}`,
`--instr-profile=${mergedProfileFile}`,
],
writableStream,
Expand All @@ -189,6 +191,20 @@ export class TestCoverage {
return buffer;
}

/**
* Constructs a string containing all the paths to exclude from the code coverage report.
* This should exclude everything in the `.build` folder as well as all the test targets.
*/
private ignoredFilenamesRegex(): string {
const basePath = this.folderContext.folder.path;
const buildFolder = path.join(basePath, ".build");
const testTargets = this.folderContext.swiftPackage
.getTargets(TargetType.test)
.map(target => path.join(basePath, target.path));

return [buildFolder, ...testTargets].join("|");
}

private async loadLcov(lcovContents: string): Promise<lcov.LcovFile[]> {
return promisify(lcov.source)(lcovContents).then(value => value ?? []);
}
Expand Down