Skip to content

Commit df68fb6

Browse files
Fix test in all resolvers
1 parent 50c6282 commit df68fb6

File tree

11 files changed

+137
-146
lines changed

11 files changed

+137
-146
lines changed

src/client/debugger/extension/configuration/resolvers/attach.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,14 @@
33

44
'use strict';
55

6-
import { inject, injectable } from 'inversify';
6+
import { injectable } from 'inversify';
77
import { CancellationToken, Uri, WorkspaceFolder } from 'vscode';
8-
import { IDocumentManager, IWorkspaceService } from '../../../../common/application/types';
9-
import { IPlatformService } from '../../../../common/platform/types';
10-
import { IConfigurationService } from '../../../../common/types';
11-
import { IInterpreterService } from '../../../../interpreter/contracts';
128
import { AttachRequestArguments, DebugOptions, PathMapping } from '../../../types';
9+
import { getOSType, OSType } from '../utils/platform';
1310
import { BaseConfigurationResolver } from './base';
1411

1512
@injectable()
1613
export class AttachConfigurationResolver extends BaseConfigurationResolver<AttachRequestArguments> {
17-
constructor(
18-
@inject(IWorkspaceService) workspaceService: IWorkspaceService,
19-
@inject(IDocumentManager) documentManager: IDocumentManager,
20-
@inject(IPlatformService) platformService: IPlatformService,
21-
@inject(IConfigurationService) configurationService: IConfigurationService,
22-
@inject(IInterpreterService) interpreterService: IInterpreterService,
23-
) {
24-
super(workspaceService, documentManager, platformService, configurationService, interpreterService);
25-
}
26-
2714
public async resolveDebugConfigurationWithSubstitutedVariables(
2815
folder: WorkspaceFolder | undefined,
2916
debugConfiguration: AttachRequestArguments,
@@ -87,10 +74,10 @@ export class AttachConfigurationResolver extends BaseConfigurationResolver<Attac
8774
// We'll need paths to be fixed only in the case where local and remote hosts are the same
8875
// I.e. only if hostName === 'localhost' or '127.0.0.1' or ''
8976
const isLocalHost = this.isLocalHost(debugConfiguration.host);
90-
if (this.platformService.isWindows && isLocalHost) {
77+
if (getOSType() == OSType.Windows && isLocalHost) {
9178
this.debugOption(debugOptions, DebugOptions.FixFilePathCase);
9279
}
93-
if (this.platformService.isWindows) {
80+
if (getOSType() == OSType.Windows) {
9481
this.debugOption(debugOptions, DebugOptions.WindowsClient);
9582
} else {
9683
this.debugOption(debugOptions, DebugOptions.UnixClient);

src/client/debugger/extension/configuration/resolvers/base.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import { injectable } from 'inversify';
77
import * as path from 'path';
8-
import * as vscode from 'vscode';
9-
import { CancellationToken, DebugConfiguration, Uri, WorkspaceFolder, window } from 'vscode';
8+
import { CancellationToken, DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
109
import { PYTHON_LANGUAGE } from '../../../../common/constants';
1110
import { IConfigurationService } from '../../../../common/types';
1211
import { SystemVariables } from '../../../../common/variables/systemVariables';
@@ -17,8 +16,8 @@ import { DebuggerTelemetry } from '../../../../telemetry/types';
1716
import { AttachRequestArguments, DebugOptions, LaunchRequestArguments, PathMapping } from '../../../types';
1817
import { PythonPathSource } from '../../types';
1918
import { IDebugConfigurationResolver } from '../types';
20-
import { resolveVariables } from '../utils/common';
21-
import { getWorkspaceFolder as getVSCodeWorkspaceFolder } from '../utils/workspaceFolder';
19+
import { getActiveTextEditor, resolveVariables } from '../utils/common';
20+
import { getWorkspaceFolder as getVSCodeWorkspaceFolder, getWorkspaceFolders } from '../utils/workspaceFolder';
2221

2322
@injectable()
2423
export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
@@ -57,12 +56,13 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
5756
return folder.uri;
5857
}
5958
const program = this.getProgram();
59+
let workspaceFolders = getWorkspaceFolders();
6060

61-
if (!Array.isArray(vscode.workspace.workspaceFolders) || vscode.workspace.workspaceFolders.length === 0) {
61+
if (!Array.isArray(workspaceFolders) || workspaceFolders.length === 0) {
6262
return program ? Uri.file(path.dirname(program)) : undefined;
6363
}
64-
if (vscode.workspace.workspaceFolders.length === 1) {
65-
return vscode.workspace.workspaceFolders[0].uri;
64+
if (workspaceFolders.length === 1) {
65+
return workspaceFolders[0].uri;
6666
}
6767
if (program) {
6868
const workspaceFolder = getVSCodeWorkspaceFolder(Uri.file(program));
@@ -73,7 +73,7 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
7373
}
7474

7575
protected getProgram(): string | undefined {
76-
const { activeTextEditor } = window;
76+
const activeTextEditor = getActiveTextEditor();
7777
if (activeTextEditor && activeTextEditor.document.languageId === PYTHON_LANGUAGE) {
7878
return activeTextEditor.document.fileName;
7979
}

src/client/debugger/extension/configuration/resolvers/launch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { IConfigurationService } from '../../../../common/types';
1111
import { IInterpreterService } from '../../../../interpreter/contracts';
1212
import { DebuggerTypeName } from '../../../constants';
1313
import { DebugOptions, DebugPurpose, LaunchRequestArguments } from '../../../types';
14+
import { getOSType, OSType } from '../utils/platform';
1415
import { BaseConfigurationResolver } from './base';
1516
import { IDebugEnvironmentVariablesService } from './helper';
1617

@@ -148,7 +149,7 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
148149
if (debugConfiguration.subProcess === true) {
149150
this.debugOption(debugOptions, DebugOptions.SubProcess);
150151
}
151-
if (/^win/.test(process.platform)) {
152+
if (getOSType() == OSType.Windows) {
152153
this.debugOption(debugOptions, DebugOptions.FixFilePathCase);
153154
}
154155
const isFastAPI = this.isDebuggingFastAPI(debugConfiguration);

src/client/debugger/extension/configuration/utils/common.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
'use strict';
88

9-
import { WorkspaceFolder } from 'vscode';
9+
import { WorkspaceFolder, window, TextEditor } from 'vscode';
1010
import { getWorkspaceFolder } from './workspaceFolder';
1111

1212
/**
@@ -41,3 +41,8 @@ export function resolveVariables(
4141
}
4242
return value;
4343
}
44+
45+
export function getActiveTextEditor(): TextEditor | undefined {
46+
const { activeTextEditor } = window;
47+
return activeTextEditor;
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export enum OSType {
2+
Unknown = 'Unknown',
3+
Windows = 'Windows',
4+
OSX = 'OSX',
5+
Linux = 'Linux',
6+
}
7+
8+
export function getOSType(platform: string = process.platform): OSType {
9+
if (/^win/.test(platform)) {
10+
return OSType.Windows;
11+
}
12+
if (/^darwin/.test(platform)) {
13+
return OSType.OSX;
14+
}
15+
if (/^linux/.test(platform)) {
16+
return OSType.Linux;
17+
}
18+
return OSType.Unknown;
19+
}

src/client/debugger/extension/configuration/utils/workspaceFolder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ import * as vscode from 'vscode';
1111
export function getWorkspaceFolder(uri: vscode.Uri): vscode.WorkspaceFolder | undefined {
1212
return vscode.workspace.getWorkspaceFolder(uri);
1313
}
14+
15+
export function getWorkspaceFolders(): readonly vscode.WorkspaceFolder[] | undefined {
16+
return vscode.workspace.workspaceFolders;
17+
}

src/test/debugger/extension/configuration/resolvers/attach.unit.test.ts

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,26 @@
55

66
import { expect } from 'chai';
77
import * as TypeMoq from 'typemoq';
8+
import * as sinon from 'sinon';
89
import { DebugConfiguration, DebugConfigurationProvider, TextDocument, TextEditor, Uri, WorkspaceFolder } from 'vscode';
9-
import { IDocumentManager, IWorkspaceService } from '../../../../../client/common/application/types';
1010
import { PYTHON_LANGUAGE } from '../../../../../client/common/constants';
11-
import { IFileSystem, IPlatformService } from '../../../../../client/common/platform/types';
1211
import { IConfigurationService } from '../../../../../client/common/types';
13-
import { OSType } from '../../../../../client/common/utils/platform';
1412
import { AttachConfigurationResolver } from '../../../../../client/debugger/extension/configuration/resolvers/attach';
1513
import { AttachRequestArguments, DebugOptions } from '../../../../../client/debugger/types';
1614
import { IInterpreterService } from '../../../../../client/interpreter/contracts';
17-
import { IServiceContainer } from '../../../../../client/ioc/types';
18-
import { getOSType } from '../../../../common';
19-
import { getInfoPerOS, setUpOSMocks } from './common';
15+
import { getInfoPerOS } from './common';
16+
import * as common from '../../../../../client/debugger/extension/configuration/utils/common';
17+
import * as workspaceFolder from '../../../../../client/debugger/extension/configuration/utils/workspaceFolder';
18+
import * as platform from '../../../../../client/debugger/extension/configuration/utils/platform';
2019

2120
getInfoPerOS().forEach(([osName, osType, path]) => {
22-
if (osType === OSType.Unknown) {
21+
if (osType === platform.OSType.Unknown) {
2322
return;
2423
}
2524

2625
function getAvailableOptions(): string[] {
2726
const options = [DebugOptions.RedirectOutput];
28-
if (osType === OSType.Windows) {
27+
if (osType === platform.OSType.Windows) {
2928
options.push(DebugOptions.FixFilePathCase);
3029
options.push(DebugOptions.WindowsClient);
3130
} else {
@@ -36,36 +35,26 @@ getInfoPerOS().forEach(([osName, osType, path]) => {
3635
}
3736

3837
suite(`Debugging - Config Resolver attach, OS = ${osName}`, () => {
39-
let serviceContainer: TypeMoq.IMock<IServiceContainer>;
4038
let debugProvider: DebugConfigurationProvider;
41-
let platformService: TypeMoq.IMock<IPlatformService>;
42-
let fileSystem: TypeMoq.IMock<IFileSystem>;
43-
let documentManager: TypeMoq.IMock<IDocumentManager>;
4439
let configurationService: TypeMoq.IMock<IConfigurationService>;
45-
let workspaceService: TypeMoq.IMock<IWorkspaceService>;
4640
let interpreterService: TypeMoq.IMock<IInterpreterService>;
41+
let getActiveTextEditorStub: sinon.SinonStub;
42+
let getWorkspaceFoldersStub: sinon.SinonStub;
43+
let getOSTypeStub: sinon.SinonStub;
4744
const debugOptionsAvailable = getAvailableOptions();
4845

4946
setup(() => {
50-
serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
51-
platformService = TypeMoq.Mock.ofType<IPlatformService>();
52-
workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
5347
configurationService = TypeMoq.Mock.ofType<IConfigurationService>();
5448
interpreterService = TypeMoq.Mock.ofType<IInterpreterService>();
55-
fileSystem = TypeMoq.Mock.ofType<IFileSystem>();
56-
serviceContainer
57-
.setup((c) => c.get(TypeMoq.It.isValue(IPlatformService)))
58-
.returns(() => platformService.object);
59-
serviceContainer.setup((c) => c.get(TypeMoq.It.isValue(IFileSystem))).returns(() => fileSystem.object);
60-
setUpOSMocks(osType, platformService);
61-
documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
62-
debugProvider = new AttachConfigurationResolver(
63-
workspaceService.object,
64-
documentManager.object,
65-
platformService.object,
66-
configurationService.object,
67-
interpreterService.object,
68-
);
49+
debugProvider = new AttachConfigurationResolver(configurationService.object, interpreterService.object);
50+
getActiveTextEditorStub = sinon.stub(common, 'getActiveTextEditor');
51+
getOSTypeStub = sinon.stub(platform, 'getOSType');
52+
getWorkspaceFoldersStub = sinon.stub(workspaceFolder, 'getWorkspaceFolders');
53+
getOSTypeStub.returns(osType);
54+
});
55+
56+
teardown(() => {
57+
sinon.restore();
6958
});
7059

7160
function createMoqWorkspaceFolder(folderPath: string) {
@@ -81,21 +70,15 @@ getInfoPerOS().forEach(([osName, osType, path]) => {
8170
document.setup((d) => d.languageId).returns(() => languageId);
8271
document.setup((d) => d.fileName).returns(() => fileName);
8372
textEditor.setup((t) => t.document).returns(() => document.object);
84-
documentManager.setup((d) => d.activeTextEditor).returns(() => textEditor.object);
73+
getActiveTextEditorStub.returns(textEditor.object);
8574
} else {
86-
documentManager.setup((d) => d.activeTextEditor).returns(() => undefined);
75+
getActiveTextEditorStub.returns(undefined);
8776
}
88-
serviceContainer
89-
.setup((c) => c.get(TypeMoq.It.isValue(IDocumentManager)))
90-
.returns(() => documentManager.object);
9177
}
9278

9379
function setupWorkspaces(folders: string[]) {
9480
const workspaceFolders = folders.map(createMoqWorkspaceFolder);
95-
workspaceService.setup((w) => w.workspaceFolders).returns(() => workspaceFolders);
96-
serviceContainer
97-
.setup((c) => c.get(TypeMoq.It.isValue(IWorkspaceService)))
98-
.returns(() => workspaceService.object);
81+
getWorkspaceFoldersStub.returns(workspaceFolders);
9982
}
10083

10184
const attach: Partial<AttachRequestArguments> = {
@@ -269,7 +252,7 @@ getInfoPerOS().forEach(([osName, osType, path]) => {
269252
});
270253

271254
test(`Ensure drive letter is lower cased for local path mappings on Windows when host is '${host}'`, async function () {
272-
if (getOSType() !== OSType.Windows || osType !== OSType.Windows) {
255+
if (platform.getOSType() !== platform.OSType.Windows || osType !== platform.OSType.Windows) {
273256
return this.skip();
274257
}
275258
const activeFile = 'xyz.py';
@@ -292,7 +275,7 @@ getInfoPerOS().forEach(([osName, osType, path]) => {
292275
});
293276

294277
test(`Ensure drive letter is not lower cased for local path mappings on non-Windows when host is '${host}'`, async function () {
295-
if (getOSType() === OSType.Windows || osType === OSType.Windows) {
278+
if (platform.getOSType() === platform.OSType.Windows || osType === platform.OSType.Windows) {
296279
return this.skip();
297280
}
298281
const activeFile = 'xyz.py';
@@ -315,7 +298,7 @@ getInfoPerOS().forEach(([osName, osType, path]) => {
315298
});
316299

317300
test(`Ensure drive letter is lower cased for local path mappings on Windows when host is '${host}' and with existing path mappings`, async function () {
318-
if (getOSType() !== OSType.Windows || osType !== OSType.Windows) {
301+
if (platform.getOSType() !== platform.OSType.Windows || osType !== platform.OSType.Windows) {
319302
return this.skip();
320303
}
321304
const activeFile = 'xyz.py';
@@ -342,7 +325,7 @@ getInfoPerOS().forEach(([osName, osType, path]) => {
342325
});
343326

344327
test(`Ensure drive letter is not lower cased for local path mappings on non-Windows when host is '${host}' and with existing path mappings`, async function () {
345-
if (getOSType() === OSType.Windows || osType === OSType.Windows) {
328+
if (platform.getOSType() === platform.OSType.Windows || osType === platform.OSType.Windows) {
346329
return this.skip();
347330
}
348331
const activeFile = 'xyz.py';

0 commit comments

Comments
 (0)