forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreportIssueCommand.unit.test.ts
More file actions
188 lines (169 loc) · 7.83 KB
/
reportIssueCommand.unit.test.ts
File metadata and controls
188 lines (169 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* eslint-disable global-require */
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as sinon from 'sinon';
import * as path from 'path';
import { anything, capture, instance, mock, verify, when } from 'ts-mockito';
import { expect } from 'chai';
import { WorkspaceFolder } from 'vscode-languageserver-protocol';
import * as fs from '../../../../client/common/platform/fs-paths';
import * as Telemetry from '../../../../client/telemetry';
import { LanguageServerType } from '../../../../client/activation/types';
import { CommandManager } from '../../../../client/common/application/commandManager';
import { ReportIssueCommandHandler } from '../../../../client/common/application/commands/reportIssueCommand';
import {
IApplicationEnvironment,
ICommandManager,
IWorkspaceService,
} from '../../../../client/common/application/types';
import { WorkspaceService } from '../../../../client/common/application/workspace';
import { IInterpreterService } from '../../../../client/interpreter/contracts';
import { MockWorkspaceConfiguration } from '../../../mocks/mockWorkspaceConfig';
import { InterpreterService } from '../../../../client/interpreter/interpreterService';
import { Commands, EXTENSION_ROOT_DIR } from '../../../../client/common/constants';
import { AllCommands } from '../../../../client/common/application/commands';
import { ConfigurationService } from '../../../../client/common/configuration/service';
import { IConfigurationService } from '../../../../client/common/types';
import { EventName } from '../../../../client/telemetry/constants';
import { EnvironmentType, PythonEnvironment } from '../../../../client/pythonEnvironments/info';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../../constants';
import * as extensionsApi from '../../../../client/common/vscodeApis/extensionsApi';
suite('Report Issue Command', () => {
let reportIssueCommandHandler: ReportIssueCommandHandler;
let cmdManager: ICommandManager;
let workspaceService: IWorkspaceService;
let interpreterService: IInterpreterService;
let configurationService: IConfigurationService;
let appEnvironment: IApplicationEnvironment;
let expectedIssueBody: string;
let getExtensionsStub: sinon.SinonStub;
setup(async () => {
workspaceService = mock(WorkspaceService);
cmdManager = mock(CommandManager);
interpreterService = mock(InterpreterService);
configurationService = mock(ConfigurationService);
appEnvironment = mock<IApplicationEnvironment>();
getExtensionsStub = sinon.stub(extensionsApi, 'getExtensions');
when(cmdManager.executeCommand('workbench.action.openIssueReporter', anything())).thenResolve();
when(workspaceService.getConfiguration('python')).thenReturn(
new MockWorkspaceConfiguration({
languageServer: LanguageServerType.Node,
}),
);
const interpreter = ({
envType: EnvironmentType.Venv,
version: { raw: '3.9.0' },
} as unknown) as PythonEnvironment;
when(interpreterService.getActiveInterpreter()).thenResolve(interpreter);
when(configurationService.getSettings()).thenReturn({
experiments: {
enabled: false,
optInto: [],
optOutFrom: [],
},
initialize: true,
venvPath: 'path',
pipenvPath: 'pipenv',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
cmdManager = mock(CommandManager);
reportIssueCommandHandler = new ReportIssueCommandHandler(
instance(cmdManager),
instance(workspaceService),
instance(interpreterService),
instance(configurationService),
instance(appEnvironment),
);
await reportIssueCommandHandler.activate();
const issueTemplatePath = path.join(
EXTENSION_ROOT_DIR_FOR_TESTS,
'src',
'test',
'common',
'application',
'commands',
'issueTemplate.md',
);
expectedIssueBody = fs.readFileSync(issueTemplatePath, 'utf8');
getExtensionsStub.returns([
{
id: 'ms-python.python',
packageJSON: {
displayName: 'Python',
version: '2020.2',
name: 'python',
publisher: 'ms-python',
},
},
]);
});
teardown(() => {
sinon.restore();
});
test('Test if issue body is filled correctly when including all the settings', async () => {
await reportIssueCommandHandler.openReportIssue();
const userDataTemplatePath = path.join(
EXTENSION_ROOT_DIR_FOR_TESTS,
'src',
'test',
'common',
'application',
'commands',
'issueUserDataTemplateVenv1.md',
);
const expectedData = fs.readFileSync(userDataTemplatePath, 'utf8');
const args: [string, { extensionId: string; issueBody: string; extensionData: string }] = capture<
AllCommands,
{ extensionId: string; issueBody: string; extensionData: string }
>(cmdManager.executeCommand).last();
verify(cmdManager.registerCommand(Commands.ReportIssue, anything(), anything())).once();
verify(cmdManager.executeCommand('workbench.action.openIssueReporter', anything())).once();
expect(args[0]).to.be.equal('workbench.action.openIssueReporter');
const { issueBody, extensionData } = args[1];
expect(issueBody).to.be.equal(expectedIssueBody);
expect(extensionData).to.be.equal(expectedData);
});
test('Test if issue body is filled when only including settings which are explicitly set', async () => {
// eslint-disable-next-line import/no-dynamic-require
when(appEnvironment.packageJson).thenReturn(require(path.join(EXTENSION_ROOT_DIR, 'package.json')));
when(workspaceService.workspaceFolders).thenReturn([
instance(mock(WorkspaceFolder)),
instance(mock(WorkspaceFolder)),
]); // Multiroot scenario
reportIssueCommandHandler = new ReportIssueCommandHandler(
instance(cmdManager),
instance(workspaceService),
instance(interpreterService),
instance(configurationService),
instance(appEnvironment),
);
await reportIssueCommandHandler.activate();
await reportIssueCommandHandler.openReportIssue();
const userDataTemplatePath = path.join(
EXTENSION_ROOT_DIR_FOR_TESTS,
'src',
'test',
'common',
'application',
'commands',
'issueUserDataTemplateVenv2.md',
);
const expectedData = fs.readFileSync(userDataTemplatePath, 'utf8');
const args: [string, { extensionId: string; issueBody: string; extensionData: string }] = capture<
AllCommands,
{ extensionId: string; issueBody: string; extensionData: string }
>(cmdManager.executeCommand).last();
verify(cmdManager.executeCommand('workbench.action.openIssueReporter', anything())).once();
expect(args[0]).to.be.equal('workbench.action.openIssueReporter');
const { issueBody, extensionData } = args[1];
expect(issueBody).to.be.equal(expectedIssueBody);
expect(extensionData).to.be.equal(expectedData);
});
test('Should send telemetry event when run Report Issue Command', async () => {
const sendTelemetryStub = sinon.stub(Telemetry, 'sendTelemetryEvent');
await reportIssueCommandHandler.openReportIssue();
sinon.assert.calledWith(sendTelemetryStub, EventName.USE_REPORT_ISSUE_COMMAND);
sinon.restore();
});
});