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

Commit aa5d63e

Browse files
committed
FUSETOOLS2-1203 adding tutorials via prefs
- add test - update to use environment variable - adjusting env variable name - fix test - cleanup Signed-off-by: Brian Fitzpatrick <[email protected]>
1 parent eac9a41 commit aa5d63e

File tree

4 files changed

+56
-69
lines changed

4 files changed

+56
-69
lines changed

src/extension.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import { clearRegisteredTutorials, getOpenAtStartupSetting,
2222
clearOutputChannels, registerTutorialWithJSON, getAutoInstallDefaultTutorialsSetting,
2323
addNewTutorialWithNameAndCategoryForDidactUri,
2424
removeTutorialByNameAndCategory,
25-
registerEmbeddedTutorials} from './utils';
25+
registerEmbeddedTutorials,
26+
appendAdditionalTutorialsFromEnv,
27+
getAppendRegisteredSettingFromEnv} from './utils';
2628
import { DidactUriCompletionItemProvider } from './didactUriCompletionItemProvider';
2729
import { DidactPanelSerializer } from './didactPanelSerializer';
2830
import { didactManager, VIEW_TYPE } from './didactManager';
@@ -116,6 +118,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
116118
await registerEmbeddedTutorials(context, 'Writing Your First Didact Tutorial', './demos/markdown/tutorial/tutorial.didact.md');
117119
}
118120

121+
// append any additional tutorials if we have them
122+
await appendAdditionalTutorialsFromEnv();
123+
119124
// create the view
120125
createIntegrationsView();
121126

src/test/suite/registry.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as assert from 'assert';
2-
import {getRegisteredTutorials, getDidactCategories, getTutorialsForCategory, getUriForDidactNameAndCategory, registerTutorialWithCategory, clearRegisteredTutorials, registerTutorialWithArgs} from '../../utils';
2+
import {getRegisteredTutorials, getDidactCategories, getTutorialsForCategory, getUriForDidactNameAndCategory, registerTutorialWithCategory, clearRegisteredTutorials, registerTutorialWithArgs, DIDACT_APPEND_REGISTERED_SETTING, getAppendRegisteredSettingFromEnv, appendAdditionalTutorialsFromEnv} from '../../utils';
33
import {before} from 'mocha';
44
import * as vscode from 'vscode';
55
import { ADD_TUTORIAL_TO_REGISTRY, getContext, REGISTER_TUTORIAL } from '../../extensionFunctions';
@@ -115,6 +115,24 @@ suite('Didact registry test suite', () => {
115115
assert.ok(foundTutorial, `Did not find new-tutorial-4 registered via JSON`);
116116
});
117117

118+
test('append registry from environment variable', async() => {
119+
const tutsToAppend : String = '[{"name":"AppendMe2","category":"AppendedCat2","sourceUri":"https%3A%2F%2Fraw.githubusercontent.com%2Fredhat-developer%2Fvscode-didact%2Fmaster%2Fexamples%2Fregistry.example.didact.md"}]';
120+
process.env[DIDACT_APPEND_REGISTERED_SETTING] = tutsToAppend.toString();
121+
122+
const envVarJson = getAppendRegisteredSettingFromEnv();
123+
console.log(`envVar = ` + envVarJson);
124+
assert.ok(envVarJson, `Did not find envVarJson`);
125+
126+
const registry = getRegisteredTutorials();
127+
assert.notStrictEqual(registry, undefined);
128+
129+
const tutName = `AppendMe2`;
130+
await appendAdditionalTutorialsFromEnv();
131+
const foundTutorial = verifyTutorialInRegistry(tutName);
132+
delete process.env.DIDACT_APPEND_REGISTERED_SETTING;
133+
assert.ok(foundTutorial, `Did not find ${tutName} registered after appending to tutorial list from settings`);
134+
});
135+
118136
test('Clear all the tutorials', async() => {
119137
const registry = getRegisteredTutorials();
120138
assert.notStrictEqual(registry, undefined);

src/test/suite/stubDemoTutorial.test.ts

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { expect } from 'chai';
19-
import { window, commands, env, Uri, Terminal, Disposable } from 'vscode';
19+
import { window, commands, env, Uri, Terminal } from 'vscode';
2020
import { START_DIDACT_COMMAND, sendTerminalText, gatherAllCommandsLinks, getContext } from '../../extensionFunctions';
2121
import { didactManager } from '../../didactManager';
2222
import { DidactUri } from '../../didactUri';
@@ -34,29 +34,11 @@ const TERMINAL_WAIT_RETRY = 2000;
3434

3535
suite('stub out a tutorial', () => {
3636

37-
let disposables: Disposable[] = [];
38-
39-
teardown(() => {
40-
disposables.forEach(d => d.dispose());
41-
disposables.length = 0;
42-
});
43-
4437
test('that we can send an echo command to the terminal and get the response', async () => {
4538
const name = 'echoTerminal';
4639
const text = `echo \"Hello World ${name}\"`;
4740
const result = `Hello World echoTerminal`;
48-
const winResult = [
49-
`Hello`,
50-
`World`,
51-
`echoTerminal`
52-
];
53-
await validateTerminalResponseWin(name, text, result);
54-
55-
// if (process.platform === 'win32') {
56-
// await validateTerminalResponseWin(name, text, winResult);
57-
// } else {
58-
// await validateTerminalResponse(name, text, result);
59-
// }
41+
await validateTerminalResponse(name, text, result);
6042
});
6143

6244
test('that we can get a response from each command in the demo tutorial', async () => {
@@ -116,53 +98,6 @@ suite('stub out a tutorial', () => {
11698
}
11799
}
118100

119-
async function validateTerminalResponseWin(terminalName : string, terminalText : string, terminalResponse : string) {
120-
let terminalData:string[] = [];
121-
console.log(`validateTerminalResponse terminal ${terminalName} executing text ${terminalText}`);
122-
const term = window.createTerminal(terminalName);
123-
expect(term).to.not.be.null;
124-
if (term) {
125-
console.log(`-current terminal = ${term?.name}`);
126-
await sendTerminalText(terminalName, terminalText);
127-
await waitUntil(async () => {
128-
await focusOnNamedTerminal(terminalName);
129-
return terminalName === window.activeTerminal?.name;
130-
}, 1000);
131-
try {
132-
const predicate = async () => {
133-
const result: string = await getActiveTerminalOutput();
134-
await commands.executeCommand('workbench.action.terminal.clear');
135-
if (result.trim().length > 0) terminalData.push(result.trim());
136-
return true;
137-
};
138-
var numberOfTimes = 5;
139-
const delay = 1000;
140-
for (let i = 0; i < numberOfTimes; i++) {
141-
await predicate();
142-
await new Promise((res) => { setTimeout(res, delay); });
143-
}
144-
console.log(`-terminal output = ${terminalData}`);
145-
146-
const foundIt = searchStringInArray(terminalResponse, terminalData);
147-
if (foundIt > -1) {
148-
return;
149-
} else {
150-
fail(`Searching for ${terminalResponse} but not found in current content of active terminal ${window.activeTerminal?.name} : ${terminalData}`);
151-
};
152-
} catch (error){
153-
fail(error);
154-
}
155-
findAndDisposeTerminal(terminalName);
156-
}
157-
}
158-
159-
function searchStringInArray (strToFind : string, strArray : string[]) : number {
160-
for (var j=0; j<strArray.length; j++) {
161-
if (strArray[j].match(strToFind)) return j;
162-
}
163-
return -1;
164-
}
165-
166101
async function getActiveTerminalOutput() : Promise<string> {
167102
const term = window.activeTerminal;
168103
console.log(`-current terminal = ${term?.name}`);

src/utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const DIDACT_OPEN_AT_STARTUP = 'didact.openDefaultTutorialAtStartup';
3030
export const DIDACT_AUTO_INSTALL_DEFAULT_TUTORIALS = 'didact.autoAddDefaultTutorials';
3131
export const DIDACT_CLI_LINK_LF_SETTING = 'didact.edit.cliLinkLF';
3232
export const DIDACT_CLI_LINK_TEXT_SETTING = 'didact.edit.cliLinkText';
33+
export const DIDACT_APPEND_REGISTERED_SETTING = 'DIDACT_APPEND_REGISTRY';
3334

3435
const CACHED_OUTPUT_CHANNELS: OutputChannel[] = new Array<OutputChannel>();
3536

@@ -500,3 +501,31 @@ export function getFileExtension(pathAsString: string) : string | undefined {
500501
}
501502
return undefined;
502503
}
504+
505+
export function getAppendRegisteredSettingFromEnv() : string | undefined {
506+
console.log(process.env);
507+
const envVar = process.env[DIDACT_APPEND_REGISTERED_SETTING];
508+
if (!envVar || (typeof envVar !== 'string')) {
509+
return;
510+
}
511+
return envVar as string;
512+
}
513+
514+
export async function appendAdditionalTutorialsFromEnv() : Promise<void> {
515+
try {
516+
const appendTutorialsAtStartup: string | undefined = getAppendRegisteredSettingFromEnv();
517+
if (appendTutorialsAtStartup) {
518+
await extensionFunctions.sendTextToOutputChannel(`Didact tutorials appended at startup via ${DIDACT_APPEND_REGISTERED_SETTING}`);
519+
const jsonTutorials = JSON.parse(appendTutorialsAtStartup);
520+
for (var i = 0; i < jsonTutorials.length; i++) {
521+
const jsonObj:any = jsonTutorials[i];
522+
await extensionFunctions.sendTextToOutputChannel(`--Adding tutorial ${jsonObj.sourceUri} as ${jsonObj.name}/${jsonObj.category}`);
523+
await registerTutorialWithCategory(jsonObj.name, jsonObj.sourceUri, jsonObj.category);
524+
}
525+
}
526+
} catch (ex) {
527+
await extensionFunctions.sendTextToOutputChannel(ex);
528+
console.error(ex);
529+
return Promise.reject(ex);
530+
}
531+
}

0 commit comments

Comments
 (0)