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

Commit f74cb1c

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 081bfd1 commit f74cb1c

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';
@@ -33,29 +33,11 @@ const TERMINAL_WAIT_RETRY = 2000;
3333

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

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

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

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