Skip to content

Commit

Permalink
Register the coverage test profile
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo committed Jan 17, 2024
1 parent 048bc83 commit f917c1e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import com.microsoft.java.test.plugin.util.JUnitPlugin;
import com.microsoft.java.test.plugin.util.ProjectTestUtils;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.jacoco.core.analysis.Analyzer;
Expand Down Expand Up @@ -192,10 +194,36 @@ private List<MethodCoverage> getMethodCoverages(final Collection<IClassCoverage>
methodCoverages.add(new MethodCoverage(
methodCoverage.getFirstLine(),
methodCoverage.getMethodCounter().getCoveredCount() > 0 ? 1 : 0,
methodCoverage.getName()
getMethodName(methodCoverage)
));
}
}
return methodCoverages;
}

private String getMethodName(IMethodCoverage methodCoverage) {
final String methodName = methodCoverage.getName();
if ("<clinit>".equals(methodName) || "<init>".equals(methodName)) {
return methodName;
}
final String signature = methodCoverage.getDesc();
if (StringUtils.isBlank(signature)) {
return methodName;
}

try {
final String[] parameterTypes = Signature.getParameterTypes(signature);
final List<String> parameterNames = new LinkedList<>();
if (parameterTypes.length > 0) {
for (final String parameterType : parameterTypes) {
final String simpleName = Signature.getSignatureSimpleName(parameterType.replace("/", "."));
parameterNames.add(simpleName);
}
}
return String.format("%s(%s)", methodName, String.join(", ", parameterNames));
} catch (IllegalArgumentException e) {
return methodName;
}

}
}
6 changes: 3 additions & 3 deletions src/controller/testController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function createTestController(): void {
testController.createRunProfile('Run Tests', TestRunProfileKind.Run, runHandler, true, runnableTag);
testController.createRunProfile('Debug Tests', TestRunProfileKind.Debug, runHandler, true, runnableTag);
if (env.appName.includes('Insiders')) {
testController.createRunProfile('Run Tests with Coverage', TestRunProfileKind.Run, runHandler, false, runnableTag);
testController.createRunProfile('Run Tests with Coverage', TestRunProfileKind.Coverage, runHandler, true, runnableTag);
}

testController.refreshHandler = () => {
Expand Down Expand Up @@ -241,15 +241,15 @@ export const runTests: (request: TestRunRequest, option: IRunOption) => any = in
await runner.tearDown();
}
}
if (request.profile?.label.includes('Coverage')) {
if (request.profile?.kind === TestRunProfileKind.Coverage) {
coverageProvider.addProject(projectName);
}
}
}
return resolve();
});
} finally {
if (request.profile?.label.includes('Coverage')) {
if (request.profile?.kind === TestRunProfileKind.Coverage) {
run.coverageProvider = coverageProvider;
}
run.end();
Expand Down
4 changes: 2 additions & 2 deletions src/utils/launchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import * as path from 'path';
import * as os from 'os';
import { DebugConfiguration, TestItem } from 'vscode';
import { DebugConfiguration, TestItem, TestRunProfileKind } from 'vscode';
import { sendError, sendInfo } from 'vscode-extension-telemetry-wrapper';
import { JavaTestRunnerDelegateCommands } from '../constants';
import { dataCache } from '../controller/testItemDataCache';
Expand Down Expand Up @@ -82,7 +82,7 @@ export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, te
};
}

if (testContext.profile?.label.includes('Coverage')) {
if (testContext.profile?.kind === TestRunProfileKind.Coverage) {
let agentArg: string = `-javaagent:${getJacocoAgentPath()}=destfile=${getJacocoDataFilePath(launchArguments.projectName)}`;
if (config?.coverage?.appendResult === false) {
agentArg += ',append=false';
Expand Down

0 comments on commit f917c1e

Please sign in to comment.