Skip to content

Commit bb1a0b3

Browse files
authored
fix - Use project's jacoco agent when it's available (#1723)
1 parent e42565b commit bb1a0b3

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/utils/coverageUtils.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
import { DebugConfiguration } from 'vscode';
45
import { extensionContext } from '../extension';
56
import * as path from 'path';
67

7-
export function getJacocoAgentPath(): string {
8+
const jacocoAgentRegex: RegExp = /org\.jacoco\.agent-\d+\.\d+\.\d+-runtime\.jar$/;
9+
10+
export function getJacocoAgentPath(debugConfiguration: DebugConfiguration): string {
11+
if (debugConfiguration.classPaths) {
12+
for (const classPath of debugConfiguration.classPaths) {
13+
if (jacocoAgentRegex.test(classPath)) {
14+
return classPath;
15+
}
16+
}
17+
}
18+
19+
if (debugConfiguration.modulePaths) {
20+
for (const modulePath of debugConfiguration.modulePaths) {
21+
if (jacocoAgentRegex.test(modulePath)) {
22+
return modulePath;
23+
}
24+
}
25+
}
26+
827
return extensionContext.asAbsolutePath('server/jacocoagent.jar');
928
}
1029

src/utils/launchUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, te
8383
}
8484

8585
if (testContext.profile?.kind === TestRunProfileKind.Coverage) {
86-
let agentArg: string = `-javaagent:${getJacocoAgentPath()}=destfile=${getJacocoDataFilePath(launchArguments.projectName)}`;
86+
let agentArg: string = `-javaagent:${getJacocoAgentPath(debugConfiguration)}=destfile=${getJacocoDataFilePath(launchArguments.projectName)}`;
8787
if (config?.coverage?.appendResult === false) {
8888
agentArg += ',append=false';
8989
}

test/suite/coverageUtils.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
'use strict';
5+
6+
import * as assert from 'assert';
7+
import * as coverageUtils from '../../src/utils/coverageUtils';
8+
import { DebugConfiguration } from 'vscode';
9+
10+
// tslint:disable: only-arrow-functions
11+
// tslint:disable: no-object-literal-type-assertion
12+
suite('CoverageUtils Tests', () => {
13+
14+
test('Use project jacoco agent if it is available', async () => {
15+
const debugConfiguration: DebugConfiguration = {
16+
name: 'Tests',
17+
type: 'java',
18+
request: 'launch',
19+
classPaths: [
20+
'/foo/bar/org.jacoco.agent-1.2.3-runtime.jar',
21+
]
22+
}
23+
assert.strictEqual(
24+
coverageUtils.getJacocoAgentPath(debugConfiguration),
25+
'/foo/bar/org.jacoco.agent-1.2.3-runtime.jar'
26+
);
27+
});
28+
});

0 commit comments

Comments
 (0)