From 08aaae29f8eabc48512ad46eb5d994992b84d8c9 Mon Sep 17 00:00:00 2001 From: Rodrigo Azevedo Date: Fri, 1 Aug 2025 14:27:38 -0300 Subject: [PATCH 1/2] Config for code coverage --- README.md | 9 +++++++++ package.json | 10 ++++++++++ src/PHPUnit/CommandBuilder/Xdebug.ts | 16 ++++++++++------ src/PHPUnit/Configuration.ts | 10 ++++++---- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1ac5be2b..37598d19 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,15 @@ The following commands are available in VS Code's command palette, use the ID to } ``` +### Coverage + +```jsonc +{ + "phpunit.coveragePath": "${workspaceFolder}/coverage", + "phpunit.coverageFile": "phpunit.xml" +} +``` + ## Troubleshooting ### PHPUnit path mapping not working diff --git a/package.json b/package.json index 103b2dab..490b7ffc 100644 --- a/package.json +++ b/package.json @@ -150,6 +150,16 @@ "type": "integer", "default": 0, "description": "The port that is used to communicate between Xdebug in phpunit and the debug extension. 0 (default) will use a random port." + }, + "phpunit.coveragePath": { + "type": "string", + "default": null, + "description": "Path for code coverage results" + }, + "phpunit.coverageFile": { + "type": "string", + "default": null, + "description": "File name for code coverage results" } } } diff --git a/src/PHPUnit/CommandBuilder/Xdebug.ts b/src/PHPUnit/CommandBuilder/Xdebug.ts index ef973023..60f631b7 100644 --- a/src/PHPUnit/CommandBuilder/Xdebug.ts +++ b/src/PHPUnit/CommandBuilder/Xdebug.ts @@ -24,7 +24,7 @@ export enum Mode { export class Xdebug { mode?: Mode; private port?: number; - private temporaryDirectory?: string; + private directory?: string; private index: number = 0; constructor(private configuration: IConfiguration) { @@ -39,7 +39,9 @@ export class Xdebug { return undefined; } - return join(this.temporaryDirectory!, `phpunit-${this.index}.xml`); + const fileName = this.configuration.get('coverageFile', `phpunit-${this.index}.xml`); + + return join(this.directory!, fileName); } setIndex(index: number) { @@ -48,8 +50,8 @@ export class Xdebug { return this; } - setTemporaryDirectory(temporaryDirectory: string) { - this.temporaryDirectory = temporaryDirectory; + setDirectory(temporaryDirectory: string) { + this.directory = temporaryDirectory; return this; } @@ -64,12 +66,14 @@ export class Xdebug { // export enum TestRunProfileKind { Run = 1, Debug = 2, Coverage = 3 } if (mode === 2) { this.mode = Mode.debug; - this.port = this.configuration.get('xdebugPort', 0) as number || await getFreePort(); + this.port = this.configuration.get('xdebugPort', 0) || await getFreePort(); } if (mode === 3) { this.mode = Mode.coverage; - this.setTemporaryDirectory(await mkdtemp(join(tmpdir(), 'phpunit'))); + const directory = this.configuration.get('coverageFile') || await mkdtemp(join(tmpdir(), 'phpunit')); + + this.setDirectory(directory); } return this; diff --git a/src/PHPUnit/Configuration.ts b/src/PHPUnit/Configuration.ts index d6bb0fc2..0fda840e 100644 --- a/src/PHPUnit/Configuration.ts +++ b/src/PHPUnit/Configuration.ts @@ -6,7 +6,8 @@ interface ConfigurationItem { } export interface IConfiguration { - get(key: string, defaultValue?: unknown): unknown | undefined; + get(key: string): T | undefined; + get(key: string, defaultValue: T): T; has(key: string): any; @@ -18,7 +19,8 @@ export interface IConfiguration { } export abstract class BaseConfiguration implements IConfiguration { - abstract get(key: string, defaultValue?: unknown): unknown | undefined; + abstract get(key: string): T | undefined; + abstract get(key: string, defaultValue: T): T; abstract has(key: string): any; @@ -57,8 +59,8 @@ export class Configuration extends BaseConfiguration { } } - get(key: string, defaultValue?: unknown): unknown | undefined { - return this.has(key) ? this.items.get(key) : defaultValue; + get(key: string, defaultValue?: T): T | undefined { + return this.has(key) ? this.items.get(key) as T : defaultValue; } has(key: string) { From 217896449262fcedffab1c3cc4932f87fbfa265d Mon Sep 17 00:00:00 2001 From: Rodrigo Azevedo Date: Fri, 1 Aug 2025 14:37:05 -0300 Subject: [PATCH 2/2] Update src/PHPUnit/CommandBuilder/Xdebug.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/PHPUnit/CommandBuilder/Xdebug.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPUnit/CommandBuilder/Xdebug.ts b/src/PHPUnit/CommandBuilder/Xdebug.ts index 60f631b7..104863a6 100644 --- a/src/PHPUnit/CommandBuilder/Xdebug.ts +++ b/src/PHPUnit/CommandBuilder/Xdebug.ts @@ -71,7 +71,7 @@ export class Xdebug { if (mode === 3) { this.mode = Mode.coverage; - const directory = this.configuration.get('coverageFile') || await mkdtemp(join(tmpdir(), 'phpunit')); + const directory = this.configuration.get('coveragePath') || await mkdtemp(join(tmpdir(), 'phpunit')); this.setDirectory(directory); }