-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtestUtilities.ts
152 lines (126 loc) · 4.47 KB
/
testUtilities.ts
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
/*---------------------------------------------------------------------------------------------
* Alexa Skills Toolkit for Visual Studio Code
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*--------------------------------------------------------------------------------------------*/
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable no-prototype-builtins */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import * as sinon from "sinon";
import * as vscode from "vscode";
import {TelemetryClient} from "../src/runtime/lib/telemetry";
const SECOND = 1000;
export const TIMEOUT = 30 * SECOND;
export class FakeExtensionContext implements vscode.ExtensionContext {
public subscriptions: Array<{dispose(): any}> = [];
public workspaceState: vscode.Memento = new FakeMemento();
public globalState: vscode.Memento & {setKeysForSync(keys: string[]): void} = new FakeGlobalStorage();
public storagePath: string | undefined;
public secrets;
public globalStoragePath = ".";
public logPath = "";
public extensionMode;
public extensionUri;
public environmentVariableCollection;
public extension;
// Flowing properties is required after @types/vscode v1.49.0
public storageUri;
public globalStorageUri;
public logUri;
private _extensionPath = "";
public constructor(preload?: FakeExtensionState) {
if (preload !== undefined) {
this.globalState = new FakeGlobalStorage(preload.globalState);
this.workspaceState = new FakeMemento(preload.workspaceState);
}
}
public get extensionPath(): string {
return this._extensionPath;
}
public set extensionPath(path: string) {
this._extensionPath = path;
}
public asAbsolutePath(relativePath: string): string {
return relativePath;
}
/**
* Creates a fake `ExtContext` for use in tests.
*/
public static getFakeExtContext(): FakeExtensionContext {
const ctx = new FakeExtensionContext();
return ctx;
}
}
class FakeMemento implements vscode.Memento {
public constructor(private readonly _storage: FakeMementoStorage = {}) {}
public get<T>(key: string): T | undefined;
public get<T>(key: string, defaultValue: T): T;
public get(key: any, defaultValue?: any) {
if (this._storage.hasOwnProperty(String(key))) {
return this._storage[key];
}
if (defaultValue) {
return defaultValue;
}
return undefined;
}
public update(key: string, value: any): Thenable<void> {
this._storage[key] = value;
return Promise.resolve();
}
}
class FakeGlobalStorage extends FakeMemento {
public setKeysForSync(keys: string[]): void {
return;
}
}
export interface FakeExtensionState {
globalState?: FakeMementoStorage;
workspaceState?: FakeMementoStorage;
}
export interface FakeMementoStorage {
[key: string]: any;
}
export class FakeWebviewPanelOnDidChangeViewStateEvent implements vscode.WebviewPanelOnDidChangeViewStateEvent {
public webviewPanel: FakeWebviewPanel = FakeWebviewPanel.getFakeWebViewPanel();
public static getFakeWebviewPanelOnDidChangeViewStateEvent(): FakeWebviewPanelOnDidChangeViewStateEvent {
const fakeEvent = new FakeWebviewPanelOnDidChangeViewStateEvent();
return fakeEvent;
}
}
export class FakeWebviewPanel implements vscode.WebviewPanel {
public viewType = "";
public title = "";
public iconPath?: vscode.Uri | {light: vscode.Uri; dark: vscode.Uri};
public webview = FakeWebView.getFakeWebView();
public options: vscode.WebviewPanelOptions = {};
public viewColumn?: vscode.ViewColumn;
public active;
public visible;
public onDidChangeViewState;
onDidDispose: any;
reveal(viewColumn?: vscode.ViewColumn, preserveFocus?: boolean): void {}
dispose(): any {}
public static getFakeWebViewPanel(): FakeWebviewPanel {
const fakeWebViewPanel = new FakeWebviewPanel();
return fakeWebViewPanel;
}
}
export class FakeWebView implements vscode.Webview {
public options;
public html = "";
public onDidReceiveMessage;
public postMessage;
public asWebviewUri;
public cspSource = "";
public static getFakeWebView(): FakeWebView {
const fakeWebView = new FakeWebView();
return fakeWebView;
}
}
export function stubTelemetryClient(sandBox: sinon.SinonSandbox) {
sandBox.stub(TelemetryClient.prototype, "store");
sandBox.stub(TelemetryClient.prototype, "startAction");
}