From 405c1780b2dbfc6cad496f1581762a3b765f7b79 Mon Sep 17 00:00:00 2001 From: Tien Suwandy Date: Wed, 10 Nov 2021 14:22:46 -0800 Subject: [PATCH] fix for 1316 -- bf orchestrator:create error when passing in .dispatch file as input (#1317) (#1319) * fix for bug 1316 fix for bug 1316 * fixed failed test --- .../src/commands/orchestrator/create.ts | 8 +---- .../orchestratorlib/src/datasourcehelper.ts | 14 ++++++++ .../test/datasourcehelper.test.ts | 35 +++++++++++++++++++ 3 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 packages/orchestratorlib/test/datasourcehelper.test.ts diff --git a/packages/orchestrator/src/commands/orchestrator/create.ts b/packages/orchestrator/src/commands/orchestrator/create.ts index 0dfe505e3..ad15d8176 100644 --- a/packages/orchestrator/src/commands/orchestrator/create.ts +++ b/packages/orchestrator/src/commands/orchestrator/create.ts @@ -63,13 +63,7 @@ export default class OrchestratorCreate extends Command { const hasDataSources: boolean = settings.DataSources?.inputs?.length > 0 ?? false; if (hasDataSources) { hierarchical = true; - // do not override the input folder from the --in parameter - const inputPathSpecified: boolean = !Utility.isEmptyString(flags.in); - if (!inputPathSpecified && !Utility.isEmptyString(settings.DataSources.path)) { - input = settings.DataSources.path; - } else { - settings.DataSources.path = flags.in; - } + input = DataSourceHelper.getInputFolderPath(flags.in, cwd, settings.DataSources); } if (refresh) { diff --git a/packages/orchestratorlib/src/datasourcehelper.ts b/packages/orchestratorlib/src/datasourcehelper.ts index 81b35409e..4fcfe865a 100644 --- a/packages/orchestratorlib/src/datasourcehelper.ts +++ b/packages/orchestratorlib/src/datasourcehelper.ts @@ -75,6 +75,20 @@ export class DataSourceHelper { return path.extname(input) === '.dispatch'; } + public static getInputFolderPath(input: string, currentWorkingDir: string, dataSources: OrchestratorDataSourceSettings): string { + // do not override the input folder from the --in parameter + input = path.resolve(input); + if (Utility.isEmptyString(input) && !Utility.isEmptyString(dataSources.path)) { + input = dataSources.path; + } else if (OrchestratorHelper.isDirectory(input)) { + dataSources.path = input; + } else { + input = path.join(currentWorkingDir, 'dataSources'); + dataSources.path = input; + } + return input; + } + public static async getQnAFileFromQnaKb(input: OrchestratorDataSource, endpoint: string = ''): Promise { const qna: any = await LuisQnaHelper.getQnaFromKb(input.Id, input.Key, endpoint); return qna; diff --git a/packages/orchestratorlib/test/datasourcehelper.test.ts b/packages/orchestratorlib/test/datasourcehelper.test.ts new file mode 100644 index 000000000..f0f882294 --- /dev/null +++ b/packages/orchestratorlib/test/datasourcehelper.test.ts @@ -0,0 +1,35 @@ +/*! + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import {} from 'mocha'; +import {DataSourceHelper} from '../src/datasourcehelper'; +import {OrchestratorDataSourceSettings} from '../src/settings'; +import * as path from 'path'; +import * as fs from 'fs-extra'; +import assert = require('assert'); + +describe('DataSourceHelperTests', () => { + const cwd: string = process.cwd(); + it('getInputFolderPathWithDispatchFileInput', () => { + const dataSourceSettings: OrchestratorDataSourceSettings = new OrchestratorDataSourceSettings([], true, ''); + const input: string = DataSourceHelper.getInputFolderPath(path.join(cwd, 'test.Dispatch'), cwd, dataSourceSettings); + assert(input === path.join(cwd, 'dataSources')); + }); + + it('getInputFolderPathWithNoInputAndWithExistingOrchestratorSettingsFile', () => { + const dataSourceSettings: OrchestratorDataSourceSettings = new OrchestratorDataSourceSettings([], true, path.join(cwd, 'dataSources')); + const input: string = DataSourceHelper.getInputFolderPath('', cwd, dataSourceSettings); + assert(input === dataSourceSettings.path); + }); + + it('getInputFolderPathWithInputAndWithExistingOrchestratorSettingsFile', () => { + const inputPath: string = path.resolve('./test/fixtures/output/dataSources'); + fs.mkdirSync(inputPath, {recursive: true}); + const dataSourceSettings: OrchestratorDataSourceSettings = new OrchestratorDataSourceSettings([], true, path.join(cwd, 'dataSources')); + const input: string = DataSourceHelper.getInputFolderPath(inputPath, cwd, dataSourceSettings); + assert(input === inputPath); + assert(dataSourceSettings.path === inputPath); + }); +});