forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandHandlers.ts
More file actions
96 lines (90 loc) · 4.15 KB
/
commandHandlers.ts
File metadata and controls
96 lines (90 loc) · 4.15 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { ICommandManager } from '../../common/application/types';
import { Commands, CommandSource } from '../../common/constants';
import { traceDecorators } from '../../common/logger';
import { IDisposable } from '../../common/types';
import { swallowExceptions } from '../../common/utils/decorators';
import { getTestDataItemType } from '../common/testUtils';
import { TestFile, TestFolder, TestFunction, TestsToRun, TestSuite } from '../common/types';
import { ITestDataItemResource, TestDataItem, TestDataItemType } from '../common/types';
import { ITestExplorerCommandHandler } from '../navigation/types';
type NavigationCommands =
| typeof Commands.navigateToTestFile
| typeof Commands.navigateToTestFunction
| typeof Commands.navigateToTestSuite;
const testNavigationCommandMapping: { [key: string]: NavigationCommands } = {
[TestDataItemType.file]: Commands.navigateToTestFile,
[TestDataItemType.function]: Commands.navigateToTestFunction,
[TestDataItemType.suite]: Commands.navigateToTestSuite,
};
@injectable()
export class TestExplorerCommandHandler implements ITestExplorerCommandHandler {
private readonly disposables: IDisposable[] = [];
constructor(
@inject(ICommandManager) private readonly cmdManager: ICommandManager,
@inject(ITestDataItemResource) private readonly testResource: ITestDataItemResource,
) {}
public register(): void {
this.disposables.push(this.cmdManager.registerCommand(Commands.runTestNode, this.onRunTestNode, this));
this.disposables.push(this.cmdManager.registerCommand(Commands.debugTestNode, this.onDebugTestNode, this));
this.disposables.push(
this.cmdManager.registerCommand(Commands.openTestNodeInEditor, this.onOpenTestNodeInEditor, this),
);
}
public dispose(): void {
this.disposables.forEach((item) => item.dispose());
}
@swallowExceptions('Run test node')
@traceDecorators.error('Run test node failed')
protected async onRunTestNode(item: TestDataItem): Promise<void> {
await this.runDebugTestNode(item, 'run');
}
@swallowExceptions('Debug test node')
@traceDecorators.error('Debug test node failed')
protected async onDebugTestNode(item: TestDataItem): Promise<void> {
await this.runDebugTestNode(item, 'debug');
}
@swallowExceptions('Open test node in Editor')
@traceDecorators.error('Open test node in editor failed')
protected async onOpenTestNodeInEditor(item: TestDataItem): Promise<void> {
const testType = getTestDataItemType(item);
if (testType === TestDataItemType.folder) {
throw new Error('Unknown Test Type');
}
const command = testNavigationCommandMapping[testType];
const testUri = this.testResource.getResource(item);
if (!command) {
throw new Error('Unknown Test Type');
}
this.cmdManager.executeCommand(command, testUri, item, true);
}
protected async runDebugTestNode(item: TestDataItem, runType: 'run' | 'debug'): Promise<void> {
let testToRun: TestsToRun;
switch (getTestDataItemType(item)) {
case TestDataItemType.file: {
testToRun = { testFile: [item as TestFile] };
break;
}
case TestDataItemType.folder: {
testToRun = { testFolder: [item as TestFolder] };
break;
}
case TestDataItemType.suite: {
testToRun = { testSuite: [item as TestSuite] };
break;
}
case TestDataItemType.function: {
testToRun = { testFunction: [item as TestFunction] };
break;
}
default:
throw new Error('Unknown Test Type');
}
const testUri = this.testResource.getResource(item);
const cmd = runType === 'run' ? Commands.Tests_Run : Commands.Tests_Debug;
this.cmdManager.executeCommand(cmd, undefined, CommandSource.testExplorer, testUri, testToRun);
}
}