Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.

Commit 8a1680b

Browse files
authored
FUSETOOLS2-737 - Adding tests to validate action results from demo tutorial (#410)
* new test to validate action results - fixing broken tests and rebasing - fixing completion test failure - using cursorEnd command - inserting additional delays to help finish command processing - updating terminal tests - addressing PR feedback - addressing PR comments round 2 - increasing timeout to help with windows failure Signed-off-by: Brian Fitzpatrick <[email protected]>
1 parent cd4a8bd commit 8a1680b

File tree

2 files changed

+169
-4
lines changed

2 files changed

+169
-4
lines changed

src/test/suite/completions.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { expect } from 'chai';
2020
import * as path from 'path';
2121
import * as vscode from 'vscode';
2222
import { removeFilesAndFolders } from '../../utils';
23+
import { Position, Range, TextEditor } from "vscode";
2324

2425
const testWorkspace = path.resolve(__dirname, '..', '..', '..', './test Fixture with speci@l chars');
2526
const foldersAndFilesToRemove: string[] = [
@@ -49,7 +50,7 @@ suite("New Didact URI completion provider tests", function () {
4950
const expected = "[My Link](didact://?commandId=";
5051

5152
suite('walk through a list of command completions', () => {
52-
listOfCompletions.forEach(function(stringToTest: string){
53+
listOfCompletions.forEach(function(stringToTest: string) {
5354
test(`test provided completion "${stringToTest}"`, async () => {
5455
await testWeGetExpectedResult(stringToTest, expected);
5556
});
@@ -61,18 +62,33 @@ suite("New Didact URI completion provider tests", function () {
6162

6263
async function testWeGetExpectedResult(textToInsert : string, expectedResult: string) {
6364
const editor = await createTestEditor(testDocumentUri);
64-
await vscode.commands.executeCommand('editor.action.selectAll');
65-
await vscode.commands.executeCommand('type', {"text": textToInsert});
65+
await delay(100);
66+
await initializeTextEditor(editor, textToInsert);
67+
await delay(100);
68+
await vscode.commands.executeCommand("cursorEnd");
69+
await delay(100);
6670
await vscode.commands.executeCommand("editor.action.triggerSuggest");
67-
await delay(1000);
71+
await delay(100);
6872
await vscode.commands.executeCommand("acceptSelectedSuggestion");
73+
await delay(100);
6974
expect(editor.document.getText()).to.include(expectedResult);
7075
}
7176

7277
function delay(ms: number) {
7378
return new Promise( resolve => setTimeout(resolve, ms) );
7479
}
7580

81+
async function initializeTextEditor(textEditor: TextEditor, initializeWith = "") {
82+
const doc = textEditor.document;
83+
await textEditor.edit((editBuilder) => {
84+
editBuilder.delete(new Range(new Position(0, 0), doc.positionAt(doc.getText().length)));
85+
});
86+
await textEditor.edit((editBuilder) => {
87+
editBuilder.insert(new Position(0, 0), initializeWith);
88+
});
89+
expect(doc.getText()).to.be.equal(initializeWith);
90+
}
91+
7692
async function createTestEditor(uri: vscode.Uri) : Promise<vscode.TextEditor> {
7793
await vscode.workspace.fs.writeFile(testDocumentUri, Buffer.from(''));
7894
const document = await vscode.workspace.openTextDocument(uri);
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect } from 'chai';
19+
import { window, commands, env, Uri, Terminal, Pseudoterminal, Event, TerminalDimensions, EventEmitter } from 'vscode';
20+
import { START_DIDACT_COMMAND, sendTerminalText, gatherAllCommandsLinks, getContext, findTerminal } from '../../extensionFunctions';
21+
import { didactManager } from '../../didactManager';
22+
import { DidactUri } from '../../didactUri';
23+
import { handleText } from '../../commandHandler';
24+
25+
const testMD = Uri.parse('vscode://redhat.vscode-didact?extension=demos/markdown/didact-demo.didact.md');
26+
27+
const delayTime = 1000;
28+
29+
suite('stub out a tutorial', () => {
30+
31+
test('that we can send an echo command to the terminal and get the response', async () => {
32+
const name = 'echoTerminal';
33+
const text = `echo Hello World ${name}`;
34+
const result = `Hello World echoTerminal`;
35+
await validateTerminalResponse(name, text, result);
36+
});
37+
38+
test('that we can get a response from each command in the demo tutorial', async () => {
39+
await commands.executeCommand(START_DIDACT_COMMAND, testMD).then( async () => {
40+
if (didactManager.active()) {
41+
42+
suite('walk through all the sendNamedTerminalAString commands in the demo', () => {
43+
const commands : any[] = gatherAllCommandsLinks().filter( (href) => href.match(/=vscode.didact.sendNamedTerminalAString&/g));
44+
expect(commands).to.not.be.empty;
45+
commands.forEach(function(href: string) {
46+
test(`test terminal command "${href}"`, async () => {
47+
const ctxt = getContext();
48+
if (ctxt) {
49+
const testUri = new DidactUri(href, ctxt);
50+
const textToParse = testUri.getText();
51+
const userToParse = testUri.getUser();
52+
let outputs : string[] = [];
53+
if (textToParse) {
54+
handleText(textToParse, outputs);
55+
} else if (userToParse) {
56+
handleText(userToParse, outputs);
57+
}
58+
expect(outputs).length.to.be.at.least(2);
59+
const terminalName = outputs[0];
60+
const terminalString = outputs[1];
61+
await validateSimpleTerminalResponse(terminalName, terminalString);
62+
}
63+
});
64+
});
65+
});
66+
}
67+
});
68+
});
69+
70+
});
71+
72+
async function validateSimpleTerminalResponse(terminalName : string, terminalText : string) {
73+
console.log(`validateSimpleTerminalResponse terminal ${terminalName} executing text ${terminalText}`);
74+
const term = window.createTerminal(terminalName);
75+
expect(term).to.not.be.null;
76+
if (term) {
77+
console.log(`-current terminal = ${term?.name}`);
78+
await sendTerminalText(terminalName, terminalText);
79+
await delay(delayTime);
80+
const term2 = focusOnNamedTerminal(terminalName);
81+
await delay(delayTime);
82+
let result = await getTerminalOutput(terminalName);
83+
console.log(`-validateSimpleTerminalResponse terminal output = ${result}`);
84+
85+
// we're just making sure we get something back and can see the text we put into the terminal
86+
expect(result).to.include(terminalText);
87+
findAndDisposeTerminal(terminalName);
88+
}
89+
}
90+
91+
async function validateTerminalResponse(terminalName : string, terminalText : string, terminalResponse : string) {
92+
console.log(`validateTerminalResponse terminal ${terminalName} executing text ${terminalText} and looking for response ${terminalResponse}`);
93+
const term = window.createTerminal(terminalName);
94+
expect(term).to.not.be.null;
95+
if (term) {
96+
console.log(`-current terminal = ${term?.name}`);
97+
await sendTerminalText(terminalName, terminalText);
98+
await delay(delayTime);
99+
const term2 = focusOnNamedTerminal(terminalName);
100+
await delay(delayTime);
101+
let result = await getTerminalOutput(terminalName);
102+
console.log(`-validateTerminalResponse terminal output = ${result}`);
103+
expect(result).to.include(terminalResponse);
104+
findAndDisposeTerminal(terminalName);
105+
}
106+
}
107+
108+
async function getTerminalOutput(terminalName : string) : Promise<string> {
109+
const terminal = getNamedTerminal(terminalName);
110+
expect(terminal).to.not.be.undefined;
111+
expect(terminal?.name).to.equal(terminalName);
112+
focusOnNamedTerminal(terminalName);
113+
const term = window.activeTerminal;
114+
console.log(`-current terminal = ${term?.name}`);
115+
await executeAndWait('workbench.action.terminal.selectAll');
116+
await delay(delayTime);
117+
await executeAndWait('workbench.action.terminal.copySelection');
118+
await executeAndWait('workbench.action.terminal.clearSelection');
119+
const clipboard_content = await env.clipboard.readText();
120+
return clipboard_content.trim();;
121+
}
122+
123+
function delay(ms: number) {
124+
return new Promise( resolve => setTimeout(resolve, ms) );
125+
}
126+
127+
async function executeAndWait(command: string): Promise<void> {
128+
await commands.executeCommand(command);
129+
delay(100);
130+
}
131+
132+
function getNamedTerminal(terminalName : string): Terminal | undefined {
133+
return window.terminals.filter(term => term.name === terminalName)[0];
134+
}
135+
136+
function findAndDisposeTerminal(terminalName: string) : void {
137+
const term = getNamedTerminal(terminalName);
138+
if (term) {
139+
term.dispose();
140+
}
141+
}
142+
143+
async function focusOnNamedTerminal(terminalName : string) : Promise<void> {
144+
let term = window.activeTerminal;
145+
while (term?.name != terminalName) {
146+
await commands.executeCommand('workbench.action.terminal.focusNext');
147+
term = window.activeTerminal;
148+
}
149+
}

0 commit comments

Comments
 (0)