Skip to content

Commit

Permalink
fix - Use project's jacoco agent when it's available (#1723)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo committed Aug 6, 2024
1 parent e42565b commit bb1a0b3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/utils/coverageUtils.ts
Original file line number Diff line number Diff line change
@@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/launchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
28 changes: 28 additions & 0 deletions test/suite/coverageUtils.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});

0 comments on commit bb1a0b3

Please sign in to comment.