This repository was archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathstubDemoTutorial.test.ts
More file actions
135 lines (120 loc) · 5.13 KB
/
Copy pathstubDemoTutorial.test.ts
File metadata and controls
135 lines (120 loc) · 5.13 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
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect } from 'chai';
import { window, commands, env, Uri, Terminal } from 'vscode';
import { START_DIDACT_COMMAND, sendTerminalText, gatherAllCommandsLinks, getContext } from '../../extensionFunctions';
import { didactManager } from '../../didactManager';
import { DidactUri } from '../../didactUri';
import { handleText } from '../../commandHandler';
import { waitUntil } from 'async-wait-until';
import { fail } from 'assert';
import { delay } from '../../utils';
const testMD = Uri.parse('vscode://redhat.vscode-didact?extension=demos/markdown/didact-demo.didact.md');
const delayTime = 2500;
const COMMAND_WAIT_TIMEOUT = 15000;
const COMMAND_WAIT_RETRY = 1500;
const TERMINAL_WAIT_RETRY = 2000;
suite('stub out a tutorial', () => {
test('that we can send an echo command to the terminal and get the response', async () => {
const name = 'echoTerminal';
const text = `echo "Hello World ${name}"`;
const result = `Hello World echoTerminal`;
await validateTerminalResponse(name, text, result);
});
test('that we can get a response from each command in the demo tutorial', async () => {
await commands.executeCommand(START_DIDACT_COMMAND, testMD);
if (didactManager.active()) {
const commandsToTest: any[] = gatherAllCommandsLinks().filter( (href) => href.match(/=vscode.didact.sendNamedTerminalAString&/g));
expect(commandsToTest).to.not.be.empty;
commandsToTest.forEach(async (href: string) => {
const ctxt = getContext();
if (ctxt) {
const testUri = new DidactUri(href, ctxt);
const textToParse = testUri.getText();
const userToParse = testUri.getUser();
const outputs : string[] = [];
if (textToParse) {
handleText(textToParse, outputs);
} else if (userToParse) {
handleText(userToParse, outputs);
}
expect(outputs).length.to.be.at.least(2);
const terminalName = outputs[0];
const terminalString = outputs[1];
await validateTerminalResponse(terminalName, terminalString);
}
});
}
});
async function validateTerminalResponse(terminalName : string, terminalText : string, terminalResponse? : string) {
console.log(`validateTerminalResponse terminal ${terminalName} executing text ${terminalText}`);
const term = window.createTerminal(terminalName);
expect(term).to.not.be.null;
if (term) {
console.log(`-current terminal = ${term?.name}`);
await sendTerminalText(terminalName, terminalText);
await waitUntil(async () => {
await focusOnNamedTerminal(terminalName);
return terminalName === window.activeTerminal?.name;
}, TERMINAL_WAIT_RETRY);
try {
const predicate = async () => {
const result: string = await getActiveTerminalOutput();
return result.includes(getExpectedTextInTerminal());
};
await waitUntil(predicate, { timeout: COMMAND_WAIT_TIMEOUT, intervalBetweenAttempts: COMMAND_WAIT_RETRY });
} catch (error){
console.log(`Searching for ${getExpectedTextInTerminal()} but not found in current content of active terminal ${window.activeTerminal?.name} : ${await getActiveTerminalOutput()}`);
fail(error);
}
findAndDisposeTerminal(terminalName);
}
function getExpectedTextInTerminal() {
return terminalResponse ? terminalResponse : terminalText;
}
}
async function getActiveTerminalOutput() : Promise<string> {
const term = window.activeTerminal;
console.log(`-current terminal = ${term?.name}`);
await executeAndWait('workbench.action.terminal.selectAll');
await delay(delayTime);
await executeAndWait('workbench.action.terminal.copySelection');
await executeAndWait('workbench.action.terminal.clearSelection');
const clipboard_content = await env.clipboard.readText();
return clipboard_content.trim();
}
async function executeAndWait(command: string): Promise<void> {
await commands.executeCommand(command);
await delay(200);
}
function getNamedTerminal(terminalName : string): Terminal | undefined {
return window.terminals.filter(term => term.name === terminalName)[0];
}
function findAndDisposeTerminal(terminalName: string) : void {
const term = getNamedTerminal(terminalName);
if (term) {
term.dispose();
}
}
async function focusOnNamedTerminal(terminalName : string) : Promise<void> {
let term = window.activeTerminal;
while (term?.name != terminalName) {
await commands.executeCommand('workbench.action.terminal.focusNext');
term = window.activeTerminal;
}
}
});