Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
FEAT-595: changes for bst init, adding dialogFlow and handler questio…
Browse files Browse the repository at this point in the history
…ns (#615)
  • Loading branch information
jricaldi authored Nov 21, 2019
1 parent e598941 commit cd0c91b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 54 deletions.
45 changes: 28 additions & 17 deletions bin/bst-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const questions = [
{
type: "list",
name: "type",
message: "What type of tests are you creating - unit, end-to-end or both:",
message: "What type of tests are you creating - unit, end-to-end:",
choices: [
{
name: "unit",
Expand All @@ -20,12 +20,7 @@ const questions = [
name: "end-to-end",
value: "e2e",
},
{
name: "both",
value: "both",
},
],
default: "both",
},
{
type: "input",
Expand All @@ -36,13 +31,23 @@ const questions = [
{
type: "list",
name: "platform",
message: "Are you developing for Alexa, Google, or both?",
message: "Are you developing for Alexa, Google?",
choices: [
"Alexa",
"Google",
"both",
],
default: "both",
{
name: "Alexa",
value: "alexa",
},
{
name: "Google",
value: "google",
},
],
},
{
type: "input",
name: "handler",
message: "Please provide the name of your handler file (or leave blank for index.js):",
default: "index.js",
},
{
type: "input",
Expand All @@ -54,7 +59,13 @@ const questions = [
type: "input",
name: "virtualDevice",
message: "For end-to-end tests, we require a virtual device token.\nIf you don't have one or are not sure just leave it blank.\nYou can create virtual devices here: https://apps.bespoken.io/dashboard/virtualdevice\nEnter your token:",
when: (answers: any) => ["e2e", "both"].indexOf(answers["type"]) > -1,
when: (answers: any) => answers["type"].includes("e2e"),
},
{
type: "input",
name: "dialogFlow",
message: "Please provide the path to your Dialogflow directory\n(if you don't know what this is, please take a look at https://read.bespoken.io/unit-testing/guide-google/#configuration-google-specific):",
when: (answers: any) => answers["type"].includes("unit") && answers["platform"].includes("Alexa"),
},
];

Expand All @@ -65,11 +76,11 @@ program
console.log(chalk.yellow("We'll set up all you need for you to start testing your voice apps."));
console.log(chalk.yellow("Please tell us:"));
prompt(questions).then(answers => {
const platform = answers["platform"] ? answers["platform"].toLowerCase() : "";
const initUtil = new InitUtil(answers["type"], platform,
answers["locales"], answers["projectName"], answers["virtualDevice"]);
const { type, platform, handler, locales, projectName, virtualDevice, dialogFlow } = answers;
const initUtil = new InitUtil(type, platform, handler, locales, projectName, virtualDevice, dialogFlow);
initUtil.createFiles();
console.log(chalk.green("\nThat's it! We've created your test files for you. To run them, simply type: `bst test`\nYou can learn more advanced topics about testing at https://read.bespoken.io"));
});
});

program.parse(process.argv);
program.parse(process.argv);
42 changes: 21 additions & 21 deletions lib/init/init-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ export class InitUtil {
private isMultilocale: boolean;

constructor(
public type: string,
public platform: string,
public locales: string,
public projectName: string,
public virtualDeviceToken?: string
private type: string,
private platform: string,
private handler: string,
private locales: string,
private projectName: string,
private virtualDeviceToken?: string,
private dialogFlow?: string,
) {
this.isMultilocale = locales.split(",").length > 1;
this.projectName = projectName || "voice hello world";
this.handler = handler || "index.js";
this.locales = locales || "en-US";
this.virtualDeviceToken = virtualDeviceToken || "[your virtual device token goes here]";
this.dialogFlow = dialogFlow || "Path to your Dialogflow directory. Read more at https://read.bespoken.io/unit-testing/guide-google/#configuration";
}

public async createFiles(): Promise<void> {
// utterances will change on unit test for google,
// if plaform both was selected, utterances will have google's form
const finalPlatform = ["both", "google"].indexOf(this.platform) > -1 ? "google" : this.platform;
if (this.type === "both") {
await this.createTestFilesForType("unit", finalPlatform);
await this.createTestFilesForType("e2e", finalPlatform);
} else {
await this.createTestFilesForType(this.type, finalPlatform);
}
await this.createTestFilesForType(this.type, this.platform);
}

private async createTestFilesForType(type: string, platform: string): Promise<void> {
Expand All @@ -39,7 +38,7 @@ export class InitUtil {
await this.createMultilocaleFiles(type);

const ymlContent = this.getYmlContent(type, platform);
const testingFileContent = this.getTestingJson(type);
const testingFileContent = this.getTestingJson();
await this.writeFile(`${testFolder}/index.test.yml`, ymlContent);
await this.writeFile(`${currentFolder}/test/${type}/testing.json`, JSON.stringify(testingFileContent, null, 4));
}
Expand Down Expand Up @@ -164,20 +163,21 @@ export class InitUtil {
};
}

private getTestingJson(type: string): any {
private getTestingJson(): any {
const testingJsonForUnit = {
handler: "relative or absolute path to your voice app entry point",
handler: this.handler,
locales: this.locales,
};
const testingJsonForE2e = {
virtualDeviceToken: this.virtualDeviceToken || "[your virtual device token goes here]",
locales: this.locales,
virtualDeviceToken: this.virtualDeviceToken,
...testingJsonForUnit,
type: "e2e",
};
if (this.platform === "google") {
testingJsonForUnit["platform"] = "google";
testingJsonForUnit["dialogFlow"] = this.dialogFlow;
}
return type === "unit" ? testingJsonForUnit : testingJsonForE2e;
return this.type === "unit" ? testingJsonForUnit : testingJsonForE2e;
}

private getHeaderComment(type: string): string {
Expand Down Expand Up @@ -254,4 +254,4 @@ export class InitUtil {
});
});
}
}
}
19 changes: 3 additions & 16 deletions test/init/init-util-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("init util", function() {

describe("createFilesStructure()", () => {
it("create file structure for unit tests", async () => {
await new InitUtil("unit", "alexa", "en-US", "hello world").createFiles();
await new InitUtil("unit", "alexa", "index.js", "en-US", "hello world").createFiles();

const existUnitTestFile = fs.existsSync("test/unit/index.test.yml");
const existUnitTestingFile = fs.existsSync("test/unit/testing.json");
Expand All @@ -31,7 +31,7 @@ describe("init util", function() {
});

it("create file structure for e2e tests", async () => {
await new InitUtil("e2e", "alexa", "en-US", "hello world").createFiles();
await new InitUtil("e2e", "alexa", "index.js", "en-US", "hello world").createFiles();

const existUnitTestFile = fs.existsSync("test/unit/index.test.yml");
const existUnitTestingFile = fs.existsSync("test/unit/testing.json");
Expand All @@ -42,19 +42,6 @@ describe("init util", function() {
assert.equal(existE2eTestFile, true);
assert.equal(existE2eTestingFile, true);
});

it("create file structure for unit and e2e tests", async () => {
await new InitUtil("both", "alexa", "en-US", "hello world").createFiles();

const existUnitTestFile = fs.existsSync("test/unit/index.test.yml");
const existUnitTestingFile = fs.existsSync("test/unit/testing.json");
const existE2eTestFile = fs.existsSync("test/e2e/index.test.yml");
const existE2eTestingFile = fs.existsSync("test/e2e/testing.json");
assert.equal(existUnitTestFile, true);
assert.equal(existUnitTestingFile, true);
assert.equal(existE2eTestFile, true);
assert.equal(existE2eTestingFile, true);
});
});

function deleteFolderRecursive(path: string) {
Expand All @@ -70,4 +57,4 @@ describe("init util", function() {
fs.rmdirSync(path);
}
}
});
});

0 comments on commit cd0c91b

Please sign in to comment.