Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/test/browser/extHostCommands.test.ts
5237 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { ExtHostCommands } from '../../common/extHostCommands.js';
8
import { MainThreadCommandsShape } from '../../common/extHost.protocol.js';
9
import { CommandsRegistry } from '../../../../platform/commands/common/commands.js';
10
import { SingleProxyRPCProtocol } from '../common/testRPCProtocol.js';
11
import { mock } from '../../../../base/test/common/mock.js';
12
import { NullLogService } from '../../../../platform/log/common/log.js';
13
import { IExtHostTelemetry } from '../../common/extHostTelemetry.js';
14
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
15
16
suite('ExtHostCommands', function () {
17
ensureNoDisposablesAreLeakedInTestSuite();
18
19
test('dispose calls unregister', function () {
20
21
let lastUnregister: string;
22
23
const shape = new class extends mock<MainThreadCommandsShape>() {
24
override $registerCommand(id: string): void {
25
//
26
}
27
override $unregisterCommand(id: string): void {
28
lastUnregister = id;
29
}
30
};
31
32
const commands = new ExtHostCommands(
33
SingleProxyRPCProtocol(shape),
34
new NullLogService(),
35
new class extends mock<IExtHostTelemetry>() {
36
override onExtensionError(): boolean {
37
return true;
38
}
39
}
40
);
41
commands.registerCommand(true, 'foo', (): any => { }).dispose();
42
assert.strictEqual(lastUnregister!, 'foo');
43
assert.strictEqual(CommandsRegistry.getCommand('foo'), undefined);
44
45
});
46
47
test('dispose bubbles only once', function () {
48
49
let unregisterCounter = 0;
50
51
const shape = new class extends mock<MainThreadCommandsShape>() {
52
override $registerCommand(id: string): void {
53
//
54
}
55
override $unregisterCommand(id: string): void {
56
unregisterCounter += 1;
57
}
58
};
59
60
const commands = new ExtHostCommands(
61
SingleProxyRPCProtocol(shape),
62
new NullLogService(),
63
new class extends mock<IExtHostTelemetry>() {
64
override onExtensionError(): boolean {
65
return true;
66
}
67
}
68
);
69
const reg = commands.registerCommand(true, 'foo', (): any => { });
70
reg.dispose();
71
reg.dispose();
72
reg.dispose();
73
assert.strictEqual(unregisterCounter, 1);
74
});
75
76
test('execute with retry', async function () {
77
78
let count = 0;
79
80
const shape = new class extends mock<MainThreadCommandsShape>() {
81
override $registerCommand(id: string): void {
82
//
83
}
84
override async $executeCommand<T>(id: string, args: any[], retry: boolean): Promise<T | undefined> {
85
count++;
86
assert.strictEqual(retry, count === 1);
87
if (count === 1) {
88
assert.strictEqual(retry, true);
89
throw new Error('$executeCommand:retry');
90
} else {
91
assert.strictEqual(retry, false);
92
// eslint-disable-next-line local/code-no-any-casts
93
return <any>17;
94
}
95
}
96
};
97
98
const commands = new ExtHostCommands(
99
SingleProxyRPCProtocol(shape),
100
new NullLogService(),
101
new class extends mock<IExtHostTelemetry>() {
102
override onExtensionError(): boolean {
103
return true;
104
}
105
}
106
);
107
108
const result: number = await commands.executeCommand('fooo', [this, true]);
109
assert.strictEqual(result, 17);
110
assert.strictEqual(count, 2);
111
});
112
113
test('onCommand:abc activates extensions when executed from command palette, but not when executed programmatically with vscode.commands.executeCommand #150293', async function () {
114
115
const activationEvents: string[] = [];
116
117
const shape = new class extends mock<MainThreadCommandsShape>() {
118
override $registerCommand(id: string): void {
119
//
120
}
121
override $fireCommandActivationEvent(id: string): void {
122
activationEvents.push(id);
123
}
124
};
125
const commands = new ExtHostCommands(
126
SingleProxyRPCProtocol(shape),
127
new NullLogService(),
128
new class extends mock<IExtHostTelemetry>() {
129
override onExtensionError(): boolean {
130
return true;
131
}
132
}
133
);
134
135
commands.registerCommand(true, 'extCmd', (args: any): any => args);
136
137
const result: unknown = await commands.executeCommand('extCmd', this);
138
assert.strictEqual(result, this);
139
assert.deepStrictEqual(activationEvents, ['extCmd']);
140
});
141
});
142
143