Skip to content

Commit

Permalink
Check config type in the ChildProcessAttachEvents (#21272)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulacamargo25 authored May 24, 2023
1 parent 4b4e5b7 commit f2f5fe2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { swallowExceptions } from '../../../common/utils/decorators';
import { AttachRequestArguments } from '../../types';
import { DebuggerEvents } from './constants';
import { IChildProcessAttachService, IDebugSessionEventHandlers } from './types';
import { DebuggerTypeName } from '../../constants';

/**
* This class is responsible for automatically attaching the debugger to any
Expand All @@ -25,7 +26,7 @@ export class ChildProcessAttachEventHandler implements IDebugSessionEventHandler

@swallowExceptions('Handle child process launch')
public async handleCustomEvent(event: DebugSessionCustomEvent): Promise<void> {
if (!event) {
if (!event || event.session.configuration.type !== DebuggerTypeName) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ChildProcessAttachEventHandler } from '../../../../client/debugger/exte
import { ChildProcessAttachService } from '../../../../client/debugger/extension/hooks/childProcessAttachService';
import { DebuggerEvents } from '../../../../client/debugger/extension/hooks/constants';
import { AttachRequestArguments } from '../../../../client/debugger/types';
import { DebuggerTypeName } from '../../../../client/debugger/constants';

suite('Debug - Child Process', () => {
test('Do not attach if the event is undefined', async () => {
Expand All @@ -21,23 +22,31 @@ suite('Debug - Child Process', () => {
const attachService = mock(ChildProcessAttachService);
const handler = new ChildProcessAttachEventHandler(instance(attachService));
const body: any = {};
const session: any = {};
const session: any = { configuration: { type: DebuggerTypeName } };
await handler.handleCustomEvent({ event: 'abc', body, session });
verify(attachService.attach(body, session)).never();
});
test('Do not attach to child process if debugger type is different', async () => {
const attachService = mock(ChildProcessAttachService);
const handler = new ChildProcessAttachEventHandler(instance(attachService));
const body: any = {};
const session: any = { configuration: { type: 'other-type' } };
await handler.handleCustomEvent({ event: 'abc', body, session });
verify(attachService.attach(body, session)).never();
});
test('Do not attach to child process if ptvsd_attach event is invalid', async () => {
const attachService = mock(ChildProcessAttachService);
const handler = new ChildProcessAttachEventHandler(instance(attachService));
const body: any = {};
const session: any = {};
const session: any = { configuration: { type: DebuggerTypeName } };
await handler.handleCustomEvent({ event: DebuggerEvents.PtvsdAttachToSubprocess, body, session });
verify(attachService.attach(body, session)).never();
});
test('Do not attach to child process if debugpy_attach event is invalid', async () => {
const attachService = mock(ChildProcessAttachService);
const handler = new ChildProcessAttachEventHandler(instance(attachService));
const body: any = {};
const session: any = {};
const session: any = { configuration: { type: DebuggerTypeName } };
await handler.handleCustomEvent({ event: DebuggerEvents.DebugpyAttachToSubprocess, body, session });
verify(attachService.attach(body, session)).never();
});
Expand All @@ -51,9 +60,11 @@ suite('Debug - Child Process', () => {
port: 1234,
subProcessId: 2,
};
const session: any = {};
const session: any = {
configuration: { type: DebuggerTypeName },
};
when(attachService.attach(body, session)).thenThrow(new Error('Kaboom'));
await handler.handleCustomEvent({ event: DebuggerEvents.DebugpyAttachToSubprocess, body, session: {} as any });
await handler.handleCustomEvent({ event: DebuggerEvents.DebugpyAttachToSubprocess, body, session });
verify(attachService.attach(body, anything())).once();
const [, secondArg] = capture(attachService.attach).last();
expect(secondArg).to.deep.equal(session);
Expand Down

0 comments on commit f2f5fe2

Please sign in to comment.