Skip to content

Commit a4077a6

Browse files
committed
Add support for cpputestTestAdapter.preLaunchTask setting
The configured task will be run at the beginning of load(), run(), and debug(). This can be used to rebuild the test executable. Addresses issue bneumann#42
1 parent 866e051 commit a4077a6

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ To let this plugin know where your tests are set the ```cpputestTestAdapter.test
1919
```
2020
They will be executed in the ```cpputestTestAdapter.testExecutablePath``` path.
2121

22+
To arrange for a task to be run prior to running tests or refreshing the test list, set ```cpputestTestAdapter.preLaunchTask``` to the name of a task from tasks.json. This can be used to rebuild the test executable, for example.
23+
2224
If you want to use the debugging functions you will also need to setup a launch.json file with your debugger path and arguments etc. The adapter will take care of the rest. Hopefully.
2325

2426

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@
8383
"type": "string",
8484
"scope": "resource"
8585
},
86+
"cpputestTestAdapter.preLaunchTask": {
87+
"description": "Task to run before running the test executable",
88+
"default": "",
89+
"type": "string",
90+
"scope": "resource"
91+
},
8692
"cpputestTestAdapter.testLocationFetchMode": {
8793
"description": "",
8894
"default": "auto",

src/Infrastructure/SettingsProvider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface SettingsProvider {
1111
GetObjDumpPath(): string;
1212
GetTestRunners(): string[];
1313
GetTestPath(): string;
14+
GetPreLaunchTask(): string;
1415
get TestLocationFetchMode(): TestLocationFetchMode;
1516
GetDebugConfiguration(): (DebugConfiguration | string);
1617
GetWorkspaceFolders(): readonly WorkspaceFolder[] | undefined

src/Infrastructure/VscodeSettingsProvider.ts

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export default class VscodeSettingsProvider implements SettingsProvider {
3333
return this.ResolveSettingsVariable(this.config.testExecutablePath);
3434
}
3535

36+
public GetPreLaunchTask(): string {
37+
return this.config.preLaunchTask;
38+
}
39+
3640
public get TestLocationFetchMode(): TestLocationFetchMode {
3741
switch(this.config.testLocationFetchMode) {
3842
case 'test query':

src/adapter.ts

+35
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,47 @@ export class CppUTestAdapter implements TestAdapter {
100100
}
101101

102102
private async updateTests(): Promise<void> {
103+
const preLaunchTask = await this.getPreLaunchTask();
104+
if (preLaunchTask) {
105+
const errorCode = await this.runTask(preLaunchTask);
106+
if (errorCode) {
107+
this.mainSuite.children = [];
108+
this.testsEmitter.fire(<TestLoadFinishedEvent>{
109+
type: 'finished',
110+
errorMessage: `preLaunchTask "${preLaunchTask.name}" Failed [exit code: ${errorCode}]`,
111+
});
112+
return;
113+
}
114+
}
103115
this.root.ClearTests();
104116
const runners = this.settingsProvider.GetTestRunners().map(runner => new ExecutableRunner(this.processExecuter, runner, this.log, this.GetExecutionOptions()));
105117
const loadedTests = await this.root.LoadTests(runners);
106118
this.mainSuite.children = loadedTests;
107119
this.testsEmitter.fire(<TestLoadFinishedEvent>{ type: 'finished', suite: this.mainSuite });
108120
}
121+
122+
private async getPreLaunchTask(): Promise<vscode.Task | undefined> {
123+
const preLaunchTaskName = this.settingsProvider.GetPreLaunchTask();
124+
if (!preLaunchTaskName)
125+
return undefined;
126+
const tasks = await vscode.tasks.fetchTasks();
127+
return tasks.find((value, ..._) => value.name == preLaunchTaskName);
128+
}
129+
130+
private async runTask(task: vscode.Task): Promise<number> {
131+
const taskProcessEnded: Promise<number> = new Promise((resolve, _) => {
132+
const hook_disposable = vscode.tasks.onDidEndTaskProcess((e) => {
133+
if (e.execution.task !== task)
134+
return;
135+
hook_disposable.dispose();
136+
resolve(e.exitCode);
137+
}, this, this.disposables)
138+
});
139+
140+
await vscode.tasks.executeTask(task);
141+
return await taskProcessEnded;
142+
}
143+
109144
private GetExecutionOptions(): ExecutableRunnerOptions | undefined {
110145
return {
111146
objDumpExecutable: this.settingsProvider.GetObjDumpPath(),

0 commit comments

Comments
 (0)