Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to get a test's WORKING_DIRECTORY in launch.json #3336

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.16
Features:
- Support different debug config for different targets. [PR #2801](https://github.com/microsoft/vscode-cmake-tools/pull/2801) [@RichardLuo0](https://github.com/RichardLuo0)
- Add ability to get a test's `WORKING_DIRECTORY` in launch.json via `cmake.testWorkingDirectory` [PR #3336](https://github.com/microsoft/vscode-cmake-tools/pull/3336)

## 1.15
Features:
Expand Down
6 changes: 3 additions & 3 deletions docs/debug-launch.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ You can also construct launch.json configurations that allow you to debug tests
> **Note:**
> These launch.json configurations are to be used specifically from the UI of the Test Explorer.

The easiest way to do this is to construct the debug configuration using `cmake.testProgram` for the `program` field, and `cmake.testArgs` for
the `args` field.
The easiest way to do this is to construct the debug configuration using `cmake.testProgram` for the `program` field, `cmake.testArgs` for
the `args` field, and `cmake.testWorkingDirectory` for the `cwd` field.

A couple of examples:

Expand All @@ -168,9 +168,9 @@ A couple of examples:
{
"name": "(ctest) Launch",
"type": "cppdbg",
"cwd": "${workspaceFolder}",
"request": "launch",
// Resolved by CMake Tools:
"cwd": "${cmake.testWorkingDirectory}",
"program": "${cmake.testProgram}",
"args": [ "${cmake.testArgs}"],
}
Expand Down
13 changes: 13 additions & 0 deletions src/ctest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ export class CTestDriver implements vscode.Disposable {
// Commands can't be used to replace array (i.e., args); and both test program and test args requires folder and
// test name as parameters, which means one lauch config for each test. So replacing them here is a better way.
chosenConfig.config = this.replaceAllInObject<vscode.DebugConfiguration>(chosenConfig.config, '${cmake.testProgram}', this.testProgram(testName));
chosenConfig.config = this.replaceAllInObject<vscode.DebugConfiguration>(chosenConfig.config, '${cmake.testWorkingDirectory}', this.testWorkingDirectory(testName));

// Replace cmake.testArgs wrapped in quotes, like `"${command:cmake.testArgs}"`, without any spaces in between,
// since we need to repalce the quotes as well.
chosenConfig.config = this.replaceArrayItems(chosenConfig.config, '${cmake.testArgs}', this.testArgs(testName)) as vscode.DebugConfiguration;
Expand Down Expand Up @@ -855,6 +857,17 @@ export class CTestDriver implements vscode.Disposable {
return '';
}

private testWorkingDirectory(testName: string): string {
const property = this.tests?.tests
.find(test => test.name === testName)?.properties
.find(prop => prop.name === 'WORKING_DIRECTORY');

if (typeof(property?.value) === 'string') {
return property.value;
}
return '';
}

private testArgs(testName: string): string[] {
if (this.tests) {
for (const test of this.tests.tests) {
Expand Down