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
3296 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
return <any>17;
93
}
94
}
95
};
96
97
const commands = new ExtHostCommands(
98
SingleProxyRPCProtocol(shape),
99
new NullLogService(),
100
new class extends mock<IExtHostTelemetry>() {
101
override onExtensionError(): boolean {
102
return true;
103
}
104
}
105
);
106
107
const result: number = await commands.executeCommand('fooo', [this, true]);
108
assert.strictEqual(result, 17);
109
assert.strictEqual(count, 2);
110
});
111
112
test('onCommand:abc activates extensions when executed from command palette, but not when executed programmatically with vscode.commands.executeCommand #150293', async function () {
113
114
const activationEvents: string[] = [];
115
116
const shape = new class extends mock<MainThreadCommandsShape>() {
117
override $registerCommand(id: string): void {
118
//
119
}
120
override $fireCommandActivationEvent(id: string): void {
121
activationEvents.push(id);
122
}
123
};
124
const commands = new ExtHostCommands(
125
SingleProxyRPCProtocol(shape),
126
new NullLogService(),
127
new class extends mock<IExtHostTelemetry>() {
128
override onExtensionError(): boolean {
129
return true;
130
}
131
}
132
);
133
134
commands.registerCommand(true, 'extCmd', (args: any): any => args);
135
136
const result: unknown = await commands.executeCommand('extCmd', this);
137
assert.strictEqual(result, this);
138
assert.deepStrictEqual(activationEvents, ['extCmd']);
139
});
140
});
141
142