Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@
"type": "boolean",
"description": "%configuration.java.test.config.coverage.appendResult.description%",
"default": true
},
"excludes": {
"type": "array",
"items": {
"type": "string"
},
"description": "%configuration.java.test.config.coverage.excludes.description%",
"default": []
}
}
}
Expand Down Expand Up @@ -501,6 +509,14 @@
"type": "boolean",
"description": "%configuration.java.test.config.coverage.appendResult.description%",
"default": true
},
"excludes": {
"type": "array",
"items": {
"type": "string"
},
"description": "%configuration.java.test.config.coverage.excludes.description%",
"default": []
}
}
}
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"configuration.java.test.config.javaExec.description": "The path to java executable to use. For example: `C:\\Program Files\\jdk\\bin\\java.exe`. If unset project JDK's java executable is used.",
"configuration.java.test.config.coverage.description": "The configurations for test coverage.",
"configuration.java.test.config.coverage.appendResult.description": "Whether the coverage result is appended.",
"configuration.java.test.config.coverage.excludes.description": "A list of source files that should be excluded from coverage analysis. The can use any valid [minimatch](https://www.npmjs.com/package/minimatch) pattern.",
"contributes.viewsWelcome.inLightWeightMode": "No test cases are listed because the Java Language Server is currently running in [LightWeight Mode](https://aka.ms/vscode-java-lightweight). To show test cases, click on the button to switch to Standard Mode.\n[Switch to Standard Mode](command:java.server.mode.switch?%5B%22Standard%22,true%5D)",
"contributes.viewsWelcome.enableTests": "Click below button to configure a test framework for your project.\n[Enable Java Tests](command:_java.test.enableTests)"
}
4 changes: 3 additions & 1 deletion src/controller/testController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export const runTests: (request: TestRunRequest, option: IRunOption) => any = in
let coverageProvider: JavaTestCoverageProvider | undefined;
if (request.profile?.kind === TestRunProfileKind.Coverage) {
coverageProvider = new JavaTestCoverageProvider();
// QUESTION: Fix this?
// eslint-disable-next-line @typescript-eslint/no-unused-vars
request.profile.loadDetailedCoverage = (_testRun: TestRun, fileCoverage: FileCoverage, _token: CancellationToken): Promise<FileCoverageDetail[]> => {
return Promise.resolve(coverageProvider!.getCoverageDetails(fileCoverage.uri));
};
Expand Down Expand Up @@ -265,7 +267,7 @@ export const runTests: (request: TestRunRequest, option: IRunOption) => any = in
}
}
if (request.profile?.kind === TestRunProfileKind.Coverage) {
await coverageProvider!.provideFileCoverage(run, projectName);
await coverageProvider!.provideFileCoverage(testContext);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/java-test-runner.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ export interface IExecutionConfig {
* @since 0.41.0
*/
appendResult?: boolean;

/**
* A list of source files that should be excluded from coverage analysis.
* The can use any valid [minimatch](https://www.npmjs.com/package/minimatch)
* pattern.
* @since 0.43.2
*/
excludes?: string[];
}

/**
Expand Down
23 changes: 19 additions & 4 deletions src/provider/JavaTestCoverageProvider.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { BranchCoverage, DeclarationCoverage, FileCoverage, FileCoverageDetail, Position, StatementCoverage, TestRun, Uri } from 'vscode';
import * as minimatch from 'minimatch';
import { BranchCoverage, DeclarationCoverage, FileCoverage, FileCoverageDetail, Position, StatementCoverage, Uri } from 'vscode';
import { getJacocoReportBasePath } from '../utils/coverageUtils';
import { executeJavaLanguageServerCommand } from '../utils/commandUtils';
import { JavaTestRunnerDelegateCommands } from '../constants';
import { IRunTestContext } from '../java-test-runner.api';

export class JavaTestCoverageProvider {

private coverageDetails: Map<Uri, FileCoverageDetail[]> = new Map<Uri, FileCoverageDetail[]>();

public async provideFileCoverage(run: TestRun, projectName: string): Promise<void> {
public async provideFileCoverage({testRun: run, projectName, testConfig}: IRunTestContext): Promise<void> {
const sourceFileCoverages: ISourceFileCoverage[] = await executeJavaLanguageServerCommand<void>(JavaTestRunnerDelegateCommands.GET_COVERAGE_DETAIL,
projectName, getJacocoReportBasePath(projectName)) || [];
for (const sourceFileCoverage of sourceFileCoverages) {
const sourceFileCoverageExclusions: minimatch.Minimatch[] = (testConfig?.coverage?.excludes ?? []).map((exclusion: string) =>
new minimatch.Minimatch(exclusion, {flipNegate: true, nonegate: true}));
const sourceFileCoveragesToReport: ISourceFileCoverage[] = [];
if (sourceFileCoverageExclusions.length <= 0) {
sourceFileCoveragesToReport.push(...sourceFileCoverages);
} else {
sourceFileCoverages.forEach((sourceFileCoverage: ISourceFileCoverage) => {
const uri: Uri = Uri.parse(sourceFileCoverage.uriString);
if (!sourceFileCoverageExclusions.some((exclusion: minimatch.Minimatch) =>
exclusion.match(uri.fsPath))) {
sourceFileCoveragesToReport.push(sourceFileCoverage);
}
});
}
for (const sourceFileCoverage of sourceFileCoveragesToReport) {
const uri: Uri = Uri.parse(sourceFileCoverage.uriString);
const detailedCoverage: FileCoverageDetail[] = [];
for (const lineCoverage of sourceFileCoverage.lineCoverages) {
Expand Down