Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/automation/src/scm.ts
3520 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 { Viewlet } from './viewlet';
7
import { IElement } from './driver';
8
import { findElement, findElements, Code } from './code';
9
10
const VIEWLET = 'div[id="workbench.view.scm"]';
11
const SCM_INPUT_NATIVE_EDIT_CONTEXT = `${VIEWLET} .scm-editor .native-edit-context`;
12
const SCM_INPUT_TEXTAREA = `${VIEWLET} .scm-editor textarea`;
13
const SCM_RESOURCE = `${VIEWLET} .monaco-list-row .resource`;
14
const REFRESH_COMMAND = `div[id="workbench.parts.sidebar"] .actions-container a.action-label[aria-label="Refresh"]`;
15
const COMMIT_COMMAND = `div[id="workbench.parts.sidebar"] .actions-container a.action-label[aria-label="Commit"]`;
16
const SCM_RESOURCE_CLICK = (name: string) => `${SCM_RESOURCE} .monaco-icon-label[aria-label*="${name}"] .label-name`;
17
const SCM_RESOURCE_ACTION_CLICK = (name: string, actionName: string) => `${SCM_RESOURCE} .monaco-icon-label[aria-label*="${name}"] .actions .action-label[aria-label="${actionName}"]`;
18
19
interface Change {
20
name: string;
21
type: string;
22
actions: string[];
23
}
24
25
function toChange(element: IElement): Change {
26
const name = findElement(element, e => /\blabel-name\b/.test(e.className))!;
27
const type = element.attributes['data-tooltip'] || '';
28
29
const actionElementList = findElements(element, e => /\baction-label\b/.test(e.className));
30
const actions = actionElementList.map(e => e.attributes['title']);
31
32
return {
33
name: name.textContent || '',
34
type,
35
actions
36
};
37
}
38
39
40
export class SCM extends Viewlet {
41
42
constructor(code: Code) {
43
super(code);
44
}
45
46
async openSCMViewlet(): Promise<any> {
47
await this.code.dispatchKeybinding('ctrl+shift+g', async () => { await this.code.waitForElement(this._editContextSelector()); });
48
}
49
50
async waitForChange(name: string, type?: string): Promise<void> {
51
const func = (change: Change) => change.name === name && (!type || change.type === type);
52
await this.code.waitForElements(SCM_RESOURCE, true, elements => elements.some(e => func(toChange(e))));
53
}
54
55
async refreshSCMViewlet(): Promise<any> {
56
await this.code.waitAndClick(REFRESH_COMMAND);
57
}
58
59
async openChange(name: string): Promise<void> {
60
await this.code.waitAndClick(SCM_RESOURCE_CLICK(name));
61
}
62
63
async stage(name: string): Promise<void> {
64
await this.code.waitAndClick(SCM_RESOURCE_ACTION_CLICK(name, 'Stage Changes'));
65
await this.waitForChange(name, 'Index Modified');
66
}
67
68
async unstage(name: string): Promise<void> {
69
await this.code.waitAndClick(SCM_RESOURCE_ACTION_CLICK(name, 'Unstage Changes'));
70
await this.waitForChange(name, 'Modified');
71
}
72
73
async commit(message: string): Promise<void> {
74
await this.code.waitAndClick(this._editContextSelector());
75
await this.code.waitForActiveElement(this._editContextSelector());
76
await this.code.waitForSetValue(this._editContextSelector(), message);
77
await this.code.waitAndClick(COMMIT_COMMAND);
78
}
79
80
private _editContextSelector(): string {
81
return !this.code.editContextEnabled ? SCM_INPUT_TEXTAREA : SCM_INPUT_NATIVE_EDIT_CONTEXT;
82
}
83
}
84
85