Skip to content

Commit ebf0c20

Browse files
committed
add test cases
1 parent 59df3ad commit ebf0c20

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { anything, instance, mock, verify, when } from 'ts-mockito';
7+
import { expect } from 'chai';
8+
import * as vscode from 'vscode';
9+
import { CopyImportPathCommand } from '../../../client/application/importPath/copyImportPathCommand';
10+
import { ICommandManager, IWorkspaceService } from '../../../client/common/application/types';
11+
import * as pythonUtils from '../../../client/common/utils/pythonUtils';
12+
13+
suite('Copy Import Path Command', () => {
14+
let command: CopyImportPathCommand;
15+
let commandManager: ICommandManager;
16+
let workspaceService: IWorkspaceService;
17+
let originalGetSysPath: () => string[];
18+
19+
let clipboardText = '';
20+
21+
setup(() => {
22+
commandManager = mock<ICommandManager>();
23+
workspaceService = mock<IWorkspaceService>();
24+
command = new CopyImportPathCommand(instance(commandManager), instance(workspaceService));
25+
originalGetSysPath = pythonUtils.getSysPath;
26+
27+
clipboardText = '';
28+
29+
(vscode.env.clipboard as typeof vscode.env.clipboard).writeText = async (text: string) => {
30+
clipboardText = text;
31+
};
32+
});
33+
34+
teardown(() => {
35+
((pythonUtils as unknown) as { getSysPath: () => string[] }).getSysPath = originalGetSysPath;
36+
});
37+
38+
test('Confirm command handler is added', async () => {
39+
await command.activate();
40+
verify(commandManager.registerCommand('python.copyImportPath', anything(), anything())).once();
41+
});
42+
43+
test('execute() – sys.path match takes precedence', async () => {
44+
const absPath = '/home/user/project/src/pkg/module.py';
45+
const uri = vscode.Uri.file(absPath);
46+
((pythonUtils as unknown) as { getSysPath: () => string[] }).getSysPath = originalGetSysPath;
47+
// ((pythonUtils as unknown) as { getSysPath: () => string[] }).getSysPath = () => ['/home/user/project/src'];
48+
49+
when(workspaceService.getWorkspaceFolder(anything())).thenReturn(undefined);
50+
((vscode.window as unknown) as { activeTextEditor: { document: { uri: vscode.Uri } } }).activeTextEditor = {
51+
document: { uri },
52+
};
53+
54+
await ((command as unknown) as { execute(u: vscode.Uri): Promise<void> }).execute(uri);
55+
expect(clipboardText).to.equal('pkg.module');
56+
});
57+
58+
test('execute() – workspaceFolder used when no sys.path match', async () => {
59+
const absPath = '/home/user/project/tools/util.py';
60+
const uri = vscode.Uri.file(absPath);
61+
((pythonUtils as unknown) as { getSysPath: () => string[] }).getSysPath = () => [];
62+
63+
const wsFolder = {
64+
uri: vscode.Uri.file('/home/user/project'),
65+
name: 'project',
66+
index: 0,
67+
} as vscode.WorkspaceFolder;
68+
when(workspaceService.getWorkspaceFolder(anything())).thenReturn(wsFolder);
69+
70+
((vscode.window as unknown) as { activeTextEditor: { document: { uri: vscode.Uri } } }).activeTextEditor = {
71+
document: { uri },
72+
};
73+
await ((command as unknown) as { execute(u: vscode.Uri): Promise<void> }).execute(uri);
74+
expect(clipboardText).to.equal('tools.util');
75+
});
76+
77+
test('execute() – falls back to filename when no matches', async () => {
78+
const absPath = '/tmp/standalone.py';
79+
const uri = vscode.Uri.file(absPath);
80+
((pythonUtils as unknown) as { getSysPath: () => string[] }).getSysPath = () => [];
81+
when(workspaceService.getWorkspaceFolder(anything())).thenReturn(undefined);
82+
83+
((vscode.window as unknown) as { activeTextEditor: { document: { uri: vscode.Uri } } }).activeTextEditor = {
84+
document: { uri },
85+
};
86+
await ((command as unknown) as { execute(u: vscode.Uri): Promise<void> }).execute(uri);
87+
expect(clipboardText).to.equal('standalone');
88+
});
89+
});

0 commit comments

Comments
 (0)