mainThreadTesting.ts 4.4 KB
Newer Older
C
Connor Peet 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { CancellationToken } from 'vs/base/common/cancellation';
7
import { Disposable, IDisposable, MutableDisposable } from 'vs/base/common/lifecycle';
8 9
import { URI, UriComponents } from 'vs/base/common/uri';
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
C
Connor Peet 已提交
10
import { getTestSubscriptionKey, RunTestsRequest, RunTestsResult, TestDiffOpType, TestsDiff } from 'vs/workbench/contrib/testing/common/testCollection';
11
import { ITestResultService } from 'vs/workbench/contrib/testing/common/testResultService';
C
Connor Peet 已提交
12 13 14
import { ITestService } from 'vs/workbench/contrib/testing/common/testService';
import { ExtHostContext, ExtHostTestingResource, ExtHostTestingShape, IExtHostContext, MainContext, MainThreadTestingShape } from '../common/extHost.protocol';

C
Connor Peet 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
const reviveDiff = (diff: TestsDiff) => {
	for (const entry of diff) {
		if (entry[0] === TestDiffOpType.Add || entry[0] === TestDiffOpType.Update) {
			const item = entry[1];
			if (item.item.location) {
				item.item.location.uri = URI.revive(item.item.location.uri);
			}

			for (const message of item.item.state.messages) {
				if (message.location) {
					message.location.uri = URI.revive(message.location.uri);
				}
			}
		}
	}
};

C
Connor Peet 已提交
32 33 34 35 36 37 38 39
@extHostNamedCustomer(MainContext.MainThreadTesting)
export class MainThreadTesting extends Disposable implements MainThreadTestingShape {
	private readonly proxy: ExtHostTestingShape;
	private readonly testSubscriptions = new Map<string, IDisposable>();

	constructor(
		extHostContext: IExtHostContext,
		@ITestService private readonly testService: ITestService,
40
		@ITestResultService resultService: ITestResultService,
C
Connor Peet 已提交
41 42 43 44 45
	) {
		super();
		this.proxy = extHostContext.getProxy(ExtHostContext.ExtHostTesting);
		this._register(this.testService.onShouldSubscribe(args => this.proxy.$subscribeToTests(args.resource, args.uri)));
		this._register(this.testService.onShouldUnsubscribe(args => this.proxy.$unsubscribeFromTests(args.resource, args.uri)));
46 47 48 49 50 51

		const testCompleteListener = this._register(new MutableDisposable());
		this._register(resultService.onNewTestResult(results => {
			testCompleteListener.value = results.onComplete(() => this.proxy.$publishTestResults({ tests: results.tests }));
		}));

52
		testService.updateRootProviderCount(1);
C
Connor Peet 已提交
53

54 55 56 57 58
		const lastCompleted = resultService.results.find(r => !r.isComplete);
		if (lastCompleted) {
			this.proxy.$publishTestResults({ tests: lastCompleted.tests });
		}

C
Connor Peet 已提交
59 60 61
		for (const { resource, uri } of this.testService.subscriptions) {
			this.proxy.$subscribeToTests(resource, uri);
		}
C
Connor Peet 已提交
62 63 64 65 66 67 68
	}

	/**
	 * @inheritdoc
	 */
	public $registerTestProvider(id: string) {
		this.testService.registerTestController(id, {
69
			runTests: (req, token) => this.proxy.$runTestsForProvider(req, token),
C
Connor Peet 已提交
70
			lookupTest: test => this.proxy.$lookupTest(test),
C
Connor Peet 已提交
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
		});
	}

	/**
	 * @inheritdoc
	 */
	public $unregisterTestProvider(id: string) {
		this.testService.unregisterTestController(id);
	}

	/**
	 * @inheritdoc
	 */
	$subscribeToDiffs(resource: ExtHostTestingResource, uriComponents: UriComponents): void {
		const uri = URI.revive(uriComponents);
		const disposable = this.testService.subscribeToDiffs(resource, uri,
			diff => this.proxy.$acceptDiff(resource, uriComponents, diff));
		this.testSubscriptions.set(getTestSubscriptionKey(resource, uri), disposable);
	}

	/**
	 * @inheritdoc
	 */
	public $unsubscribeFromDiffs(resource: ExtHostTestingResource, uriComponents: UriComponents): void {
		const key = getTestSubscriptionKey(resource, URI.revive(uriComponents));
		this.testSubscriptions.get(key)?.dispose();
		this.testSubscriptions.delete(key);
	}

	/**
	 * @inheritdoc
	 */
	public $publishDiff(resource: ExtHostTestingResource, uri: UriComponents, diff: TestsDiff): void {
C
Connor Peet 已提交
104
		reviveDiff(diff);
C
Connor Peet 已提交
105 106 107
		this.testService.publishDiff(resource, URI.revive(uri), diff);
	}

108 109
	public $runTests(req: RunTestsRequest, token: CancellationToken): Promise<RunTestsResult> {
		return this.testService.runTests(req, token);
C
Connor Peet 已提交
110 111 112
	}

	public dispose() {
113 114 115 116 117
		this.testService.updateRootProviderCount(-1);
		for (const subscription of this.testSubscriptions.values()) {
			subscription.dispose();
		}
		this.testSubscriptions.clear();
C
Connor Peet 已提交
118 119
	}
}