diff --git a/src/utils/coverageUtils.ts b/src/utils/coverageUtils.ts index 579ba9d6..c12a34d1 100644 --- a/src/utils/coverageUtils.ts +++ b/src/utils/coverageUtils.ts @@ -1,10 +1,29 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +import { DebugConfiguration } from 'vscode'; import { extensionContext } from '../extension'; import * as path from 'path'; -export function getJacocoAgentPath(): string { +const jacocoAgentRegex: RegExp = /org\.jacoco\.agent-\d+\.\d+\.\d+-runtime\.jar$/; + +export function getJacocoAgentPath(debugConfiguration: DebugConfiguration): string { + if (debugConfiguration.classPaths) { + for (const classPath of debugConfiguration.classPaths) { + if (jacocoAgentRegex.test(classPath)) { + return classPath; + } + } + } + + if (debugConfiguration.modulePaths) { + for (const modulePath of debugConfiguration.modulePaths) { + if (jacocoAgentRegex.test(modulePath)) { + return modulePath; + } + } + } + return extensionContext.asAbsolutePath('server/jacocoagent.jar'); } diff --git a/src/utils/launchUtils.ts b/src/utils/launchUtils.ts index a98dfc80..b59c49b8 100644 --- a/src/utils/launchUtils.ts +++ b/src/utils/launchUtils.ts @@ -83,7 +83,7 @@ export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, te } if (testContext.profile?.kind === TestRunProfileKind.Coverage) { - let agentArg: string = `-javaagent:${getJacocoAgentPath()}=destfile=${getJacocoDataFilePath(launchArguments.projectName)}`; + let agentArg: string = `-javaagent:${getJacocoAgentPath(debugConfiguration)}=destfile=${getJacocoDataFilePath(launchArguments.projectName)}`; if (config?.coverage?.appendResult === false) { agentArg += ',append=false'; } diff --git a/test/suite/coverageUtils.test.ts b/test/suite/coverageUtils.test.ts new file mode 100644 index 00000000..95501f7e --- /dev/null +++ b/test/suite/coverageUtils.test.ts @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +'use strict'; + +import * as assert from 'assert'; +import * as coverageUtils from '../../src/utils/coverageUtils'; +import { DebugConfiguration } from 'vscode'; + +// tslint:disable: only-arrow-functions +// tslint:disable: no-object-literal-type-assertion +suite('CoverageUtils Tests', () => { + + test('Use project jacoco agent if it is available', async () => { + const debugConfiguration: DebugConfiguration = { + name: 'Tests', + type: 'java', + request: 'launch', + classPaths: [ + '/foo/bar/org.jacoco.agent-1.2.3-runtime.jar', + ] + } + assert.strictEqual( + coverageUtils.getJacocoAgentPath(debugConfiguration), + '/foo/bar/org.jacoco.agent-1.2.3-runtime.jar' + ); + }); +});