Skip to content

Commit 9b503f4

Browse files
committed
Allow passing command line arguments to adapter without shell
Remove shell=true when spawning adapter in tests The shell=true was a workaround so that arguments could be passed to node. But it leads to platform specific handling. Therefore remove the shell=true which also means the workarounds that would have been needed to quote JSON on the command line properly are not needed. Needed to test #227
1 parent 6c554c1 commit 9b503f4

File tree

2 files changed

+82
-26
lines changed

2 files changed

+82
-26
lines changed

src/integration-tests/debugClient.ts

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*********************************************************************
2-
* Copyright (c) 2018 Ericsson and others
2+
* Copyright (c) 2018, 2023 Ericsson and others
33
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,9 @@
1010
import * as cp from 'child_process';
1111
import { DebugClient } from '@vscode/debugadapter-testsupport';
1212
import { DebugProtocol } from '@vscode/debugprotocol';
13+
import * as path from 'path';
14+
import { defaultAdapter } from './utils';
15+
import * as os from 'os';
1316

1417
export type ReverseRequestHandler<
1518
A = any,
@@ -23,11 +26,83 @@ export interface ReverseRequestHandlers {
2326
>;
2427
}
2528

29+
function getAdapterAndArgs(adapter?: string): string[] {
30+
const chosenAdapter = adapter !== undefined ? adapter : defaultAdapter;
31+
const adapterPath: string = path.join(
32+
__dirname,
33+
'../../dist',
34+
chosenAdapter
35+
);
36+
if (process.env.INSPECT_DEBUG_ADAPTER) {
37+
return ['--inspect-brk', adapterPath];
38+
}
39+
return [adapterPath];
40+
}
41+
2642
/**
27-
* Extend the DebugClient to support Reverse Requests:
28-
* https://microsoft.github.io/debug-adapter-protocol/specification#Reverse_Requests_RunInTerminal
43+
* Extend the standard DebugClient to support additional client features
2944
*/
3045
export class CdtDebugClient extends DebugClient {
46+
private _cdt_args: string[];
47+
private _cdt_adapterProcess?: cp.ChildProcess;
48+
constructor(adapter?: string, extraArgs?: string[]) {
49+
// The unused are as such because we do override process launching
50+
super('unused', 'unused', 'gdb');
51+
this._cdt_args = getAdapterAndArgs(adapter);
52+
if (extraArgs) {
53+
this._cdt_args.push(...extraArgs);
54+
}
55+
// These timeouts should match what is in .mocharc.json and .mocharc-windows-ci.json
56+
this.defaultTimeout = os.platform() === 'win32' ? 25000 : 5000;
57+
}
58+
59+
/**
60+
* Start a debug session allowing command line arguments to be supplied
61+
*/
62+
public start(port?: number): Promise<void> {
63+
if (typeof port === 'number') {
64+
return super.start(port);
65+
}
66+
67+
return new Promise<void>((resolve, reject) => {
68+
this._cdt_adapterProcess = cp.spawn('node', this._cdt_args);
69+
this._cdt_adapterProcess.on('error', (err) => {
70+
console.log(err);
71+
reject(err);
72+
});
73+
74+
if (
75+
this._cdt_adapterProcess.stdout === null ||
76+
this._cdt_adapterProcess.stdin === null
77+
) {
78+
reject('Missing stdout/stdin');
79+
return;
80+
}
81+
this.connect(
82+
this._cdt_adapterProcess.stdout,
83+
this._cdt_adapterProcess.stdin
84+
);
85+
resolve();
86+
});
87+
}
88+
89+
public stop(): Promise<void> {
90+
return super
91+
.stop()
92+
.then(() => {
93+
this.killAdapter();
94+
})
95+
.catch(() => {
96+
this.killAdapter();
97+
});
98+
}
99+
100+
private killAdapter() {
101+
if (this._cdt_adapterProcess) {
102+
this._cdt_adapterProcess.kill();
103+
this._cdt_adapterProcess = undefined;
104+
}
105+
}
31106
/**
32107
* Reverse Request Handlers:
33108
*/

src/integration-tests/utils.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*********************************************************************
2-
* Copyright (c) 2018 Ericsson and others
2+
* Copyright (c) 2018, 2023 Ericsson and others
33
*
44
* This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0
@@ -181,30 +181,11 @@ export const testProgramsDir = path.join(
181181
'test-programs'
182182
);
183183

184-
function getAdapterAndArgs(adapter?: string): string {
185-
const chosenAdapter = adapter !== undefined ? adapter : defaultAdapter;
186-
let args: string = path.join(__dirname, '../../dist', chosenAdapter);
187-
if (process.env.INSPECT_DEBUG_ADAPTER) {
188-
args = '--inspect-brk ' + args;
189-
}
190-
return args;
191-
}
192-
193184
export async function standardBeforeEach(
194-
adapter?: string
185+
adapter?: string,
186+
extraArgs?: string[]
195187
): Promise<CdtDebugClient> {
196-
const dc: CdtDebugClient = new CdtDebugClient(
197-
'node',
198-
getAdapterAndArgs(adapter),
199-
'cppdbg',
200-
{
201-
shell: true,
202-
}
203-
);
204-
205-
// These timeouts should match what is in .mocharc.json and .mocharc-windows-ci.json
206-
dc.defaultTimeout = os.platform() === 'win32' ? 25000 : 5000;
207-
188+
const dc: CdtDebugClient = new CdtDebugClient(adapter, extraArgs);
208189
await dc.start(debugServerPort);
209190
await dc.initializeRequest();
210191

0 commit comments

Comments
 (0)