Skip to content

Commit

Permalink
make config.ts export getConfig function (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
microsoftly committed Dec 21, 2017
1 parent 587d07b commit 04f6abe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/BotTester.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Promise from 'bluebird';
import { IAddress, IMessage, Message, Session, UniversalBot } from 'botbuilder';
import { ignoreEndOfConversationEventFilter, ignoreTypingEventFilter } from './builtInMessageFilters';
import { config, IConfig, MessageFilter } from './config';
import { getConfig, IConfig, MessageFilter } from './config';
import { ExpectedMessage, PossibleExpectedMessageCollections, PossibleExpectedMessageType } from './ExpectedMessage';
import { botToUserMessageCheckerFunction, MessageService } from './MessageService';
import { SessionService } from './SessionService';
Expand Down Expand Up @@ -109,8 +109,8 @@ export class BotTester implements IBotTester, IOptionsModifier {
private testSteps: TestStep[];
private config: IConfig;

constructor(bot: UniversalBot, options: IConfig = config) {
this.config = Object.assign({}, config, options);
constructor(bot: UniversalBot, options?: IConfig) {
this.config = Object.assign({}, getConfig(), options);
this.config.messageFilters = this.config.messageFilters.slice();
this.bot = bot;
this.messageService = new MessageService(bot, this.config);
Expand Down
31 changes: 21 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ export interface IConfig {
}

const configFilePath = `bot-tester.json`;
const configFileExists = fs.existsSync(configFilePath);

/**
* default value for timeout. If config/options are set to this value, no timeout will be used
*/
export const NO_TIMEOUT = -1;

let configInternal: IConfig = {
const defaultConfig: IConfig = {
timeout: NO_TIMEOUT,
defaultAddress: {
channelId: 'console',
Expand All @@ -57,14 +56,26 @@ let configInternal: IConfig = {
}
};

if (configFileExists) {
configInternal = JSON.parse(fs.readFileSync(configFilePath, { encoding: 'utf8' }));
}
export function getConfig(): IConfig {
let configInternal: IConfig;

configInternal.messageFilters = [];
if (configInternal) {
return configInternal;
}

if (configInternal.ignoreInternalSaveMessage) {
configInternal.messageFilters.push(ignoreInternalSaveMessageFilter);
}
configInternal = defaultConfig;

const configFileExists = fs.existsSync(configFilePath);

if (configFileExists) {
configInternal = JSON.parse(fs.readFileSync(configFilePath, { encoding: 'utf8' }));
}

configInternal.messageFilters = [];

export const config = configInternal;
if (configInternal.ignoreInternalSaveMessage) {
configInternal.messageFilters.push(ignoreInternalSaveMessageFilter);
}

return configInternal;
}

0 comments on commit 04f6abe

Please sign in to comment.