Skip to content

Commit

Permalink
Cache debug config selection when using run and debug. (#16934)
Browse files Browse the repository at this point in the history
* Cache debug config seletion when using run and debug.

* Add tests, and news item
  • Loading branch information
karthiknadig authored Aug 11, 2021
1 parent 5a3d13a commit 79e72f6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/1 Enhancements/16934.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cache last selection for debug configuration when debugging without launch.json.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'use strict';

import { inject, injectable, named } from 'inversify';
import { cloneDeep } from 'lodash';
import { CancellationToken, DebugConfiguration, QuickPickItem, WorkspaceFolder } from 'vscode';
import { DebugConfigStrings } from '../../../common/utils/localize';
import {
Expand All @@ -18,6 +19,7 @@ import { IDebugConfigurationProviderFactory, IDebugConfigurationResolver } from

@injectable()
export class PythonDebugConfigurationService implements IDebugConfigurationService {
private cacheDebugConfig: DebugConfiguration | undefined = undefined;
constructor(
@inject(IDebugConfigurationResolver)
@named('attach')
Expand Down Expand Up @@ -62,6 +64,9 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
} else if (debugConfiguration.request === 'test') {
throw Error("Please use the command 'Python: Debug All Tests'");
} else {
if (this.cacheDebugConfig) {
debugConfiguration = cloneDeep(this.cacheDebugConfig);
}
if (Object.keys(debugConfiguration).length === 0) {
const configs = await this.provideDebugConfigurations(folder, token);
if (configs === undefined) {
Expand All @@ -70,6 +75,7 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
if (Array.isArray(configs) && configs.length === 1) {
debugConfiguration = configs[0];
}
this.cacheDebugConfig = cloneDeep(debugConfiguration);
}
return this.launchResolver.resolveDebugConfiguration(
folder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,35 @@ suite('Debugging - Configuration Service', () => {

expect(config).to.equal(undefined, `Config should be undefined`);
});
test('Use cached debug configuration', async () => {
const folder = { name: '1', index: 0, uri: Uri.parse('1234') };
const expectedConfig = {
name: 'File',
type: 'python',
request: 'launch',
program: '${file}',
console: 'integratedTerminal',
};
const multiStepInput = {
run: (_: any, state: any) => {
Object.assign(state.config, expectedConfig);
return Promise.resolve();
},
};
multiStepFactory
.setup((f) => f.create())
.returns(() => multiStepInput as any)
.verifiable(typemoq.Times.once()); // this should be called only once.

launchResolver
.setup((a) => a.resolveDebugConfiguration(typemoq.It.isAny(), typemoq.It.isAny(), typemoq.It.isAny()))
.returns(() => Promise.resolve(expectedConfig as any))
.verifiable(typemoq.Times.exactly(2)); // this should be called twice with the same config.

await configService.resolveDebugConfiguration(folder, {} as any);
await configService.resolveDebugConfiguration(folder, {} as any);

multiStepFactory.verifyAll();
launchResolver.verifyAll();
});
});

0 comments on commit 79e72f6

Please sign in to comment.