forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocks.ts
More file actions
136 lines (132 loc) · 4.9 KB
/
mocks.ts
File metadata and controls
136 lines (132 loc) · 4.9 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
import { EventEmitter } from 'events';
import { injectable } from 'inversify';
import { CancellationToken, Disposable, Uri } from 'vscode';
import { Product } from '../../client/common/types';
import { createDeferred, Deferred } from '../../client/common/utils/async';
import { IServiceContainer } from '../../client/ioc/types';
import { CANCELLATION_REASON } from '../../client/testing/common/constants';
import { BaseTestManager } from '../../client/testing/common/managers/baseTestManager';
import {
ITestDebugLauncher,
ITestDiscoveryService,
IUnitTestSocketServer,
LaunchOptions,
TestDiscoveryOptions,
TestProvider,
Tests,
TestsToRun
} from '../../client/testing/common/types';
@injectable()
export class MockDebugLauncher implements ITestDebugLauncher, Disposable {
public get launched(): Promise<boolean> {
return this._launched.promise;
}
public get debuggerPromise(): Deferred<Tests> {
// tslint:disable-next-line:no-non-null-assertion
return this._promise!;
}
public get cancellationToken(): CancellationToken {
if (this._token === undefined) {
throw Error('debugger not launched');
}
return this._token;
}
// tslint:disable-next-line:variable-name
private _launched: Deferred<boolean>;
// tslint:disable-next-line:variable-name
private _promise?: Deferred<Tests>;
// tslint:disable-next-line:variable-name
private _token?: CancellationToken;
constructor() {
this._launched = createDeferred<boolean>();
}
public async getLaunchOptions(_resource?: Uri): Promise<{ port: number; host: string }> {
return { port: 0, host: 'localhost' };
}
public async launchDebugger(options: LaunchOptions): Promise<void> {
this._launched.resolve(true);
// tslint:disable-next-line:no-non-null-assertion
this._token = options.token!;
this._promise = createDeferred<Tests>();
// tslint:disable-next-line:no-non-null-assertion
options.token!.onCancellationRequested(() => {
if (this._promise) {
this._promise.reject('Mock-User Cancelled');
}
});
return (this._promise.promise as {}) as Promise<void>;
}
public dispose() {
this._promise = undefined;
}
}
@injectable()
export class MockTestManagerWithRunningTests extends BaseTestManager {
// tslint:disable-next-line:no-any
public readonly runnerDeferred = createDeferred<Tests>();
public readonly enabled = true;
// tslint:disable-next-line:no-any
public readonly discoveryDeferred = createDeferred<Tests>();
constructor(
testProvider: TestProvider,
product: Product,
workspaceFolder: Uri,
rootDirectory: string,
serviceContainer: IServiceContainer
) {
super(testProvider, product, workspaceFolder, rootDirectory, serviceContainer);
}
protected getDiscoveryOptions(_ignoreCache: boolean) {
// tslint:disable-next-line:no-object-literal-type-assertion
return {} as TestDiscoveryOptions;
}
// tslint:disable-next-line:no-any
protected async runTestImpl(
_tests: Tests,
_testsToRun?: TestsToRun,
_runFailedTests?: boolean,
_debug?: boolean
): Promise<Tests> {
// tslint:disable-next-line:no-non-null-assertion
this.testRunnerCancellationToken!.onCancellationRequested(() => {
this.runnerDeferred.reject(CANCELLATION_REASON);
});
return this.runnerDeferred.promise;
}
protected async discoverTestsImpl(_ignoreCache: boolean, _debug?: boolean): Promise<Tests> {
// tslint:disable-next-line:no-non-null-assertion
this.testDiscoveryCancellationToken!.onCancellationRequested(() => {
this.discoveryDeferred.reject(CANCELLATION_REASON);
});
return this.discoveryDeferred.promise;
}
}
@injectable()
export class MockDiscoveryService implements ITestDiscoveryService {
constructor(private discoverPromise: Promise<Tests>) {}
public async discoverTests(_options: TestDiscoveryOptions): Promise<Tests> {
return this.discoverPromise;
}
}
// tslint:disable-next-line:max-classes-per-file
@injectable()
export class MockUnitTestSocketServer extends EventEmitter implements IUnitTestSocketServer {
private results: {}[] = [];
public reset() {
this.removeAllListeners();
}
public addResults(results: {}[]) {
this.results.push(...results);
}
public async start(options: { port: number; host: string } = { port: 0, host: 'localhost' }): Promise<number> {
this.results.forEach(result => {
this.emit('result', result);
});
this.results = [];
return typeof options.port === 'number' ? options.port! : 0;
}
// tslint:disable-next-line:no-empty
public stop(): void {}
// tslint:disable-next-line:no-empty
public dispose() {}
}